@wordpress/commands 0.4.0 → 0.5.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 (51) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +16 -0
  3. package/build/components/command-menu.js +60 -51
  4. package/build/components/command-menu.js.map +1 -1
  5. package/build/hooks/use-command-context.js +45 -0
  6. package/build/hooks/use-command-context.js.map +1 -0
  7. package/build/hooks/use-command-loader.js +6 -11
  8. package/build/hooks/use-command-loader.js.map +1 -1
  9. package/build/hooks/use-command.js +4 -3
  10. package/build/hooks/use-command.js.map +1 -1
  11. package/build/index.js +18 -0
  12. package/build/index.js.map +1 -1
  13. package/build/private-apis.js +2 -5
  14. package/build/private-apis.js.map +1 -1
  15. package/build/store/actions.js +35 -40
  16. package/build/store/actions.js.map +1 -1
  17. package/build/store/reducer.js +38 -22
  18. package/build/store/reducer.js.map +1 -1
  19. package/build/store/selectors.js +20 -17
  20. package/build/store/selectors.js.map +1 -1
  21. package/build-module/components/command-menu.js +60 -51
  22. package/build-module/components/command-menu.js.map +1 -1
  23. package/build-module/hooks/use-command-context.js +35 -0
  24. package/build-module/hooks/use-command-context.js.map +1 -0
  25. package/build-module/hooks/use-command-loader.js +6 -11
  26. package/build-module/hooks/use-command-loader.js.map +1 -1
  27. package/build-module/hooks/use-command.js +4 -3
  28. package/build-module/hooks/use-command.js.map +1 -1
  29. package/build-module/index.js +2 -0
  30. package/build-module/index.js.map +1 -1
  31. package/build-module/private-apis.js +2 -4
  32. package/build-module/private-apis.js.map +1 -1
  33. package/build-module/store/actions.js +33 -40
  34. package/build-module/store/actions.js.map +1 -1
  35. package/build-module/store/reducer.js +38 -22
  36. package/build-module/store/reducer.js.map +1 -1
  37. package/build-module/store/selectors.js +17 -16
  38. package/build-module/store/selectors.js.map +1 -1
  39. package/build-style/style-rtl.css +6 -1
  40. package/build-style/style.css +6 -1
  41. package/package.json +9 -9
  42. package/src/components/command-menu.js +38 -35
  43. package/src/components/style.scss +8 -1
  44. package/src/hooks/use-command-context.js +32 -0
  45. package/src/hooks/use-command-loader.js +12 -6
  46. package/src/hooks/use-command.js +5 -3
  47. package/src/index.js +2 -0
  48. package/src/private-apis.js +2 -4
  49. package/src/store/actions.js +31 -26
  50. package/src/store/reducer.js +33 -27
  51. package/src/store/selectors.js +18 -20
@@ -5,11 +5,12 @@
5
5
  *
6
6
  * @typedef {Object} WPCommandConfig
7
7
  *
8
- * @property {string} name Command name.
9
- * @property {string} label Command label.
10
- * @property {string=} group Command group.
11
- * @property {JSX.Element} icon Command icon.
12
- * @property {Function} callback Command callback.
8
+ * @property {string} name Command name.
9
+ * @property {string} label Command label.
10
+ * @property {string=} searchLabel Command search label.
11
+ * @property {string=} context Command context.
12
+ * @property {JSX.Element} icon Command icon.
13
+ * @property {Function} callback Command callback.
13
14
  */
14
15
 
15
16
  /**
@@ -21,9 +22,9 @@
21
22
  *
22
23
  * @typedef {Object} WPCommandLoaderConfig
23
24
  *
24
- * @property {string} name Command loader name.
25
- * @property {string=} group Command loader group.
26
- * @property {WPCommandLoaderHook} hook Command loader hook.
25
+ * @property {string} name Command loader name.
26
+ * @property {string=} context Command loader context.
27
+ * @property {WPCommandLoaderHook} hook Command loader hook.
27
28
  */
28
29
 
29
30
  /**
@@ -33,37 +34,24 @@
33
34
  *
34
35
  * @return {Object} action.
35
36
  */
36
- export function registerCommand(_ref) {
37
- let {
38
- name,
39
- label,
40
- icon,
41
- callback,
42
- group = ''
43
- } = _ref;
37
+ export function registerCommand(config) {
44
38
  return {
45
39
  type: 'REGISTER_COMMAND',
46
- name,
47
- label,
48
- icon,
49
- callback,
50
- group
40
+ ...config
51
41
  };
52
42
  }
53
43
  /**
54
44
  * Returns an action object used to unregister a command.
55
45
  *
56
- * @param {string} name Command name.
57
- * @param {string} group Command group.
46
+ * @param {string} name Command name.
58
47
  *
59
48
  * @return {Object} action.
60
49
  */
61
50
 
62
- export function unregisterCommand(name, group) {
51
+ export function unregisterCommand(name) {
63
52
  return {
64
53
  type: 'UNREGISTER_COMMAND',
65
- name,
66
- group
54
+ name
67
55
  };
68
56
  }
69
57
  /**
@@ -74,33 +62,24 @@ export function unregisterCommand(name, group) {
74
62
  * @return {Object} action.
75
63
  */
76
64
 
77
- export function registerCommandLoader(_ref2) {
78
- let {
79
- name,
80
- group = '',
81
- hook
82
- } = _ref2;
65
+ export function registerCommandLoader(config) {
83
66
  return {
84
67
  type: 'REGISTER_COMMAND_LOADER',
85
- name,
86
- group,
87
- hook
68
+ ...config
88
69
  };
89
70
  }
90
71
  /**
91
72
  * Unregister command loader hook.
92
73
  *
93
- * @param {string} name Command loader name.
94
- * @param {string} group Command loader group.
74
+ * @param {string} name Command loader name.
95
75
  *
96
76
  * @return {Object} action.
97
77
  */
98
78
 
99
- export function unregisterCommandLoader(name, group) {
79
+ export function unregisterCommandLoader(name) {
100
80
  return {
101
81
  type: 'UNREGISTER_COMMAND_LOADER',
102
- name,
103
- group
82
+ name
104
83
  };
105
84
  }
106
85
  /**
@@ -125,4 +104,18 @@ export function close() {
125
104
  type: 'CLOSE'
126
105
  };
127
106
  }
107
+ /**
108
+ * Sets the active context.
109
+ *
110
+ * @param {string} context Context.
111
+ *
112
+ * @return {Object} action.
113
+ */
114
+
115
+ export function setContext(context) {
116
+ return {
117
+ type: 'SET_CONTEXT',
118
+ context
119
+ };
120
+ }
128
121
  //# sourceMappingURL=actions.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/src/store/actions.js"],"names":["registerCommand","name","label","icon","callback","group","type","unregisterCommand","registerCommandLoader","hook","unregisterCommandLoader","open","close"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,eAAT,OAAwE;AAAA,MAA9C;AAAEC,IAAAA,IAAF;AAAQC,IAAAA,KAAR;AAAeC,IAAAA,IAAf;AAAqBC,IAAAA,QAArB;AAA+BC,IAAAA,KAAK,GAAG;AAAvC,GAA8C;AAC9E,SAAO;AACNC,IAAAA,IAAI,EAAE,kBADA;AAENL,IAAAA,IAFM;AAGNC,IAAAA,KAHM;AAINC,IAAAA,IAJM;AAKNC,IAAAA,QALM;AAMNC,IAAAA;AANM,GAAP;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,iBAAT,CAA4BN,IAA5B,EAAkCI,KAAlC,EAA0C;AAChD,SAAO;AACNC,IAAAA,IAAI,EAAE,oBADA;AAENL,IAAAA,IAFM;AAGNI,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASG,qBAAT,QAA6D;AAAA,MAA7B;AAAEP,IAAAA,IAAF;AAAQI,IAAAA,KAAK,GAAG,EAAhB;AAAoBI,IAAAA;AAApB,GAA6B;AACnE,SAAO;AACNH,IAAAA,IAAI,EAAE,yBADA;AAENL,IAAAA,IAFM;AAGNI,IAAAA,KAHM;AAINI,IAAAA;AAJM,GAAP;AAMA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,uBAAT,CAAkCT,IAAlC,EAAwCI,KAAxC,EAAgD;AACtD,SAAO;AACNC,IAAAA,IAAI,EAAE,2BADA;AAENL,IAAAA,IAFM;AAGNI,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASM,IAAT,GAAgB;AACtB,SAAO;AACNL,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASM,KAAT,GAAiB;AACvB,SAAO;AACNN,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPCommandConfig\n *\n * @property {string} name Command name.\n * @property {string} label Command label.\n * @property {string=} group Command group.\n * @property {JSX.Element} icon Command icon.\n * @property {Function} callback Command callback.\n */\n\n/**\n * @typedef {(search: string) => WPCommandConfig[]} WPCommandLoaderHook hoo\n */\n\n/**\n * Command loader config.\n *\n * @typedef {Object} WPCommandLoaderConfig\n *\n * @property {string} name Command loader name.\n * @property {string=} group Command loader group.\n * @property {WPCommandLoaderHook} hook Command loader hook.\n */\n\n/**\n * Returns an action object used to register a new command.\n *\n * @param {WPCommandConfig} config Command config.\n *\n * @return {Object} action.\n */\nexport function registerCommand( { name, label, icon, callback, group = '' } ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND',\n\t\tname,\n\t\tlabel,\n\t\ticon,\n\t\tcallback,\n\t\tgroup,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a command.\n *\n * @param {string} name Command name.\n * @param {string} group Command group.\n *\n * @return {Object} action.\n */\nexport function unregisterCommand( name, group ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND',\n\t\tname,\n\t\tgroup,\n\t};\n}\n\n/**\n * Register command loader.\n *\n * @param {WPCommandLoaderConfig} config Command loader config.\n *\n * @return {Object} action.\n */\nexport function registerCommandLoader( { name, group = '', hook } ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND_LOADER',\n\t\tname,\n\t\tgroup,\n\t\thook,\n\t};\n}\n\n/**\n * Unregister command loader hook.\n *\n * @param {string} name Command loader name.\n * @param {string} group Command loader group.\n *\n * @return {Object} action.\n */\nexport function unregisterCommandLoader( name, group ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND_LOADER',\n\t\tname,\n\t\tgroup,\n\t};\n}\n\n/**\n * Opens the command center.\n *\n * @return {Object} action.\n */\nexport function open() {\n\treturn {\n\t\ttype: 'OPEN',\n\t};\n}\n\n/**\n * Closes the command center.\n *\n * @return {Object} action.\n */\nexport function close() {\n\treturn {\n\t\ttype: 'CLOSE',\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/commands/src/store/actions.js"],"names":["registerCommand","config","type","unregisterCommand","name","registerCommandLoader","unregisterCommandLoader","open","close","setContext","context"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,eAAT,CAA0BC,MAA1B,EAAmC;AACzC,SAAO;AACNC,IAAAA,IAAI,EAAE,kBADA;AAEN,OAAGD;AAFG,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,iBAAT,CAA4BC,IAA5B,EAAmC;AACzC,SAAO;AACNF,IAAAA,IAAI,EAAE,oBADA;AAENE,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,qBAAT,CAAgCJ,MAAhC,EAAyC;AAC/C,SAAO;AACNC,IAAAA,IAAI,EAAE,yBADA;AAEN,OAAGD;AAFG,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASK,uBAAT,CAAkCF,IAAlC,EAAyC;AAC/C,SAAO;AACNF,IAAAA,IAAI,EAAE,2BADA;AAENE,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASG,IAAT,GAAgB;AACtB,SAAO;AACNL,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASM,KAAT,GAAiB;AACvB,SAAO;AACNN,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASO,UAAT,CAAqBC,OAArB,EAA+B;AACrC,SAAO;AACNR,IAAAA,IAAI,EAAE,aADA;AAENQ,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPCommandConfig\n *\n * @property {string} name Command name.\n * @property {string} label Command label.\n * @property {string=} searchLabel Command search label.\n * @property {string=} context Command context.\n * @property {JSX.Element} icon Command icon.\n * @property {Function} callback Command callback.\n */\n\n/**\n * @typedef {(search: string) => WPCommandConfig[]} WPCommandLoaderHook hoo\n */\n\n/**\n * Command loader config.\n *\n * @typedef {Object} WPCommandLoaderConfig\n *\n * @property {string} name Command loader name.\n * @property {string=} context Command loader context.\n * @property {WPCommandLoaderHook} hook Command loader hook.\n */\n\n/**\n * Returns an action object used to register a new command.\n *\n * @param {WPCommandConfig} config Command config.\n *\n * @return {Object} action.\n */\nexport function registerCommand( config ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND',\n\t\t...config,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a command.\n *\n * @param {string} name Command name.\n *\n * @return {Object} action.\n */\nexport function unregisterCommand( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND',\n\t\tname,\n\t};\n}\n\n/**\n * Register command loader.\n *\n * @param {WPCommandLoaderConfig} config Command loader config.\n *\n * @return {Object} action.\n */\nexport function registerCommandLoader( config ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND_LOADER',\n\t\t...config,\n\t};\n}\n\n/**\n * Unregister command loader hook.\n *\n * @param {string} name Command loader name.\n *\n * @return {Object} action.\n */\nexport function unregisterCommandLoader( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND_LOADER',\n\t\tname,\n\t};\n}\n\n/**\n * Opens the command center.\n *\n * @return {Object} action.\n */\nexport function open() {\n\treturn {\n\t\ttype: 'OPEN',\n\t};\n}\n\n/**\n * Closes the command center.\n *\n * @return {Object} action.\n */\nexport function close() {\n\treturn {\n\t\ttype: 'CLOSE',\n\t};\n}\n\n/**\n * Sets the active context.\n *\n * @param {string} context Context.\n *\n * @return {Object} action.\n */\nexport function setContext( context ) {\n\treturn {\n\t\ttype: 'SET_CONTEXT',\n\t\tcontext,\n\t};\n}\n"]}
@@ -18,14 +18,13 @@ function commands() {
18
18
  switch (action.type) {
19
19
  case 'REGISTER_COMMAND':
20
20
  return { ...state,
21
- [action.group]: { ...state[action.group],
22
- [action.name]: {
23
- name: action.name,
24
- label: action.label,
25
- group: action.group,
26
- callback: action.callback,
27
- icon: action.icon
28
- }
21
+ [action.name]: {
22
+ name: action.name,
23
+ label: action.label,
24
+ searchLabel: action.searchLabel,
25
+ context: action.context,
26
+ callback: action.callback,
27
+ icon: action.icon
29
28
  }
30
29
  };
31
30
 
@@ -34,10 +33,8 @@ function commands() {
34
33
  const {
35
34
  [action.name]: _,
36
35
  ...remainingState
37
- } = state === null || state === void 0 ? void 0 : state[action.group];
38
- return { ...state,
39
- [action.group]: remainingState
40
- };
36
+ } = state;
37
+ return remainingState;
41
38
  }
42
39
  }
43
40
 
@@ -60,11 +57,10 @@ function commandLoaders() {
60
57
  switch (action.type) {
61
58
  case 'REGISTER_COMMAND_LOADER':
62
59
  return { ...state,
63
- [action.group]: { ...state[action.group],
64
- [action.name]: {
65
- name: action.name,
66
- hook: action.hook
67
- }
60
+ [action.name]: {
61
+ name: action.name,
62
+ context: action.context,
63
+ hook: action.hook
68
64
  }
69
65
  };
70
66
 
@@ -73,10 +69,8 @@ function commandLoaders() {
73
69
  const {
74
70
  [action.name]: _,
75
71
  ...remainingState
76
- } = state === null || state === void 0 ? void 0 : state[action.group];
77
- return { ...state,
78
- [action.group]: remainingState
79
- };
72
+ } = state;
73
+ return remainingState;
80
74
  }
81
75
  }
82
76
 
@@ -106,11 +100,33 @@ function isOpen() {
106
100
 
107
101
  return state;
108
102
  }
103
+ /**
104
+ * Reducer returning the command center's active context.
105
+ *
106
+ * @param {Object} state Current state.
107
+ * @param {Object} action Dispatched action.
108
+ *
109
+ * @return {boolean} Updated state.
110
+ */
111
+
112
+
113
+ function context() {
114
+ let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'root';
115
+ let action = arguments.length > 1 ? arguments[1] : undefined;
116
+
117
+ switch (action.type) {
118
+ case 'SET_CONTEXT':
119
+ return action.context;
120
+ }
121
+
122
+ return state;
123
+ }
109
124
 
110
125
  const reducer = combineReducers({
111
126
  commands,
112
127
  commandLoaders,
113
- isOpen
128
+ isOpen,
129
+ context
114
130
  });
115
131
  export default reducer;
116
132
  //# sourceMappingURL=reducer.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/src/store/reducer.js"],"names":["combineReducers","commands","state","action","type","group","name","label","callback","icon","_","remainingState","commandLoaders","hook","isOpen","reducer"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAT,QAAgC,iBAAhC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,QAAT,GAAwC;AAAA,MAArBC,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AACvC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,kBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,KAAT,GAAkB,EACjB,GAAGH,KAAK,CAAEC,MAAM,CAACE,KAAT,CADS;AAEjB,WAAEF,MAAM,CAACG,IAAT,GAAiB;AAChBA,YAAAA,IAAI,EAAEH,MAAM,CAACG,IADG;AAEhBC,YAAAA,KAAK,EAAEJ,MAAM,CAACI,KAFE;AAGhBF,YAAAA,KAAK,EAAEF,MAAM,CAACE,KAHE;AAIhBG,YAAAA,QAAQ,EAAEL,MAAM,CAACK,QAJD;AAKhBC,YAAAA,IAAI,EAAEN,MAAM,CAACM;AALG;AAFA;AAFZ,OAAP;;AAaD,SAAK,oBAAL;AAA2B;AAC1B,cAAM;AAAE,WAAEN,MAAM,CAACG,IAAT,GAAiBI,CAAnB;AAAsB,aAAGC;AAAzB,YACLT,KADK,aACLA,KADK,uBACLA,KAAK,CAAIC,MAAM,CAACE,KAAX,CADN;AAEA,eAAO,EACN,GAAGH,KADG;AAEN,WAAEC,MAAM,CAACE,KAAT,GAAkBM;AAFZ,SAAP;AAIA;AAtBF;;AAyBA,SAAOT,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASU,cAAT,GAA8C;AAAA,MAArBV,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AAC7C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,yBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,KAAT,GAAkB,EACjB,GAAGH,KAAK,CAAEC,MAAM,CAACE,KAAT,CADS;AAEjB,WAAEF,MAAM,CAACG,IAAT,GAAiB;AAChBA,YAAAA,IAAI,EAAEH,MAAM,CAACG,IADG;AAEhBO,YAAAA,IAAI,EAAEV,MAAM,CAACU;AAFG;AAFA;AAFZ,OAAP;;AAUD,SAAK,2BAAL;AAAkC;AACjC,cAAM;AAAE,WAAEV,MAAM,CAACG,IAAT,GAAiBI,CAAnB;AAAsB,aAAGC;AAAzB,YACLT,KADK,aACLA,KADK,uBACLA,KAAK,CAAIC,MAAM,CAACE,KAAX,CADN;AAEA,eAAO,EACN,GAAGH,KADG;AAEN,WAAEC,MAAM,CAACE,KAAT,GAAkBM;AAFZ,SAAP;AAIA;AAnBF;;AAsBA,SAAOT,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASY,MAAT,GAAyC;AAAA,MAAxBZ,KAAwB,uEAAhB,KAAgB;AAAA,MAATC,MAAS;;AACxC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,MAAL;AACC,aAAO,IAAP;;AACD,SAAK,OAAL;AACC,aAAO,KAAP;AAJF;;AAOA,SAAOF,KAAP;AACA;;AAED,MAAMa,OAAO,GAAGf,eAAe,CAAE;AAChCC,EAAAA,QADgC;AAEhCW,EAAAA,cAFgC;AAGhCE,EAAAA;AAHgC,CAAF,CAA/B;AAMA,eAAeC,OAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer returning the registered commands\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commands( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: {\n\t\t\t\t\t...state[ action.group ],\n\t\t\t\t\t[ action.name ]: {\n\t\t\t\t\t\tname: action.name,\n\t\t\t\t\t\tlabel: action.label,\n\t\t\t\t\t\tgroup: action.group,\n\t\t\t\t\t\tcallback: action.callback,\n\t\t\t\t\t\ticon: action.icon,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } =\n\t\t\t\tstate?.[ action.group ];\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: remainingState,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command loaders\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commandLoaders( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND_LOADER':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: {\n\t\t\t\t\t...state[ action.group ],\n\t\t\t\t\t[ action.name ]: {\n\t\t\t\t\t\tname: action.name,\n\t\t\t\t\t\thook: action.hook,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND_LOADER': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } =\n\t\t\t\tstate?.[ action.group ];\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: remainingState,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center open state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction isOpen( state = false, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'OPEN':\n\t\t\treturn true;\n\t\tcase 'CLOSE':\n\t\t\treturn false;\n\t}\n\n\treturn state;\n}\n\nconst reducer = combineReducers( {\n\tcommands,\n\tcommandLoaders,\n\tisOpen,\n} );\n\nexport default reducer;\n"]}
1
+ {"version":3,"sources":["@wordpress/commands/src/store/reducer.js"],"names":["combineReducers","commands","state","action","type","name","label","searchLabel","context","callback","icon","_","remainingState","commandLoaders","hook","isOpen","reducer"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAT,QAAgC,iBAAhC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,QAAT,GAAwC;AAAA,MAArBC,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AACvC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,kBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBA,UAAAA,IAAI,EAAEF,MAAM,CAACE,IADG;AAEhBC,UAAAA,KAAK,EAAEH,MAAM,CAACG,KAFE;AAGhBC,UAAAA,WAAW,EAAEJ,MAAM,CAACI,WAHJ;AAIhBC,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAJA;AAKhBC,UAAAA,QAAQ,EAAEN,MAAM,CAACM,QALD;AAMhBC,UAAAA,IAAI,EAAEP,MAAM,CAACO;AANG;AAFX,OAAP;;AAWD,SAAK,oBAAL;AAA2B;AAC1B,cAAM;AAAE,WAAEP,MAAM,CAACE,IAAT,GAAiBM,CAAnB;AAAsB,aAAGC;AAAzB,YAA4CV,KAAlD;AACA,eAAOU,cAAP;AACA;AAhBF;;AAmBA,SAAOV,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASW,cAAT,GAA8C;AAAA,MAArBX,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AAC7C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,yBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBA,UAAAA,IAAI,EAAEF,MAAM,CAACE,IADG;AAEhBG,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAFA;AAGhBM,UAAAA,IAAI,EAAEX,MAAM,CAACW;AAHG;AAFX,OAAP;;AAQD,SAAK,2BAAL;AAAkC;AACjC,cAAM;AAAE,WAAEX,MAAM,CAACE,IAAT,GAAiBM,CAAnB;AAAsB,aAAGC;AAAzB,YAA4CV,KAAlD;AACA,eAAOU,cAAP;AACA;AAbF;;AAgBA,SAAOV,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASa,MAAT,GAAyC;AAAA,MAAxBb,KAAwB,uEAAhB,KAAgB;AAAA,MAATC,MAAS;;AACxC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,MAAL;AACC,aAAO,IAAP;;AACD,SAAK,OAAL;AACC,aAAO,KAAP;AAJF;;AAOA,SAAOF,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASM,OAAT,GAA2C;AAAA,MAAzBN,KAAyB,uEAAjB,MAAiB;AAAA,MAATC,MAAS;;AAC1C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,aAAL;AACC,aAAOD,MAAM,CAACK,OAAd;AAFF;;AAKA,SAAON,KAAP;AACA;;AAED,MAAMc,OAAO,GAAGhB,eAAe,CAAE;AAChCC,EAAAA,QADgC;AAEhCY,EAAAA,cAFgC;AAGhCE,EAAAA,MAHgC;AAIhCP,EAAAA;AAJgC,CAAF,CAA/B;AAOA,eAAeQ,OAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer returning the registered commands\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commands( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tname: action.name,\n\t\t\t\t\tlabel: action.label,\n\t\t\t\t\tsearchLabel: action.searchLabel,\n\t\t\t\t\tcontext: action.context,\n\t\t\t\t\tcallback: action.callback,\n\t\t\t\t\ticon: action.icon,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } = state;\n\t\t\treturn remainingState;\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command loaders\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commandLoaders( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND_LOADER':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tname: action.name,\n\t\t\t\t\tcontext: action.context,\n\t\t\t\t\thook: action.hook,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND_LOADER': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } = state;\n\t\t\treturn remainingState;\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center open state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction isOpen( state = false, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'OPEN':\n\t\t\treturn true;\n\t\tcase 'CLOSE':\n\t\t\treturn false;\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center's active context.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction context( state = 'root', action ) {\n\tswitch ( action.type ) {\n\t\tcase 'SET_CONTEXT':\n\t\t\treturn action.context;\n\t}\n\n\treturn state;\n}\n\nconst reducer = combineReducers( {\n\tcommands,\n\tcommandLoaders,\n\tisOpen,\n\tcontext,\n} );\n\nexport default reducer;\n"]}
@@ -2,23 +2,24 @@
2
2
  * External dependencies
3
3
  */
4
4
  import createSelector from 'rememo';
5
-
6
- function unique(array) {
7
- return array.filter((value, index, current) => current.indexOf(value) === index);
8
- }
9
-
10
- export const getGroups = createSelector(state => unique(Object.keys(state.commands).concat(Object.keys(state.commandLoaders))), state => [state.commands, state.commandLoaders]);
11
- export const getCommands = createSelector((state, group) => {
12
- var _state$commands$group;
13
-
14
- return Object.values((_state$commands$group = state.commands[group]) !== null && _state$commands$group !== void 0 ? _state$commands$group : {});
15
- }, (state, group) => [state.commands[group]]);
16
- export const getCommandLoaders = createSelector((state, group) => {
17
- var _state$commandLoaders;
18
-
19
- return Object.values((_state$commandLoaders = state.commandLoaders[group]) !== null && _state$commandLoaders !== void 0 ? _state$commandLoaders : {});
20
- }, (state, group) => [state.commandLoaders[group]]);
5
+ export const getCommands = createSelector(function (state) {
6
+ let contextual = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
7
+ return Object.values(state.commands).filter(command => {
8
+ const isContextual = command.context && command.context === state.context;
9
+ return contextual ? isContextual : !isContextual;
10
+ });
11
+ }, state => [state.commands, state.context]);
12
+ export const getCommandLoaders = createSelector(function (state) {
13
+ let contextual = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
14
+ return Object.values(state.commandLoaders).filter(loader => {
15
+ const isContextual = loader.context && loader.context === state.context;
16
+ return contextual ? isContextual : !isContextual;
17
+ });
18
+ }, state => [state.commandLoaders, state.context]);
21
19
  export function isOpen(state) {
22
20
  return state.isOpen;
23
21
  }
22
+ export function getContext(state) {
23
+ return state.context;
24
+ }
24
25
  //# sourceMappingURL=selectors.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/src/store/selectors.js"],"names":["createSelector","unique","array","filter","value","index","current","indexOf","getGroups","state","Object","keys","commands","concat","commandLoaders","getCommands","group","values","getCommandLoaders","isOpen"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,cAAP,MAA2B,QAA3B;;AAEA,SAASC,MAAT,CAAiBC,KAAjB,EAAyB;AACxB,SAAOA,KAAK,CAACC,MAAN,CACN,CAAEC,KAAF,EAASC,KAAT,EAAgBC,OAAhB,KAA6BA,OAAO,CAACC,OAAR,CAAiBH,KAAjB,MAA6BC,KADpD,CAAP;AAGA;;AAED,OAAO,MAAMG,SAAS,GAAGR,cAAc,CACpCS,KAAF,IACCR,MAAM,CACLS,MAAM,CAACC,IAAP,CAAaF,KAAK,CAACG,QAAnB,EAA8BC,MAA9B,CACCH,MAAM,CAACC,IAAP,CAAaF,KAAK,CAACK,cAAnB,CADD,CADK,CAF+B,EAOpCL,KAAF,IAAa,CAAEA,KAAK,CAACG,QAAR,EAAkBH,KAAK,CAACK,cAAxB,CAPyB,CAAhC;AAUP,OAAO,MAAMC,WAAW,GAAGf,cAAc,CACxC,CAAES,KAAF,EAASO,KAAT;AAAA;;AAAA,SAAoBN,MAAM,CAACO,MAAP,0BAAeR,KAAK,CAACG,QAAN,CAAgBI,KAAhB,CAAf,yEAA0C,EAA1C,CAApB;AAAA,CADwC,EAExC,CAAEP,KAAF,EAASO,KAAT,KAAoB,CAAEP,KAAK,CAACG,QAAN,CAAgBI,KAAhB,CAAF,CAFoB,CAAlC;AAKP,OAAO,MAAME,iBAAiB,GAAGlB,cAAc,CAC9C,CAAES,KAAF,EAASO,KAAT;AAAA;;AAAA,SAAoBN,MAAM,CAACO,MAAP,0BAAeR,KAAK,CAACK,cAAN,CAAsBE,KAAtB,CAAf,yEAAgD,EAAhD,CAApB;AAAA,CAD8C,EAE9C,CAAEP,KAAF,EAASO,KAAT,KAAoB,CAAEP,KAAK,CAACK,cAAN,CAAsBE,KAAtB,CAAF,CAF0B,CAAxC;AAKP,OAAO,SAASG,MAAT,CAAiBV,KAAjB,EAAyB;AAC/B,SAAOA,KAAK,CAACU,MAAb;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\n\nfunction unique( array ) {\n\treturn array.filter(\n\t\t( value, index, current ) => current.indexOf( value ) === index\n\t);\n}\n\nexport const getGroups = createSelector(\n\t( state ) =>\n\t\tunique(\n\t\t\tObject.keys( state.commands ).concat(\n\t\t\t\tObject.keys( state.commandLoaders )\n\t\t\t)\n\t\t),\n\t( state ) => [ state.commands, state.commandLoaders ]\n);\n\nexport const getCommands = createSelector(\n\t( state, group ) => Object.values( state.commands[ group ] ?? {} ),\n\t( state, group ) => [ state.commands[ group ] ]\n);\n\nexport const getCommandLoaders = createSelector(\n\t( state, group ) => Object.values( state.commandLoaders[ group ] ?? {} ),\n\t( state, group ) => [ state.commandLoaders[ group ] ]\n);\n\nexport function isOpen( state ) {\n\treturn state.isOpen;\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/commands/src/store/selectors.js"],"names":["createSelector","getCommands","state","contextual","Object","values","commands","filter","command","isContextual","context","getCommandLoaders","commandLoaders","loader","isOpen","getContext"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,cAAP,MAA2B,QAA3B;AAEA,OAAO,MAAMC,WAAW,GAAGD,cAAc,CACxC,UAAEE,KAAF;AAAA,MAASC,UAAT,uEAAsB,KAAtB;AAAA,SACCC,MAAM,CAACC,MAAP,CAAeH,KAAK,CAACI,QAArB,EAAgCC,MAAhC,CAA0CC,OAAF,IAAe;AACtD,UAAMC,YAAY,GACjBD,OAAO,CAACE,OAAR,IAAmBF,OAAO,CAACE,OAAR,KAAoBR,KAAK,CAACQ,OAD9C;AAEA,WAAOP,UAAU,GAAGM,YAAH,GAAkB,CAAEA,YAArC;AACA,GAJD,CADD;AAAA,CADwC,EAOtCP,KAAF,IAAa,CAAEA,KAAK,CAACI,QAAR,EAAkBJ,KAAK,CAACQ,OAAxB,CAP2B,CAAlC;AAUP,OAAO,MAAMC,iBAAiB,GAAGX,cAAc,CAC9C,UAAEE,KAAF;AAAA,MAASC,UAAT,uEAAsB,KAAtB;AAAA,SACCC,MAAM,CAACC,MAAP,CAAeH,KAAK,CAACU,cAArB,EAAsCL,MAAtC,CAAgDM,MAAF,IAAc;AAC3D,UAAMJ,YAAY,GACjBI,MAAM,CAACH,OAAP,IAAkBG,MAAM,CAACH,OAAP,KAAmBR,KAAK,CAACQ,OAD5C;AAEA,WAAOP,UAAU,GAAGM,YAAH,GAAkB,CAAEA,YAArC;AACA,GAJD,CADD;AAAA,CAD8C,EAO5CP,KAAF,IAAa,CAAEA,KAAK,CAACU,cAAR,EAAwBV,KAAK,CAACQ,OAA9B,CAPiC,CAAxC;AAUP,OAAO,SAASI,MAAT,CAAiBZ,KAAjB,EAAyB;AAC/B,SAAOA,KAAK,CAACY,MAAb;AACA;AAED,OAAO,SAASC,UAAT,CAAqBb,KAArB,EAA6B;AACnC,SAAOA,KAAK,CAACQ,OAAb;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\n\nexport const getCommands = createSelector(\n\t( state, contextual = false ) =>\n\t\tObject.values( state.commands ).filter( ( command ) => {\n\t\t\tconst isContextual =\n\t\t\t\tcommand.context && command.context === state.context;\n\t\t\treturn contextual ? isContextual : ! isContextual;\n\t\t} ),\n\t( state ) => [ state.commands, state.context ]\n);\n\nexport const getCommandLoaders = createSelector(\n\t( state, contextual = false ) =>\n\t\tObject.values( state.commandLoaders ).filter( ( loader ) => {\n\t\t\tconst isContextual =\n\t\t\t\tloader.context && loader.context === state.context;\n\t\t\treturn contextual ? isContextual : ! isContextual;\n\t\t} ),\n\t( state ) => [ state.commandLoaders, state.context ]\n);\n\nexport function isOpen( state ) {\n\treturn state.isOpen;\n}\n\nexport function getContext( state ) {\n\treturn state.context;\n}\n"]}
@@ -97,7 +97,7 @@
97
97
  --wp-block-synced-color: #7a00df;
98
98
  --wp-block-synced-color--rgb: 122, 0, 223;
99
99
  }
100
- @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
100
+ @media (min-resolution: 192dpi) {
101
101
  :root {
102
102
  --wp-admin-border-width-focus: 1.5px;
103
103
  }
@@ -184,6 +184,8 @@
184
184
  .commands-command-menu__container [cmdk-root] > [cmdk-list] {
185
185
  max-height: 400px;
186
186
  overflow: auto;
187
+ }
188
+ .commands-command-menu__container [cmdk-root] > [cmdk-list] > [cmdk-list-sizer] :has([cmdk-group-items]:not(:empty)) {
187
189
  padding: 8px;
188
190
  }
189
191
  .commands-command-menu__container [cmdk-empty] {
@@ -200,6 +202,9 @@
200
202
  .commands-command-menu__container [cmdk-list-sizer] {
201
203
  position: relative;
202
204
  }
205
+ .commands-command-menu__container [cmdk-group]:has([cmdk-group-items]:not(:empty)) + [cmdk-group]:has([cmdk-group-items]:not(:empty)) {
206
+ border-top: 1px solid #e0e0e0;
207
+ }
203
208
 
204
209
  .commands-command-menu__item mark {
205
210
  color: inherit;
@@ -97,7 +97,7 @@
97
97
  --wp-block-synced-color: #7a00df;
98
98
  --wp-block-synced-color--rgb: 122, 0, 223;
99
99
  }
100
- @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
100
+ @media (min-resolution: 192dpi) {
101
101
  :root {
102
102
  --wp-admin-border-width-focus: 1.5px;
103
103
  }
@@ -184,6 +184,8 @@
184
184
  .commands-command-menu__container [cmdk-root] > [cmdk-list] {
185
185
  max-height: 400px;
186
186
  overflow: auto;
187
+ }
188
+ .commands-command-menu__container [cmdk-root] > [cmdk-list] > [cmdk-list-sizer] :has([cmdk-group-items]:not(:empty)) {
187
189
  padding: 8px;
188
190
  }
189
191
  .commands-command-menu__container [cmdk-empty] {
@@ -200,6 +202,9 @@
200
202
  .commands-command-menu__container [cmdk-list-sizer] {
201
203
  position: relative;
202
204
  }
205
+ .commands-command-menu__container [cmdk-group]:has([cmdk-group-items]:not(:empty)) + [cmdk-group]:has([cmdk-group-items]:not(:empty)) {
206
+ border-top: 1px solid #e0e0e0;
207
+ }
203
208
 
204
209
  .commands-command-menu__item mark {
205
210
  color: inherit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/commands",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Handles the commands menu.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -27,13 +27,13 @@
27
27
  "react-native": "src/index",
28
28
  "dependencies": {
29
29
  "@babel/runtime": "^7.16.0",
30
- "@wordpress/components": "^24.0.0",
31
- "@wordpress/data": "^9.3.0",
32
- "@wordpress/element": "^5.10.0",
33
- "@wordpress/i18n": "^4.33.0",
34
- "@wordpress/icons": "^9.24.0",
35
- "@wordpress/keyboard-shortcuts": "^4.10.0",
36
- "@wordpress/private-apis": "^0.15.0",
30
+ "@wordpress/components": "^25.0.0",
31
+ "@wordpress/data": "^9.4.0",
32
+ "@wordpress/element": "^5.11.0",
33
+ "@wordpress/i18n": "^4.34.0",
34
+ "@wordpress/icons": "^9.25.0",
35
+ "@wordpress/keyboard-shortcuts": "^4.11.0",
36
+ "@wordpress/private-apis": "^0.16.0",
37
37
  "cmdk": "^0.2.0",
38
38
  "rememo": "^4.0.2"
39
39
  },
@@ -43,5 +43,5 @@
43
43
  "publishConfig": {
44
44
  "access": "public"
45
45
  },
46
- "gitHead": "e936127e1e13881f1a940b7bd1593a9e500147f3"
46
+ "gitHead": "c7c79cb11b677adcbf06cf5f8cfb6c5ec1699f19"
47
47
  }
@@ -31,17 +31,17 @@ function CommandMenuLoader( { name, search, hook, setLoader, close } ) {
31
31
  setLoader( name, isLoading );
32
32
  }, [ setLoader, name, isLoading ] );
33
33
 
34
+ if ( ! commands.length ) {
35
+ return null;
36
+ }
37
+
34
38
  return (
35
39
  <>
36
40
  <Command.List>
37
- { isLoading && (
38
- <Command.Loading>{ __( 'Searching…' ) }</Command.Loading>
39
- ) }
40
-
41
41
  { commands.map( ( command ) => (
42
42
  <Command.Item
43
43
  key={ command.name }
44
- value={ command.name }
44
+ value={ command.searchLabel ?? command.label }
45
45
  onSelect={ () => command.callback( { close } ) }
46
46
  >
47
47
  <HStack
@@ -89,24 +89,28 @@ export function CommandMenuLoaderWrapper( { hook, search, setLoader, close } ) {
89
89
  );
90
90
  }
91
91
 
92
- export function CommandMenuGroup( { group, search, setLoader, close } ) {
92
+ export function CommandMenuGroup( { isContextual, search, setLoader, close } ) {
93
93
  const { commands, loaders } = useSelect(
94
94
  ( select ) => {
95
95
  const { getCommands, getCommandLoaders } = select( commandsStore );
96
96
  return {
97
- commands: getCommands( group ),
98
- loaders: getCommandLoaders( group ),
97
+ commands: getCommands( isContextual ),
98
+ loaders: getCommandLoaders( isContextual ),
99
99
  };
100
100
  },
101
- [ group ]
101
+ [ isContextual ]
102
102
  );
103
103
 
104
+ if ( ! commands.length && ! loaders.length ) {
105
+ return null;
106
+ }
107
+
104
108
  return (
105
109
  <Command.Group>
106
110
  { commands.map( ( command ) => (
107
111
  <Command.Item
108
112
  key={ command.name }
109
- value={ command.name }
113
+ value={ command.searchLabel ?? command.label }
110
114
  onSelect={ () => command.callback( { close } ) }
111
115
  >
112
116
  <HStack
@@ -139,13 +143,10 @@ export function CommandMenuGroup( { group, search, setLoader, close } ) {
139
143
  export function CommandMenu() {
140
144
  const { registerShortcut } = useDispatch( keyboardShortcutsStore );
141
145
  const [ search, setSearch ] = useState( '' );
142
- const { groups, isOpen } = useSelect( ( select ) => {
143
- const { getGroups, isOpen: _isOpen } = select( commandsStore );
144
- return {
145
- groups: getGroups(),
146
- isOpen: _isOpen(),
147
- };
148
- }, [] );
146
+ const isOpen = useSelect(
147
+ ( select ) => select( commandsStore ).isOpen(),
148
+ []
149
+ );
149
150
  const { open, close } = useDispatch( commandsStore );
150
151
  const [ loaders, setLoaders ] = useState( {} );
151
152
  const commandMenuInput = useRef();
@@ -219,24 +220,26 @@ export function CommandMenu() {
219
220
  placeholder={ __( 'Type a command or search' ) }
220
221
  />
221
222
  </div>
222
- { search && (
223
- <Command.List>
224
- { ! isLoading && (
225
- <Command.Empty>
226
- { __( 'No results found.' ) }
227
- </Command.Empty>
228
- ) }
229
- { groups.map( ( group ) => (
230
- <CommandMenuGroup
231
- key={ group }
232
- group={ group }
233
- search={ search }
234
- setLoader={ setLoader }
235
- close={ closeAndReset }
236
- />
237
- ) ) }
238
- </Command.List>
239
- ) }
223
+ <Command.List>
224
+ { search && ! isLoading && (
225
+ <Command.Empty>
226
+ { __( 'No results found.' ) }
227
+ </Command.Empty>
228
+ ) }
229
+ <CommandMenuGroup
230
+ search={ search }
231
+ setLoader={ setLoader }
232
+ close={ closeAndReset }
233
+ isContextual
234
+ />
235
+ { search && (
236
+ <CommandMenuGroup
237
+ search={ search }
238
+ setLoader={ setLoader }
239
+ close={ closeAndReset }
240
+ />
241
+ ) }
242
+ </Command.List>
240
243
  </Command>
241
244
  </div>
242
245
  </Modal>
@@ -93,7 +93,10 @@
93
93
  [cmdk-root] > [cmdk-list] {
94
94
  max-height: 400px;
95
95
  overflow: auto;
96
- padding: $grid-unit;
96
+
97
+ & > [cmdk-list-sizer] :has([cmdk-group-items]:not(:empty)) {
98
+ padding: $grid-unit;
99
+ }
97
100
  }
98
101
 
99
102
  [cmdk-empty] {
@@ -112,6 +115,10 @@
112
115
  [cmdk-list-sizer] {
113
116
  position: relative;
114
117
  }
118
+
119
+ [cmdk-group]:has([cmdk-group-items]:not(:empty)) + [cmdk-group]:has([cmdk-group-items]:not(:empty)) {
120
+ border-top: 1px solid $gray-200;
121
+ }
115
122
  }
116
123
 
117
124
  .commands-command-menu__item mark {
@@ -0,0 +1,32 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useEffect, useRef } from '@wordpress/element';
5
+ import { useDispatch, useSelect } from '@wordpress/data';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import { store as commandsStore } from '../store';
11
+
12
+ /**
13
+ * Sets the active context of the command center
14
+ *
15
+ * @param {string} context Context to set.
16
+ */
17
+ export default function useCommandContext( context ) {
18
+ const { getContext } = useSelect( commandsStore );
19
+ const initialContext = useRef( getContext() );
20
+ const { setContext } = useDispatch( commandsStore );
21
+
22
+ useEffect( () => {
23
+ setContext( context );
24
+ }, [ context, setContext ] );
25
+
26
+ // This effects ensures that on unmount, we restore the context
27
+ // that was set before the component actually mounts.
28
+ useEffect( () => {
29
+ const initialContextRef = initialContext.current;
30
+ return () => setContext( initialContextRef );
31
+ }, [ setContext ] );
32
+ }
@@ -14,17 +14,23 @@ import { store as commandsStore } from '../store';
14
14
  *
15
15
  * @param {import('../store/actions').WPCommandLoaderConfig} loader command loader config.
16
16
  */
17
- export default function useCommandLoader( { name, group, hook } ) {
17
+ export default function useCommandLoader( loader ) {
18
18
  const { registerCommandLoader, unregisterCommandLoader } =
19
19
  useDispatch( commandsStore );
20
20
  useEffect( () => {
21
21
  registerCommandLoader( {
22
- name,
23
- group,
24
- hook,
22
+ name: loader.name,
23
+ hook: loader.hook,
24
+ context: loader.context,
25
25
  } );
26
26
  return () => {
27
- unregisterCommandLoader( name, group );
27
+ unregisterCommandLoader( loader.name );
28
28
  };
29
- }, [ name, group, hook, registerCommandLoader, unregisterCommandLoader ] );
29
+ }, [
30
+ loader.name,
31
+ loader.hook,
32
+ loader.context,
33
+ registerCommandLoader,
34
+ unregisterCommandLoader,
35
+ ] );
30
36
  }