Skip to content

Commit 31f5bff

Browse files
committed
RFC: Slots
1 parent 4f62379 commit 31f5bff

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

text/0000-slots.md

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
- Start Date: 2022-08-05
2+
- RFC PR: (leave this empty)
3+
- React Issue: (leave this empty)
4+
5+
# Summary
6+
7+
A way to support slots pattern in React, works similar to [Slots for Web Components](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots)
8+
but more powerful as we can interpolate slots.
9+
10+
# Basic example
11+
12+
In this example we introduced two method called `createHost` and `createSlot`.
13+
14+
`createSlot` creates a component won't be rendered to real DOM but collect props.
15+
16+
`createHost` mounts the children included slotted components and read the
17+
collected slots props, and render the result to real DOM conditionally.
18+
19+
```jsx
20+
import { createHost, createSlot } from "react-create-slots";
21+
22+
const Tag = (props) => <span {...props} />;
23+
24+
const TextField = (props) => {
25+
const id = useId();
26+
27+
return createHost(props.children, (Slots) => {
28+
const labelProps = Slots.get("label");
29+
const inputProps = Slots.get("input");
30+
const tagPropsList = Slots.getAll("tag");
31+
32+
return (
33+
<div>
34+
{labelProps && <label htmlFor={inputProps.id ?? id} {...labelProps} />}
35+
<input id={id} {...label} />
36+
{tagPropsList.length > 0 && (
37+
<div>
38+
{tagPropsList.map((tagProps, index) => (
39+
<Tag data-index={index} {...tagProps} />
40+
))}
41+
</div>
42+
)}
43+
</div>
44+
);
45+
});
46+
};
47+
48+
const TextFieldLabel = createSlot("label");
49+
const TextFieldInput = createSlot("input");
50+
const TextFieldTag = createSlot("tag");
51+
52+
export default function App() {
53+
return (
54+
<div>
55+
<TextField>
56+
<TextFieldInput id="input-id" />
57+
<TextFieldLabel>It will be rendered above input</TextFieldLabel>
58+
<TextFieldTag>Tag 1</TextFieldTag>
59+
<TextFieldTag>Tag 2</TextFieldTag>
60+
</TextField>
61+
</div>
62+
);
63+
}
64+
```
65+
66+
# Motivation
67+
68+
## Composite component in a configure manor
69+
70+
When creating UI library, we have different approaches on the component
71+
API design. The most common way is the Configuration pattern used by
72+
most UI libraries, everything in one component and provides child props
73+
for composition, like `<TextField label helperText />`. It's easy to use,
74+
but when we need to to add some extra props to label like `data-testid`,
75+
then we have to introduce new props for that which will bloat the api
76+
very easily.
77+
78+
Another way to solve this problem is Composition pattern, like
79+
`<TextField><TextFieldLabel /><TextFieldInput /></TextField>`,
80+
it provides the best flexibility, we are free to customise every
81+
part of our component, but it causes another problem: consistency,
82+
we have to organise your sub components exactly same order as expected,
83+
and the biggest problem is that it's harder to communicate between
84+
parent and children.
85+
86+
For a dedicated Design System, we need consistent ui regardless how
87+
we compose it.
88+
89+
Here the Slots pattern solve those problem perfectly, we can compose
90+
our component with both flexibility and consistency, and it's extreme
91+
easy to add A11y support thanks the ability of Inversion of Control.
92+
93+
## Accessible List
94+
95+
Quoted from [the comment](https://github.com/facebook/react/issues/24979#issuecomment-1193176328)
96+
by @devongovett
97+
98+
> A bunch of libraries have a problem where they need to know about certain types of descendants. For example, a list component with keyboard navigation needs to know what elements exist in the collection in order to implement things like typeahead, arrow keys, selection, etc. Reach UI has a good [overview](https://github.com/reach/reach-ui/tree/dev/packages/descendants) of a bunch of different approaches to this. The most commonly used of them involve rendering all of the items to the DOM, and using some kind of context-based registration system to tell the parent about themselves, and the DOM to sort them into the correct order.
99+
> This has the downside that all of the items must be in the DOM at all times. In some cases, like virtualized scrolling, or a combobox/select where users can set the item without showing the list, some or all of the items shouldn't be rendered to the DOM. In React Aria, we walk the JSX tree to do this, which makes for a more natural API than giving up JSX completely (info). But this breaks composition, because only certain known element types are allowed.
100+
101+
Even with the Reach UI's solution, it doesn't work well with SSR.
102+
With the Slots pattern, we don't render the children to real DOM but
103+
only collect information, so virtualisation is supported by nature.
104+
105+
## Why we want it built in core
106+
107+
I've researched a lot different approaches, none of the current solutions
108+
work perfectly without any drawbacks, my [solution](https://github.com/nihgwu/create-slots)
109+
is the closest one. But as @devongovett pointed out [here](https://github.com/facebook/react/issues/24979#issuecomment-1205909188), `react-reconciler` is designed to do this job,
110+
It would be nice to see it in core, like `react-call-return`.
111+
112+
# Detailed design
113+
114+
The api is very similar to the [deleted](https://github.com/facebook/react/pull/12820)
115+
experimental package [`react-call-return`](https://github.com/facebook/react/pull/11364),
116+
but with a simpler mental model.
117+
118+
`createSlot(slotName)` creates a slot component and won't be rendered to
119+
real DOM but only collect props, which will be used by `createHost`
120+
121+
`createHost(children, callback)` mounts the children included slotted
122+
components and read the collected slots' props, and render the result to
123+
real DOM conditionally. The argument of `callback` provides the following
124+
methods:
125+
126+
- `get(slotName: string)` returns the registered slot's props by name,
127+
will return the last registered named slot if the host expect only one
128+
but consumer provides more.
129+
- `getAll(slotName: string)` returns all the registered slots as array
130+
this method is useful to create a list.
131+
- `getChildren()` returns the rest of children without slots, e.g.
132+
`<Host>rest<Slot /></Host>` it will return `rest`.
133+
134+
# Drawbacks
135+
136+
Even we are going to support this feature in a separate package or entry,
137+
it will still increase the bundle size of React a bit as we need core support.
138+
139+
# Alternatives
140+
141+
- Bring back `react-call-return` which also could be used to implement this
142+
feature, but the mental model is hard to understand.
143+
- Leave it to userspace to implement with current api, like [create-slots](https://github.com/nihgwu/create-slots), but not very efficient and have
144+
some drawbacks, e.g. unable to catch children not wrapped in slots.
145+
146+
# Adoption strategy
147+
148+
There is no breaking changes, it's a new feature more for UI library authors
149+
instead of application developers.
150+
151+
# How we teach this
152+
153+
Slots pattern is a native feature of Web Components, other popular frameworks
154+
like Vue and Svelte also provide similar concepts. So the concept itself is
155+
easy to understand, we only have to document the api.
156+
157+
# Unresolved questions
158+
159+
- Finalise the namings
160+
- Do we need to provide internal key for list rendering, though I don't see it
161+
in `react-call-return`
162+
- Adding to a new package or to `React` directly

0 commit comments

Comments
 (0)