UNPKG

@wordpress/widget-dashboard

Version:

Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.

1 lines • 2.71 kB
{"version":3,"file":"use-inline-fit.mjs","names":[],"sources":["../../../src/components/widget-attributes/use-inline-fit.ts"],"sourcesContent":["import type { Ref } from 'react';\nimport { useResizeObserver } from '@wordpress/compose';\nimport { useState } from '@wordpress/element';\nimport { useWidgetHeaderAvailableSize } from '../widget-header/widget-header-fit';\n\ninterface InlineFitOptions {\n\t/**\n\t * Holds the current presentation while the user interacts with it (an\n\t * open dropdown, focus inside the inline form). Measurements keep\n\t * flowing; the pending transition applies when the hold releases.\n\t */\n\tlocked?: boolean;\n}\n\ninterface InlineFit {\n\t/**\n\t * Ref for the element wrapping the inline controls at their natural\n\t * width. While mounted, the measurement tracks it live; when the caller\n\t * unmounts it (collapsed presentation), the last value is retained and\n\t * drives the expand-back decision, and remounting re-measures it.\n\t */\n\tmeasureRef: Ref< HTMLDivElement >;\n\n\t/**\n\t * Whether the inline controls exceed the header's budget and the\n\t * collapsed presentation should show instead.\n\t */\n\tcollapsed: boolean;\n}\n\n/**\n * Decides whether the inline attribute controls fit the space the header\n * row can grant its toolbar.\n *\n * @param {InlineFitOptions} [options] Fit options.\n */\nexport function useInlineFit( options: InlineFitOptions = {} ): InlineFit {\n\tconst { locked = false } = options;\n\n\tconst availableSize = useWidgetHeaderAvailableSize();\n\tconst [ naturalSize, setNaturalSize ] = useState( 0 );\n\n\tconst measureRef = useResizeObserver< HTMLDivElement >( ( [ entry ] ) =>\n\t\tsetNaturalSize( entry.contentRect.width )\n\t);\n\n\t// The zero guards keep environments without layout (JSDOM) on the\n\t// inline presentation.\n\tconst computed =\n\t\tavailableSize !== null &&\n\t\tnaturalSize > 0 &&\n\t\tnaturalSize > availableSize;\n\n\t// The latch: while locked, the surface the user is interacting with\n\t// must not be replaced under them, so the last unlocked decision holds.\n\t// Releasing the lock lets the pending transition apply.\n\tconst [ held, setHeld ] = useState( computed );\n\tif ( ! locked && held !== computed ) {\n\t\tsetHeld( computed );\n\t}\n\n\treturn { measureRef, collapsed: locked ? held : computed };\n}\n"],"mappings":";;;;;;;;;;AAoCA,SAAgB,aAAc,UAA4B,CAAC,GAAe;CACzE,MAAM,EAAE,SAAS,UAAU;CAE3B,MAAM,gBAAgB,6BAA6B;CACnD,MAAM,CAAE,aAAa,kBAAmB,SAAU,CAAE;CAEpD,MAAM,aAAa,mBAAuC,CAAE,WAC3D,eAAgB,MAAM,YAAY,KAAM,CACzC;CAIA,MAAM,WACL,kBAAkB,QAClB,cAAc,KACd,cAAc;CAKf,MAAM,CAAE,MAAM,WAAY,SAAU,QAAS;CAC7C,IAAK,CAAE,UAAU,SAAS,UACzB,QAAS,QAAS;CAGnB,OAAO;EAAE;EAAY,WAAW,SAAS,OAAO;CAAS;AAC1D"}