You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-slots.md
+61-79
Original file line number
Diff line number
Diff line change
@@ -4,39 +4,44 @@
4
4
5
5
# Summary
6
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.
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) but more powerful as we can interpolate slots.
9
8
10
9
# Basic example
11
10
12
-
In this example we introduced two method called `createHost` and `createSlot`.
11
+
In this example we introduced two methods called `createHost` and `createSlot`.
13
12
14
-
`createSlot` creates a component won't be rendered to real DOM but collect props.
13
+
`createSlot` creates a component which won't be rendered to real DOM but only collect node info.
15
14
16
-
`createHost` mounts the children included slotted components and read the
17
-
collected slots props, and render the result to real DOM conditionally.
15
+
`createHost` mounts the children which hosting slotted components and read the collected slots info, and render the result to real DOM conditionally.
@@ -65,98 +66,79 @@ export default function App() {
65
66
66
67
# Motivation
67
68
68
-
## Composite component in a configure manor
69
+
## Composite component in a configure manner
69
70
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.
71
+
When creating UI library, we have different approaches on the component API design. The most common way is the Configuration pattern used by most UI libraries, everything in one component and provides child props for composition, like `<TextField label helperText />`. It's easy to use, but when we need to to add some extra props to label like `data-testid`, then we have to introduce new props for that which will bloat the api very easily.
77
72
78
-
Another way to solve this problem is Composition pattern, like
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.
73
+
Another way to solve this problem is Composition pattern, like `<TextField><TextFieldLabel /><TextFieldInput /></TextField>`, it provides the best flexibility, we are free to customise every part of our component, but it causes another problem: consistency, we have to organise your sub components exactly same order as expected, and the biggest problem is that it's harder to communicate between parent and children.
85
74
86
-
For a dedicated Design System, we need consistent ui regardless how
87
-
we compose it.
75
+
For a dedicated Design System, we need consistent ui regardless how we compose it.
88
76
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.
77
+
Here the Slots pattern solve those problem perfectly, we can compose our component with both flexibility and consistency, and it's extreme easy to add A11y support thanks the ability of Inversion of Control.
92
78
93
-
## Accessible List
79
+
## Accessible List and Virtualisation
94
80
95
-
Quoted from [the comment](https://github.com/facebook/react/issues/24979#issuecomment-1193176328)
96
-
by @devongovett
81
+
Quoted from [the comment](https://github.com/facebook/react/issues/24979#issuecomment-1193176328) by @devongovett
97
82
98
83
> 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
84
> 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
85
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.
86
+
Even with the Reach UI's solution, it doesn't work well with SSR. With the Slots pattern, we don't render the children to real DOM but only collect information, so virtualisation is supported by nature.
104
87
105
88
## Why we want it built in core
106
89
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`.
90
+
I've researched a lot different approaches, none of the current solutions work perfectly without any drawbacks, my [solution](https://github.com/nihgwu/create-slots) 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, It would be nice to see it in core, like `react-call-return`.
111
91
112
-
# Detailed design
92
+
It seems `React.Children.forEach` can do the same job demoed in the example above, but there are a lot limitations which are well documented [here](https://github.com/reach/reach-ui/tree/dev/packages/descendants), with this proposal we are free to extend the slot and compose in any way, e.g.
`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`
117
+
The api is very similar to the [deleted](https://github.com/facebook/react/pull/12820) experimental package [`react-call-return`](https://github.com/facebook/react/pull/11364), but with a simpler mental model.
120
118
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:
119
+
`createSlot<SlotProps>()` creates a slot component and won't be rendered to real DOM but only collect node info, which will be used by `createHost`, for TS/Flow users, `SlotProps` is the props we support in each slot, will be `LabelProps`, `InputProps`, `TagProps` for the example
125
120
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`.
121
+
`createHost(children:JSX.Element, callback: (slots:React.Element) =>JSX.Element|null)` mounts the children which hosting slotted components and read the collected slots elements, and render the result to real DOM conditionally. We can find the slots by filter the `slots`(shown in the example above), similar to Web Components, there could be nodes not wrapped in slots at all will be treated as unnamed slot(`<slot></slot>)`
133
122
134
123
# Drawbacks
135
124
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.
125
+
Even we are going to support this feature in a separate package or entry, it will still increase the bundle size of React a bit as we need core support.
138
126
139
127
# Alternatives
140
128
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.
129
+
- Bring back `react-call-return` which also could be used to implement this feature [demo](https://codesandbox.io/s/long-hill-os6msf?file=/src/App.js), but the mental model is hard to understand.
130
+
- Leave it to userspace to implement with current api, like [create-slots](https://github.com/nihgwu/create-slots), but not very efficient and have some drawbacks, e.g. unable to catch children not wrapped in slots, for list slots we have to force update to get the correct index.
145
131
146
132
# Adoption strategy
147
133
148
-
There is no breaking changes, it's a new feature more for UI library authors
149
-
instead of application developers.
134
+
There is no breaking changes, it's a new feature more for UI library authors instead of application developers.
150
135
151
136
# How we teach this
152
137
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.
138
+
Slots pattern is a native feature of Web Components, other popular frameworks like Vue and Svelte also provide similar concepts. So the concept itself is easy to understand, we only have to document the api.
156
139
157
140
# Unresolved questions
158
141
159
142
- 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`
143
+
- Do we need to provide internal key for list rendering?
0 commit comments