UNPKG

css-vars-hook

Version:

css-vars-hook contains React hooks to set and manipulate CSS custom properties from React component.

112 lines (100 loc) 4.12 kB
import { AriaAttributes } from 'react'; import { FC } from 'react'; import { HTMLAttributes } from 'react'; import { JSX as JSX_2 } from 'react/jsx-runtime'; import { ReactNode } from 'react'; import { RefObject } from 'react'; import { Theme } from '../ThemeType'; import { UnitType as UnitType_2 } from '..'; export declare type DataAttributeKey = `data-${string}`; export declare type DataAttributes = Record<DataAttributeKey, string>; export declare type LibraryProps<TElement = HTMLDivElement> = AriaAttributes & { /** Provide an id for Provider component */ id?: HTMLAttributes<TElement>['id']; /** * Set native ARIA role attribute * @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles */ role?: HTMLAttributes<TElement>['role']; /** * Specify additional CSS class. This allows you to use styled(Component) * or the css prop in styled-components or emotion. */ className?: HTMLAttributes<TElement>['className']; }; /** * @public */ export declare type LocalRootProps = DataAttributes & LibraryProps & HTMLAttributes<HTMLElement> & { children?: ReactNode; /** Choose which HTMLElement to render as a root. div is default. */ as?: string; /** Apply initial theme. */ theme?: Theme_2; /** Provide theme setter function. */ setTheme?: (arg0: Theme_2) => void; }; /** * @public * Root theme context provider also creates div to contain CSS properties. * `ThemeType` is declared globally. * @see ThemeType * @see https://github.com/morewings/css-vars-hook#type-safety */ export declare const RootThemeProvider: FC<RootThemeProviderProps>; /** * @public */ export declare type RootThemeProviderProps = DataAttributes & LibraryProps & { children: ReactNode; theme: Theme_2; }; declare type Theme_2 = Record<string, UnitType>; export declare type UnitType = string | number; /** * @public * React hook to apply multiple CSS variables to generated local root element (LocalRoot) and manipulate them. * Theme type is inferred from provided theme parameter. * @example * const {setTheme, getTheme, LocalRoot, getVariable, setVariable} = useLocalTheme(); * const setThemeIvory = () => { * setTheme({foo: 'ivory'}); * console.log('full theme', getTheme()) // => {foo: 'ivory'}; * console.log('foo value', getVariable('foo')) // => 'ivory'; *}; * return <LocalRoot theme={{foo: 'bar'}} className="demo-local">//... */ export declare const useLocalTheme: <TElement extends HTMLElement>() => { /** Effect to apply new theme to LocalRoot */ setTheme: (nextTheme: Theme_2) => void; /** Get current theme set for LocalRoot */ getTheme: () => Theme_2 | undefined; /** Wrapper component which creates DOM node to store theme data */ LocalRoot: ({ children, ...restProps }: Omit<LocalRootProps, "setTheme">) => JSX_2.Element; /** React Mutable Ref object attached to LocalRoot */ ref: RefObject<TElement>; /** Get variable value within LocalRoot theme */ getVariable: (variableName: string) => UnitType | undefined; /** Effect to set new variable value within LocalRoot theme */ setVariable: (variableName: string, variableValue: UnitType) => void; }; /** * @public * React hook to apply multiple CSS variables to theme root and manipulate them. * `ThemeType` is defined on project level. * @see ThemeType * @see https://github.com/morewings/css-vars-hook#type-safety */ export declare const useRootTheme: () => { /** Effect to apply new theme to the application */ setTheme: (nextTheme: Theme) => void; /** Get current theme */ getTheme: () => Theme; /** Effect to set new variable value within active theme */ setVariable: (variableName: string, value: UnitType_2) => void; /** Get variable value within active theme */ getVariable: (variableName: string) => string; /** Effect to remove variable within active theme */ removeVariable: (variableName: string) => void; }; export { }