UNPKG

@wordpress/widget-dashboard

Version:

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

1 lines • 10.6 kB
{"version":3,"file":"types.mjs","names":[],"sources":["../src/types.ts"],"sourcesContent":["/**\n * Dashboard-specific types: `DashboardWidget`, grid settings, and the\n * `WidgetDashboard` prop bag.\n *\n * The widget contract types (`WidgetName`, `WidgetType`, `WidgetRenderProps`,\n * `ResolveWidgetModule`) live in `@wordpress/widget-primitives` and are\n * imported from there directly; this module does not re-export them.\n */\nimport type { ReactNode } from 'react';\nimport type {\n\tColumnSteps,\n\tDashboardGridLayoutItem,\n\tDashboardLanesLayoutItem,\n} from '@wordpress/grid';\nimport type {\n\tWidgetName,\n\tWidgetType,\n\tResolveWidgetModule,\n} from '@wordpress/widget-primitives';\n\nexport type GridTilePlacement = Omit< DashboardGridLayoutItem, 'key' >;\nexport type MasonryTilePlacement = Omit< DashboardLanesLayoutItem, 'key' >;\n\n/**\n * Storage shape for a widget's placement.\n *\n * Structurally a union of every supported per-model shape, but the\n * intended invariant is stronger than the type suggests: every\n * placement in a given layout must match the shape of the currently\n * active `gridSettings.model`. Consumers that switch the model must\n * supply placements in the new shape; the render layer is allowed to\n * trust the active model and treat each placement as the matching\n * shape.\n *\n * The type system cannot enforce that invariant on its own (there is\n * no discriminator on the placement itself), so consider this union a\n * declaration of which shapes are *valid*, not which shape any given\n * placement happens to be at runtime.\n */\nexport type DashboardTilePlacement = GridTilePlacement | MasonryTilePlacement;\n\n/**\n * Attribute bag of a widget instance, as edited by the dashboard's\n * attribute surfaces.\n */\nexport type WidgetAttributeValues = Record< string, unknown >;\n\n/**\n * A widget placed on the dashboard.\n *\n * A `WidgetType` describes the blueprint. A `DashboardWidget` is a\n * concrete placement of that type on a specific dashboard: its unique\n * id, the type it references, user-configured attributes, and its\n * `placement` in the grid.\n */\nexport interface DashboardWidget< Item = unknown > {\n\t/**\n\t * Unique instance identifier.\n\t */\n\tuuid: string;\n\n\t/**\n\t * Widget type name — must match a `WidgetType.name` in `widgetTypes`.\n\t */\n\ttype: WidgetName;\n\n\t/**\n\t * User-configured attributes for this instance.\n\t */\n\tattributes?: Item;\n\n\t/**\n\t * Grid-model-specific placement (column/row spans, ordering,\n\t * etc.). Must match the shape implied by the dashboard's active\n\t * `gridSettings.model`; see `DashboardTilePlacement` for the\n\t * invariant.\n\t */\n\tplacement?: DashboardTilePlacement;\n}\n\n/**\n * Identity of a widget within the rendering tree. Returned by\n * `useWidgetContext()`; `null` when called outside a widget render subtree.\n */\nexport interface WidgetContextValue {\n\t/**\n\t * Widget instance id.\n\t */\n\tuuid: string;\n\n\t/**\n\t * Widget type name.\n\t */\n\tname: WidgetName;\n\n\t/**\n\t * Index of the widget in the `layout` array.\n\t */\n\tindex: number;\n}\n\n/**\n * Identifier for the active grid model. Drives which `@wordpress/grid`\n * surface the dashboard mounts and which per-model settings the\n * `WidgetGridSettings` union admits.\n *\n * Model names describe user-facing concepts. The mapping to the\n * underlying `@wordpress/grid` component is an implementation detail\n * resolved in the render layer; `'masonry'` is rendered today through\n * `DashboardLanes` (skyline placement) but could swap to a future\n * native `display: grid-lanes` path without affecting the model name.\n */\nexport type WidgetGridModel = 'grid' | 'masonry';\n\n/**\n * Column count of the default policy on a wide container. A default, not\n * a ceiling: the package renders whatever the host asks for.\n */\nexport const WIDGET_DASHBOARD_COLUMN_COUNT = 4;\n\n/**\n * Column policy used when the host sets neither `gridSettings.columns` nor\n * `gridSettings.minColumnWidth`: one column from 0px, two from 600px and\n * {@link WIDGET_DASHBOARD_COLUMN_COUNT} from 960px of container width. The\n * steps are data the host can replace; the package applies none of its own.\n */\nexport const WIDGET_DASHBOARD_COLUMN_STEPS: ColumnSteps = {\n\t0: 1,\n\t600: 2,\n\t960: WIDGET_DASHBOARD_COLUMN_COUNT,\n};\n\n/**\n * Settings common to every grid model. The column policy travels to the\n * `@wordpress/grid` surface untouched: the surface measures its own width\n * and resolves the count from `columns` and `minColumnWidth`.\n *\n * `spacing` is intentionally absent: the gap between tiles is\n * presentational and lives with the design-system theme/density, not\n * with per-dashboard settings. The grid surface keeps the prop for\n * programmatic overrides, but the dashboard does not propagate it.\n */\ninterface BaseWidgetGridSettings {\n\t/**\n\t * Column count. A number applies at every width; a map applies a count\n\t * from each container width up (`{ 0: 1, 600: 2, 960: 4 }` is one\n\t * column from 0px, two from 600px, four from 960px). A step can be\n\t * `'auto'`, which lifts the cap from that width up and leaves the count\n\t * to `minColumnWidth`; with no floor set, the step is skipped and the\n\t * count below it holds. Counts are floored at one with no ceiling.\n\t * Absent, the policy is the default steps (`DEFAULT_GRID.columns`), or\n\t * whatever `minColumnWidth` fits when that is set.\n\t */\n\tcolumns?: number | ColumnSteps;\n\n\t/**\n\t * Per-column floor in pixels. The surface fits as many columns as its\n\t * width allows at this floor, capped by `columns` when set, down to one.\n\t * A non-finite or non-positive value is unset.\n\t */\n\tminColumnWidth?: number;\n}\n\n/**\n * 2D packed grid settings. Items declare explicit width and height\n * spans; rows use a uniform track height via `rowHeight`.\n */\nexport interface WidgetGridLayoutSettings extends BaseWidgetGridSettings {\n\tmodel?: 'grid';\n\n\t/**\n\t * Row height in pixels. Any positive value is honored; `ROW_HEIGHT_PRESETS`\n\t * suggests three. Every tile in a row fills the row vertically, and a\n\t * tile's stored `height` counts rows of this size.\n\t */\n\trowHeight?: number;\n}\n\n/**\n * Masonry settings. Heights are content-driven; resize is\n * horizontal-only. `flowTolerance` tunes how aggressively the placer\n * preserves source order vs. minimizing empty regions.\n */\nexport interface WidgetMasonryLayoutSettings extends BaseWidgetGridSettings {\n\tmodel: 'masonry';\n\n\t/**\n\t * Pixel tolerance for source-order tiebreaking when two candidate\n\t * columns have similar baselines.\n\t */\n\tflowTolerance?: number;\n}\n\n/**\n * Discriminated union of supported grid-model configurations.\n *\n * When `model` is omitted the dashboard treats the settings as the\n * 2D packed grid (`'grid'`) for backwards compatibility with the\n * pre-union shape.\n */\nexport type WidgetGridSettings =\n\tWidgetGridLayoutSettings | WidgetMasonryLayoutSettings;\n\n/**\n * The operations that act on one placed widget.\n */\nexport type DashboardInstanceOperation = 'remove' | 'move' | 'resize' | 'edit';\n\n/**\n * A request for an instance operation: the placed widget, and its type\n * when registered. The type is absent when it is not in `widgetTypes`\n * (a plugin gone, a module that failed to load): the engine still asks,\n * because the instance is still on the board, but a policy that keys\n * locks on the type has nothing to match.\n */\nexport interface DashboardInstanceOperationRequest {\n\toperation: DashboardInstanceOperation;\n\twidget: DashboardWidget;\n\twidgetType?: WidgetType;\n}\n\n/**\n * An operation the dashboard asks permission for, with its subject.\n * `customize` is entering customize mode; `reset` is restoring the default\n * layout; `insert` is offering a widget type in the inserter; `remove`,\n * `move`, `resize`, and `edit` act on a placed widget.\n */\nexport type DashboardOperationRequest =\n\t| { operation: 'customize' }\n\t| { operation: 'reset' }\n\t| { operation: 'insert'; widgetType: WidgetType }\n\t| DashboardInstanceOperationRequest;\n\n/**\n * Answers whether an operation is allowed. Return `true` for operations\n * you do not govern: policies compose restrictively, so a default `false`\n * would deny every operation the engine adds later.\n */\nexport type CanPerformDashboardOperation = (\n\trequest: DashboardOperationRequest\n) => boolean;\n\n/**\n * Props for `WidgetDashboard`.\n *\n * The consumer owns the committed layout state; the dashboard maintains\n * a staging copy internally for in-progress edits, and `onLayoutChange`\n * fires only when the user commits via the Done action.\n */\nexport interface WidgetDashboardProps {\n\t/**\n\t * Widget instances to render. Consumer owns this state.\n\t */\n\tlayout: DashboardWidget[];\n\n\t/**\n\t * Called when the user commits in-progress edits via the Done action.\n\t * Receives the full layout array as it should be persisted. In-progress\n\t * mutations (reorder, resize, add, remove, attribute edits) accumulate\n\t * in the dashboard's internal staging layer and do not fire this\n\t * callback until commit.\n\t */\n\tonLayoutChange: ( layout: DashboardWidget[] ) => void;\n\n\t/**\n\t * Called when the layout is reset to the default.\n\t */\n\tonLayoutReset?: () => void;\n\n\t/**\n\t * Widget types available for rendering. The dashboard never queries a\n\t * store directly — consumers scope and filter via this prop.\n\t */\n\twidgetTypes: WidgetType[];\n\n\t/**\n\t * When true, widget types are still loading. Instances whose type is\n\t * not yet in `widgetTypes` show a loading state instead of missing.\n\t */\n\tisResolvingWidgetTypes?: boolean;\n\n\t/**\n\t * Whether the dashboard is in edit mode (enables drag/resize).\n\t */\n\teditMode?: boolean;\n\n\t/**\n\t * Called when edit mode toggles via `WidgetDashboard.Actions`.\n\t */\n\tonEditChange?: ( next: boolean ) => void;\n\n\t/**\n\t * Overrides the default `import()` resolution of\n\t * `WidgetType.renderModule`. Useful for tests, Storybook, or future\n\t * remote-URL loading.\n\t */\n\tresolveWidgetModule?: ResolveWidgetModule;\n\n\t/**\n\t * Grid model configuration. See `WidgetGridSettings` for the shape.\n\t * Read-only for the dashboard: the consumer owns the settings and\n\t * their persistence.\n\t */\n\tgridSettings?: WidgetGridSettings;\n\n\tchildren?: ReactNode;\n}\n"],"mappings":";;;;;AAsHA,MAAa,gCAAgC;;;;;;;AAQ7C,MAAa,gCAA6C;CACzD,GAAG;CACH,KAAK;CACL,KAAA;AACD"}