|
1 | 1 | # @zemd/react-slottable
|
2 | 2 |
|
3 |
| -To install dependencies: |
| 3 | +[](https://npmjs.com/package/@zemd/react-slottable) |
4 | 4 |
|
5 |
| -```bash |
6 |
| -bun install |
| 5 | +The package provides a way to create your components in a customizable, easy-to-use, and maintainable way. Zero dependencies. |
| 6 | + |
| 7 | +With it, you can build your own complex component libraries, composing different components without losing control over their internals, providing a great developer experience to your team. |
| 8 | + |
| 9 | +Sounds interesting? Just look at the example of such a `Button` built with the library: |
| 10 | + |
| 11 | +```typescript |
| 12 | +import { |
| 13 | + type TSlottablePropsFactory, |
| 14 | + slottable, |
| 15 | + useSlot, |
| 16 | + clsx |
| 17 | +} from "@zemd/react-slottable"; |
| 18 | + |
| 19 | +type TButtonProps = TSlottablePropsFactory< |
| 20 | + { |
| 21 | + fullWidth?: boolean; |
| 22 | + disabled?: boolean; |
| 23 | + size?: "sm" | "md" | "xl"; |
| 24 | + variant?: "solid" | "outlined"; |
| 25 | + color?: "primary" | "secondary"; |
| 26 | + }, |
| 27 | + "startDecorator" | "endDecorator" // this is how cool you can define your slots |
| 28 | + // in case you want control the type of slots explicitly, you can use an object: |
| 29 | + // { startDecorator: typeof MyCustomComponent, endDecorator: React.ElementType } |
| 30 | +>; |
| 31 | + |
| 32 | +export const Button = slottable<"button", TButtonProps>( |
| 33 | + // params for the component are the same as you would expect from `forwardRef` |
| 34 | + function Button(inProps, ref) { |
| 35 | + const { |
| 36 | + component = "button", // each Slottable component is overridable by default using `component` prop, if you don't need - just Omit it |
| 37 | + className, // TSlottablePropsFactory provides `className` prop by default |
| 38 | + children, // TSlottablePropsFactory provides `children` prop by default |
| 39 | + startDecorator, // TSlottablePropsFactory provides ability to provide kind of `children` element for your slot |
| 40 | + endDecorator, // same as ^ |
| 41 | + slots = {}, // slots are not receiving default value by default |
| 42 | + slotProps = {}, // as well as slotProps |
| 43 | + disabled, // here we just handle all props that we defined in `TButtonProps` |
| 44 | + fullWidth, |
| 45 | + size = "md", |
| 46 | + variant = "solid", |
| 47 | + color = "primary", |
| 48 | + ...rest |
| 49 | + } = inProps; |
| 50 | + |
| 51 | + const props = Object.assign({}, rest, { slots, slotProps }); // building a map with all important props for root slot and slot information |
| 52 | + |
| 53 | + const [SlotRoot, rootProps] = useSlot("root", { |
| 54 | + ref, // ref is mandatory for the `root` slot |
| 55 | + component, |
| 56 | + className: clsx( |
| 57 | + "button-default-className", // use Tailwind, jss, or any other styling solution you want |
| 58 | + `button__size_${size}`, |
| 59 | + `button__color_${color}`, |
| 60 | + `button__variant_${variant}`, |
| 61 | + "text-blue-400 text-xl text-green-500", |
| 62 | + { |
| 63 | + "button__width_full": fullWidth |
| 64 | + }, |
| 65 | + className // don't forget that your users would like to customize this prop |
| 66 | + ), |
| 67 | + props, |
| 68 | + extraProps: { |
| 69 | + // optional `extraProps` field, which can include everything you want to send specifically to the slot component |
| 70 | + }, |
| 71 | + // classNameMergeFn: twMerge, // if you need to normalize classNames you can pass a function reference |
| 72 | + }); |
| 73 | + |
| 74 | + const [SlotStartDecorator, startDecoratorProps] = useSlot( |
| 75 | + "startDecorator", |
| 76 | + { |
| 77 | + component: slots["startDecorator"] ?? ("div" as ElementType), |
| 78 | + className: "start-decorator-className", |
| 79 | + props, |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + const [SlotEndDecorator, endDecoratorProps] = useSlot("endDecorator", { |
| 84 | + component: slots["endDecorator"] ?? ("div" as ElementType), |
| 85 | + className: "end-decorator-className", |
| 86 | + props, |
| 87 | + }); |
| 88 | + |
| 89 | + // Wow! How easy to maintain it looks!!! Such declarative! |
| 90 | + return ( |
| 91 | + <SlotRoot {...rootProps}> |
| 92 | + {startDecorator ? ( |
| 93 | + <SlotStartDecorator {...startDecoratorProps}> |
| 94 | + {startDecorator} |
| 95 | + </SlotStartDecorator> |
| 96 | + ) : null} |
| 97 | + {children} |
| 98 | + {endDecorator ? ( |
| 99 | + <SlotEndDecorator {...endDecoratorProps}> |
| 100 | + {endDecorator} |
| 101 | + </SlotEndDecorator> |
| 102 | + ) : null} |
| 103 | + </SlotRoot> |
| 104 | + ); |
| 105 | + } |
| 106 | +); |
7 | 107 | ```
|
8 | 108 |
|
9 |
| -To run: |
| 109 | +Now your users can use this `Button`: |
10 | 110 |
|
11 |
| -```bash |
12 |
| -bun run index.ts |
| 111 | +```typescript |
| 112 | +import { Button } from "./MyButton"; |
| 113 | + |
| 114 | +export function HomePage() { |
| 115 | + const buttonRef = useRef(null); |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + <Button |
| 119 | + // ref={buttonRef} // just in case if you need |
| 120 | + startDecorator={<MySvgIcon />} |
| 121 | + endDecorator={<span>no icon. just label.</span>} |
| 122 | + slots={{ |
| 123 | + endDecorator: MyCustomLabelComponent |
| 124 | + }} |
| 125 | + slotProps={{ |
| 126 | + startDecorator: { |
| 127 | + prop1: "value" |
| 128 | + } |
| 129 | + }} |
| 130 | + className: "my-custom-button-className" |
| 131 | + > |
| 132 | + Click me! |
| 133 | + </Button> |
| 134 | + </div> |
| 135 | + ); |
| 136 | +} |
13 | 137 | ```
|
14 | 138 |
|
15 |
| -This project was created using `bun init` in bun v1.0.29. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. |
| 139 | +The package exposes three essential parts: `slottable`, the `useSlot` hook, and the `TSlottablePropsFactory` typescript type. Combining them together, you get a backbone for your fantastic components. |
| 140 | + |
| 141 | +## License |
| 142 | + |
| 143 | +All the code in the repository released under the Apache 2.0 license |
| 144 | + |
| 145 | +## Donate |
| 146 | + |
| 147 | +[](https://www.patreon.com/red_rabbit) |
| 148 | +[](https://u24.gov.ua/) |
0 commit comments