@hitachivantara/uikit-react-utils
Version:
UI Kit utilities package.
55 lines (54 loc) • 1.45 kB
JavaScript
import { useMemo } from "react";
import { serializeStyles } from "@emotion/serialize";
import { getRegisteredStyles, insertStyles } from "@emotion/utils";
import { clsx } from "clsx";
import { useEmotionCache } from "@hitachivantara/uikit-react-shared";
//#region src/hooks/useCss.ts
function getRef(args) {
if (args.length !== 1) return {
args,
ref: void 0
};
const [arg] = args;
if (!(arg instanceof Object)) return {
args,
ref: void 0
};
if (!("ref" in arg)) return {
args,
ref: void 0
};
const { ref, ...argCopy } = arg;
return {
args: [argCopy],
ref
};
}
var cssFactory = (() => {
function merge(registered, css, className) {
const registeredStyles = [];
const rawClassName = getRegisteredStyles(registered, registeredStyles, className);
if (registeredStyles.length < 2) return className;
return rawClassName + css(registeredStyles);
}
function innerCssFactory(cache) {
const css = (...styles) => {
const { ref, args } = getRef(styles);
const serialized = serializeStyles(args, cache.registered);
insertStyles(cache, serialized, false);
return `${cache.key}-${serialized.name}${ref === void 0 ? "" : ` ${ref}`}`;
};
const cx = (...args) => merge(cache.registered, css, clsx(args));
return {
css,
cx
};
}
return innerCssFactory;
})();
function useCss() {
const cache = useEmotionCache();
return useMemo(() => cssFactory(cache), [cache]);
}
//#endregion
export { useCss };