@veracity/vui
Version:
Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.
60 lines (59 loc) • 2.35 kB
JavaScript
//#region src/button/utils.ts
/** Matches DEFAULT_BORDER_WIDTH in consts.ts; duplicated here to avoid a circular import. */
const DEFAULT_BORDER_WIDTH_FALLBACK = 1;
/** Canonical hierarchy keys recognised by the two-prop API. */
const HIERARCHY_KEYS = /* @__PURE__ */ new Set([
"primary",
"secondary",
"tertiary"
]);
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
/**
* Converts a two-prop variant+intent pair into the combined theme key used
* internally (e.g. `resolveVariantKey('primary','brand') => 'primaryBrand'`).
* When `intent` is omitted, `'brand'` is used as the default
* (e.g. `resolveVariantKey('primary') => 'primaryBrand'`).
*
* If `variant` is already a fully-qualified legacy key (e.g. `'primaryBrand'`)
* it is returned unchanged so the new API is backward-compatible.
*
* Returns `'primaryBrand'` (the default variant) when `variant` is absent.
*/
const resolveVariantKey = (variant, intent) => {
if (!variant) return `primary${capitalize(intent ?? "brand")}`;
if (!HIERARCHY_KEYS.has(variant)) return variant;
return `${variant}${capitalize(intent ?? "brand")}`;
};
/**
* Extracts a numeric value from various input types (number, string with px, etc.).
* Moved here from button.tsx so it can be used and tested independently.
*
* @param value - Value to extract number from
* @param fallback - Default value if extraction fails
*/
const extractNumericValue = (value, fallback = DEFAULT_BORDER_WIDTH_FALLBACK) => {
if (typeof value === "number") return value;
if (typeof value === "string") {
const parsed = parseInt(value, 10);
if (!isNaN(parsed)) return parsed;
}
return fallback;
};
/**
* Wraps a props object so each mapped key becomes `var(--css-var, originalValue)`.
* Unmapped keys are passed through unchanged.
* Defined here (not in button.tsx) so it can be imported without pulling in React.
*/
const wrapWithVars = (props, mapping) => {
if (!props || Object.keys(props).length === 0) return {};
const result = {};
Object.entries(props).forEach(([key, value]) => {
if (key in mapping && value !== void 0) result[key] = `var(${mapping[key]}, ${value})`;
else if (!(key in mapping)) result[key] = value;
});
return result;
};
//#endregion
export { extractNumericValue, resolveVariantKey, wrapWithVars };
globalThis.__vuiVersion__ = "5.3.1"
//# sourceMappingURL=utils.js.map