diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-10-28 17:12:58 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-11 18:15:27 +0100 |
| commit | 60c49f18389ff625177a57277ef8f292a31097bf (patch) | |
| tree | 76b0f1f1792b57659e54d282f93df70088446e3c /src/utils/hooks/use-attributes.tsx | |
| parent | 05f1dfc6896d3affa7c494a1b955f230d836a4b7 (diff) | |
refactor(providers,hooks): rewrite PrismThemeProvider & usePrismTheme
* reuse Theme provider logic
* move DOM mutation from provider to hook
* add a script to init theme before page load
Diffstat (limited to 'src/utils/hooks/use-attributes.tsx')
| -rw-r--r-- | src/utils/hooks/use-attributes.tsx | 50 |
1 files changed, 0 insertions, 50 deletions
diff --git a/src/utils/hooks/use-attributes.tsx b/src/utils/hooks/use-attributes.tsx deleted file mode 100644 index 20e9947..0000000 --- a/src/utils/hooks/use-attributes.tsx +++ /dev/null @@ -1,50 +0,0 @@ -import { useCallback, useEffect } from 'react'; -import { fromKebabCaseToCamelCase } from '../helpers'; - -export type useAttributesProps = { - /** - * An HTML element. - */ - element?: HTMLElement; - /** - * A node list of HTML Element. - */ - elements?: NodeListOf<HTMLElement> | HTMLElement[]; - /** - * The attribute name. - */ - attribute: string; - /** - * The attribute value. - */ - value: string; -}; - -/** - * Set HTML attributes to the given element or to the HTML document. - * - * @param props - An object with element, attribute name and value. - */ -export const useAttributes = ({ - element, - elements, - attribute, - value, -}: useAttributesProps) => { - const setAttribute = useCallback( - (el: HTMLElement) => { - if (attribute.startsWith('data')) { - el.setAttribute(attribute, value); - } else { - const camelCaseAttribute = fromKebabCaseToCamelCase(attribute); - el.dataset[camelCaseAttribute] = value; - } - }, - [attribute, value] - ); - - useEffect(() => { - if (element) setAttribute(element); - if (elements && elements.length > 0) elements.forEach(setAttribute); - }, [element, elements, setAttribute]); -}; |
