@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
26 lines (25 loc) • 1.16 kB
JavaScript
import * as React from 'react';
import { useTheme, CanvasProvider, } 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, ...elemProps }) => {
const localTheme = useTheme(theme);
localTheme._styleRewriteFn = convertToStaticStates;
return (React.createElement(CanvasProvider, { theme: localTheme, ...elemProps }, children));
};