@wordpress/widget-primitives 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE.md +788 -0
- package/README.md +76 -0
- package/build/components/widget-render/index.cjs +31 -0
- package/build/components/widget-render/index.cjs.map +7 -0
- package/build/components/widget-render/widget-render.cjs +50 -0
- package/build/components/widget-render/widget-render.cjs.map +7 -0
- package/build/hooks/index.cjs +31 -0
- package/build/hooks/index.cjs.map +7 -0
- package/build/hooks/use-widget-types.cjs +84 -0
- package/build/hooks/use-widget-types.cjs.map +7 -0
- package/build/index.cjs +34 -0
- package/build/index.cjs.map +7 -0
- package/build/tools/get-lazy-widget-component/get-lazy-widget-component.cjs +50 -0
- package/build/tools/get-lazy-widget-component/get-lazy-widget-component.cjs.map +7 -0
- package/build/tools/get-lazy-widget-component/index.cjs +31 -0
- package/build/tools/get-lazy-widget-component/index.cjs.map +7 -0
- package/build/types.cjs +19 -0
- package/build/types.cjs.map +7 -0
- package/build-module/components/widget-render/index.mjs +6 -0
- package/build-module/components/widget-render/index.mjs.map +7 -0
- package/build-module/components/widget-render/widget-render.mjs +25 -0
- package/build-module/components/widget-render/widget-render.mjs.map +7 -0
- package/build-module/hooks/index.mjs +6 -0
- package/build-module/hooks/index.mjs.map +7 -0
- package/build-module/hooks/use-widget-types.mjs +59 -0
- package/build-module/hooks/use-widget-types.mjs.map +7 -0
- package/build-module/index.mjs +8 -0
- package/build-module/index.mjs.map +7 -0
- package/build-module/tools/get-lazy-widget-component/get-lazy-widget-component.mjs +25 -0
- package/build-module/tools/get-lazy-widget-component/get-lazy-widget-component.mjs.map +7 -0
- package/build-module/tools/get-lazy-widget-component/index.mjs +6 -0
- package/build-module/tools/get-lazy-widget-component/index.mjs.map +7 -0
- package/build-module/types.mjs +1 -0
- package/build-module/types.mjs.map +7 -0
- package/build-types/components/widget-render/index.d.ts +2 -0
- package/build-types/components/widget-render/index.d.ts.map +1 -0
- package/build-types/components/widget-render/stories/index.story.d.ts +19 -0
- package/build-types/components/widget-render/stories/index.story.d.ts.map +1 -0
- package/build-types/components/widget-render/widget-render.d.ts +13 -0
- package/build-types/components/widget-render/widget-render.d.ts.map +1 -0
- package/build-types/hooks/index.d.ts +2 -0
- package/build-types/hooks/index.d.ts.map +1 -0
- package/build-types/hooks/use-widget-types.d.ts +17 -0
- package/build-types/hooks/use-widget-types.d.ts.map +1 -0
- package/build-types/index.d.ts +13 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/tools/get-lazy-widget-component/get-lazy-widget-component.d.ts +12 -0
- package/build-types/tools/get-lazy-widget-component/get-lazy-widget-component.d.ts.map +1 -0
- package/build-types/tools/get-lazy-widget-component/index.d.ts +2 -0
- package/build-types/tools/get-lazy-widget-component/index.d.ts.map +1 -0
- package/build-types/types.d.ts +169 -0
- package/build-types/types.d.ts.map +1 -0
- package/package.json +72 -0
- package/src/components/widget-render/index.ts +1 -0
- package/src/components/widget-render/stories/index.story.tsx +356 -0
- package/src/components/widget-render/widget-render.tsx +44 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-widget-types.ts +90 -0
- package/src/index.ts +21 -0
- package/src/stories/introduction.mdx +14 -0
- package/src/tools/get-lazy-widget-component/get-lazy-widget-component.ts +62 -0
- package/src/tools/get-lazy-widget-component/index.ts +1 -0
- package/src/types.ts +196 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-widget-types.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useState } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { WidgetModuleRecord, WidgetName, WidgetType } from '../types';\n\n/* `true` while records or their metadata imports are still resolving; hosts\n must not treat a widget instance as missing until it is `false`. */\ntype UseWidgetTypesResult = readonly [ WidgetType[], boolean ];\n\n/**\n * Resolves widget types from host-supplied records.\n *\n * For each record it dynamically imports `widget_module` and merges the\n * module's default export with the runtime fields (`name`, `renderModule`).\n * Pass `null`/`undefined` while records are still loading.\n *\n * @param records Host-supplied records, or `null`/`undefined` while loading.\n */\nexport function useWidgetTypes(\n\trecords: WidgetModuleRecord[] | null | undefined\n): UseWidgetTypesResult {\n\tconst [ widgetTypes, setWidgetTypes ] = useState< WidgetType[] >( [] );\n\tconst [ isResolvingWidgetTypes, setIsResolvingWidgetTypes ] =\n\t\tuseState( true );\n\n\tuseEffect( () => {\n\t\tif ( records === null || records === undefined ) {\n\t\t\tsetIsResolvingWidgetTypes( true );\n\t\t\treturn;\n\t\t}\n\n\t\tif ( records.length === 0 ) {\n\t\t\tsetWidgetTypes( [] );\n\t\t\tsetIsResolvingWidgetTypes( false );\n\t\t\treturn;\n\t\t}\n\n\t\tlet cancelled = false;\n\t\tsetIsResolvingWidgetTypes( true );\n\n\t\tPromise.all(\n\t\t\trecords.map( async ( record ) => {\n\t\t\t\tif ( ! record.widget_module ) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst module = await import(\n\t\t\t\t\t\t/* webpackIgnore: true */ record.widget_module\n\t\t\t\t\t);\n\n\t\t\t\t\tif ( ! module?.default ) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...( module.default as Partial< WidgetType > ),\n\t\t\t\t\t\tname: record.name as WidgetName,\n\t\t\t\t\t\trenderModule: record.render_module ?? '',\n\t\t\t\t\t\t...( record.presentation\n\t\t\t\t\t\t\t? { presentation: record.presentation }\n\t\t\t\t\t\t\t: {} ),\n\t\t\t\t\t} as WidgetType;\n\t\t\t\t} catch {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t} )\n\t\t).then( ( results ) => {\n\t\t\tif ( cancelled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetWidgetTypes(\n\t\t\t\tresults.filter( ( t ): t is WidgetType => t !== null )\n\t\t\t);\n\t\t\tsetIsResolvingWidgetTypes( false );\n\t\t} );\n\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t};\n\t}, [ records ] );\n\n\treturn [ widgetTypes, isResolvingWidgetTypes ];\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,WAAW,gBAAgB;AAoB7B,SAAS,eACf,SACuB;AACvB,QAAM,CAAE,aAAa,cAAe,IAAI,SAA0B,CAAC,CAAE;AACrE,QAAM,CAAE,wBAAwB,yBAA0B,IACzD,SAAU,IAAK;AAEhB,YAAW,MAAM;AAChB,QAAK,YAAY,QAAQ,YAAY,QAAY;AAChD,gCAA2B,IAAK;AAChC;AAAA,IACD;AAEA,QAAK,QAAQ,WAAW,GAAI;AAC3B,qBAAgB,CAAC,CAAE;AACnB,gCAA2B,KAAM;AACjC;AAAA,IACD;AAEA,QAAI,YAAY;AAChB,8BAA2B,IAAK;AAEhC,YAAQ;AAAA,MACP,QAAQ,IAAK,OAAQ,WAAY;AAChC,YAAK,CAAE,OAAO,eAAgB;AAC7B,iBAAO;AAAA,QACR;AAEA,YAAI;AACH,gBAAM,SAAS,MAAM;AAAA;AAAA,YACM,OAAO;AAAA;AAGlC,cAAK,CAAE,QAAQ,SAAU;AACxB,mBAAO;AAAA,UACR;AAEA,iBAAO;AAAA,YACN,GAAK,OAAO;AAAA,YACZ,MAAM,OAAO;AAAA,YACb,cAAc,OAAO,iBAAiB;AAAA,YACtC,GAAK,OAAO,eACT,EAAE,cAAc,OAAO,aAAa,IACpC,CAAC;AAAA,UACL;AAAA,QACD,QAAQ;AACP,iBAAO;AAAA,QACR;AAAA,MACD,CAAE;AAAA,IACH,EAAE,KAAM,CAAE,YAAa;AACtB,UAAK,WAAY;AAChB;AAAA,MACD;AAEA;AAAA,QACC,QAAQ,OAAQ,CAAE,MAAwB,MAAM,IAAK;AAAA,MACtD;AACA,gCAA2B,KAAM;AAAA,IAClC,CAAE;AAEF,WAAO,MAAM;AACZ,kBAAY;AAAA,IACb;AAAA,EACD,GAAG,CAAE,OAAQ,CAAE;AAEf,SAAO,CAAE,aAAa,sBAAuB;AAC9C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Components\n */\nexport { WidgetRender } from './components/widget-render';\n\n/**\n * Hooks\n */\nexport { useWidgetTypes } from './hooks';\n\n/**\n * Types\n */\nexport type {\n\tWidgetName,\n\tWidgetIcon,\n\tWidgetType,\n\tWidgetRenderProps,\n\tResolveWidgetModule,\n\tWidgetModuleRecord,\n} from './types';\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,oBAAoB;AAK7B,SAAS,sBAAsB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// packages/widget-primitives/src/tools/get-lazy-widget-component/get-lazy-widget-component.ts
|
|
2
|
+
import { lazy } from "@wordpress/element";
|
|
3
|
+
function isValidWidgetModule(module) {
|
|
4
|
+
return typeof module === "object" && module !== null && "default" in module && typeof module.default === "function";
|
|
5
|
+
}
|
|
6
|
+
var componentCache = /* @__PURE__ */ new Map();
|
|
7
|
+
function getLazyWidgetComponent(renderModule, resolveWidgetModule) {
|
|
8
|
+
const cached = componentCache.get(renderModule);
|
|
9
|
+
if (cached) {
|
|
10
|
+
return cached;
|
|
11
|
+
}
|
|
12
|
+
const lazyComponent = lazy(async () => {
|
|
13
|
+
const module = await resolveWidgetModule(renderModule);
|
|
14
|
+
if (!isValidWidgetModule(module)) {
|
|
15
|
+
throw new Error(`Invalid widget module: ${renderModule}`);
|
|
16
|
+
}
|
|
17
|
+
return module;
|
|
18
|
+
});
|
|
19
|
+
componentCache.set(renderModule, lazyComponent);
|
|
20
|
+
return lazyComponent;
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
getLazyWidgetComponent
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=get-lazy-widget-component.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/tools/get-lazy-widget-component/get-lazy-widget-component.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentType } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { lazy } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tResolveWidgetModule,\n\tWidgetModule,\n\tWidgetRenderProps,\n} from '../../types';\n\ntype LazyWidgetComponent = ComponentType< WidgetRenderProps< unknown > >;\n\nfunction isValidWidgetModule( module: unknown ): module is WidgetModule {\n\treturn (\n\t\ttypeof module === 'object' &&\n\t\tmodule !== null &&\n\t\t'default' in module &&\n\t\ttypeof ( module as { default: unknown } ).default === 'function'\n\t);\n}\n\n/*\n * Cache keyed by `renderModule`. The lazy component must keep a stable\n * identity across renders; rebuilding it inline (e.g. via `useMemo`) resets\n * the Suspense boundary and the resolved module.\n */\nconst componentCache = new Map< string, LazyWidgetComponent >();\n\n/*\n * Resolve a widget render module to a `lazy()` React component, cached by\n * `renderModule` id so repeated calls return the same instance.\n */\nexport function getLazyWidgetComponent(\n\trenderModule: string,\n\tresolveWidgetModule: ResolveWidgetModule\n): LazyWidgetComponent {\n\tconst cached = componentCache.get( renderModule );\n\tif ( cached ) {\n\t\treturn cached;\n\t}\n\n\tconst lazyComponent = lazy< LazyWidgetComponent >( async () => {\n\t\tconst module: unknown = await resolveWidgetModule( renderModule );\n\t\tif ( ! isValidWidgetModule( module ) ) {\n\t\t\tthrow new Error( `Invalid widget module: ${ renderModule }` );\n\t\t}\n\n\t\treturn module;\n\t} );\n\n\tcomponentCache.set( renderModule, lazyComponent );\n\treturn lazyComponent;\n}\n"],
|
|
5
|
+
"mappings": ";AAQA,SAAS,YAAY;AAarB,SAAS,oBAAqB,QAA0C;AACvE,SACC,OAAO,WAAW,YAClB,WAAW,QACX,aAAa,UACb,OAAS,OAAiC,YAAY;AAExD;AAOA,IAAM,iBAAiB,oBAAI,IAAmC;AAMvD,SAAS,uBACf,cACA,qBACsB;AACtB,QAAM,SAAS,eAAe,IAAK,YAAa;AAChD,MAAK,QAAS;AACb,WAAO;AAAA,EACR;AAEA,QAAM,gBAAgB,KAA6B,YAAY;AAC9D,UAAM,SAAkB,MAAM,oBAAqB,YAAa;AAChE,QAAK,CAAE,oBAAqB,MAAO,GAAI;AACtC,YAAM,IAAI,MAAO,0BAA2B,YAAa,EAAG;AAAA,IAC7D;AAEA,WAAO;AAAA,EACR,CAAE;AAEF,iBAAe,IAAK,cAAc,aAAc;AAChD,SAAO;AACR;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/widget-render/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
5
|
+
/**
|
|
6
|
+
* WordPress dependencies
|
|
7
|
+
*/
|
|
8
|
+
import '@wordpress/components/build-style/style.css';
|
|
9
|
+
import '@wordpress/dataviews/build-style/style.css';
|
|
10
|
+
/**
|
|
11
|
+
* Internal dependencies
|
|
12
|
+
*/
|
|
13
|
+
import { WidgetRender } from '..';
|
|
14
|
+
declare const meta: Meta<typeof WidgetRender>;
|
|
15
|
+
export default meta;
|
|
16
|
+
export declare const Default: StoryObj;
|
|
17
|
+
export declare const WithSettings: StoryObj;
|
|
18
|
+
export declare const WithHostChrome: StoryObj;
|
|
19
|
+
//# sourceMappingURL=index.story.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.story.d.ts","sourceRoot":"","sources":["../../../../src/components/widget-render/stories/index.story.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAG5D;;GAEG;AAGH,OAAO,6CAA6C,CAAC;AAErD,OAAO,4CAA4C,CAAC;AAOpD;;GAEG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AA4HlC,QAAA,MAAM,IAAI,EAAE,IAAI,CAAE,OAAO,YAAY,CAwBpC,CAAC;eAEa,IAAI;AAqBnB,eAAO,MAAM,OAAO,EAAE,QAgBrB,CAAC;AAyDF,eAAO,MAAM,YAAY,EAAE,QAiB1B,CAAC;AAwDF,eAAO,MAAM,cAAc,EAAE,QAe5B,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { ResolveWidgetModule, WidgetType } from '../../types';
|
|
5
|
+
interface WidgetRenderProps<Item = unknown> {
|
|
6
|
+
widgetType: WidgetType<Item>;
|
|
7
|
+
attributes?: Item;
|
|
8
|
+
setAttributes?: (next: Partial<Item>) => void;
|
|
9
|
+
resolveWidgetModule: ResolveWidgetModule;
|
|
10
|
+
}
|
|
11
|
+
export declare function WidgetRender<Item = unknown>({ widgetType, attributes, setAttributes, resolveWidgetModule }: WidgetRenderProps<Item>): import("react").JSX.Element;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=widget-render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"widget-render.d.ts","sourceRoot":"","sources":["../../../src/components/widget-render/widget-render.tsx"],"names":[],"mappings":"AAAA;;GAEG;AAMH,OAAO,KAAK,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEnE,UAAU,iBAAiB,CAAE,IAAI,GAAG,OAAO;IAC1C,UAAU,EAAE,UAAU,CAAE,IAAI,CAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,IAAI,CAAC;IAClB,aAAa,CAAC,EAAE,CAAE,IAAI,EAAE,OAAO,CAAE,IAAI,CAAE,KAAM,IAAI,CAAC;IAClD,mBAAmB,EAAE,mBAAmB,CAAC;CACzC;AAOD,wBAAgB,YAAY,CAAE,IAAI,GAAG,OAAO,EAAI,EAC/C,UAAU,EACV,UAAU,EACV,aAAa,EACb,mBAAmB,EACnB,EAAE,iBAAiB,CAAE,IAAI,CAAE,+BAgB3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { WidgetModuleRecord, WidgetType } from '../types';
|
|
5
|
+
type UseWidgetTypesResult = readonly [WidgetType[], boolean];
|
|
6
|
+
/**
|
|
7
|
+
* Resolves widget types from host-supplied records.
|
|
8
|
+
*
|
|
9
|
+
* For each record it dynamically imports `widget_module` and merges the
|
|
10
|
+
* module's default export with the runtime fields (`name`, `renderModule`).
|
|
11
|
+
* Pass `null`/`undefined` while records are still loading.
|
|
12
|
+
*
|
|
13
|
+
* @param records Host-supplied records, or `null`/`undefined` while loading.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useWidgetTypes(records: WidgetModuleRecord[] | null | undefined): UseWidgetTypesResult;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=use-widget-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-widget-types.d.ts","sourceRoot":"","sources":["../../src/hooks/use-widget-types.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAc,UAAU,EAAE,MAAM,UAAU,CAAC;AAI3E,KAAK,oBAAoB,GAAG,SAAS,CAAE,UAAU,EAAE,EAAE,OAAO,CAAE,CAAC;AAE/D;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,kBAAkB,EAAE,GAAG,IAAI,GAAG,SAAS,GAC9C,oBAAoB,CAgEtB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Components
|
|
3
|
+
*/
|
|
4
|
+
export { WidgetRender } from './components/widget-render';
|
|
5
|
+
/**
|
|
6
|
+
* Hooks
|
|
7
|
+
*/
|
|
8
|
+
export { useWidgetTypes } from './hooks';
|
|
9
|
+
/**
|
|
10
|
+
* Types
|
|
11
|
+
*/
|
|
12
|
+
export type { WidgetName, WidgetIcon, WidgetType, WidgetRenderProps, ResolveWidgetModule, WidgetModuleRecord, } from './types';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D;;GAEG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;GAEG;AACH,YAAY,EACX,UAAU,EACV,UAAU,EACV,UAAU,EACV,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,GAClB,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { ComponentType } from 'react';
|
|
5
|
+
/**
|
|
6
|
+
* Internal dependencies
|
|
7
|
+
*/
|
|
8
|
+
import type { ResolveWidgetModule, WidgetRenderProps } from '../../types';
|
|
9
|
+
type LazyWidgetComponent = ComponentType<WidgetRenderProps<unknown>>;
|
|
10
|
+
export declare function getLazyWidgetComponent(renderModule: string, resolveWidgetModule: ResolveWidgetModule): LazyWidgetComponent;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=get-lazy-widget-component.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-lazy-widget-component.d.ts","sourceRoot":"","sources":["../../../src/tools/get-lazy-widget-component/get-lazy-widget-component.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAO3C;;GAEG;AACH,OAAO,KAAK,EACX,mBAAmB,EAEnB,iBAAiB,EACjB,MAAM,aAAa,CAAC;AAErB,KAAK,mBAAmB,GAAG,aAAa,CAAE,iBAAiB,CAAE,OAAO,CAAE,CAAE,CAAC;AAsBzE,wBAAgB,sBAAsB,CACrC,YAAY,EAAE,MAAM,EACpB,mBAAmB,EAAE,mBAAmB,GACtC,mBAAmB,CAiBrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tools/get-lazy-widget-component/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Widget type definitions.
|
|
3
|
+
*
|
|
4
|
+
* Canonical home for widget identity types consumed by the registry and
|
|
5
|
+
* hosts that render widgets.
|
|
6
|
+
*
|
|
7
|
+
* Each type is generic over the widget's attribute object (`Item`), so a
|
|
8
|
+
* widget binds its attribute shape once and gets typed `attributes`,
|
|
9
|
+
* `example`, and `setAttributes`.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* External dependencies
|
|
13
|
+
*/
|
|
14
|
+
import type { ComponentProps, ComponentType, ReactElement } from 'react';
|
|
15
|
+
import type { Field } from '@wordpress/dataviews';
|
|
16
|
+
/**
|
|
17
|
+
* Widget type identifier, structured as `<widget-namespace>/<widget-name>`.
|
|
18
|
+
* Both segments are lowercase, kebab-case.
|
|
19
|
+
*/
|
|
20
|
+
export type WidgetName = `${string}/${string}`;
|
|
21
|
+
/**
|
|
22
|
+
* Icon for a widget type: a rendered SVG element, typically one from
|
|
23
|
+
* `@wordpress/icons`.
|
|
24
|
+
*/
|
|
25
|
+
export type WidgetIcon = ReactElement<ComponentProps<'svg'>>;
|
|
26
|
+
/**
|
|
27
|
+
* Literal contents of a widget's `widget.json` metadata file.
|
|
28
|
+
*
|
|
29
|
+
* Captures the *authoring* shape only; module entry points and style
|
|
30
|
+
* assets are discovered by convention from the widget directory, not
|
|
31
|
+
* declared here.
|
|
32
|
+
*/
|
|
33
|
+
export interface WidgetTypeMetadata<Item = unknown> {
|
|
34
|
+
/**
|
|
35
|
+
* Version of the Widget API used by the widget.
|
|
36
|
+
*/
|
|
37
|
+
apiVersion: number;
|
|
38
|
+
/**
|
|
39
|
+
* Stable type identifier.
|
|
40
|
+
*/
|
|
41
|
+
name: WidgetName;
|
|
42
|
+
/**
|
|
43
|
+
* Display title; hosts surface it in pickers and chrome.
|
|
44
|
+
*/
|
|
45
|
+
title: string;
|
|
46
|
+
/**
|
|
47
|
+
* Short description; hosts surface it in pickers and help panels.
|
|
48
|
+
*/
|
|
49
|
+
description?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Visual identifier for the widget type; hosts decide where, and
|
|
52
|
+
* whether, to render it.
|
|
53
|
+
*/
|
|
54
|
+
icon?: WidgetIcon;
|
|
55
|
+
/**
|
|
56
|
+
* Grouping category. Core provides `dashboard`; plugins and themes may
|
|
57
|
+
* register custom categories.
|
|
58
|
+
*/
|
|
59
|
+
category?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Authoring intent about how the widget renders. Not a user-editable
|
|
62
|
+
* attribute.
|
|
63
|
+
*
|
|
64
|
+
* - `'framed'` (default when absent): the widget renders its
|
|
65
|
+
* content only.
|
|
66
|
+
* - `'content-bleed'`: the host's chrome stays visible while the
|
|
67
|
+
* content fills the content area edge-to-edge, with no padding.
|
|
68
|
+
* - `'full-bleed'`: the widget renders edge-to-edge with no
|
|
69
|
+
* surrounding chrome.
|
|
70
|
+
*/
|
|
71
|
+
presentation?: 'framed' | 'content-bleed' | 'full-bleed';
|
|
72
|
+
/**
|
|
73
|
+
* Search aliases hosts use to match the widget in their pickers.
|
|
74
|
+
*/
|
|
75
|
+
keywords?: string[];
|
|
76
|
+
/**
|
|
77
|
+
* Widget version, used for asset cache invalidation.
|
|
78
|
+
*/
|
|
79
|
+
version?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Gettext text domain for translations.
|
|
82
|
+
*/
|
|
83
|
+
textdomain?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Experiment gate; boolean `true`, or a specific experiment name.
|
|
86
|
+
*/
|
|
87
|
+
__experimental?: string | boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Declarative attribute schema, bound to the widget's attribute
|
|
90
|
+
* object via `Item`. Hosts render forms straight from this list
|
|
91
|
+
* via `DataForm`, with no per-widget form wiring.
|
|
92
|
+
*/
|
|
93
|
+
attributes?: Field<Item>[];
|
|
94
|
+
/**
|
|
95
|
+
* Structured example data hosts use for previews, and the default
|
|
96
|
+
* attributes applied when a new instance is created without initial
|
|
97
|
+
* attributes.
|
|
98
|
+
*/
|
|
99
|
+
example?: {
|
|
100
|
+
attributes?: Partial<Item>;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Runtime widget type consumed by hosts.
|
|
105
|
+
*
|
|
106
|
+
* Extends `WidgetTypeMetadata` with runtime-only fields, notably
|
|
107
|
+
* `renderModule`. Hosts supply the raw records in snake_case
|
|
108
|
+
* (`WidgetModuleRecord`); `useWidgetTypes` is the single boundary that
|
|
109
|
+
* resolves them into this camelCase shape.
|
|
110
|
+
*/
|
|
111
|
+
export interface WidgetType<Item = unknown> extends WidgetTypeMetadata<Item> {
|
|
112
|
+
/**
|
|
113
|
+
* Script-module identifier resolved to a React component at render
|
|
114
|
+
* time, produced from the conventional `render.*` entry point.
|
|
115
|
+
*/
|
|
116
|
+
renderModule: string;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Props passed to a widget's render component by the host, bound over
|
|
120
|
+
* `Item` so `attributes` and `setAttributes` are typed against the
|
|
121
|
+
* widget's attribute object.
|
|
122
|
+
*/
|
|
123
|
+
export interface WidgetRenderProps<Item = unknown> {
|
|
124
|
+
/**
|
|
125
|
+
* User-configured attributes for this widget instance.
|
|
126
|
+
*/
|
|
127
|
+
attributes: Item;
|
|
128
|
+
/**
|
|
129
|
+
* Updates the attributes of this instance. Optional because some
|
|
130
|
+
* hosts render widgets in read-only contexts.
|
|
131
|
+
*/
|
|
132
|
+
setAttributes?: (next: Partial<Item>) => void;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Widget render module shape returned by the module resolver.
|
|
136
|
+
*/
|
|
137
|
+
export interface WidgetModule {
|
|
138
|
+
default: ComponentType<WidgetRenderProps<unknown>>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Resolver function: maps a `WidgetType.renderModule` id to a React
|
|
142
|
+
* component. Override for tests, Storybook, or to load from a non-URL
|
|
143
|
+
* source.
|
|
144
|
+
*/
|
|
145
|
+
export type ResolveWidgetModule = (moduleId: string) => Promise<WidgetModule>;
|
|
146
|
+
/**
|
|
147
|
+
* Per-widget record a host feeds to `useWidgetTypes`, in snake_case wire
|
|
148
|
+
* format. The host fetches these however it likes; only the field shape is
|
|
149
|
+
* part of the contract.
|
|
150
|
+
*/
|
|
151
|
+
export interface WidgetModuleRecord {
|
|
152
|
+
/**
|
|
153
|
+
* Stable widget type identifier.
|
|
154
|
+
*/
|
|
155
|
+
name: string;
|
|
156
|
+
/**
|
|
157
|
+
* Script-module id resolved to the render component at render time.
|
|
158
|
+
*/
|
|
159
|
+
render_module?: string | null;
|
|
160
|
+
/**
|
|
161
|
+
* Script-module id dynamically imported for the widget's live metadata.
|
|
162
|
+
*/
|
|
163
|
+
widget_module?: string | null;
|
|
164
|
+
/**
|
|
165
|
+
* Authoring presentation hint; overrides the metadata module's value.
|
|
166
|
+
*/
|
|
167
|
+
presentation?: WidgetTypeMetadata['presentation'] | null;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;GAEG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AACzE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,GAAI,MAAO,IAAK,MAAO,EAAE,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,YAAY,CAAE,cAAc,CAAE,KAAK,CAAE,CAAE,CAAC;AAEjE;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB,CAAE,IAAI,GAAG,OAAO;IAClD;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IAEjB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,IAAI,CAAC,EAAE,UAAU,CAAC;IAElB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;;;;;;;OAUG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,eAAe,GAAG,YAAY,CAAC;IAEzD;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IAEpB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAElC;;;;OAIG;IACH,UAAU,CAAC,EAAE,KAAK,CAAE,IAAI,CAAE,EAAE,CAAC;IAE7B;;;;OAIG;IACH,OAAO,CAAC,EAAE;QACT,UAAU,CAAC,EAAE,OAAO,CAAE,IAAI,CAAE,CAAC;KAC7B,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU,CAAE,IAAI,GAAG,OAAO,CAC1C,SAAQ,kBAAkB,CAAE,IAAI,CAAE;IAClC;;;OAGG;IACH,YAAY,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB,CAAE,IAAI,GAAG,OAAO;IACjD;;OAEG;IACH,UAAU,EAAE,IAAI,CAAC;IAEjB;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAE,IAAI,EAAE,OAAO,CAAE,IAAI,CAAE,KAAM,IAAI,CAAC;CAClD;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC5B,OAAO,EAAE,aAAa,CAAE,iBAAiB,CAAE,OAAO,CAAE,CAAE,CAAC;CACvD;AAED;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,GAAG,CACjC,QAAQ,EAAE,MAAM,KACZ,OAAO,CAAE,YAAY,CAAE,CAAC;AAE7B;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAE9B;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAE9B;;OAEG;IACH,YAAY,CAAC,EAAE,kBAAkB,CAAE,cAAc,CAAE,GAAG,IAAI,CAAC;CAC3D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wordpress/widget-primitives",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Host-agnostic toolkit for dashboard widgets: the widget contract types, the discovery hook, and the render entry point.",
|
|
5
|
+
"author": "The WordPress Contributors",
|
|
6
|
+
"license": "GPL-2.0-or-later",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"wordpress",
|
|
9
|
+
"gutenberg",
|
|
10
|
+
"widgets",
|
|
11
|
+
"dashboard"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/widget-primitives/README.md",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/WordPress/gutenberg.git",
|
|
17
|
+
"directory": "packages/widget-primitives"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/WordPress/gutenberg/issues"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20.10.0",
|
|
24
|
+
"npm": ">=10.2.3"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"build",
|
|
29
|
+
"build-module",
|
|
30
|
+
"build-types",
|
|
31
|
+
"*.md"
|
|
32
|
+
],
|
|
33
|
+
"main": "build/index.cjs",
|
|
34
|
+
"module": "build-module/index.mjs",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./build-types/index.d.ts",
|
|
38
|
+
"import": "./build-module/index.mjs",
|
|
39
|
+
"default": "./build/index.cjs"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"wpScript": false,
|
|
44
|
+
"types": "build-types",
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@wordpress/dataviews": "^17.0.0",
|
|
48
|
+
"@wordpress/element": "^8.1.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@storybook/addon-docs": "^10.4.3",
|
|
52
|
+
"@storybook/react-vite": "^10.4.3",
|
|
53
|
+
"@wordpress/components": "^36.0.0",
|
|
54
|
+
"@wordpress/icons": "^15.0.0",
|
|
55
|
+
"@wordpress/ui": "^0.16.0",
|
|
56
|
+
"storybook": "^10.4.3"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@types/react": "^18.3.27",
|
|
60
|
+
"react": "^18.0.0",
|
|
61
|
+
"react-dom": "^18.0.0"
|
|
62
|
+
},
|
|
63
|
+
"peerDependenciesMeta": {
|
|
64
|
+
"@types/react": {
|
|
65
|
+
"optional": true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
},
|
|
71
|
+
"gitHead": "0e7112a4f4fde4ea15bd9060489b8f6fe11eb6ca"
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WidgetRender } from './widget-render';
|