UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

29 lines (28 loc) 1.54 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { ThemeProvider } from '@emotion/react'; import { CanvasProvider, isNumericalTheme, useTheme, } from '@workday/canvas-kit-react/common'; export const convertToStaticStates = obj => { if (!obj) { return obj; } return Object.keys(obj).reduce((result, key) => { const newKey = key .replace(/^:/, '&:') // handle shorthand like ":focus" .replace(/,(\s+):/g, ',$1&:') // handle selectors like ":focus, :hover" .replace(/:(focus|hover|active)/g, '.$1'); // Remove any selectors that use data-whatinput. Leaving them in would cause focus rings to disappear when the user clicks with a mouse. if (/data-whatinput/.test(key)) { return result; } const value = typeof obj[key] === 'object' ? convertToStaticStates(obj[key]) : obj[key]; const newObj = { ...result, [newKey]: value }; return newObj; }, {}); }; export const StaticStates = ({ children, theme, className, ...elemProps }) => { const localTheme = useTheme(theme && !isNumericalTheme(theme) ? theme : undefined); localTheme._styleRewriteFn = convertToStaticStates; // Nest ThemeProvider *inside* CanvasProvider so CanvasProvider's own ThemeProvider // (used for legacy Emotion theme consumers) does not wipe `_styleRewriteFn`. return (_jsx(CanvasProvider, { className: className, ...elemProps, theme: theme, children: _jsx(ThemeProvider, { theme: localTheme, children: children }) })); };