@wordpress/preferences 4.33.1 → 4.34.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.
Files changed (126) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +10 -10
  3. package/build/components/index.js +1 -0
  4. package/build/components/index.js.map +2 -2
  5. package/build/components/preference-base-option/index.js +8 -1
  6. package/build/components/preference-base-option/index.js.map +3 -3
  7. package/build/components/preference-base-option/types.js +17 -0
  8. package/build/components/preference-base-option/types.js.map +7 -0
  9. package/build/components/preference-toggle-control/index.js +3 -2
  10. package/build/components/preference-toggle-control/index.js.map +3 -3
  11. package/build/components/preference-toggle-menu-item/index.js +2 -1
  12. package/build/components/preference-toggle-menu-item/index.js.map +3 -3
  13. package/build/components/preferences-modal/index.js +5 -1
  14. package/build/components/preferences-modal/index.js.map +3 -3
  15. package/build/components/preferences-modal/types.js +17 -0
  16. package/build/components/preferences-modal/types.js.map +7 -0
  17. package/build/components/preferences-modal-section/index.js +1 -0
  18. package/build/components/preferences-modal-section/index.js.map +3 -3
  19. package/build/components/preferences-modal-tabs/index.js +25 -18
  20. package/build/components/preferences-modal-tabs/index.js.map +3 -3
  21. package/build/index.js +1 -0
  22. package/build/index.js.map +2 -2
  23. package/build/lock-unlock.js +1 -0
  24. package/build/lock-unlock.js.map +2 -2
  25. package/build/private-apis.js +1 -0
  26. package/build/private-apis.js.map +2 -2
  27. package/build/store/actions.js +1 -0
  28. package/build/store/actions.js.map +3 -3
  29. package/build/store/constants.js +1 -0
  30. package/build/store/constants.js.map +3 -3
  31. package/build/store/index.js +1 -0
  32. package/build/store/index.js.map +3 -3
  33. package/build/store/reducer.js +1 -0
  34. package/build/store/reducer.js.map +3 -3
  35. package/build/store/selectors.js +7 -4
  36. package/build/store/selectors.js.map +3 -3
  37. package/build/store/types.js +17 -0
  38. package/build/store/types.js.map +7 -0
  39. package/build-module/components/index.js.map +1 -1
  40. package/build-module/components/preference-base-option/index.js +7 -1
  41. package/build-module/components/preference-base-option/index.js.map +3 -3
  42. package/build-module/components/preference-base-option/types.js +1 -0
  43. package/build-module/components/preference-base-option/types.js.map +7 -0
  44. package/build-module/components/preference-toggle-control/index.js +2 -2
  45. package/build-module/components/preference-toggle-control/index.js.map +3 -3
  46. package/build-module/components/preference-toggle-menu-item/index.js +1 -1
  47. package/build-module/components/preference-toggle-menu-item/index.js.map +3 -3
  48. package/build-module/components/preferences-modal/index.js +4 -1
  49. package/build-module/components/preferences-modal/index.js.map +3 -3
  50. package/build-module/components/preferences-modal/types.js +1 -0
  51. package/build-module/components/preferences-modal/types.js.map +7 -0
  52. package/build-module/components/preferences-modal-section/index.js.map +3 -3
  53. package/build-module/components/preferences-modal-tabs/index.js +24 -18
  54. package/build-module/components/preferences-modal-tabs/index.js.map +3 -3
  55. package/build-module/index.js.map +1 -1
  56. package/build-module/lock-unlock.js.map +1 -1
  57. package/build-module/private-apis.js.map +1 -1
  58. package/build-module/store/actions.js.map +3 -3
  59. package/build-module/store/constants.js.map +3 -3
  60. package/build-module/store/index.js.map +3 -3
  61. package/build-module/store/reducer.js.map +3 -3
  62. package/build-module/store/selectors.js +6 -4
  63. package/build-module/store/selectors.js.map +3 -3
  64. package/build-module/store/types.js +1 -0
  65. package/build-module/store/types.js.map +7 -0
  66. package/build-types/components/index.d.ts +2 -0
  67. package/build-types/components/index.d.ts.map +1 -0
  68. package/build-types/components/preference-base-option/index.d.ts +7 -0
  69. package/build-types/components/preference-base-option/index.d.ts.map +1 -0
  70. package/build-types/components/preference-base-option/types.d.ts +11 -0
  71. package/build-types/components/preference-base-option/types.d.ts.map +1 -0
  72. package/build-types/components/preference-toggle-control/index.d.ts +9 -0
  73. package/build-types/components/preference-toggle-control/index.d.ts.map +1 -0
  74. package/build-types/components/preference-toggle-menu-item/index.d.ts +18 -0
  75. package/build-types/components/preference-toggle-menu-item/index.d.ts.map +1 -0
  76. package/build-types/components/preferences-modal/index.d.ts +6 -0
  77. package/build-types/components/preferences-modal/index.d.ts.map +1 -0
  78. package/build-types/components/preferences-modal/types.d.ts +11 -0
  79. package/build-types/components/preferences-modal/types.d.ts.map +1 -0
  80. package/build-types/components/preferences-modal-section/index.d.ts +8 -0
  81. package/build-types/components/preferences-modal-section/index.d.ts.map +1 -0
  82. package/build-types/components/preferences-modal-tabs/index.d.ts +9 -0
  83. package/build-types/components/preferences-modal-tabs/index.d.ts.map +1 -0
  84. package/build-types/index.d.ts +4 -0
  85. package/build-types/index.d.ts.map +1 -0
  86. package/build-types/lock-unlock.d.ts +2 -0
  87. package/build-types/lock-unlock.d.ts.map +1 -0
  88. package/build-types/private-apis.d.ts +2 -0
  89. package/build-types/private-apis.d.ts.map +1 -0
  90. package/build-types/store/actions.d.ts +69 -0
  91. package/build-types/store/actions.d.ts.map +1 -0
  92. package/build-types/store/constants.d.ts +5 -0
  93. package/build-types/store/constants.d.ts.map +1 -0
  94. package/build-types/store/index.d.ts +10 -0
  95. package/build-types/store/index.d.ts.map +1 -0
  96. package/build-types/store/reducer.d.ts +39 -0
  97. package/build-types/store/reducer.d.ts.map +1 -0
  98. package/build-types/store/selectors.d.ts +16 -0
  99. package/build-types/store/selectors.d.ts.map +1 -0
  100. package/build-types/store/types.d.ts +27 -0
  101. package/build-types/store/types.d.ts.map +1 -0
  102. package/package.json +16 -12
  103. package/src/components/preference-base-option/{index.js → index.tsx} +11 -1
  104. package/src/components/preference-base-option/types.ts +10 -0
  105. package/src/components/preference-toggle-control/{index.js → index.tsx} +9 -2
  106. package/src/components/preference-toggle-menu-item/{index.js → index.tsx} +14 -2
  107. package/src/components/preferences-modal/{index.js → index.tsx} +8 -1
  108. package/src/components/preferences-modal/types.ts +11 -0
  109. package/src/components/preferences-modal-section/{index.js → index.tsx} +7 -1
  110. package/src/components/preferences-modal-tabs/{index.js → index.tsx} +13 -3
  111. package/src/store/{actions.js → actions.ts} +48 -18
  112. package/src/store/{constants.js → constants.ts} +0 -2
  113. package/src/store/{index.js → index.ts} +6 -3
  114. package/src/store/{reducer.js → reducer.ts} +23 -10
  115. package/src/store/selectors.ts +67 -0
  116. package/src/store/types.ts +29 -0
  117. package/tsconfig.json +16 -0
  118. package/tsconfig.tsbuildinfo +1 -0
  119. package/src/store/selectors.js +0 -57
  120. /package/src/components/{index.js → index.ts} +0 -0
  121. /package/src/{index.js → index.ts} +0 -0
  122. /package/src/{lock-unlock.js → lock-unlock.ts} +0 -0
  123. /package/src/{private-apis.js → private-apis.ts} +0 -0
  124. /package/src/store/test/{actions.js → actions.ts} +0 -0
  125. /package/src/store/test/{reducer.js → reducer.ts} +0 -0
  126. /package/src/store/test/{selectors.js → selectors.ts} +0 -0
@@ -1,3 +1,12 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type {
5
+ ActionObject,
6
+ StoreState,
7
+ WPPreferencesPersistenceLayer,
8
+ } from './types';
9
+
1
10
  /**
2
11
  * Returns an action object used in signalling that a preference should be
3
12
  * toggled.
@@ -5,13 +14,19 @@
5
14
  * @param {string} scope The preference scope (e.g. core/edit-post).
6
15
  * @param {string} name The preference name.
7
16
  */
8
- export function toggle( scope, name ) {
17
+ export function toggle( scope: string, name: string ) {
18
+ // @ts-expect-error We cannot properly type this until we have a fully-typed datastore
9
19
  return function ( { select, dispatch } ) {
10
20
  const currentValue = select.get( scope, name );
11
21
  dispatch.set( scope, name, ! currentValue );
12
22
  };
13
23
  }
14
24
 
25
+ type SetAction = ActionObject<
26
+ 'SET_PREFERENCE_VALUE',
27
+ { scope: string; name: string; value: any }
28
+ >;
29
+
15
30
  /**
16
31
  * Returns an action object used in signalling that a preference should be set
17
32
  * to a value
@@ -20,9 +35,9 @@ export function toggle( scope, name ) {
20
35
  * @param {string} name The preference name.
21
36
  * @param {*} value The value to set.
22
37
  *
23
- * @return {Object} Action object.
38
+ * @return {SetAction} Action object.
24
39
  */
25
- export function set( scope, name, value ) {
40
+ export function set( scope: string, name: string, value: any ): SetAction {
26
41
  return {
27
42
  type: 'SET_PREFERENCE_VALUE',
28
43
  scope,
@@ -31,16 +46,25 @@ export function set( scope, name, value ) {
31
46
  };
32
47
  }
33
48
 
49
+ type ScopedDefaults = StoreState[ 'defaults' ][ string ];
50
+ type SetDefaultsAction = ActionObject<
51
+ 'SET_PREFERENCE_DEFAULTS',
52
+ { scope: string; defaults: ScopedDefaults }
53
+ >;
54
+
34
55
  /**
35
56
  * Returns an action object used in signalling that preference defaults should
36
57
  * be set.
37
58
  *
38
- * @param {string} scope The preference scope (e.g. core/edit-post).
39
- * @param {Object<string, *>} defaults A key/value map of preference names to values.
59
+ * @param scope The preference scope (e.g. core/edit-post).
60
+ * @param defaults A key/value map of preference names to values.
40
61
  *
41
- * @return {Object} Action object.
62
+ * @return Action object.
42
63
  */
43
- export function setDefaults( scope, defaults ) {
64
+ export function setDefaults(
65
+ scope: string,
66
+ defaults: ScopedDefaults
67
+ ): SetDefaultsAction {
44
68
  return {
45
69
  type: 'SET_PREFERENCE_DEFAULTS',
46
70
  scope,
@@ -48,14 +72,13 @@ export function setDefaults( scope, defaults ) {
48
72
  };
49
73
  }
50
74
 
51
- /** @typedef {() => Promise<Object>} WPPreferencesPersistenceLayerGet */
52
- /** @typedef {(Object) => void} WPPreferencesPersistenceLayerSet */
53
- /**
54
- * @typedef WPPreferencesPersistenceLayer
55
- *
56
- * @property {WPPreferencesPersistenceLayerGet} get An async function that gets data from the persistence layer.
57
- * @property {WPPreferencesPersistenceLayerSet} set A function that sets data in the persistence layer.
58
- */
75
+ type SetPersistenceLayerAction< D extends Object > = ActionObject<
76
+ 'SET_PERSISTENCE_LAYER',
77
+ {
78
+ persistenceLayer: WPPreferencesPersistenceLayer< D >;
79
+ persistedData: D;
80
+ }
81
+ >;
59
82
 
60
83
  /**
61
84
  * Sets the persistence layer.
@@ -68,11 +91,13 @@ export function setDefaults( scope, defaults ) {
68
91
  * application's lifecycle, before any other actions have been dispatched to
69
92
  * the preferences store.
70
93
  *
71
- * @param {WPPreferencesPersistenceLayer} persistenceLayer The persistence layer.
94
+ * @param persistenceLayer The persistence layer.
72
95
  *
73
- * @return {Object} Action object.
96
+ * @return Action object.
74
97
  */
75
- export async function setPersistenceLayer( persistenceLayer ) {
98
+ export async function setPersistenceLayer< D extends Object >(
99
+ persistenceLayer: WPPreferencesPersistenceLayer< D >
100
+ ): Promise< SetPersistenceLayerAction< D > > {
76
101
  const persistedData = await persistenceLayer.get();
77
102
  return {
78
103
  type: 'SET_PERSISTENCE_LAYER',
@@ -80,3 +105,8 @@ export async function setPersistenceLayer( persistenceLayer ) {
80
105
  persistedData,
81
106
  };
82
107
  }
108
+
109
+ export type AvailableActions =
110
+ | SetAction
111
+ | SetDefaultsAction
112
+ | SetPersistenceLayerAction< any >;
@@ -1,6 +1,4 @@
1
1
  /**
2
2
  * The identifier for the data store.
3
- *
4
- * @type {string}
5
3
  */
6
4
  export const STORE_NAME = 'core/preferences';
@@ -10,15 +10,18 @@ import reducer from './reducer';
10
10
  import * as actions from './actions';
11
11
  import * as selectors from './selectors';
12
12
  import { STORE_NAME } from './constants';
13
+ import type { StoreState } from './types';
13
14
 
14
15
  /**
15
16
  * Store definition for the preferences namespace.
16
17
  *
17
18
  * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
18
- *
19
- * @type {Object}
20
19
  */
21
- export const store = createReduxStore( STORE_NAME, {
20
+ export const store = createReduxStore<
21
+ StoreState,
22
+ typeof actions,
23
+ typeof selectors
24
+ >( STORE_NAME, {
22
25
  reducer,
23
26
  actions,
24
27
  selectors,
@@ -2,6 +2,11 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { combineReducers } from '@wordpress/data';
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import type { StoreState, WPPreferencesPersistenceLayer } from './types';
9
+ import type { AvailableActions } from './actions';
5
10
 
6
11
  /**
7
12
  * Reducer returning the defaults for user preferences.
@@ -9,12 +14,15 @@ import { combineReducers } from '@wordpress/data';
9
14
  * This is kept intentionally separate from the preferences
10
15
  * themselves so that defaults are not persisted.
11
16
  *
12
- * @param {Object} state Current state.
13
- * @param {Object} action Dispatched action.
17
+ * @param state Current state.
18
+ * @param action Dispatched action.
14
19
  *
15
- * @return {Object} Updated state.
20
+ * @return Updated state.
16
21
  */
17
- export function defaults( state = {}, action ) {
22
+ export function defaults(
23
+ state: StoreState[ 'defaults' ] = {},
24
+ action: AvailableActions
25
+ ): StoreState[ 'defaults' ] {
18
26
  if ( action.type === 'SET_PREFERENCE_DEFAULTS' ) {
19
27
  const { scope, defaults: values } = action;
20
28
  return {
@@ -29,21 +37,26 @@ export function defaults( state = {}, action ) {
29
37
  return state;
30
38
  }
31
39
 
40
+ type PreferencesReducer = (
41
+ state: StoreState[ 'preferences' ],
42
+ action: AvailableActions
43
+ ) => StoreState[ 'preferences' ];
44
+
32
45
  /**
33
46
  * Higher order reducer that does the following:
34
47
  * - Merges any data from the persistence layer into the state when the
35
48
  * `SET_PERSISTENCE_LAYER` action is received.
36
49
  * - Passes any preferences changes to the persistence layer.
37
50
  *
38
- * @param {Function} reducer The preferences reducer.
51
+ * @param reducer The preferences reducer.
39
52
  *
40
- * @return {Function} The enhanced reducer.
53
+ * @return The enhanced reducer.
41
54
  */
42
- function withPersistenceLayer( reducer ) {
43
- let persistenceLayer;
55
+ function withPersistenceLayer( reducer: PreferencesReducer ) {
56
+ let persistenceLayer: WPPreferencesPersistenceLayer< any >;
44
57
 
45
- return ( state, action ) => {
46
- // Setup the persistence layer, and return the persisted data
58
+ return ( state: StoreState[ 'preferences' ], action: AvailableActions ) => {
59
+ // Set up the persistence layer, and return the persisted data
47
60
  // as the state.
48
61
  if ( action.type === 'SET_PERSISTENCE_LAYER' ) {
49
62
  const { persistenceLayer: persistence, persistedData } = action;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import deprecated from '@wordpress/deprecated';
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import type { StoreState } from './types';
9
+
10
+ const withDeprecatedKeys =
11
+ (
12
+ originalGet: ( state: StoreState, scope: string, name: string ) => any
13
+ ) =>
14
+ ( state: StoreState, scope: string, name: string ) => {
15
+ const settingsToMoveToCore = [
16
+ 'allowRightClickOverrides',
17
+ 'distractionFree',
18
+ 'editorMode',
19
+ 'fixedToolbar',
20
+ 'focusMode',
21
+ 'hiddenBlockTypes',
22
+ 'inactivePanels',
23
+ 'keepCaretInsideBlock',
24
+ 'mostUsedBlocks',
25
+ 'openPanels',
26
+ 'showBlockBreadcrumbs',
27
+ 'showIconLabels',
28
+ 'showListViewByDefault',
29
+ 'isPublishSidebarEnabled',
30
+ 'isComplementaryAreaVisible',
31
+ 'pinnedItems',
32
+ ];
33
+
34
+ if (
35
+ settingsToMoveToCore.includes( name ) &&
36
+ [ 'core/edit-post', 'core/edit-site' ].includes( scope )
37
+ ) {
38
+ deprecated(
39
+ `wp.data.select( 'core/preferences' ).get( '${ scope }', '${ name }' )`,
40
+ {
41
+ since: '6.5',
42
+ alternative: `wp.data.select( 'core/preferences' ).get( 'core', '${ name }' )`,
43
+ }
44
+ );
45
+
46
+ return originalGet( state, 'core', name );
47
+ }
48
+
49
+ return originalGet( state, scope, name );
50
+ };
51
+
52
+ /**
53
+ * Returns a boolean indicating whether a prefer is active for a particular
54
+ * scope.
55
+ *
56
+ * @param {StoreState} state The store state.
57
+ * @param {string} scope The scope of the feature (e.g. core/edit-post).
58
+ * @param {string} name The name of the feature.
59
+ *
60
+ * @return {*} Is the feature enabled?
61
+ */
62
+ export const get = withDeprecatedKeys(
63
+ ( state: StoreState, scope: string, name: string ) => {
64
+ const value = state.preferences[ scope ]?.[ name ];
65
+ return value !== undefined ? value : state.defaults[ scope ]?.[ name ];
66
+ }
67
+ );
@@ -0,0 +1,29 @@
1
+ export type StoreState = {
2
+ preferences: { [ k in string ]: { [ p in string ]: any } };
3
+ defaults: { [ k in string ]: { [ p in string ]: any } };
4
+ };
5
+
6
+ export type OmitFirstArg< F > = F extends (
7
+ x: any,
8
+ ...args: infer P
9
+ ) => infer R
10
+ ? ( ...args: P ) => R
11
+ : never;
12
+
13
+ export interface WPPreferencesPersistenceLayer< D extends Object > {
14
+ /**
15
+ * An async function that gets data from the persistence layer.
16
+ */
17
+ get: () => Promise< D >;
18
+ /**
19
+ * A function that sets data in the persistence layer.
20
+ */
21
+ set: ( value: D ) => void;
22
+ }
23
+
24
+ export type ActionObject<
25
+ T extends string,
26
+ D extends Record< Exclude< string, 'type' >, any > = {},
27
+ > = {
28
+ type: T;
29
+ } & D;
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig.json",
3
+ "extends": "../../tsconfig.base.json",
4
+ "references": [
5
+ { "path": "../a11y" },
6
+ { "path": "../components" },
7
+ { "path": "../compose" },
8
+ { "path": "../data" },
9
+ { "path": "../deprecated" },
10
+ { "path": "../element" },
11
+ { "path": "../i18n" },
12
+ { "path": "../icons" },
13
+ { "path": "../private-apis" }
14
+ ],
15
+ "include": [ "src/**/*" ]
16
+ }