From cb901708a0adf751f635baf758d94a42c797f1de Mon Sep 17 00:00:00 2001 From: Neo Nie Date: Sun, 7 Aug 2022 00:38:41 +0100 Subject: [PATCH] remove string based slot name --- text/0000-slots.md | 138 ++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 78 deletions(-) diff --git a/text/0000-slots.md b/text/0000-slots.md index d0e1111a..c4189cb3 100644 --- a/text/0000-slots.md +++ b/text/0000-slots.md @@ -4,39 +4,44 @@ # Summary -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. +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. # Basic example -In this example we introduced two method called `createHost` and `createSlot`. +In this example we introduced two methods called `createHost` and `createSlot`. -`createSlot` creates a component won't be rendered to real DOM but collect props. +`createSlot` creates a component which won't be rendered to real DOM but only collect node info. -`createHost` mounts the children included slotted components and read the -collected slots props, and render the result to real DOM conditionally. +`createHost` mounts the children which hosting slotted components and read the collected slots info, and render the result to real DOM conditionally. ```jsx import { createHost, createSlot } from "react-create-slots"; +const TextFieldLabel = createSlot(); +const TextFieldInput = createSlot(); +const TextFieldTag = createSlot(); + const Tag = (props) => ; const TextField = (props) => { const id = useId(); - return createHost(props.children, (Slots) => { - const labelProps = Slots.get("label"); - const inputProps = Slots.get("input"); - const tagPropsList = Slots.getAll("tag"); + return createHost(props.children, (slots) => { + // library author can create utlis to reduce the boilerplate + const labelSlot = slots.findLast((slot) => slot.type === TextFieldLabel); + const inputSlot = slots.findLast((slot) => slot.type === TextFieldInput); + const tagSlots = slots.filter((slot) => slot.type === TextFieldTag); return (
- {labelProps &&