A super lightweight modern alternative to [`react-slot-fill`](https://github.com/camwest/react-slot-fill) with familiar API.
npm install nano-slotsA super lightweight modern alternative to react-slot-fill with familiar API.
- [x] Control sub-components rendering with Slot and Fill
- [x] Render content of sub-component in multiple places
- [x] Speedy - Fill and Slot communicate directly with each other
- [x] Tested with testing-library
- [x] Written in TypeScript
- [x] Zero dependencies
- [x] Only ~431 B
``sh`
npm i -S nano-slots
`sh`
yarn add nano-slots
`js
import { Box, Flex } from 'theme-ui'
import { SlotsProvider, Slot } from 'nano-slots'
export const MediaObject = ({ children }) => (
{children}
)
`
`js
import { Fill } from 'nano-slots'
import { MediaObject } from './media-object'
const MyApp = () => ( Purr purr purr
Mew
)
`

`js`
import { SlotsProvider } from 'nano-slots'
#### Props
- children: ReactNode — any valid react children element
#### Description
Creates a context for Slot / Fill components.
`js`
import { Slot } from 'nano-slots'
#### Props
- name: string — unique slot name for current SlotsProviderchildren?: ReactNode
- — fallback in case Fill with matching name not provided, optionalonChange?(hasFilled: boolean): void
- — callback for detecting state changes, on true children of matching Fill is rendered and fallback is hidden
#### Description
Define the target slot for Fill component. Can be used multiple times with the same name inside each SlotsProvider.
`js`
import { Fill } from 'nano-slots'
#### Props
- name: string — unique slot name for current SlotsProviderchildren: ReactNode
- — will be rendered inside matching Slot
#### Description
Render children into matching Slot of current SlotsProvider.
`js`
import createSlots from 'nano-slots'
#### Description
Designed for more advanced usages and stronger types.
Returns an object containing:
- .Provider — same as SlotsProvider, but with different context.Slot
- — same as Slot, but with own context.Fill
- — same as Fill, but with own context
Returned Slot and Fill can be used without a Provider.
`ts
export interface ProviderProps {
children: React.ReactNode;
}
export function SlotsProvider(props: ProviderProps): JSX.Element;
export interface SlotProps
name: Names;
children?: React.ReactNode;
}
export function Slot(props: SlotProps): JSX.Element;
export interface FillProps
name: Names;
children?: React.ReactNode;
}
export function Fill(props: FillProps): null;
export default function createSlots
Provider: (props: SlotsProviderProps): JSX.Element;
Slot: (props: SlotProps
Fill: (props: FillProps
}
`
- react-slot-fill - abandoned project that inspired this one
- react-view-slot` - more modern approach, but 12x times bigger
---
MIT © John Grishin