UNPKG

@wordpress/block-library

Version:
1 lines • 7 kB
{"version":3,"file":"icon-grid.cjs","names":["memo","forwardRef","Button","HtmlRenderer","useInstanceId","useMemo","groupIconsByCollection","useAsyncList","useRef","getScrollContainer","__"],"sources":["../../../../src/icon/components/custom-inserter/icon-grid.jsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport { Button } from '@wordpress/components';\nimport { useAsyncList, useInstanceId } from '@wordpress/compose';\nimport {\n\tforwardRef,\n\tmemo,\n\tuseRef,\n\tuseLayoutEffect,\n\tuseMemo,\n} from '@wordpress/element';\nimport { getScrollContainer } from '@wordpress/dom';\nimport HtmlRenderer from '../../../utils/html-renderer';\nimport { groupIconsByCollection } from './utils';\n\nconst BATCH_SIZE = 20;\n\n// Memoized so each async batch renders only the newly added icons.\nconst IconGridItem = memo(\n\tforwardRef( function IconGridItemContent(\n\t\t{ icon, isSelected, onChange },\n\t\tref\n\t) {\n\t\treturn (\n\t\t\t<Button\n\t\t\t\tref={ ref }\n\t\t\t\tclassName=\"wp-block-icon__inserter-grid-icons-list-item\"\n\t\t\t\tonClick={ () => onChange( icon.name ) }\n\t\t\t\tvariant={ isSelected ? 'primary' : undefined }\n\t\t\t\t__next40pxDefaultSize\n\t\t\t>\n\t\t\t\t<span className=\"wp-block-icon__inserter-grid-icons-list-item-icon\">\n\t\t\t\t\t<HtmlRenderer html={ icon.content } />\n\t\t\t\t</span>\n\t\t\t\t<span className=\"wp-block-icon__inserter-grid-icons-list-item-title\">\n\t\t\t\t\t{ icon.label }\n\t\t\t\t</span>\n\t\t\t</Button>\n\t\t);\n\t} )\n);\n\nexport default function IconGrid( { icons, onChange, value, collections } ) {\n\tconst instanceId = useInstanceId(\n\t\tIconGrid,\n\t\t'wp-block-icon__inserter-grid'\n\t);\n\n\tconst { groups, orderedIcons } = useMemo( () => {\n\t\tif ( ! icons?.length || ! collections ) {\n\t\t\treturn { groups: null, orderedIcons: icons };\n\t\t}\n\t\tconst iconGroups = groupIconsByCollection( icons, collections );\n\t\treturn {\n\t\t\tgroups: iconGroups,\n\t\t\torderedIcons: iconGroups.flatMap( ( group ) => group.icons ),\n\t\t};\n\t}, [ icons, collections ] );\n\n\tconst shownIcons = useAsyncList( orderedIcons, {\n\t\tstep: BATCH_SIZE,\n\t} );\n\n\t// Scroll the selected icon into view, but wait until enough icons render\n\t// below it so it can be centered rather than stuck at the bottom. Skip it\n\t// if the user has already scrolled the list.\n\tconst selectedIconRef = useRef();\n\tconst selectedIndex =\n\t\torderedIcons?.findIndex( ( icon ) => icon.name === value ) ?? -1;\n\tconst isReadyToScroll =\n\t\tselectedIndex >= 0 &&\n\t\t( shownIcons.length >= selectedIndex + BATCH_SIZE ||\n\t\t\tshownIcons.length === orderedIcons.length );\n\n\tuseLayoutEffect( () => {\n\t\tconst node = selectedIconRef.current;\n\t\tif ( ! isReadyToScroll || ! node ) {\n\t\t\treturn;\n\t\t}\n\t\tif ( getScrollContainer( node )?.scrollTop ) {\n\t\t\treturn;\n\t\t}\n\t\tnode.scrollIntoView( { block: 'center' } );\n\t}, [ isReadyToScroll ] );\n\n\tconst renderIcons = ( iconsToRender ) => (\n\t\t<div className=\"wp-block-icon__inserter-grid-icons-list\">\n\t\t\t{ iconsToRender.map( ( icon ) => (\n\t\t\t\t<IconGridItem\n\t\t\t\t\tkey={ icon.name }\n\t\t\t\t\tref={ icon.name === value ? selectedIconRef : undefined }\n\t\t\t\t\ticon={ icon }\n\t\t\t\t\tisSelected={ icon.name === value }\n\t\t\t\t\tonChange={ onChange }\n\t\t\t\t/>\n\t\t\t) ) }\n\t\t</div>\n\t);\n\n\tconst renderGroups = () => {\n\t\tlet offset = 0;\n\t\treturn groups.map( ( group ) => {\n\t\t\tconst start = offset;\n\t\t\toffset += group.icons.length;\n\t\t\t// Show this group's icons, as many as have loaded so far.\n\t\t\tconst shownGroupIcons = group.icons.slice(\n\t\t\t\t0,\n\t\t\t\tMath.max( 0, shownIcons.length - start )\n\t\t\t);\n\t\t\tif ( ! shownGroupIcons.length ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tconst headingId = `${ instanceId }-${ group.slug }`;\n\t\t\treturn (\n\t\t\t\t<div\n\t\t\t\t\tkey={ group.slug }\n\t\t\t\t\trole=\"group\"\n\t\t\t\t\taria-labelledby={ headingId }\n\t\t\t\t>\n\t\t\t\t\t<h2\n\t\t\t\t\t\tid={ headingId }\n\t\t\t\t\t\tclassName=\"wp-block-icon__inserter-grid-group-title\"\n\t\t\t\t\t>\n\t\t\t\t\t\t{ group.label }\n\t\t\t\t\t</h2>\n\t\t\t\t\t{ renderIcons( shownGroupIcons ) }\n\t\t\t\t</div>\n\t\t\t);\n\t\t} );\n\t};\n\n\tif ( ! icons?.length ) {\n\t\treturn (\n\t\t\t<div className=\"wp-block-icon__inserter-grid\">\n\t\t\t\t<div className=\"wp-block-icon__inserter-grid-no-results\">\n\t\t\t\t\t<p>{ __( 'No results found.' ) }</p>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t);\n\t}\n\n\treturn (\n\t\t<div className=\"wp-block-icon__inserter-grid\">\n\t\t\t{ groups ? renderGroups() : renderIcons( shownIcons ) }\n\t\t</div>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,MAAM,aAAa;AAGnB,MAAM,gBAAA,GAAeA,mBAAAA,KAAAA,EAAAA,GACpBC,mBAAAA,WAAAA,CAAY,SAAS,oBACpB,EAAE,MAAM,YAAY,YACpB,KACC;CACD,OACC,iBAAA,GAAA,kBAAA,KAAA,CAACC,sBAAAA,QAAD;EACO;EACN,WAAU;EACV,eAAgB,SAAU,KAAK,IAAK;EACpC,SAAU,aAAa,YAAY,KAAA;EACnC,uBAAA;EALD,UAAA,CAOC,iBAAA,GAAA,kBAAA,IAAA,CAAC,QAAD;GAAM,WAAU;GACf,UAAA,iBAAA,GAAA,kBAAA,IAAA,CAACC,iCAAAA,SAAD,EAAc,MAAO,KAAK,QAAW,CAAA;EAChC,CAAA,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