UNPKG

@zag-js/core

Version:

A minimal implementation of xstate fsm for UI machines

56 lines (55 loc) 1.51 kB
// src/merge-props.ts import { callAll, isString } from "@zag-js/utils"; var clsx = (...args) => args.map((str) => str?.trim?.()).filter(Boolean).join(" "); var CSS_REGEX = /((?:--)?(?:\w+-?)+)\s*:\s*([^;]*)/g; var serialize = (style) => { const res = {}; let match; while (match = CSS_REGEX.exec(style)) { res[match[1]] = match[2]; } return res; }; var css = (a, b) => { if (isString(a)) { if (isString(b)) return `${a};${b}`; a = serialize(a); } else if (isString(b)) { b = serialize(b); } return Object.assign({}, a ?? {}, b ?? {}); }; function mergeProps(...args) { let result = {}; for (let props of args) { if (!props) continue; for (let key in result) { if (key.startsWith("on") && typeof result[key] === "function" && typeof props[key] === "function") { result[key] = callAll(props[key], result[key]); continue; } if (key === "className" || key === "class") { result[key] = clsx(result[key], props[key]); continue; } if (key === "style") { result[key] = css(result[key], props[key]); continue; } result[key] = props[key] !== void 0 ? props[key] : result[key]; } for (let key in props) { if (result[key] === void 0) { result[key] = props[key]; } } const symbols = Object.getOwnPropertySymbols(props); for (let symbol of symbols) { result[symbol] = props[symbol]; } } return result; } export { mergeProps };