UNPKG

@cerberus-design/react

Version:

The Cerberus Design React component library.

270 lines (261 loc) 9.42 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/components/rating/index.ts var rating_exports = {}; __export(rating_exports, { Rating: () => Rating, RatingContext: () => RatingContext, RatingControl: () => RatingControl, RatingHiddenInput: () => RatingHiddenInput, RatingItem: () => RatingItem, RatingItemContext: () => RatingItemContext, RatingLabel: () => RatingLabel, RatingParts: () => RatingParts, RatingRoot: () => RatingRoot }); module.exports = __toCommonJS(rating_exports); // src/components/rating/primitives.tsx var import_rating_group = require("@ark-ui/react/rating-group"); var import_recipes = require("styled-system/recipes"); // src/system/primitive-factory.tsx var import_css = require("styled-system/css"); var import_jsx_runtime = require("react/jsx-runtime"); var CerberusPrimitive = class { constructor(recipe) { __publicField(this, "recipe"); /** * Creates a Cerberus component with bare features and no recipe. * @param Component - The React component to enhance with Cerberus features. * Can be a string or a component reference. * @returns A new React component that applies Cerberus features to the * original component. * * @example * ```ts * const { withNoRecipe } = createCerberusPrimitive(buttonRecipe) * const Button = withNoRecipe('button') * ``` */ __publicField(this, "withNoRecipe", (Component, options) => { const { defaultProps } = options || {}; const El = Component; const CerbComponent = (props) => { const { css: customCss, className, ...nativeProps } = props; const styles = this.hasStyles((0, import_css.cx)(className, (0, import_css.css)(customCss))); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(El, { ...defaultProps, ...styles, ...nativeProps }); }; if (this.validateComponent(El)) { const ElName = typeof El === "string" ? El : El.displayName || El.name; CerbComponent.displayName = ElName; } return CerbComponent; }); /** * Creates a Cerberus component with the given recipe. * @param Component - The React component to enhance with the recipe. * @param options - Options for the recipe. * @returns A new React component that applies the recipe to the original * component. */ __publicField(this, "withRecipe", (Component, options) => { const { defaultProps } = options || {}; const El = Component; const recipe = this.recipe; const CerbComponent = (internalProps) => { const { css: customCss, className, ...restOfInternalProps } = internalProps; const [variantOptions, nativeProps] = recipe.splitVariantProps(restOfInternalProps); const recipeStyles = recipe(variantOptions); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( Component, { ...defaultProps, ...nativeProps, className: (0, import_css.cx)(className, recipeStyles, (0, import_css.css)(customCss)) } ); }; if (this.validateComponent(El)) { const ElName = typeof El === "string" ? El : El.displayName || El.name; CerbComponent.displayName = ElName; } return CerbComponent; }); /** * Creates a Cerberus component with a slot recipe applied. * @param Component - The React component to enhance with Cerberus features. * @param recipe - The slot recipe to apply to the component. * @returns A new React component that applies Cerberus features and the * specified slot recipe to the original component. * @example * ```typescript * const { withSlotRecipe } = createCerberusPrimitive(field) * const Field = withSlotRecipe(RawField, field) * ``` */ __publicField(this, "withSlotRecipe", (Component, slot, options) => { const { defaultProps } = options || {}; const El = Component; const recipe = this.recipe; const CerbComponent = (internalProps) => { const { css: customCss, className, ...restOfInternalProps } = internalProps; const [variantOptions, nativeProps] = recipe.splitVariantProps(restOfInternalProps); const styles = recipe(variantOptions); const slotStyles = styles[slot]; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( Component, { ...defaultProps, ...nativeProps, className: (0, import_css.cx)(className, slotStyles, (0, import_css.css)(customCss)) } ); }; if (this.validateComponent(El)) { const ElName = typeof El === "string" ? El : El.displayName || El.name; CerbComponent.displayName = ElName; } return CerbComponent; }); this.recipe = recipe ?? null; } hasStyles(styles) { if (styles) { return { className: styles }; } return {}; } validateComponent(Component) { if (typeof Component !== "function" && typeof Component !== "object") { return false; } return true; } }; // src/system/index.ts function createCerberusPrimitive(recipe) { return new CerberusPrimitive(recipe); } // src/components/rating/primitives.tsx var { withSlotRecipe } = createCerberusPrimitive(import_recipes.ratingGroup); var RatingRoot = withSlotRecipe( import_rating_group.RatingGroup.Root, "root" ); var RatingLabel = withSlotRecipe( import_rating_group.RatingGroup.Label, "label" ); var RatingControl = withSlotRecipe( import_rating_group.RatingGroup.Control, "control" ); var RatingContext = import_rating_group.RatingGroup.Context; var RatingItem = withSlotRecipe( import_rating_group.RatingGroup.Item, "item" ); var RatingItemContext = import_rating_group.RatingGroup.ItemContext; var RatingHiddenInput = import_rating_group.RatingGroup.HiddenInput; // src/components/rating/parts.tsx var RatingParts = { Root: RatingRoot, Label: RatingLabel, Control: RatingControl, Context: RatingContext, Item: RatingItem, ItemContext: RatingItemContext, HiddenInput: RatingHiddenInput }; // src/utils/index.ts function splitProps(props, ...keyGroups) { const result = keyGroups.map(() => ({})); const rest = {}; for (const key in props) { let assigned = false; for (let i = 0; i < keyGroups.length; i++) { if (keyGroups[i].includes(key)) { result[i][key] = props[key]; assigned = true; break; } } if (!assigned) { rest[key] = props[key]; } } return [...result, rest]; } // src/components/show/show.tsx var import_jsx_runtime2 = require("react/jsx-runtime"); function Show(props) { const { when, children, fallback } = props; if (when) { return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children }); } if (fallback) { return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: fallback }); } return null; } // src/components/rating/rating.tsx var import_jsx_runtime3 = require("react/jsx-runtime"); function Rating(props) { const [{ label }, styleProps, rootProps] = splitProps( props, ["label"], ["orientation", "palette", "size"] ); return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(RatingParts.Root, { ...styleProps, ...rootProps, children: [ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Show, { when: label, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(RatingParts.Label, { children: label }) }), /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(RatingParts.Control, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(RatingParts.Context, { children: (context) => context.items.map((item) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( RatingParts.Item, { index: item, palette: styleProps.palette, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(RatingParts.ItemContext, { children: rootProps.children }) }, item )) }) }) ] }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Rating, RatingContext, RatingControl, RatingHiddenInput, RatingItem, RatingItemContext, RatingLabel, RatingParts, RatingRoot }); //# sourceMappingURL=index.cjs.map