From c6212f927daf3c928f479afa052e4772216a2d8a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 21 Nov 2023 16:10:20 +0100 Subject: refactor(components): replace items prop in Navbar component * replace `items` prop with `children` prop: it is more readable this way, * handle navbar item state inside NavbarItem component: it avoid using three differents states and their methods to do exactly the same thing * remove useAutofocus hook since we can't use it anymore * add `onActivation` and `activationHandlerDelay` prop to NavbarItem component to be able to focus the search input only when the item is activated (it replicates the functioning of useAutofocus hook) * replace `ref` type in SearchForm component: it does not make sense to use an input ref for a form. Instead I use useImperativeHandle to provide different a focus method to the given ref. --- src/utils/hooks/use-autofocus/use-autofocus.ts | 40 -------------------------- 1 file changed, 40 deletions(-) delete mode 100644 src/utils/hooks/use-autofocus/use-autofocus.ts (limited to 'src/utils/hooks/use-autofocus/use-autofocus.ts') diff --git a/src/utils/hooks/use-autofocus/use-autofocus.ts b/src/utils/hooks/use-autofocus/use-autofocus.ts deleted file mode 100644 index 0d21a59..0000000 --- a/src/utils/hooks/use-autofocus/use-autofocus.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { useCallback, useRef, type MutableRefObject } from 'react'; -import { useTimeout } from '../use-timeout'; - -export type UseAutofocusCondition = () => boolean; - -export type UseAutofocusConfig = { - /** - * A condition to met before giving focus to the element. - */ - condition?: UseAutofocusCondition; - /** - * A delay in ms before giving focus to the element. - */ - delay?: number; -}; - -/** - * React hook to give focus to an element automatically. - * - * @param {UseAutofocusConfig} [config] - A configuration object. - * @returns {RefObject} The element reference. - */ -export const useAutofocus = ( - config?: UseAutofocusConfig -): MutableRefObject => { - const { condition, delay } = config ?? {}; - const ref = useRef(null); - - const setFocus = useCallback(() => { - const shouldFocus = condition ? condition() : true; - - if (ref.current && shouldFocus) { - ref.current.focus(); - } - }, [condition]); - - useTimeout(setFocus, delay); - - return ref; -}; -- cgit v1.2.3