UNPKG

@crossed/primitive

Version:

A universal & performant styling library for React Native, Next.js & React

107 lines (106 loc) 4.38 kB
"use strict"; "use client"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Slot_exports = {}; __export(Slot_exports, { Slot: () => Slot, Slottable: () => Slottable }); module.exports = __toCommonJS(Slot_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_core = require("@crossed/core"); var React = __toESM(require("react")); const Slot = React.forwardRef((props, forwardedRef) => { const { children, ...slotProps } = props; const childrenArray = React.Children.toArray(children); const slottable = childrenArray.find(isSlottable); if (slottable) { const newElement = slottable.props.children; const newChildren = childrenArray.map((child) => { if (child === slottable) { if (React.Children.count(newElement) > 1) return React.Children.only(null); return React.isValidElement(newElement) ? newElement.props.children : null; } else { return child; } }); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children: React.isValidElement(newElement) ? React.cloneElement(newElement, void 0, newChildren) : null }); } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotClone, { ...slotProps, ref: forwardedRef, children }); }); Slot.displayName = "Slot"; const SlotClone = React.forwardRef( (props, forwardedRef) => { const { children, ...slotProps } = props; if (React.isValidElement(children)) { return React.cloneElement(children, { ...mergeProps(slotProps, children.props), ref: forwardedRef ? (0, import_core.composeRefs)(forwardedRef, children.ref) : children.ref }); } return React.Children.count(children) > 1 ? React.Children.only(null) : null; } ); SlotClone.displayName = "SlotClone"; const Slottable = ({ children }) => { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children }); }; function isSlottable(child) { return React.isValidElement(child) && child.type === Slottable; } function mergeProps(slotProps, childProps) { const overrideProps = { ...childProps }; for (const propName in childProps) { const slotPropValue = slotProps[propName]; const childPropValue = childProps[propName]; const isHandler = /^on[A-Z]/.test(propName); if (isHandler) { if (slotPropValue && childPropValue) { overrideProps[propName] = (...args) => { childPropValue(...args); slotPropValue(...args); }; } else if (slotPropValue) { overrideProps[propName] = slotPropValue; } } else if (propName === "style") { overrideProps[propName] = { ...slotPropValue, ...childPropValue }; } else if (propName === "className") { overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" "); } } return { ...slotProps, ...overrideProps }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Slot, Slottable }); //# sourceMappingURL=Slot.js.map