'use client' import { forwardRef, useState } from 'react' import { Slot } from '@radix-ui/react-slot' import { useIsomorphicLayoutEffect } from '~/src/hooks/useIsomorphicLayoutEffect' import useMergeRefs from '~/src/hooks/useMergeRefs' import { type AutoFocusProps } from './AutoFocus.types' /** * `AutoFocus` is a component that automatically focuses its child element when they are added to the document. * It is useful when you want to focus on a specific element when the component is mounted. * It doesn't render any DOM node. * @example * * ```tsx * * * * * // You can also use it with the additional condition * * * * ``` */ export const AutoFocus = forwardRef( function AutoFocus({ children, when = true, ...rest }, forwardedRef) { const [target, setTarget] = useState(null) useIsomorphicLayoutEffect( function focus() { if (target && when) { target.focus() } }, [target, when] ) const ref = useMergeRefs(setTarget, forwardedRef) return ( {children} ) } )