I'm Siddharth, and I work on the design engineering team at GitHub. I'm going to talk to you about some code crimes we do to create good component APIs. Before we get there, let me ask you, what makes a good user interface? The kind of words that I hear a lot are, it should be intuitive, it should be accessible, and it should be consistent. If you know how to use one part of the application, you can guess how to use another part, because it's all consistent.
Now, I work on the React side of things. I work on this component library that we use to build other pages on GitHub, and this is a question that I think about, which is what makes a component API interface good? Because that's the thing that your users, the developers, are consuming. And it's kind of the same thing. It should be intuitive to read and author code, it should be accessible by default, and it should be consistent. So, by consistent, I mean it should have a small API surface area. If you know how to use one component, you can guess how to use another component, because the API is consistent. So, that's the goal, at least. But it's not always easy. Sometimes it's not in the good, happy parts of React, and that's where the crimes come in. So, let's look at a few.
I have this component. It's called AnchoredOverlay, and it's called AnchoredOverlay because it opens an overlay which is anchored to this button. So, AnchoredOverlay. And the way this button, the way this component works is that you have to manage its state. So, you pass open, you give it onOpen and onClose function, and then you give it a render. So, this is a render prop. You can render the element inside. In this case, I'm rendering a button. And then it passes you some props that you're expected to pass through to the element. So, there are things like, I have props in this. There's some styles for this AnchoredOverlay. So, kind of a simple API, not a lot going on. And then inside it, you can put children, which get rendered there. So, good component.
Now, the place where we might use this component is, this is a pull request page from GitHub. And you see there's a bunch of menus here.
Comments