@ehsaneha/react-slot
Version:
A React <Slot> component that merges props and forwards refs to a single child element, similar to Radix UIโs Slot.
77 lines (48 loc) โข 1.52 kB
Markdown
# @ehsaneha/react-slot
A lightweight and flexible React `<Slot>` component that merges props and forwards refs to a single child element โ inspired by [Radix UIโs Slot](https://www.radix-ui.com/docs/primitives/utilities/slot).
## ๐ฆ Installation
```bash
npm install @ehsaneha/react-slot
```
Or
```bash
yarn add @ehsaneha/react-slot
```
## ๐ Usage
```tsx
import Slot from "@ehsaneha/react-slot";
<Slot className="bg-blue-500 p-2" onClick={() => console.log("Parent click")}>
<button onClick={() => console.log("Child click")}>Click me</button>
</Slot>;
```
โ
What happens:
- Both `onClick` handlers are triggered
- `className` and `style` props are merged
- The `ref` is forwarded to the child element
## โจ Features
- ๐ Merges `className`, `style`, and event handlers
- ๐ฏ Forwards `ref` to child DOM or `forwardRef` component
- ๐ซ Rejects `React.Fragment` and multiple children (with dev warning)
- ๐ชถ Tiny, dependency-free, and tree-shakable
## ๐งช Running Tests
```bash
npm run test
```
Covers:
- Prop and style merging
- Ref forwarding
- Invalid child handling
- Event handler composition
## ๐ Limitations
- Only accepts one valid React element as a child.
- Ignores and warns about fragments or multiple children.
## License
This package is licensed under the MIT License. See LICENSE for more information.
Feel free to modify or contribute to this package!