{"version":3,"file":"icon-grid.mjs","names":[],"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,eAAe,KACpB,WAAY,SAAS,oBACpB,EAAE,MAAM,YAAY,YACpB,KACC;CACD,OACC,qBAAC,QAAD;EACO;EACN,WAAU;EACV,eAAgB,SAAU,KAAK,IAAK;EACpC,SAAU,aAAa,YAAY,KAAA;EACnC,uBAAA;EALD,UAAA,CAOC,oBAAC,QAAD;GAAM,WAAU;GACf,UAAA,oBAAC,cAAD,EAAc,MAAO,KAAK,QAAW,CAAA;EAChC,CAAA,GACN,oBAAC,QAAD;GAAM,WAAU;GACb,UAAA,KAAK;EACF,CAAA,CACC;;AAEV,CAAE,CACH;AAEA,SAAwB,SAAU,EAAE,OAAO,UAAU,OAAO,eAAgB;CAC3E,MAAM,aAAa,cAClB,UACA,8BACD;CAEA,MAAM,EAAE,QAAQ,iBAAiB,cAAe;EAC/C,IAAK,CAAE,OAAO,UAAU,CAAE,aACzB,OAAO;GAAE,QAAQ;GAAM,cAAc;EAAM;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