From 60c49f18389ff625177a57277ef8f292a31097bf Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sat, 28 Oct 2023 17:12:58 +0200 Subject: 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 --- src/utils/hooks/use-attributes.tsx | 50 -------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 src/utils/hooks/use-attributes.tsx (limited to 'src/utils/hooks/use-attributes.tsx') 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[]; - /** - * 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]); -}; -- cgit v1.2.3