From 521c2c91b44a3fbe72cc84cd7dea5069fb8e564a 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 | 140 ++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 79 deletions(-) diff --git a/text/0000-slots.md b/text/0000-slots.md index d0e1111a..a2992ae6 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 &&