@ehsaneha/react-slot
v1.0.6
Published
A React <Slot> component that merges props and forwards refs to a single child element, similar to Radix UI’s Slot.
Downloads
29
Maintainers
Readme
@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.
📦 Installation
npm install @ehsaneha/react-slotOr
yarn add @ehsaneha/react-slot🚀 Usage
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
onClickhandlers are triggered classNameandstyleprops are merged- The
refis forwarded to the child element
✨ Features
- 🔁 Merges
className,style, and event handlers - 🎯 Forwards
refto child DOM orforwardRefcomponent - 🚫 Rejects
React.Fragmentand multiple children (with dev warning) - 🪶 Tiny, dependency-free, and tree-shakable
🧪 Running Tests
npm run testCovers:
- 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!
