Skip to content

Instantly share code, notes, and snippets.

@lunelson
Last active March 20, 2021 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunelson/77610487d76eda5a8265403fc019f646 to your computer and use it in GitHub Desktop.
Save lunelson/77610487d76eda5a8265403fc019f646 to your computer and use it in GitHub Desktop.
react-hook-use-slot
function createSlot() {
const Slot = ({ children }) => children;
const getSlot = (children) =>
React.Children.toArray(children).find((child) => child.type === Slot) ||
null;
return [Slot, getSlot];
}
function createSlots(n) {
const SlotSets = Array(n)
.fill()
.map(createSlot);
const Slots = SlotSets.map(([Slot]) => Slot);
const getRest = (children) =>
React.Children.toArray(children).filter(
(child) => !~Slots.indexOf(child.type)
);
return [...SlotSets, getRest];
}
console.log(createSlots(3));
const [SlotA, getSlotA] = createSlot();
const [SlotB, getSlotB] = createSlot();
const [SlotC, getSlotC] = createSlot();
// console.log(SlotA);
const Foo = ({ children }) => {
return (
<div>
<div style={{ backgroundColor: 'lightblue' }}>
<p>slot A</p>
{getSlotA(children)}
</div>
<div style={{ backgroundColor: 'pink' }}>
<p>slot B</p>
{getSlotB(children)}
</div>
<div style={{ backgroundColor: 'green' }}>
<p>slot C</p>
{getSlotC(children)}
</div>
</div>
);
};
Object.assign(Foo, { SlotA, SlotB, SlotC });
const [
[SlotD, getSlotD],
// [SlotE, getSlotE],
// [SlotF, getSlotF],
// getRest
] = createSlots(3);
// const Bar = ({ children }) => {
// return (
// <div>
// <div style={{ backgroundColor: 'lightblue' }}>
// <p>slot A</p>
// {getSlotA(children)}
// </div>
// <div style={{ backgroundColor: 'pink' }}>
// <p>slot B</p>
// {getSlotB(children)}
// </div>
// <div style={{ backgroundColor: 'green' }}>
// <p>slot C</p>
// {getSlotC(children)}
// </div>
// </div>
// );
// };
// Object.assign(Bar, { SlotD, SlotE, SlotF });
export default function App() {
return (
<div className="App">
<Foo>
<Foo.SlotA>this is slot A content</Foo.SlotA>
<Foo.SlotB>this is slot B content</Foo.SlotB>
<Foo.SlotC>this stuff goes in slot C</Foo.SlotC>
</Foo>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
{
"scripts": [
"react",
"react-dom"
],
"styles": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment