@wordpress/views 3.0.0 → 3.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.
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/use-view.ts"],
4
- "sourcesContent": ["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tgetApplicablePersistedView,\n\tgetUserModifications,\n\tresolveView,\n} from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\n *\n * Only the properties the user actually modified are persisted: every other\n * property keeps resolving out of the layers below, so a change to the default\n * view, to the layout defaults or to the active view overrides keeps showing\n * through.\n *\n * @param config Configuration object for loading the view.\n *\n * @return Object with current view, modification state, and update functions.\n */\nexport function useView( config: ViewConfig ): UseViewReturn {\n\tconst {\n\t\tkind,\n\t\tname,\n\t\tslug,\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = useSelect(\n\t\t( select ) => {\n\t\t\treturn select( preferencesStore ).get(\n\t\t\t\t'core/views',\n\t\t\t\tpreferenceKey\n\t\t\t) as ViewOverrides | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst page = Number( queryParams?.page ?? 1 );\n\tconst search = queryParams?.search ?? '';\n\n\tconst view = useMemo(\n\t\t() =>\n\t\t\tresolveView( {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t} ),\n\t\t[\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t]\n\t);\n\n\t// A persisted `type` the layouts do not offer is ignored on read, so it\n\t// does not count as a modification either: there is nothing to reset.\n\tconst applicablePersistedView = getApplicablePersistedView(\n\t\tpersistedView,\n\t\tdefaultLayouts\n\t);\n\tconst isModified =\n\t\t!! applicablePersistedView &&\n\t\tObject.keys( applicablePersistedView ).length > 0;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// `page` and `search` live in the URL, not in the preference: they\n\t\t\t// are reported back to the consumer instead of being persisted.\n\t\t\tconst newQueryParams = {\n\t\t\t\tpage: Number( newView?.page ?? 1 ),\n\t\t\t\tsearch: newView?.search ?? '',\n\t\t\t};\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( newQueryParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( newQueryParams );\n\t\t\t}\n\n\t\t\tconst modifications = getUserModifications( newView, {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t} );\n\t\t\t// Compared against the preference as stored, so a persisted `type`\n\t\t\t// the layouts do not offer is written out on the first update.\n\t\t\tif ( ! dequal( modifications, persistedView ) ) {\n\t\t\t\t// `undefined` clears the preference: the user reverted every\n\t\t\t\t// property they had modified.\n\t\t\t\tset( 'core/views', preferenceKey, modifications );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tonChangeQueryParams,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undefined );\n\t}, [ preferenceKey, set ] );\n\n\treturn {\n\t\tview,\n\t\tisModified,\n\t\tupdateView,\n\t\tresetToDefault,\n\t};\n}\n"],
5
- "mappings": ";AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,eAAe;AACrC,SAAS,aAAa,iBAAiB;AAEvC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,6BAA6B;AACtC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAsBA,SAAS,QAAS,QAAoC;AAC5D,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,IAAI;AAEJ,QAAM,gBAAgB,sBAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,gBAA2C;AAAA,IAChD,CAAE,WAAY;AACb,aAAO,OAAQ,gBAAiB,EAAE;AAAA,QACjC;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,IACA,CAAE,aAAc;AAAA,EACjB;AACA,QAAM,EAAE,IAAI,IAAI,YAAa,gBAAiB;AAE9C,QAAM,OAAO,OAAQ,aAAa,QAAQ,CAAE;AAC5C,QAAM,SAAS,aAAa,UAAU;AAEtC,QAAM,OAAO;AAAA,IACZ,MACC,YAAa;AAAA,MACZ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD,CAAE;AAAA,IACH;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAIA,QAAM,0BAA0B;AAAA,IAC/B;AAAA,IACA;AAAA,EACD;AACA,QAAM,aACL,CAAC,CAAE,2BACH,OAAO,KAAM,uBAAwB,EAAE,SAAS;AAEjD,QAAM,aAAa;AAAA,IAClB,CAAE,YAAmB;AAGpB,YAAM,iBAAiB;AAAA,QACtB,MAAM,OAAQ,SAAS,QAAQ,CAAE;AAAA,QACjC,QAAQ,SAAS,UAAU;AAAA,MAC5B;AACA,UACC,uBACA,CAAE,OAAQ,gBAAgB,EAAE,MAAM,OAAO,CAAE,GAC1C;AACD,4BAAqB,cAAe;AAAA,MACrC;AAEA,YAAM,gBAAgB,qBAAsB,SAAS;AAAA,QACpD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD,CAAE;AAGF,UAAK,CAAE,OAAQ,eAAe,aAAc,GAAI;AAG/C,YAAK,cAAc,eAAe,aAAc;AAAA,MACjD;AAAA,IACD;AAAA,IACA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,QAAM,iBAAiB,YAAa,MAAM;AACzC,QAAK,cAAc,eAAe,MAAU;AAAA,EAC7C,GAAG,CAAE,eAAe,GAAI,CAAE;AAE1B,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"use-view.mjs","names":["preferencesStore"],"sources":["../src/use-view.ts"],"sourcesContent":["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tgetApplicablePersistedView,\n\tgetUserModifications,\n\tresolveView,\n} from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\n *\n * Only the properties the user actually modified are persisted: every other\n * property keeps resolving out of the layers below, so a change to the default\n * view, to the layout defaults or to the active view overrides keeps showing\n * through.\n *\n * @param config Configuration object for loading the view.\n *\n * @return Object with current view, modification state, and update functions.\n */\nexport function useView( config: ViewConfig ): UseViewReturn {\n\tconst {\n\t\tkind,\n\t\tname,\n\t\tslug,\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = useSelect(\n\t\t( select ) => {\n\t\t\treturn select( preferencesStore ).get(\n\t\t\t\t'core/views',\n\t\t\t\tpreferenceKey\n\t\t\t) as ViewOverrides | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst page = Number( queryParams?.page ?? 1 );\n\tconst search = queryParams?.search ?? '';\n\n\tconst view = useMemo(\n\t\t() =>\n\t\t\tresolveView( {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t} ),\n\t\t[\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t]\n\t);\n\n\t// A persisted `type` the layouts do not offer is ignored on read, so it\n\t// does not count as a modification either: there is nothing to reset.\n\tconst applicablePersistedView = getApplicablePersistedView(\n\t\tpersistedView,\n\t\tdefaultLayouts\n\t);\n\tconst isModified =\n\t\t!! applicablePersistedView &&\n\t\tObject.keys( applicablePersistedView ).length > 0;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// `page` and `search` live in the URL, not in the preference: they\n\t\t\t// are reported back to the consumer instead of being persisted.\n\t\t\tconst newQueryParams = {\n\t\t\t\tpage: Number( newView?.page ?? 1 ),\n\t\t\t\tsearch: newView?.search ?? '',\n\t\t\t};\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( newQueryParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( newQueryParams );\n\t\t\t}\n\n\t\t\tconst modifications = getUserModifications( newView, {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t} );\n\t\t\t// Compared against the preference as stored, so a persisted `type`\n\t\t\t// the layouts do not offer is written out on the first update.\n\t\t\tif ( ! dequal( modifications, persistedView ) ) {\n\t\t\t\t// `undefined` clears the preference: the user reverted every\n\t\t\t\t// property they had modified.\n\t\t\t\tset( 'core/views', preferenceKey, modifications );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tonChangeQueryParams,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undefined );\n\t}, [ preferenceKey, set ] );\n\n\treturn {\n\t\tview,\n\t\tisModified,\n\t\tupdateView,\n\t\tresetToDefault,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,QAAS,QAAoC;CAC5D,MAAM,EACL,MACA,MACA,MACA,aACA,gBACA,qBACA,aACA,wBACG;CAEJ,MAAM,gBAAgB,sBAAuB,MAAM,MAAM,IAAK;CAC9D,MAAM,gBAA2C,WAC9C,WAAY;EACb,OAAO,OAAQA,KAAiB,CAAC,CAAC,IACjC,cACA,aACD;CACD,GACA,CAAE,aAAc,CACjB;CACA,MAAM,EAAE,QAAQ,YAAaA,KAAiB;CAE9C,MAAM,OAAO,OAAQ,aAAa,QAAQ,CAAE;CAC5C,MAAM,SAAS,aAAa,UAAU;CAEtC,MAAM,OAAO,cAEX,YAAa;EACZ;EACA;EACA;EACA;EACA;EACA;CACD,CAAE,GACH;EACC;EACA;EACA;EACA;EACA;EACA;CACD,CACD;CAIA,MAAM,0BAA0B,2BAC/B,eACA,cACD;CAmDA,OAAO;EACN;EACA,YAnDA,CAAC,CAAE,2BACH,OAAO,KAAM,uBAAwB,CAAC,CAAC,SAAS;EAmDhD,YAjDkB,aAChB,YAAmB;GAGpB,MAAM,iBAAiB;IACtB,MAAM,OAAQ,SAAS,QAAQ,CAAE;IACjC,QAAQ,SAAS,UAAU;GAC5B;GACA,IACC,uBACA,CAAE,OAAQ,gBAAgB;IAAE;IAAM;GAAO,CAAE,GAE3C,oBAAqB,cAAe;GAGrC,MAAM,gBAAgB,qBAAsB,SAAS;IACpD;IACA;IACA;IACA;GACD,CAAE;GAGF,IAAK,CAAE,OAAQ,eAAe,aAAc,GAG3C,IAAK,cAAc,eAAe,aAAc;EAElD,GACA;GACC;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EACD,CAUS;EACT,gBARsB,kBAAmB;GACzC,IAAK,cAAc,eAAe,KAAA,CAAU;EAC7C,GAAG,CAAE,eAAe,GAAI,CAMV;CACd;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/views",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "View persistence and management for WordPress DataViews.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -39,19 +39,19 @@
39
39
  },
40
40
  "types": "build-types",
41
41
  "dependencies": {
42
- "@wordpress/core-data": "^9.0.0",
43
- "@wordpress/data": "^11.0.0",
44
- "@wordpress/dataviews": "^21.0.0",
45
- "@wordpress/element": "^10.0.0",
46
- "@wordpress/preferences": "^5.0.0",
47
- "@wordpress/private-apis": "^3.0.0",
42
+ "@wordpress/core-data": "^9.1.0",
43
+ "@wordpress/data": "^11.1.0",
44
+ "@wordpress/dataviews": "^22.0.0",
45
+ "@wordpress/element": "^10.1.0",
46
+ "@wordpress/preferences": "^5.1.0",
47
+ "@wordpress/private-apis": "^3.1.0",
48
48
  "dequal": "^2.0.3"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@testing-library/react": "^16.3.3",
52
52
  "react": "^19.3.0",
53
53
  "react-dom": "^19.3.0",
54
- "vitest": "^5.0.3"
54
+ "vitest": "^5.0.5"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "@types/react": "^18 || ^19",
@@ -70,5 +70,5 @@
70
70
  "publishConfig": {
71
71
  "access": "public"
72
72
  },
73
- "gitHead": "8d30f92cbbde3ddf781c8799443c649befbbcb88"
73
+ "gitHead": "9b70363b9f93cff0583a1f3153738d383cf5a49e"
74
74
  }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/index.ts"],
4
- "sourcesContent": ["export { useView } from './use-view';\nexport { loadView } from './load-view';\nexport { useViewConfig } from './use-view-config';\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAwB;AACxB,uBAAyB;AACzB,6BAA8B;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/types.ts"],
4
- "sourcesContent": ["import type { View, SupportedLayouts } from '@wordpress/dataviews';\n\n/**\n * A layer merged on top of a view: the active view overrides, and the user's\n * persisted modifications.\n *\n * It is derived from `View` so it stays in sync with it, but every property is\n * optional — including `type`, which `View` requires as the discriminant of its\n * layout union. A layer only ever carries the subset of properties it means to\n * override, and `layout` is loosened to a plain record because a `type`\n * override may change which layout shape applies.\n */\nexport type ViewOverrides = Partial< Omit< View, 'type' | 'layout' > > & {\n\ttype?: View[ 'type' ];\n\tlayout?: Record< string, unknown >;\n};\n\nexport interface ViewConfig {\n\t/**\n\t * Entity kind (e.g. postType, root).\n\t * Similar to core-data entity kinds.\n\t */\n\tkind: string;\n\n\t/**\n\t * Entity name (e.g. post, page, user, site).\n\t * Similar to core-data entity names.\n\t */\n\tname: string;\n\n\t/**\n\t * Identifier for the current view (all, published, mine, etc.)\n\t */\n\tslug: string;\n\n\t/**\n\t * Default view configuration.\n\t */\n\tdefaultView: View;\n\n\t/**\n\t * View overrides applied on top of the default view and the default\n\t * layouts, but never persisted. These represent tab-specific configuration\n\t * (filters, sort) and developer-defined view defaults. The properties the\n\t * user modified take precedence over them; the ones they never touched\n\t * resolve out of them.\n\t */\n\tactiveViewOverrides?: ViewOverrides;\n\n\t/**\n\t * Default layout configurations keyed by layout type.\n\t * Used to apply layout-type-specific defaults (e.g., table column styles)\n\t * that are merged as overrides based on the resolved view type.\n\t */\n\tdefaultLayouts?: SupportedLayouts;\n\n\t/**\n\t * Optional query parameters from URL (page, search).\n\t */\n\tqueryParams?: {\n\t\tpage?: number;\n\t\tsearch?: string;\n\t};\n\n\t/**\n\t * Callback for when URL query parameters should change\n\t */\n\tonChangeQueryParams?: ( params: {\n\t\tpage?: number;\n\t\tsearch?: string;\n\t} ) => void;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/index.ts"],
4
- "sourcesContent": ["export { useView } from './use-view';\nexport { loadView } from './load-view';\nexport { useViewConfig } from './use-view-config';\n"],
5
- "mappings": ";AAAA,SAAS,eAAe;AACxB,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": [],
4
- "sourcesContent": [],
5
- "mappings": "",
6
- "names": []
7
- }