@cerberus-design/react
Version:
The Cerberus Design React component library.
358 lines (343 loc) • 12.7 kB
JavaScript
;
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/admonition/admonition.tsx
var admonition_exports = {};
__export(admonition_exports, {
Admonition: () => Admonition
});
module.exports = __toCommonJS(admonition_exports);
// 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_runtime = require("react/jsx-runtime");
function Show(props) {
const { when, children, fallback } = props;
if (when) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
}
if (fallback) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback });
}
return null;
}
// src/components/admonition/primitives.tsx
var import_factory = require("@ark-ui/react/factory");
var import_recipes = require("styled-system/recipes");
// src/system/primitive-factory.tsx
var import_css = require("styled-system/css");
var import_jsx_runtime2 = 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_runtime2.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_runtime2.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_runtime2.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/admonition/primitives.tsx
var { withSlotRecipe, withNoRecipe } = createCerberusPrimitive(import_recipes.admonition);
var AdmonitionRoot = withSlotRecipe(
import_factory.ark.aside,
"root",
{
defaultProps: {
"data-scope": "admonition",
"data-part": "root"
}
}
);
var AdmonitionIndicator = withNoRecipe(
import_factory.ark.span,
{
defaultProps: {
"data-scope": "admonition",
"data-part": "indicator"
}
}
);
var AdmonitionContent = withNoRecipe(import_factory.ark.div, {
defaultProps: {
"data-scope": "admonition",
"data-part": "content"
}
});
var AdmonitionHeading = withSlotRecipe(
import_factory.ark.p,
"heading",
{
defaultProps: {
"data-scope": "admonition",
"data-part": "heading"
}
}
);
var AdmonitionDescription = withSlotRecipe(
import_factory.ark.p,
"description",
{
defaultProps: {
"data-scope": "admonition",
"data-part": "description"
}
}
);
// src/components/admonition/parts.ts
var AdmonitionParts = {
Root: AdmonitionRoot,
Indicator: AdmonitionIndicator,
Content: AdmonitionContent,
Heading: AdmonitionHeading,
Description: AdmonitionDescription
};
// src/components/admonition/match-avatar.tsx
var import_react2 = require("react");
// src/context/cerberus.tsx
var import_react = require("react");
var import_jsx_runtime3 = require("react/jsx-runtime");
var CerberusContext = (0, import_react.createContext)(null);
function useCerberusContext() {
const context = (0, import_react.useContext)(CerberusContext);
if (!context) {
throw new Error("useCerberus must be used within a CerberusProvider");
}
return context;
}
// src/components/avatar/primitives.tsx
var import_avatar = require("@ark-ui/react/avatar");
var import_recipes2 = require("styled-system/recipes");
var { withSlotRecipe: withSlotRecipe2 } = createCerberusPrimitive(import_recipes2.avatar);
var AvatarRoot = withSlotRecipe2(import_avatar.Avatar.Root, "root");
var AvatarImage = withSlotRecipe2(
import_avatar.Avatar.Image,
"image"
);
var AvatarFallback = withSlotRecipe2(
import_avatar.Avatar.Fallback,
"fallback"
);
// src/components/avatar/parts.ts
var AvatarParts = {
Root: AvatarRoot,
Image: AvatarImage,
Fallback: AvatarFallback
};
// src/components/avatar/avatar.tsx
var import_jsx_runtime4 = require("react/jsx-runtime");
function Avatar2(props) {
const [imgProps, { fallback, children }, rootProps] = splitProps(
props,
["alt", "src"],
["fallback", "children"]
);
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AvatarParts.Root, { ...rootProps, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
Show,
{
when: children,
fallback: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AvatarParts.Fallback, { children: fallback }),
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AvatarParts.Image, { ...imgProps })
] }),
children
}
) });
}
// src/components/admonition/match-avatar.tsx
var import_jsx_runtime5 = require("react/jsx-runtime");
function MatchAvatar(props) {
const { palette = "page" } = props;
const { icons } = useCerberusContext();
const {
infoNotification: InfoIcon,
successNotification: SuccessIcon,
warningNotification: WarningIcon,
dangerNotification: DangerIcon
} = icons;
const elRef = (0, import_react2.useRef)({
page: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Avatar2, { gradient: "charon-light", fallback: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(InfoIcon, {}), size: "sm" }),
info: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Avatar2, { gradient: "amphiaraus-dark", fallback: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(InfoIcon, {}), size: "sm" }),
success: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Avatar2, { gradient: "thanatos-dark", fallback: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SuccessIcon, {}), size: "sm" }),
warning: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Avatar2, { gradient: "asphodel-light", fallback: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(WarningIcon, {}), size: "sm" }),
danger: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Avatar2, { gradient: "hades-light", fallback: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(DangerIcon, {}), size: "sm" })
});
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: elRef.current[palette] });
}
// src/components/admonition/admonition.tsx
var import_jsx_runtime6 = require("react/jsx-runtime");
function Admonition(props) {
const [elProps, styleProps, rootProps] = splitProps(
props,
["heading", "icon", "children"],
["palette", "usage"]
);
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(AdmonitionParts.Root, { ...styleProps, ...rootProps, children: [
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
Show,
{
when: elProps.icon,
fallback: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(MatchAvatar, { palette: styleProps.palette }),
children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AdmonitionParts.Indicator, { children: elProps.icon })
}
),
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(AdmonitionParts.Content, { children: [
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Show, { when: elProps.heading, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AdmonitionParts.Heading, { children: elProps.heading }) }),
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AdmonitionParts.Description, { children: elProps.children })
] })
] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Admonition
});
//# sourceMappingURL=admonition.cjs.map