react slots
npm install @goncharovv/react-slots``bash`
npm install @goncharovv/react-slots
1) Define slots with createSlot:
`tsx
import { createSlot } from '@goncharovv/react-slots';
import { PropsWithChildren } from 'react';
const HeaderSlot = createSlot
const FooterSlot = createSlot
`
2) Build a component and read slots with useSlots:
`tsx
import { useSlots } from '@goncharovv/react-slots';
import { FC, PropsWithChildren } from 'react';
const _Modal: FC
const slots = useSlots(children);
const header = slots.get(HeaderSlot);
const footer = slots.get(FooterSlot);
return (
const Modal = Object.assign(_Modal, {
Header: HeaderSlot,
Footer: FooterSlot,
});
`
3) Use it:
`tsx
`
Slots can carry typed props. Read them via :
`tsx
import { createSlot, useSlots } from '@goncharovv/react-slots';
import { FC, PropsWithChildren } from 'react';
interface TitleProps extends PropsWithChildren {
as: 'h1' | 'h2' | 'h3';
}
const Title: FC
const { as: Component, children } = props;
return (
);
};
const TitleSlot = createSlot
const _Heading: FC
const slots = useSlots(children);
const title = slots.get(TitleSlot);
if (!title) throw new Error('Title slot is required');
return
const Heading = Object.assign(_Heading, {
Title: TitleSlot,
});
``