@cerberus-design/react
Version:
The Cerberus Design React component library.
647 lines (624 loc) • 23.1 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/file-upload/index.ts
var file_upload_exports = {};
__export(file_upload_exports, {
FileStatus: () => FileStatus,
FileUploader: () => FileUploader,
processStatus: () => processStatus
});
module.exports = __toCommonJS(file_upload_exports);
// src/components/file-upload/file-status.tsx
var import_react3 = require("react");
var import_recipes5 = require("styled-system/recipes");
var import_css2 = require("styled-system/css");
var import_patterns = require("styled-system/patterns");
// src/context/cerberus.tsx
var import_react = require("react");
var import_jsx_runtime = 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/progress/primitives.ts
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/progress/primitives.ts
var { withSlotRecipe } = createCerberusPrimitive(import_recipes.progressBar);
var ProgressBarRoot = withSlotRecipe(
import_factory.ark.div,
"root",
{
defaultProps: {
"aria-valuemin": "0",
"aria-valuemax": "100",
role: "progressbar"
}
}
);
var ProgressBarBar = withSlotRecipe(
import_factory.ark.div,
"bar"
);
// src/components/progress/progress-bar.tsx
var import_jsx_runtime3 = require("react/jsx-runtime");
function ProgressBar(props) {
const { indeterminate, now, label, ...nativeProps } = props;
const nowClamped = Math.min(100, Math.max(0, now || 0));
const width = {
width: indeterminate ? "50%" : `${nowClamped}%`
};
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
ProgressBarRoot,
{
...nativeProps,
"aria-label": label,
"aria-valuenow": indeterminate ? 0 : nowClamped,
children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
ProgressBarBar,
{
...indeterminate && { "data-state": "indeterminate" },
"data-complete": nowClamped === 100,
style: width
}
)
}
);
}
// src/components/icon-button/primitives.ts
var import_factory2 = require("@ark-ui/react/factory");
var import_recipes2 = require("styled-system/recipes");
var { withRecipe } = createCerberusPrimitive(import_recipes2.iconButton);
var IconButtonRoot = withRecipe(import_factory2.ark.button);
// src/components/icon-button/button.tsx
var import_jsx_runtime4 = require("react/jsx-runtime");
function IconButton(props) {
const { ariaLabel, ...rootProps } = props;
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(IconButtonRoot, { ...rootProps, "aria-label": ariaLabel ?? "Icon Button" });
}
// 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_runtime5 = require("react/jsx-runtime");
function Show(props) {
const { when, children, fallback } = props;
if (when) {
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children });
}
if (fallback) {
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: fallback });
}
return null;
}
// src/components/avatar/primitives.tsx
var import_avatar = require("@ark-ui/react/avatar");
var import_recipes3 = require("styled-system/recipes");
var { withSlotRecipe: withSlotRecipe2 } = createCerberusPrimitive(import_recipes3.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_runtime6 = require("react/jsx-runtime");
function Avatar2(props) {
const [imgProps, { fallback, children }, rootProps] = splitProps(
props,
["alt", "src"],
["fallback", "children"]
);
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AvatarParts.Root, { ...rootProps, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
Show,
{
when: children,
fallback: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_jsx_runtime6.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AvatarParts.Fallback, { children: fallback }),
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(AvatarParts.Image, { ...imgProps })
] }),
children
}
) });
}
// src/components/field/field.tsx
var import_jsx = require("styled-system/jsx");
// src/components/field/primitives.tsx
var import_field2 = require("@ark-ui/react/field");
var import_react2 = require("@ark-ui/react");
var import_recipes4 = require("styled-system/recipes");
// src/components/field/error-text.tsx
var import_field = require("@ark-ui/react/field");
var import_jsx_runtime7 = require("react/jsx-runtime");
function CerberusFieldErrorText(props) {
if (!props.children) return null;
return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_field.Field.ErrorText, { ...props });
}
// src/components/field/primitives.tsx
var import_jsx_runtime8 = require("react/jsx-runtime");
var { withSlotRecipe: withSlotRecipe3, withNoRecipe } = createCerberusPrimitive(import_recipes4.field);
var FieldRoot = withSlotRecipe3(import_field2.Field.Root, "root");
function FieldLabelEl(props) {
const { children, ...nativeProps } = props;
return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_field2.Field.Label, { ...nativeProps, children: [
children,
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_field2.Field.RequiredIndicator, { children: "(required)" })
] });
}
var FieldLabel = withSlotRecipe3(FieldLabelEl, "label");
function FieldRequiredIndicatorEl(props) {
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_field2.Field.RequiredIndicator, { ...props, children: "(required)" });
}
var FieldRequiredIndicator = withNoRecipe(
FieldRequiredIndicatorEl
);
var FieldInputRoot = withSlotRecipe3(
import_react2.ark.div,
"inputRoot"
);
var FieldInput = withSlotRecipe3(import_field2.Field.Input, "input");
var FieldHelperText = withSlotRecipe3(
import_field2.Field.HelperText,
"helperText"
);
var FieldErrorText = withSlotRecipe3(
CerberusFieldErrorText,
"errorText"
);
var FieldTextarea = withSlotRecipe3(
import_field2.Field.Textarea,
"textarea"
);
// src/components/field/helper-text.tsx
var import_jsx_runtime9 = require("react/jsx-runtime");
function HelperText(props) {
if (props.invalid) return null;
return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(FieldHelperText, { "data-has-content": Boolean(props.children), children: props.children });
}
// src/components/field/field.tsx
var import_jsx_runtime10 = require("react/jsx-runtime");
function Field3(props) {
const [statusProps, fieldProps, rootProps] = splitProps(
props,
["disabled", "required", "readOnly", "invalid"],
["label", "helperText", "secondaryHelperText", "errorText", "children"]
);
return /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(FieldRoot, { ...statusProps, ...rootProps, children: [
/* @__PURE__ */ (0, import_jsx_runtime10.jsx)(Show, { when: fieldProps.label, children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(FieldLabel, { children: fieldProps.label }) }),
fieldProps.children,
/* @__PURE__ */ (0, import_jsx_runtime10.jsxs)(import_jsx.HStack, { justifyContent: "space-between", w: "full", children: [
/* @__PURE__ */ (0, import_jsx_runtime10.jsx)(HelperText, { invalid: statusProps.invalid, children: fieldProps.helperText }),
/* @__PURE__ */ (0, import_jsx_runtime10.jsx)(FieldErrorText, { children: fieldProps.errorText }),
/* @__PURE__ */ (0, import_jsx_runtime10.jsx)(Show, { when: fieldProps.secondaryHelperText, children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(FieldHelperText, { children: fieldProps.secondaryHelperText }) })
] })
] });
}
// src/components/file-upload/file-status.tsx
var import_jsx_runtime11 = require("react/jsx-runtime");
var processStatus = /* @__PURE__ */ ((processStatus2) => {
processStatus2["TODO"] = "todo";
processStatus2["PROCESSING"] = "processing";
processStatus2["DONE"] = "done";
processStatus2["ERROR"] = "error";
return processStatus2;
})(processStatus || {});
function FileStatus(props) {
const { file, now, status, onClick, ...nativeProps } = props;
const actionLabel = (0, import_react3.useMemo)(() => getStatusActionLabel(status), [status]);
const palette = (0, import_react3.useMemo)(() => getPalette(status), [status]);
const modalIconPalette = (0, import_react3.useMemo)(() => getModalIconPalette(status), [status]);
const styles = (0, import_react3.useMemo)(() => {
switch (status) {
case "todo" /* TODO */:
return (0, import_recipes5.fileStatus)({ status: "todo" });
case "processing" /* PROCESSING */:
return (0, import_recipes5.fileStatus)({ status: "processing" });
case "done" /* DONE */:
return (0, import_recipes5.fileStatus)({ status: "done" });
case "error" /* ERROR */:
return (0, import_recipes5.fileStatus)({ status: "error" });
default:
return (0, import_recipes5.fileStatus)();
}
}, [status]);
const handleClick = (0, import_react3.useCallback)(
(e) => {
const actionStatus = getStatusActionLabel(
status
).toLocaleLowerCase();
onClick(actionStatus, e);
},
[onClick, status]
);
return /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(
"div",
{
...nativeProps,
className: (0, import_css2.cx)(nativeProps.className, styles.root, (0, import_patterns.hstack)()),
children: [
/* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
Avatar2,
{
gradient: modalIconPalette,
fallback: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(MatchFileStatusIcon, { size: 24, status })
}
),
/* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(
"div",
{
className: (0, import_patterns.vstack)({
alignItems: "flex-start",
gap: "0.12rem",
w: "full"
}),
children: [
/* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
"small",
{
className: (0, import_css2.css)({
color: "page.text.initial",
textStyle: "label-sm"
}),
children: file
}
),
/* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
ProgressBar,
{
id: props.id,
label: "File upload status",
now,
size: "sm"
}
),
/* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Field3, { label: "", invalid: modalIconPalette === "hades-dark", children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
FieldHelperText,
{
className: (0, import_css2.css)({
color: "page.text.100"
}),
id: `help:${file}`,
children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(MatchFileStatusText, { status, now })
}
) })
]
}
),
/* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
IconButton,
{
ariaLabel: actionLabel,
onClick: handleClick,
palette,
size: "sm",
children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(MatchStatusAction, { status })
}
)
]
}
);
}
function MatchFileStatusIcon(props) {
const { icons } = useCerberusContext();
const {
waitingFileUploader: TodoIcon,
fileUploader: FileUploaderIcon,
invalidAlt: InvalidIcon,
successNotification: DoneIcon
} = icons;
switch (props.status) {
case "todo" /* TODO */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(TodoIcon, { size: props.size });
case "processing" /* PROCESSING */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(FileUploaderIcon, { size: props.size });
case "done" /* DONE */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(DoneIcon, { size: props.size });
case "error" /* ERROR */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(InvalidIcon, { size: props.size });
default:
throw new Error("Unknown status");
}
}
function MatchFileStatusText(props) {
switch (props.status) {
case "todo" /* TODO */:
return "Waiting to upload";
case "processing" /* PROCESSING */:
return `${props.now}% Complete`;
case "done" /* DONE */:
return "File uploaded successfully";
case "error" /* ERROR */:
return "There was an error uploading the file";
default:
throw new Error("Invalid status");
}
}
function MatchStatusAction(props) {
const { icons } = useCerberusContext();
const { close: CloseIcon, redo: RedoIcon, delete: TrashIcon } = icons;
switch (props.status) {
case "todo" /* TODO */:
case "processing" /* PROCESSING */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(CloseIcon, {});
case "error" /* ERROR */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(RedoIcon, {});
case "done" /* DONE */:
return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(TrashIcon, {});
default:
throw new Error("Invalid status");
}
}
function getStatusActionLabel(status) {
switch (status) {
case "todo" /* TODO */:
case "processing" /* PROCESSING */:
return "Cancel";
case "error" /* ERROR */:
return "Retry";
case "done" /* DONE */:
return "Delete";
default:
return "";
}
}
function getPalette(status) {
switch (status) {
case "todo" /* TODO */:
case "processing" /* PROCESSING */:
return "danger";
case "error" /* ERROR */:
return "action";
case "done" /* DONE */:
return "danger";
default:
return "action";
}
}
function getModalIconPalette(status) {
switch (status) {
case "todo" /* TODO */:
case "processing" /* PROCESSING */:
return "charon-light";
case "error" /* ERROR */:
return "hades-dark";
case "done" /* DONE */:
return "thanatos-dark";
default:
return "charon-light";
}
}
// src/components/file-upload/file-uploader.tsx
var import_css3 = require("styled-system/css");
var import_patterns2 = require("styled-system/patterns");
var import_recipes6 = require("styled-system/recipes");
var import_jsx_runtime12 = require("react/jsx-runtime");
function FileUploader(props) {
var _a;
const { icons } = useCerberusContext();
const styles = (0, import_recipes6.fileUploader)();
const { waitingFileUploader: Icon } = icons;
return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(
"div",
{
...props.disabled ? { "aria-disabled": true } : {},
className: (0, import_css3.cx)(
(0, import_patterns2.vstack)({
justify: "center"
}),
styles.container
),
children: [
/* @__PURE__ */ (0, import_jsx_runtime12.jsx)("div", { className: styles.icon, children: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(Avatar2, { gradient: "charon-light", fallback: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(Icon, {}) }) }),
/* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(
"label",
{
className: (0, import_css3.cx)(
(0, import_patterns2.vstack)({
justify: "center"
}),
styles.label
),
htmlFor: props.name,
children: [
/* @__PURE__ */ (0, import_jsx_runtime12.jsx)(Show, { when: props.heading, children: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("p", { className: styles.heading, children: props.heading }) }),
"Import ",
(_a = props.accept) == null ? void 0 : _a.replace(",", ", "),
" files",
/* @__PURE__ */ (0, import_jsx_runtime12.jsx)("p", { className: styles.description, children: "Click to select files" }),
/* @__PURE__ */ (0, import_jsx_runtime12.jsx)(
"input",
{
...props,
className: (0, import_css3.cx)(props.className, styles.input),
type: "file"
}
)
]
}
)
]
}
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FileStatus,
FileUploader,
processStatus
});
//# sourceMappingURL=index.cjs.map