UNPKG

@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
# @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!