@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
8 lines (7 loc) • 4.42 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/widget-header/widget-header-fit.ts"],
"sourcesContent": ["import type { Ref } from 'react';\nimport { useResizeObserver } from '@wordpress/compose';\nimport { createContext, useContext, useEffect } from '@wordpress/element';\n\nconst WidgetHeaderAvailableSizeContext = createContext< number | null >( null );\n\nexport const WidgetHeaderAvailableSizeProvider =\n\tWidgetHeaderAvailableSizeContext.Provider;\n\n/**\n * Inline size (px) the header row can still grant its toolbar, or `null`\n * while unmeasured or outside a header. Toolbar controls compare their\n * natural width against it to pick a presentation that fits.\n */\nexport function useWidgetHeaderAvailableSize(): number | null {\n\treturn useContext( WidgetHeaderAvailableSizeContext );\n}\n\ninterface WidgetHeaderReserve {\n\tregisterReserved: ( id: string, width: number ) => void;\n\tunregisterReserved: ( id: string ) => void;\n}\n\nconst WidgetHeaderReserveContext = createContext< WidgetHeaderReserve >( {\n\tregisterReserved: () => {},\n\tunregisterReserved: () => {},\n} );\n\nexport const WidgetHeaderReserveProvider = WidgetHeaderReserveContext.Provider;\n\n/**\n * Reserves a trailing section's footprint from the header's fit budget.\n *\n * A section that sits beside the collapsible controls (the actions menu, and\n * whatever the header gains next) attaches the returned ref to its root. Its\n * measured width, plus the gap before it, is discounted from the space the\n * collapsible controls plan for, so no section overflows onto another.\n *\n * @param {string} id Stable identifier for the reserving section.\n */\nexport function useReserveHeaderSpace< T extends HTMLElement = HTMLElement >(\n\tid: string\n): Ref< T > {\n\tconst { registerReserved, unregisterReserved } = useContext(\n\t\tWidgetHeaderReserveContext\n\t);\n\n\tconst ref = useResizeObserver< T >(\n\t\t( [ entry ] ) => {\n\t\t\tconst { columnGap } = getComputedStyle(\n\t\t\t\tentry.target.parentElement as HTMLElement\n\t\t\t);\n\n\t\t\tregisterReserved(\n\t\t\t\tid,\n\t\t\t\t( entry.borderBoxSize?.[ 0 ]?.inlineSize ?? 0 ) +\n\t\t\t\t\t( parseFloat( columnGap ) || 0 )\n\t\t\t);\n\t\t},\n\t\t{ box: 'border-box' }\n\t);\n\n\tuseEffect(\n\t\t() => () => unregisterReserved( id ),\n\t\t[ id, unregisterReserved ]\n\t);\n\n\treturn ref;\n}\n\n/**\n * Reserves a container's own padding and border from the header's fit budget.\n *\n * Observing the border box is what makes a padding change notify: it leaves\n * the content box untouched.\n *\n * @param {string} id Stable identifier for the reserving container.\n */\nexport function useReserveHeaderPadding< T extends HTMLElement = HTMLElement >(\n\tid: string\n): Ref< T > {\n\tconst { registerReserved, unregisterReserved } = useContext(\n\t\tWidgetHeaderReserveContext\n\t);\n\n\tconst ref = useResizeObserver< T >(\n\t\t( [ entry ] ) => {\n\t\t\tconst border = entry.borderBoxSize?.[ 0 ]?.inlineSize ?? 0;\n\t\t\tconst content = entry.contentBoxSize?.[ 0 ]?.inlineSize ?? 0;\n\n\t\t\tregisterReserved( id, Math.max( 0, border - content ) );\n\t\t},\n\t\t{ box: 'border-box' }\n\t);\n\n\tuseEffect(\n\t\t() => () => unregisterReserved( id ),\n\t\t[ id, unregisterReserved ]\n\t);\n\n\treturn ref;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,qBAAkC;AAClC,qBAAqD;AAErD,IAAM,uCAAmC,8BAAgC,IAAK;AAEvE,IAAM,oCACZ,iCAAiC;AAO3B,SAAS,+BAA8C;AAC7D,aAAO,2BAAY,gCAAiC;AACrD;AAOA,IAAM,iCAA6B,8BAAsC;AAAA,EACxE,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,oBAAoB,MAAM;AAAA,EAAC;AAC5B,CAAE;AAEK,IAAM,8BAA8B,2BAA2B;AAY/D,SAAS,sBACf,IACW;AACX,QAAM,EAAE,kBAAkB,mBAAmB,QAAI;AAAA,IAChD;AAAA,EACD;AAEA,QAAM,UAAM;A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