summaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-styles.tsx
blob: d47e9fb8d65491edbce8b01ede6493ec5871ea4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { RefObject, useEffect } from 'react';

export type UseStylesProps = {
  /**
   * A property name or a CSS variable.
   */
  property: string;
  /**
   * The styles.
   */
  styles: string;
  /**
   * A targeted element reference.
   */
  target: RefObject<HTMLElement>;
};

/**
 * Add styles to an element using a React reference.
 *
 * @param {UseStylesProps} props - An object with property, styles and target.
 */
const useStyles = ({ property, styles, target }: UseStylesProps) => {
  useEffect(() => {
    if (target.current) target.current.style.setProperty(property, styles);
  }, [property, styles, target]);
};

export default useStyles;