|
| 1 | +import { |
| 2 | + type CSSProperties, |
| 3 | + type HTMLAttributes, |
| 4 | + type PropsWithChildren, |
| 5 | + type Ref, |
| 6 | + type RefCallback, |
| 7 | + cloneElement, |
| 8 | + isValidElement, |
| 9 | +} from "react" |
| 10 | + |
| 11 | +const isFunction = (value: unknown): value is (...args: unknown[]) => unknown => |
| 12 | + typeof value === "function" |
| 13 | + |
| 14 | +const isObject = (value: unknown): value is Record<string, unknown> => |
| 15 | + typeof value === "object" && value !== null |
| 16 | + |
| 17 | +const mergeRefs = <T,>(...refs: unknown[]): RefCallback<T> | undefined => { |
| 18 | + if (refs.every(ref => ref == null)) { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + return (value: T | null) => { |
| 23 | + refs.forEach(ref => { |
| 24 | + if (!ref) return |
| 25 | + |
| 26 | + if (isFunction(ref)) { |
| 27 | + ref(value) |
| 28 | + } else if (isObject(ref) && "current" in ref) { |
| 29 | + ref["current"] = value |
| 30 | + } |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const mergeFunctions = |
| 36 | + (slotFn: unknown, childFn: unknown) => |
| 37 | + (...args: unknown[]) => { |
| 38 | + if (isFunction(slotFn)) slotFn(...args) |
| 39 | + if (isFunction(childFn)) childFn(...args) |
| 40 | + } |
| 41 | + |
| 42 | +const mergeClassNames = (slot: unknown, child: unknown) => |
| 43 | + [child, slot] |
| 44 | + .filter(className => className && typeof className === "string") |
| 45 | + .join(" ") |
| 46 | + |
| 47 | +const mergeStyles = (slot: CSSProperties, child: CSSProperties) => ({ |
| 48 | + ...slot, |
| 49 | + ...child, |
| 50 | +}) |
| 51 | + |
| 52 | +const mergeProps = ( |
| 53 | + slotProps: Record<string, unknown>, |
| 54 | + childProps: Record<string, unknown> |
| 55 | +) => { |
| 56 | + const result: Record<string, unknown> = { ...childProps } |
| 57 | + |
| 58 | + for (const key in slotProps) { |
| 59 | + const slotValue = slotProps[key] |
| 60 | + const childValue = childProps[key] |
| 61 | + |
| 62 | + if (key === "ref") { |
| 63 | + result[key] = mergeRefs(slotValue, childValue) |
| 64 | + } else if (key === "className") { |
| 65 | + result[key] = mergeClassNames(slotValue, childValue) |
| 66 | + } else if (key === "style" && isObject(slotValue) && isObject(childValue)) { |
| 67 | + result[key] = mergeStyles(slotValue, childValue) |
| 68 | + } else if (/^on[A-Z]/.test(key)) { |
| 69 | + result[key] = mergeFunctions(slotValue, childValue) |
| 70 | + } else { |
| 71 | + result[key] = childValue ?? slotValue |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return result |
| 76 | +} |
| 77 | + |
| 78 | +type SlotProps = PropsWithChildren< |
| 79 | + HTMLAttributes<HTMLElement> & { |
| 80 | + ref?: Ref<HTMLElement> |
| 81 | + } |
| 82 | +> |
| 83 | + |
| 84 | +export const Slot = ({ children, ...slotProps }: SlotProps) => { |
| 85 | + if (!isValidElement(children)) { |
| 86 | + throw new Error("Slot requires a single React element as child.") |
| 87 | + } |
| 88 | + |
| 89 | + return cloneElement( |
| 90 | + children, |
| 91 | + mergeProps( |
| 92 | + slotProps as Record<string, unknown>, |
| 93 | + children.props as Record<string, unknown> |
| 94 | + ) |
| 95 | + ) |
| 96 | +} |
0 commit comments