Download wp-calypso/packages/domain-search/src/hooks/use-container-query.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/packages/domain-search/src/hooks/use-container-query.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/packages/domain-search/src/hooks/use-container-query.ts
-
curl -L -o use-container-query.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/packages/domain-search/src/hooks/use-container-query.ts
1.1 kB
| import { useEvent, useResizeObserver } from '@wordpress/compose'; | |
| import { useLayoutEffect, useRef, useState } from 'react'; | |
| export const useContainerQuery = < T extends string >( | |
| queries: Record< T, number > | |
| ): { | |
| ref: ( element: Element | null | undefined ) => void; | |
| activeQuery: T; | |
| } => { | |
| const [ activeQuery, setActiveQuery ] = useState< T >( Object.keys( queries )[ 0 ] as T ); | |
| const initialMountRef = useRef< Element | null | undefined >( null ); | |
| const callback = useEvent( ( width: number ) => { | |
| const query = Object.entries< number >( queries ).findLast( ( [ , value ] ) => width >= value ); | |
| if ( query ) { | |
| setActiveQuery( query[ 0 ] as T ); | |
| } | |
| } ); | |
| useLayoutEffect( () => { | |
| if ( ! initialMountRef.current ) { | |
| return; | |
| } | |
| callback( initialMountRef.current.getBoundingClientRect().width ); | |
| }, [ callback ] ); | |
| const resizableContainerRef = useResizeObserver( ( [ element ] ) => { | |
| callback( element.contentRect.width ); | |
| } ); | |
| return { | |
| ref: ( element ) => { | |
| initialMountRef.current = element; | |
| resizableContainerRef( element ); | |
| }, | |
| activeQuery, | |
| }; | |
| }; | |