UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

46 lines 2.38 kB
import cl from "clsx"; import React, { forwardRef } from "react"; import { Slot } from "../slot/Slot.js"; import { createContext } from "../util/create-context.js"; const [RenameCSSProvider, useRenameCSS] = createContext({ hookName: "useRenameCSS", name: "RenameCSS", providerName: "RenameCSSProvider", defaultValue: { cn: cl }, }); export const compositeClassFunction = (...inputs) => { const classes = cl(inputs) /* Replaces only if start of string "navds- navds-"*/ .replace(/^navds-/g, "aksel-") /* Replaces all " navds-" hits */ .replace(/\snavds-/g, " aksel-"); return classes.trim(); }; const RenameCSS = ({ children }) => { return (React.createElement(RenameCSSProvider, { cn: compositeClassFunction }, children)); }; /* -------------------------------------------------------------------------- */ /* Theme provider */ /* -------------------------------------------------------------------------- */ const DEFAULT_COLOR = "accent"; const [ThemeProvider, useThemeInternal] = createContext({ hookName: "useTheme", name: "ThemeProvider", providerName: "ThemeProvider", defaultValue: { color: DEFAULT_COLOR, isDarkside: false, }, }); const Theme = forwardRef((props, ref) => { const context = useThemeInternal(false); const { children, className, asChild = false, theme = context === null || context === void 0 ? void 0 : context.theme, hasBackground: hasBackgroundProp = true, "data-color": color = context === null || context === void 0 ? void 0 : context.color, } = props; const isRoot = !(context === null || context === void 0 ? void 0 : context.isDarkside); const hasBackground = hasBackgroundProp !== null && hasBackgroundProp !== void 0 ? hasBackgroundProp : (isRoot && props.theme !== undefined); const SlotElement = asChild ? Slot : "div"; return (React.createElement(ThemeProvider, { theme: theme, color: color, isDarkside: true }, React.createElement(RenameCSS, null, React.createElement(SlotElement, { ref: ref, className: cl("aksel-theme", className, theme), "data-background": hasBackground, "data-color": color !== null && color !== void 0 ? color : "" }, children)))); }); export { Theme, useRenameCSS, useThemeInternal }; //# sourceMappingURL=Theme.js.map