@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
1 lines • 5.46 kB
Source Map (JSON)
{"version":3,"file":"widget-layout-controls.cjs","names":["useDashboardInternalContext","Menu","IconButton","moreVertical","__","trash"],"sources":["../../../src/components/widget-layout-controls/widget-layout-controls.tsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport { moreVertical, trash } from '@wordpress/icons';\nimport {\n\t// eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of IconButton.\n\tIconButton,\n\t// eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of the new Menu, pending WordPress/gutenberg#76135.\n\tMenu,\n} from '@wordpress/ui';\nimport { useDashboardInternalContext } from '../../context/dashboard-context';\nimport type { DashboardWidget, GridTilePlacement } from '../../types';\n\ntype NamedGridWidth = Exclude<\n\tNonNullable< GridTilePlacement[ 'width' ] >,\n\tnumber\n>;\n\nexport interface WidgetLayoutControlsProps {\n\t/**\n\t * The instance these controls manage within the layout.\n\t */\n\twidget: DashboardWidget< unknown >;\n\n\t/**\n\t * Whether the policy allows removing the instance.\n\t *\n\t * @default true\n\t */\n\tcanRemove?: boolean;\n\n\t/**\n\t * Whether the policy allows resizing the instance.\n\t *\n\t * @default true\n\t */\n\tcanResize?: boolean;\n}\n\n/**\n * Customize-mode controls: width menu and removal, each following the\n * policy's answer for its operation.\n *\n * @param {WidgetLayoutControlsProps} props Component props.\n */\nexport function WidgetLayoutControls( {\n\twidget,\n\tcanRemove = true,\n\tcanResize = true,\n}: WidgetLayoutControlsProps ): React.ReactNode {\n\tconst { layout, onLayoutChange } = useDashboardInternalContext();\n\tconst width = widget.placement?.width;\n\n\tconst updateWidth = ( nextWidth: GridTilePlacement[ 'width' ] ) => {\n\t\tconst nextLayout = layout.map( ( currentWidget ) =>\n\t\t\tcurrentWidget.uuid === widget.uuid\n\t\t\t\t? {\n\t\t\t\t\t\t...currentWidget,\n\t\t\t\t\t\tplacement: {\n\t\t\t\t\t\t\t...currentWidget.placement,\n\t\t\t\t\t\t\twidth: nextWidth,\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\t: currentWidget\n\t\t);\n\t\tonLayoutChange( nextLayout );\n\t};\n\n\tconst onNamedWidthChange = ( nextWidth: NamedGridWidth ) => {\n\t\tupdateWidth( nextWidth );\n\t};\n\n\tconst onRemove = () => {\n\t\tonLayoutChange(\n\t\t\tlayout.filter(\n\t\t\t\t( currentWidget ) => currentWidget.uuid !== widget.uuid\n\t\t\t)\n\t\t);\n\t};\n\n\treturn (\n\t\t<>\n\t\t\t{ canResize && (\n\t\t\t\t<Menu.Root>\n\t\t\t\t\t<Menu.Trigger\n\t\t\t\t\t\trender={\n\t\t\t\t\t\t\t<IconButton\n\t\t\t\t\t\t\t\ticon={ moreVertical }\n\t\t\t\t\t\t\t\tlabel={ __( 'Widget options' ) }\n\t\t\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\t\t\tvariant=\"minimal\"\n\t\t\t\t\t\t\t\ttone=\"neutral\"\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t}\n\t\t\t\t\t/>\n\n\t\t\t\t\t<Menu.Popup>\n\t\t\t\t\t\t<Menu.RadioGroup\n\t\t\t\t\t\t\tvalue={ width ?? null }\n\t\t\t\t\t\t\tonValueChange={ onNamedWidthChange }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Menu.GroupLabel>{ __( 'Width' ) }</Menu.GroupLabel>\n\t\t\t\t\t\t\t<Menu.RadioItem value=\"fill\" closeOnClick>\n\t\t\t\t\t\t\t\t<Menu.ItemLabel>\n\t\t\t\t\t\t\t\t\t{ __( 'Use available width' ) }\n\t\t\t\t\t\t\t\t</Menu.ItemLabel>\n\t\t\t\t\t\t\t</Menu.RadioItem>\n\t\t\t\t\t\t\t<Menu.RadioItem value=\"full\" closeOnClick>\n\t\t\t\t\t\t\t\t<Menu.ItemLabel>\n\t\t\t\t\t\t\t\t\t{ __( 'Make full width' ) }\n\t\t\t\t\t\t\t\t</Menu.ItemLabel>\n\t\t\t\t\t\t\t</Menu.RadioItem>\n\t\t\t\t\t\t</Menu.RadioGroup>\n\t\t\t\t\t</Menu.Popup>\n\t\t\t\t</Menu.Root>\n\t\t\t) }\n\n\t\t\t{ canRemove && (\n\t\t\t\t<IconButton\n\t\t\t\t\ticon={ trash }\n\t\t\t\t\tlabel={ __( 'Remove' ) }\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tvariant=\"minimal\"\n\t\t\t\t\ttone=\"neutral\"\n\t\t\t\t\tonClick={ onRemove }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA2CA,SAAgB,qBAAsB,EACrC,QACA,YAAY,MACZ,YAAY,QACmC;CAC/C,MAAM,EAAE,QAAQ,oBAAA,GAAmBA,oCAAAA,4BAAAA,CAA4B;CAC/D,MAAM,QAAQ,OAAO,WAAW;CAEhC,MAAM,eAAgB,cAA6C;EAClE,MAAM,aAAa,OAAO,KAAO,kBAChC,cAAc,SAAS,OAAO,OAC3B;GACA,GAAG;GACH,WAAW;IACV,GAAG,cAAc;IACjB,OAAO;GACR;EACD,IACC,aACJ;EACA,eAAgB,UAAW;CAC5B;CAEA,MAAM,sBAAuB,cAA+B;EAC3D,YAAa,SAAU;CACxB;CAEA,MAAM,iBAAiB;EACtB,eACC,OAAO,QACJ,kBAAmB,cAAc,SAAS,OAAO,IACpD,CACD;CACD;CAEA,OACC,iBAAA,GAAA,kBAAA,KAAA,CAAA,kBAAA,UAAA,EAAA,UAAA,CACG,aACD,iBAAA,GAAA,kBAAA,KAAA,CAACC,cAAAA,KAAK,MAAN,EAAA,UAAA,CACC,iBAAA,GAAA,kBAAA,IAAA,CAACA,cAAAA,KAAK,SAAN,EACC,QACC,iBAAA,GAAA,kBAAA,IAAA,CAACC,cAAAA,YAAD;EACC,MAAOC,iBAAAA;EACP,QAAA,GAAQC,gBAAAA,GAAA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