UNPKG

@wordpress/widget-dashboard

Version:

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

1 lines • 4.17 kB
{"version":3,"file":"widget-header-fit.mjs","names":[],"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":";;;AAIA,MAAM,mCAAmC,cAAgC,IAAK;AAE9E,MAAa,oCACZ,iCAAiC;;;;;;AAOlC,SAAgB,+BAA8C;CAC7D,OAAO,WAAY,gCAAiC;AACrD;AAOA,MAAM,6BAA6B,cAAsC;CACxE,wBAAwB,CAAC;CACzB,0BAA0B,CAAC;AAC5B,CAAE;AAEF,MAAa,8BAA8B,2BAA2B;;;;;;;;;;;AAYtE,SAAgB,sBACf,IACW;CACX,MAAM,EAAE,kBAAkB,uBAAuB,WAChD,0BACD;CAEA,MAAM,MAAM,mBACT,CAAE,WAAa;EAChB,MAAM,EAAE,cAAc,iBACrB,MAAM,OAAO,aACd;EAEA,iBACC,KACE,MAAM,gBAAiB,EAAG,EAAE,cAAc,MACzC,WAAY,SAAU,KAAK,EAC/B;CACD,GACA,EAAE,KAAK,aAAa,CACrB;CAEA,sBACa,mBAAoB,EAAG,GACnC,CAAE,IAAI,kBAAmB,CAC1B;CAEA,OAAO;AACR;;;;;;;;;AAUA,SAAgB,wBACf,IACW;CACX,MAAM,EAAE,kBAAkB,uBAAuB,WAChD,0BACD;CAEA,MAAM,MAAM,mBACT,CAAE,WAAa;EAChB,MAAM,SAAS,MAAM,gBAAiB,EAAG,EAAE,cAAc;EACzD,MAAM,UAAU,MAAM,iBAAkB,EAAG,EAAE,cAAc;EAE3D,iBAAkB,IAAI,KAAK,IAAK,GAAG,SAAS,OAAQ,CAAE;CACvD,GACA,EAAE,KAAK,aAAa,CACrB;CAEA,sBACa,mBAAoB,EAAG,GACnC,CAAE,IAAI,kBAAmB,CAC1B;CAEA,OAAO;AACR"}