UNPKG

@wordpress/components

Version:
1 lines • 9.57 kB
{"version":3,"file":"autocompleter-ui.cjs","names":["Button","getDefaultUseItems","useAnchor","useState","useRef","useMergeRefs","useRefEffect","useDebounce","speak","sprintf","_n","__","Popover","createPortal","VisuallyHidden"],"sources":["../../src/autocomplete/autocompleter-ui.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport { createPortal } from 'react-dom';\nimport {\n\tuseLayoutEffect,\n\tuseRef,\n\tuseEffect,\n\tuseState,\n} from '@wordpress/element';\nimport { useAnchor } from '@wordpress/rich-text';\nimport { useDebounce, useMergeRefs, useRefEffect } from '@wordpress/compose';\nimport { speak } from '@wordpress/a11y';\nimport { __, _n, sprintf } from '@wordpress/i18n';\nimport getDefaultUseItems from './get-default-use-items';\nimport Button from '../button';\nimport Popover from '../popover';\nimport { VisuallyHidden } from '../visually-hidden';\nimport type { AutocompleterUIProps, KeyedOption } from './types';\n\ntype ListBoxProps = {\n\titems: KeyedOption[];\n\tonSelect: ( option: KeyedOption ) => void;\n\tonHover: ( index: number ) => void;\n\tselectedIndex: number;\n\tinstanceId: number;\n\tlistBoxId: string | undefined;\n\tclassName?: string;\n\tComponent?: React.ElementType;\n};\n\nfunction ListBox( {\n\titems,\n\tonSelect,\n\tonHover,\n\tselectedIndex,\n\tinstanceId,\n\tlistBoxId,\n\tclassName,\n\tComponent = 'div',\n}: ListBoxProps ) {\n\treturn (\n\t\t<Component\n\t\t\tid={ listBoxId }\n\t\t\trole=\"listbox\"\n\t\t\tclassName=\"components-autocomplete__results\"\n\t\t>\n\t\t\t{ items.map( ( option, index ) => (\n\t\t\t\t<Button\n\t\t\t\t\tkey={ option.key }\n\t\t\t\t\tid={ `components-autocomplete-item-${ instanceId }-${ option.key }` }\n\t\t\t\t\trole=\"option\"\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\taria-selected={ index === selectedIndex }\n\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\tdisabled={ option.isDisabled }\n\t\t\t\t\tclassName={ clsx(\n\t\t\t\t\t\t'components-autocomplete__result',\n\t\t\t\t\t\tclassName,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'is-selected': index === selectedIndex,\n\t\t\t\t\t\t}\n\t\t\t\t\t) }\n\t\t\t\t\tonClick={ () => onSelect( option ) }\n\t\t\t\t\tonMouseMove={ () => {\n\t\t\t\t\t\tif ( index !== selectedIndex ) {\n\t\t\t\t\t\t\tonHover( index );\n\t\t\t\t\t\t}\n\t\t\t\t\t} }\n\t\t\t\t>\n\t\t\t\t\t{ option.label }\n\t\t\t\t</Button>\n\t\t\t) ) }\n\t\t</Component>\n\t);\n}\n\nexport function AutocompleterUI( {\n\tautocompleter,\n\tfilterValue,\n\tinstanceId,\n\tlistBoxId,\n\tclassName,\n\tselectedIndex,\n\tonChangeOptions,\n\tonSelect,\n\tonHover,\n\treset,\n\tcontentRef,\n}: AutocompleterUIProps ) {\n\t// The useItems hook is derived from the autocompleter prop. This is safe\n\t// because the parent renders this component with key={autocompleter.name},\n\t// ensuring a fresh mount (and stable hook identity) when the completer changes.\n\tconst useItems =\n\t\tautocompleter.useItems ?? getDefaultUseItems( autocompleter );\n\tconst [ items ] = useItems( filterValue );\n\tconst popoverAnchor = useAnchor( {\n\t\teditableContentElement: contentRef.current,\n\t} );\n\n\tconst [ needsA11yCompat, setNeedsA11yCompat ] = useState( false );\n\tconst popoverRef = useRef< HTMLElement >( null );\n\tconst popoverRefs = useMergeRefs( [\n\t\tpopoverRef,\n\t\tuseRefEffect(\n\t\t\t( node ) => {\n\t\t\t\tif ( ! contentRef.current ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// If the popover is rendered in a different document than\n\t\t\t\t// the content, we need to duplicate the options list in the\n\t\t\t\t// content document so that it's available to the screen\n\t\t\t\t// readers, which check the DOM ID based aria-* attributes.\n\t\t\t\tsetNeedsA11yCompat(\n\t\t\t\t\tnode.ownerDocument !== contentRef.current.ownerDocument\n\t\t\t\t);\n\t\t\t},\n\t\t\t[ contentRef ]\n\t\t),\n\t] );\n\n\tuseOnClickOutside( popoverRef, reset );\n\n\tconst debouncedSpeak = useDebounce( speak, 500 );\n\n\tfunction announce( options: Array< KeyedOption > ) {\n\t\tif ( ! debouncedSpeak ) {\n\t\t\treturn;\n\t\t}\n\t\tif ( !! options.length ) {\n\t\t\tif ( filterValue ) {\n\t\t\t\tdebouncedSpeak(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t/* translators: %d: number of results. */\n\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t'%d result found, use up and down arrow keys to navigate.',\n\t\t\t\t\t\t\t'%d results found, use up and down arrow keys to navigate.',\n\t\t\t\t\t\t\toptions.length\n\t\t\t\t\t\t),\n\t\t\t\t\t\toptions.length\n\t\t\t\t\t),\n\t\t\t\t\t'assertive'\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tdebouncedSpeak(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t/* translators: %d: number of results. */\n\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t'Initial %d result loaded. Type to filter all available results. Use up and down arrow keys to navigate.',\n\t\t\t\t\t\t\t'Initial %d results loaded. Type to filter all available results. Use up and down arrow keys to navigate.',\n\t\t\t\t\t\t\toptions.length\n\t\t\t\t\t\t),\n\t\t\t\t\t\toptions.length\n\t\t\t\t\t),\n\t\t\t\t\t'assertive'\n\t\t\t\t);\n\t\t\t}\n\t\t} else {\n\t\t\tdebouncedSpeak( __( 'No results.' ), 'assertive' );\n\t\t}\n\t}\n\n\tuseLayoutEffect( () => {\n\t\tonChangeOptions( items );\n\t\tannounce( items );\n\t\t// We want to avoid introducing unexpected side effects.\n\t\t// See https://github.com/WordPress/gutenberg/pull/41820\n\t}, [ items ] );\n\n\tif ( items.length === 0 ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t<Popover\n\t\t\t\toffset={ 8 }\n\t\t\t\tfocusOnMount={ false }\n\t\t\t\tplacement=\"top-start\"\n\t\t\t\tclassName=\"components-autocomplete__popover\"\n\t\t\t\tanchor={ popoverAnchor }\n\t\t\t\tref={ popoverRefs }\n\t\t\t>\n\t\t\t\t<ListBox\n\t\t\t\t\titems={ items }\n\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\tonHover={ onHover }\n\t\t\t\t\tselectedIndex={ selectedIndex }\n\t\t\t\t\tinstanceId={ instanceId }\n\t\t\t\t\tlistBoxId={ listBoxId }\n\t\t\t\t\tclassName={ className }\n\t\t\t\t/>\n\t\t\t</Popover>\n\t\t\t{ contentRef.current &&\n\t\t\t\tneedsA11yCompat &&\n\t\t\t\tcreatePortal(\n\t\t\t\t\t<ListBox\n\t\t\t\t\t\titems={ items }\n\t\t\t\t\t\tonSelect={ onSelect }\n\t\t\t\t\t\tonHover={ onHover }\n\t\t\t\t\t\tselectedIndex={ selectedIndex }\n\t\t\t\t\t\tinstanceId={ instanceId }\n\t\t\t\t\t\tlistBoxId={ listBoxId }\n\t\t\t\t\t\tclassName={ className }\n\t\t\t\t\t\tComponent={ VisuallyHidden }\n\t\t\t\t\t/>,\n\t\t\t\t\tcontentRef.current.ownerDocument.body\n\t\t\t\t) }\n\t\t</>\n\t);\n}\n\nfunction useOnClickOutside(\n\tref: React.RefObject< HTMLElement | null >,\n\thandler: AutocompleterUIProps[ 'reset' ]\n) {\n\tuseEffect( () => {\n\t\tconst listener = ( event: MouseEvent | TouchEvent ) => {\n\t\t\t// Do nothing if clicking ref's element or descendent elements, or if the ref is not referencing an element\n\t\t\tif (\n\t\t\t\t! ref.current ||\n\t\t\t\tref.current.contains( event.target as Node )\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\thandler( event );\n\t\t};\n\t\tdocument.addEventListener( 'mousedown', listener );\n\t\tdocument.addEventListener( 'touchstart', listener );\n\t\treturn () => {\n\t\t\tdocument.removeEventListener( 'mousedown', listener );\n\t\t\tdocument.removeEventListener( 'touchstart', listener );\n\t\t};\n\t}, [ handler, ref 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