@wordpress/block-editor 8.0.8 → 8.0.12-next.33ec3857e2.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 (294) hide show
  1. package/README.md +4 -0
  2. package/build/components/block-caption/index.native.js +14 -3
  3. package/build/components/block-caption/index.native.js.map +1 -1
  4. package/build/components/block-inspector/index.js +12 -23
  5. package/build/components/block-inspector/index.js.map +1 -1
  6. package/build/components/block-list-appender/index.js +3 -11
  7. package/build/components/block-list-appender/index.js.map +1 -1
  8. package/build/components/block-preview/auto.js +10 -2
  9. package/build/components/block-preview/auto.js.map +1 -1
  10. package/build/components/block-preview/index.js +51 -0
  11. package/build/components/block-preview/index.js.map +1 -1
  12. package/build/components/block-settings/container.native.js +2 -1
  13. package/build/components/block-settings/container.native.js.map +1 -1
  14. package/build/components/block-styles/index.js +110 -134
  15. package/build/components/block-styles/index.js.map +1 -1
  16. package/build/components/block-styles/menu-items.js +63 -0
  17. package/build/components/block-styles/menu-items.js.map +1 -0
  18. package/build/components/block-styles/preview-panel.js +45 -0
  19. package/build/components/block-styles/preview-panel.js.map +1 -0
  20. package/build/components/block-styles/use-styles-for-block.js +119 -0
  21. package/build/components/block-styles/use-styles-for-block.js.map +1 -0
  22. package/build/components/block-styles/utils.js +39 -0
  23. package/build/components/block-styles/utils.js.map +1 -1
  24. package/build/components/block-switcher/block-styles-menu.js +3 -23
  25. package/build/components/block-switcher/block-styles-menu.js.map +1 -1
  26. package/build/components/block-tools/back-compat.js +2 -1
  27. package/build/components/block-tools/back-compat.js.map +1 -1
  28. package/build/components/block-tools/insertion-point.js +11 -1
  29. package/build/components/block-tools/insertion-point.js.map +1 -1
  30. package/build/components/button-block-appender/index.js +2 -1
  31. package/build/components/button-block-appender/index.js.map +1 -1
  32. package/build/components/colors-gradients/control.js +1 -0
  33. package/build/components/colors-gradients/control.js.map +1 -1
  34. package/build/components/colors-gradients/panel-color-gradient-settings.js +51 -10
  35. package/build/components/colors-gradients/panel-color-gradient-settings.js.map +1 -1
  36. package/build/components/default-block-appender/index.js +16 -19
  37. package/build/components/default-block-appender/index.js.map +1 -1
  38. package/build/components/default-style-picker/index.js +18 -3
  39. package/build/components/default-style-picker/index.js.map +1 -1
  40. package/build/components/iframe/index.js +3 -4
  41. package/build/components/iframe/index.js.map +1 -1
  42. package/build/components/index.js +17 -10
  43. package/build/components/index.js.map +1 -1
  44. package/build/components/inner-blocks/default-block-appender.js +2 -4
  45. package/build/components/inner-blocks/default-block-appender.js.map +1 -1
  46. package/build/components/inserter/index.native.js +1 -1
  47. package/build/components/inserter/index.native.js.map +1 -1
  48. package/build/components/inserter/tabs.native.js +7 -4
  49. package/build/components/inserter/tabs.native.js.map +1 -1
  50. package/build/components/inspector-controls/block-support-slot-container.js +2 -1
  51. package/build/components/inspector-controls/block-support-slot-container.js.map +1 -1
  52. package/build/components/inspector-controls/fill.native.js +3 -5
  53. package/build/components/inspector-controls/fill.native.js.map +1 -1
  54. package/build/components/inspector-controls/groups.js +2 -0
  55. package/build/components/inspector-controls/groups.js.map +1 -1
  56. package/build/components/inspector-controls/slot.js +1 -3
  57. package/build/components/inspector-controls/slot.js.map +1 -1
  58. package/build/components/letter-spacing-control/index.js +6 -6
  59. package/build/components/letter-spacing-control/index.js.map +1 -1
  60. package/build/components/list-view/block-select-button.js +23 -3
  61. package/build/components/list-view/block-select-button.js.map +1 -1
  62. package/build/components/media-placeholder/index.js +2 -0
  63. package/build/components/media-placeholder/index.js.map +1 -1
  64. package/build/components/media-replace-flow/index.js +2 -0
  65. package/build/components/media-replace-flow/index.js.map +1 -1
  66. package/build/components/rich-text/file-paste-handler.js +1 -1
  67. package/build/components/rich-text/file-paste-handler.js.map +1 -1
  68. package/build/components/rich-text/prevent-event-discovery.js +33 -0
  69. package/build/components/rich-text/prevent-event-discovery.js.map +1 -0
  70. package/build/components/rich-text/use-input-rules.js +3 -1
  71. package/build/components/rich-text/use-input-rules.js.map +1 -1
  72. package/build/components/selection-scroll-into-view/index.js +2 -1
  73. package/build/components/selection-scroll-into-view/index.js.map +1 -1
  74. package/build/components/use-display-block-controls/index.native.js +45 -0
  75. package/build/components/use-display-block-controls/index.native.js.map +1 -0
  76. package/build/components/use-on-block-drop/index.js +7 -3
  77. package/build/components/use-on-block-drop/index.js.map +1 -1
  78. package/build/components/writing-flow/use-multi-selection.js +3 -1
  79. package/build/components/writing-flow/use-multi-selection.js.map +1 -1
  80. package/build/hooks/border-color.js +63 -5
  81. package/build/hooks/border-color.js.map +1 -1
  82. package/build/hooks/border-radius.js +47 -0
  83. package/build/hooks/border-radius.js.map +1 -1
  84. package/build/hooks/border-style.js +41 -0
  85. package/build/hooks/border-style.js.map +1 -1
  86. package/build/hooks/border-width.js +70 -31
  87. package/build/hooks/border-width.js.map +1 -1
  88. package/build/hooks/border.js +81 -11
  89. package/build/hooks/border.js.map +1 -1
  90. package/build/hooks/letter-spacing.js +1 -1
  91. package/build/hooks/letter-spacing.js.map +1 -1
  92. package/build/hooks/typography.js +1 -1
  93. package/build/hooks/typography.js.map +1 -1
  94. package/build/store/actions.js +1 -2
  95. package/build/store/actions.js.map +1 -1
  96. package/build/store/defaults.js +5 -1
  97. package/build/store/defaults.js.map +1 -1
  98. package/build/store/reducer.js +10 -82
  99. package/build/store/reducer.js.map +1 -1
  100. package/build/store/selectors.js +25 -7
  101. package/build/store/selectors.js.map +1 -1
  102. package/build/utils/get-paste-event-data.js +1 -1
  103. package/build/utils/get-paste-event-data.js.map +1 -1
  104. package/build/utils/parse-css-unit-to-px.js +1 -1
  105. package/build/utils/parse-css-unit-to-px.js.map +1 -1
  106. package/build/utils/transform-styles/index.js +2 -3
  107. package/build/utils/transform-styles/index.js.map +1 -1
  108. package/build-module/components/block-caption/index.native.js +13 -3
  109. package/build-module/components/block-caption/index.native.js.map +1 -1
  110. package/build-module/components/block-inspector/index.js +12 -23
  111. package/build-module/components/block-inspector/index.js.map +1 -1
  112. package/build-module/components/block-list-appender/index.js +3 -10
  113. package/build-module/components/block-list-appender/index.js.map +1 -1
  114. package/build-module/components/block-preview/auto.js +10 -2
  115. package/build-module/components/block-preview/auto.js.map +1 -1
  116. package/build-module/components/block-preview/index.js +46 -0
  117. package/build-module/components/block-preview/index.js.map +1 -1
  118. package/build-module/components/block-settings/container.native.js +2 -1
  119. package/build-module/components/block-settings/container.native.js.map +1 -1
  120. package/build-module/components/block-styles/index.js +112 -133
  121. package/build-module/components/block-styles/index.js.map +1 -1
  122. package/build-module/components/block-styles/menu-items.js +50 -0
  123. package/build-module/components/block-styles/menu-items.js.map +1 -0
  124. package/build-module/components/block-styles/preview-panel.js +35 -0
  125. package/build-module/components/block-styles/preview-panel.js.map +1 -0
  126. package/build-module/components/block-styles/use-styles-for-block.js +107 -0
  127. package/build-module/components/block-styles/use-styles-for-block.js.map +1 -0
  128. package/build-module/components/block-styles/utils.js +34 -0
  129. package/build-module/components/block-styles/utils.js.map +1 -1
  130. package/build-module/components/block-switcher/block-styles-menu.js +3 -21
  131. package/build-module/components/block-switcher/block-styles-menu.js.map +1 -1
  132. package/build-module/components/block-tools/back-compat.js +2 -1
  133. package/build-module/components/block-tools/back-compat.js.map +1 -1
  134. package/build-module/components/block-tools/insertion-point.js +11 -1
  135. package/build-module/components/block-tools/insertion-point.js.map +1 -1
  136. package/build-module/components/button-block-appender/index.js +2 -1
  137. package/build-module/components/button-block-appender/index.js.map +1 -1
  138. package/build-module/components/colors-gradients/control.js +1 -0
  139. package/build-module/components/colors-gradients/control.js.map +1 -1
  140. package/build-module/components/colors-gradients/panel-color-gradient-settings.js +54 -13
  141. package/build-module/components/colors-gradients/panel-color-gradient-settings.js.map +1 -1
  142. package/build-module/components/default-block-appender/index.js +15 -18
  143. package/build-module/components/default-block-appender/index.js.map +1 -1
  144. package/build-module/components/default-style-picker/index.js +17 -3
  145. package/build-module/components/default-style-picker/index.js.map +1 -1
  146. package/build-module/components/iframe/index.js +3 -4
  147. package/build-module/components/iframe/index.js.map +1 -1
  148. package/build-module/components/index.js +2 -2
  149. package/build-module/components/index.js.map +1 -1
  150. package/build-module/components/inner-blocks/default-block-appender.js +2 -4
  151. package/build-module/components/inner-blocks/default-block-appender.js.map +1 -1
  152. package/build-module/components/inserter/index.native.js +2 -2
  153. package/build-module/components/inserter/index.native.js.map +1 -1
  154. package/build-module/components/inserter/tabs.native.js +7 -4
  155. package/build-module/components/inserter/tabs.native.js.map +1 -1
  156. package/build-module/components/inspector-controls/block-support-slot-container.js +2 -1
  157. package/build-module/components/inspector-controls/block-support-slot-container.js.map +1 -1
  158. package/build-module/components/inspector-controls/fill.native.js +3 -5
  159. package/build-module/components/inspector-controls/fill.native.js.map +1 -1
  160. package/build-module/components/inspector-controls/groups.js +2 -0
  161. package/build-module/components/inspector-controls/groups.js.map +1 -1
  162. package/build-module/components/inspector-controls/slot.js +1 -3
  163. package/build-module/components/inspector-controls/slot.js.map +1 -1
  164. package/build-module/components/letter-spacing-control/index.js +6 -6
  165. package/build-module/components/letter-spacing-control/index.js.map +1 -1
  166. package/build-module/components/list-view/block-select-button.js +22 -3
  167. package/build-module/components/list-view/block-select-button.js.map +1 -1
  168. package/build-module/components/media-placeholder/index.js +2 -0
  169. package/build-module/components/media-placeholder/index.js.map +1 -1
  170. package/build-module/components/media-replace-flow/index.js +2 -0
  171. package/build-module/components/media-replace-flow/index.js.map +1 -1
  172. package/build-module/components/rich-text/file-paste-handler.js +1 -1
  173. package/build-module/components/rich-text/file-paste-handler.js.map +1 -1
  174. package/build-module/components/rich-text/prevent-event-discovery.js +25 -0
  175. package/build-module/components/rich-text/prevent-event-discovery.js.map +1 -0
  176. package/build-module/components/rich-text/use-input-rules.js +2 -1
  177. package/build-module/components/rich-text/use-input-rules.js.map +1 -1
  178. package/build-module/components/selection-scroll-into-view/index.js +2 -1
  179. package/build-module/components/selection-scroll-into-view/index.js.map +1 -1
  180. package/build-module/components/use-display-block-controls/index.native.js +34 -0
  181. package/build-module/components/use-display-block-controls/index.native.js.map +1 -0
  182. package/build-module/components/use-on-block-drop/index.js +7 -3
  183. package/build-module/components/use-on-block-drop/index.js.map +1 -1
  184. package/build-module/components/writing-flow/use-multi-selection.js +3 -4
  185. package/build-module/components/writing-flow/use-multi-selection.js.map +1 -1
  186. package/build-module/hooks/border-color.js +61 -7
  187. package/build-module/hooks/border-color.js.map +1 -1
  188. package/build-module/hooks/border-radius.js +42 -0
  189. package/build-module/hooks/border-radius.js.map +1 -1
  190. package/build-module/hooks/border-style.js +36 -0
  191. package/build-module/hooks/border-style.js.map +1 -1
  192. package/build-module/hooks/border-width.js +66 -32
  193. package/build-module/hooks/border-width.js.map +1 -1
  194. package/build-module/hooks/border.js +80 -12
  195. package/build-module/hooks/border.js.map +1 -1
  196. package/build-module/hooks/letter-spacing.js +1 -1
  197. package/build-module/hooks/letter-spacing.js.map +1 -1
  198. package/build-module/hooks/typography.js +1 -1
  199. package/build-module/hooks/typography.js.map +1 -1
  200. package/build-module/store/actions.js +1 -2
  201. package/build-module/store/actions.js.map +1 -1
  202. package/build-module/store/defaults.js +5 -1
  203. package/build-module/store/defaults.js.map +1 -1
  204. package/build-module/store/reducer.js +11 -83
  205. package/build-module/store/reducer.js.map +1 -1
  206. package/build-module/store/selectors.js +24 -7
  207. package/build-module/store/selectors.js.map +1 -1
  208. package/build-module/utils/get-paste-event-data.js +1 -1
  209. package/build-module/utils/get-paste-event-data.js.map +1 -1
  210. package/build-module/utils/parse-css-unit-to-px.js +1 -1
  211. package/build-module/utils/parse-css-unit-to-px.js.map +1 -1
  212. package/build-module/utils/transform-styles/index.js +2 -3
  213. package/build-module/utils/transform-styles/index.js.map +1 -1
  214. package/build-style/style-rtl.css +229 -182
  215. package/build-style/style.css +229 -182
  216. package/package.json +27 -27
  217. package/src/components/block-caption/index.native.js +22 -4
  218. package/src/components/block-inspector/index.js +14 -17
  219. package/src/components/block-list-appender/index.js +5 -21
  220. package/src/components/block-preview/auto.js +7 -2
  221. package/src/components/block-preview/index.js +60 -0
  222. package/src/components/block-preview/style.scss +23 -0
  223. package/src/components/block-preview/test/index.js +114 -0
  224. package/src/components/block-settings/container.native.js +1 -0
  225. package/src/components/block-styles/index.js +125 -145
  226. package/src/components/block-styles/menu-items.js +49 -0
  227. package/src/components/block-styles/preview-panel.js +36 -0
  228. package/src/components/block-styles/style.scss +59 -51
  229. package/src/components/block-styles/test/{index.js → utils.js} +60 -1
  230. package/src/components/block-styles/use-styles-for-block.js +99 -0
  231. package/src/components/block-styles/utils.js +39 -0
  232. package/src/components/block-switcher/block-styles-menu.js +3 -38
  233. package/src/components/block-tools/back-compat.js +1 -0
  234. package/src/components/block-tools/insertion-point.js +10 -1
  235. package/src/components/border-style-control/style.scss +3 -3
  236. package/src/components/button-block-appender/index.js +1 -0
  237. package/src/components/color-palette/test/__snapshots__/control.js.snap +1 -0
  238. package/src/components/colors-gradients/control.js +3 -0
  239. package/src/components/colors-gradients/panel-color-gradient-settings.js +75 -19
  240. package/src/components/colors-gradients/style.scss +42 -5
  241. package/src/components/default-block-appender/index.js +17 -24
  242. package/src/components/default-block-appender/style.scss +4 -0
  243. package/src/components/default-block-appender/test/__snapshots__/index.js.snap +12 -24
  244. package/src/components/default-block-appender/test/index.js +4 -14
  245. package/src/components/default-style-picker/index.js +18 -6
  246. package/src/components/iframe/index.js +6 -3
  247. package/src/components/index.js +5 -2
  248. package/src/components/inner-blocks/README.md +2 -0
  249. package/src/components/inner-blocks/default-block-appender.js +2 -7
  250. package/src/components/inserter/index.native.js +2 -2
  251. package/src/components/inserter/tabs.native.js +5 -4
  252. package/src/components/inspector-controls/block-support-slot-container.js +3 -1
  253. package/src/components/inspector-controls/fill.native.js +4 -3
  254. package/src/components/inspector-controls/groups.js +2 -0
  255. package/src/components/inspector-controls/slot.js +2 -7
  256. package/src/components/letter-spacing-control/index.js +6 -6
  257. package/src/components/link-control/README.md +1 -1
  258. package/src/components/link-control/test/index.js +2 -0
  259. package/src/components/list-view/block-select-button.js +20 -1
  260. package/src/components/media-placeholder/index.js +2 -0
  261. package/src/components/media-replace-flow/index.js +2 -0
  262. package/src/components/rich-text/file-paste-handler.js +3 -1
  263. package/src/components/rich-text/prevent-event-discovery.js +23 -0
  264. package/src/components/rich-text/use-input-rules.js +2 -1
  265. package/src/components/selection-scroll-into-view/index.js +1 -0
  266. package/src/components/url-input/README.md +5 -0
  267. package/src/components/use-display-block-controls/index.native.js +38 -0
  268. package/src/components/use-on-block-drop/index.js +7 -3
  269. package/src/components/writing-flow/test/use-multi-selection.js +36 -0
  270. package/src/components/writing-flow/use-multi-selection.js +12 -9
  271. package/src/hooks/border-color.js +55 -3
  272. package/src/hooks/border-radius.js +32 -0
  273. package/src/hooks/border-style.js +26 -0
  274. package/src/hooks/border-width.js +56 -32
  275. package/src/hooks/border.js +115 -20
  276. package/src/hooks/border.scss +3 -17
  277. package/src/hooks/dimensions.scss +5 -0
  278. package/src/hooks/letter-spacing.js +1 -1
  279. package/src/hooks/typography.js +1 -1
  280. package/src/store/actions.js +1 -2
  281. package/src/store/defaults.js +2 -0
  282. package/src/store/reducer.js +13 -104
  283. package/src/store/selectors.js +37 -7
  284. package/src/style.scss +4 -3
  285. package/src/utils/get-paste-event-data.js +1 -1
  286. package/src/utils/parse-css-unit-to-px.js +1 -1
  287. package/src/utils/test/parse-css-unit-to-px.js +1 -0
  288. package/src/utils/transform-styles/index.js +13 -16
  289. package/tsconfig.tsbuildinfo +1 -1
  290. package/build/components/use-canvas-click-redirect/index.js +0 -66
  291. package/build/components/use-canvas-click-redirect/index.js.map +0 -1
  292. package/build-module/components/use-canvas-click-redirect/index.js +0 -54
  293. package/build-module/components/use-canvas-click-redirect/index.js.map +0 -1
  294. package/src/components/use-canvas-click-redirect/index.js +0 -57
@@ -1,182 +1,162 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import { find, noop } from 'lodash';
4
+ import { noop, debounce } from 'lodash';
5
5
  import classnames from 'classnames';
6
6
 
7
7
  /**
8
8
  * WordPress dependencies
9
9
  */
10
- import { useMemo } from '@wordpress/element';
11
- import { useSelect, useDispatch } from '@wordpress/data';
10
+ import { useState, useLayoutEffect } from '@wordpress/element';
11
+ import { useViewportMatch } from '@wordpress/compose';
12
12
  import { ENTER, SPACE } from '@wordpress/keycodes';
13
- import { _x } from '@wordpress/i18n';
14
13
  import {
15
- getBlockType,
16
- cloneBlock,
17
- getBlockFromExample,
18
- store as blocksStore,
19
- } from '@wordpress/blocks';
14
+ Button,
15
+ __experimentalText as Text,
16
+ Slot,
17
+ Fill,
18
+ } from '@wordpress/components';
20
19
 
21
20
  /**
22
21
  * Internal dependencies
23
22
  */
24
- import { getActiveStyle, replaceActiveStyle } from './utils';
25
- import BlockPreview from '../block-preview';
26
- import { store as blockEditorStore } from '../../store';
23
+ import BlockStylesPreviewPanel from './preview-panel';
24
+ import useStylesForBlocks from './use-styles-for-block';
27
25
 
28
- const EMPTY_OBJECT = {};
29
-
30
- function useGenericPreviewBlock( block, type ) {
31
- return useMemo( () => {
32
- const example = type?.example;
33
- const blockName = type?.name;
34
-
35
- if ( example && blockName ) {
36
- return getBlockFromExample( blockName, {
37
- attributes: example.attributes,
38
- innerBlocks: example.innerBlocks,
39
- } );
40
- }
26
+ function BlockStylesPreviewPanelSlot( { scope } ) {
27
+ return <Slot name={ `BlockStylesPreviewPanel/${ scope }` } />;
28
+ }
41
29
 
42
- if ( block ) {
43
- return cloneBlock( block );
44
- }
45
- }, [ type?.example ? block?.name : block, type ] );
30
+ function BlockStylesPreviewPanelFill( { children, scope, ...props } ) {
31
+ return (
32
+ <Fill name={ `BlockStylesPreviewPanel/${ scope }` }>
33
+ <div { ...props }>{ children }</div>
34
+ </Fill>
35
+ );
46
36
  }
47
37
 
38
+ // Top position (in px) of the Block Styles container
39
+ // relative to the editor pane.
40
+ // The value is the equivalent of the container's right position.
41
+ const DEFAULT_POSITION_TOP = 16;
42
+
43
+ // Block Styles component for the Settings Sidebar.
48
44
  function BlockStyles( {
49
45
  clientId,
50
46
  onSwitch = noop,
51
47
  onHoverClassName = noop,
52
- itemRole,
48
+ scope,
53
49
  } ) {
54
- const selector = ( select ) => {
55
- const { getBlock } = select( blockEditorStore );
56
- const block = getBlock( clientId );
57
-
58
- if ( ! block ) {
59
- return EMPTY_OBJECT;
60
- }
61
-
62
- const blockType = getBlockType( block.name );
63
- const { getBlockStyles } = select( blocksStore );
64
- return {
65
- block,
66
- type: blockType,
67
- styles: getBlockStyles( block.name ),
68
- className: block.attributes.className || '',
69
- };
70
- };
71
-
72
- const { styles, block, type, className } = useSelect( selector, [
50
+ const {
51
+ onSelect,
52
+ stylesToRender,
53
+ activeStyle,
54
+ genericPreviewBlock,
55
+ className: previewClassName,
56
+ } = useStylesForBlocks( {
73
57
  clientId,
74
- ] );
75
-
76
- const { updateBlockAttributes } = useDispatch( blockEditorStore );
77
- const genericPreviewBlock = useGenericPreviewBlock( block, type );
78
-
79
- if ( ! styles || styles.length === 0 ) {
58
+ onSwitch,
59
+ } );
60
+ const [ hoveredStyle, setHoveredStyle ] = useState( null );
61
+ const [ containerScrollTop, setContainerScrollTop ] = useState( 0 );
62
+ const isMobileViewport = useViewportMatch( 'medium', '<' );
63
+
64
+ useLayoutEffect( () => {
65
+ const scrollContainer = document.querySelector(
66
+ '.interface-interface-skeleton__content'
67
+ );
68
+ const scrollTop = scrollContainer?.scrollTop || 0;
69
+ setContainerScrollTop( scrollTop + DEFAULT_POSITION_TOP );
70
+ }, [ hoveredStyle ] );
71
+
72
+ if ( ! stylesToRender || stylesToRender.length === 0 ) {
80
73
  return null;
81
74
  }
82
75
 
83
- const renderedStyles = find( styles, 'isDefault' )
84
- ? styles
85
- : [
86
- {
87
- name: 'default',
88
- label: _x( 'Default', 'block style' ),
89
- isDefault: true,
90
- },
91
- ...styles,
92
- ];
76
+ const debouncedSetHoveredStyle = debounce( setHoveredStyle, 250 );
93
77
 
94
- const activeStyle = getActiveStyle( renderedStyles, className );
95
- return (
96
- <div className="block-editor-block-styles">
97
- { renderedStyles.map( ( style ) => {
98
- const styleClassName = replaceActiveStyle(
99
- className,
100
- activeStyle,
101
- style
102
- );
103
- return (
104
- <BlockStyleItem
105
- genericPreviewBlock={ genericPreviewBlock }
106
- viewportWidth={ type.example?.viewportWidth ?? 500 }
107
- className={ className }
108
- isActive={ activeStyle === style }
109
- key={ style.name }
110
- onSelect={ () => {
111
- updateBlockAttributes( clientId, {
112
- className: styleClassName,
113
- } );
114
- onHoverClassName( null );
115
- onSwitch();
116
- } }
117
- onBlur={ () => onHoverClassName( null ) }
118
- onHover={ () => onHoverClassName( styleClassName ) }
119
- style={ style }
120
- styleClassName={ styleClassName }
121
- itemRole={ itemRole }
122
- />
123
- );
124
- } ) }
125
- </div>
126
- );
127
- }
78
+ const onSelectStylePreview = ( style ) => {
79
+ onSelect( style );
80
+ onHoverClassName( null );
81
+ setHoveredStyle( null );
82
+ debouncedSetHoveredStyle.cancel();
83
+ };
128
84
 
129
- function BlockStyleItem( {
130
- genericPreviewBlock,
131
- viewportWidth,
132
- style,
133
- isActive,
134
- onBlur,
135
- onHover,
136
- onSelect,
137
- styleClassName,
138
- itemRole,
139
- } ) {
140
- const previewBlocks = useMemo( () => {
141
- return {
142
- ...genericPreviewBlock,
143
- attributes: {
144
- ...genericPreviewBlock.attributes,
145
- className: styleClassName,
146
- },
147
- };
148
- }, [ genericPreviewBlock, styleClassName ] );
85
+ const styleItemHandler = ( item ) => {
86
+ if ( hoveredStyle === item ) {
87
+ debouncedSetHoveredStyle.cancel();
88
+ return;
89
+ }
90
+ debouncedSetHoveredStyle( item );
91
+ onHoverClassName( item?.name ?? null );
92
+ };
149
93
 
150
94
  return (
151
- <div
152
- key={ style.name }
153
- className={ classnames( 'block-editor-block-styles__item', {
154
- 'is-active': isActive,
155
- } ) }
156
- onClick={ () => onSelect() }
157
- onKeyDown={ ( event ) => {
158
- if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
159
- event.preventDefault();
160
- onSelect();
161
- }
162
- } }
163
- onMouseEnter={ onHover }
164
- onMouseLeave={ onBlur }
165
- role={ itemRole || 'button' }
166
- tabIndex="0"
167
- aria-label={ style.label || style.name }
168
- >
169
- <div className="block-editor-block-styles__item-preview">
170
- <BlockPreview
171
- viewportWidth={ viewportWidth }
172
- blocks={ previewBlocks }
173
- />
174
- </div>
175
- <div className="block-editor-block-styles__item-label">
176
- { style.label || style.name }
95
+ <div className="block-editor-block-styles">
96
+ <div className="block-editor-block-styles__variants">
97
+ { stylesToRender.map( ( style ) => {
98
+ const buttonText = style.label || style.name;
99
+
100
+ return (
101
+ <Button
102
+ className={ classnames(
103
+ 'block-editor-block-styles__item',
104
+ {
105
+ 'is-active':
106
+ activeStyle.name === style.name,
107
+ }
108
+ ) }
109
+ key={ style.name }
110
+ variant="secondary"
111
+ label={ buttonText }
112
+ onMouseEnter={ () => styleItemHandler( style ) }
113
+ onFocus={ () => styleItemHandler( style ) }
114
+ onMouseLeave={ () => styleItemHandler( null ) }
115
+ onBlur={ () => styleItemHandler( null ) }
116
+ onKeyDown={ ( event ) => {
117
+ if (
118
+ ENTER === event.keyCode ||
119
+ SPACE === event.keyCode
120
+ ) {
121
+ event.preventDefault();
122
+ onSelectStylePreview( style );
123
+ }
124
+ } }
125
+ onClick={ () => onSelectStylePreview( style ) }
126
+ role="button"
127
+ tabIndex="0"
128
+ >
129
+ <Text
130
+ as="span"
131
+ limit={ 12 }
132
+ ellipsizeMode="tail"
133
+ className="block-editor-block-styles__item-text"
134
+ truncate
135
+ >
136
+ { buttonText }
137
+ </Text>
138
+ </Button>
139
+ );
140
+ } ) }
177
141
  </div>
142
+ { hoveredStyle && ! isMobileViewport && (
143
+ <BlockStylesPreviewPanelFill
144
+ scope={ scope }
145
+ className="block-editor-block-styles__preview-panel"
146
+ style={ { top: containerScrollTop } }
147
+ onMouseLeave={ () => styleItemHandler( null ) }
148
+ >
149
+ <BlockStylesPreviewPanel
150
+ activeStyle={ activeStyle }
151
+ className={ previewClassName }
152
+ genericPreviewBlock={ genericPreviewBlock }
153
+ style={ hoveredStyle }
154
+ />
155
+ </BlockStylesPreviewPanelFill>
156
+ ) }
178
157
  </div>
179
158
  );
180
159
  }
181
160
 
161
+ BlockStyles.Slot = BlockStylesPreviewPanelSlot;
182
162
  export default BlockStyles;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { noop } from 'lodash';
5
+
6
+ /**
7
+ * WordPress dependencies
8
+ */
9
+ import { MenuItem, __experimentalText as Text } from '@wordpress/components';
10
+ import { check } from '@wordpress/icons';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+ import useStylesForBlocks from './use-styles-for-block';
16
+
17
+ export default function BlockStylesMenuItems( { clientId, onSwitch = noop } ) {
18
+ const { onSelect, stylesToRender, activeStyle } = useStylesForBlocks( {
19
+ clientId,
20
+ onSwitch,
21
+ } );
22
+
23
+ if ( ! stylesToRender || stylesToRender.length === 0 ) {
24
+ return null;
25
+ }
26
+ return (
27
+ <>
28
+ { stylesToRender.map( ( style ) => {
29
+ const menuItemText = style.label || style.name;
30
+ return (
31
+ <MenuItem
32
+ key={ style.name }
33
+ icon={ activeStyle.name === style.name ? check : null }
34
+ onClick={ () => onSelect( style ) }
35
+ >
36
+ <Text
37
+ as="span"
38
+ limit={ 18 }
39
+ ellipsizeMode="tail"
40
+ truncate
41
+ >
42
+ { menuItemText }
43
+ </Text>
44
+ </MenuItem>
45
+ );
46
+ } ) }
47
+ </>
48
+ );
49
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useMemo } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import InserterPreviewPanel from '../inserter/preview-panel';
10
+ import { replaceActiveStyle } from './utils';
11
+
12
+ export default function BlockStylesPreviewPanel( {
13
+ genericPreviewBlock,
14
+ style,
15
+ className,
16
+ activeStyle,
17
+ } ) {
18
+ const styleClassName = replaceActiveStyle( className, activeStyle, style );
19
+ const previewBlocks = useMemo( () => {
20
+ return {
21
+ ...genericPreviewBlock,
22
+ title: style.label || style.name,
23
+ description: style.description,
24
+ initialAttributes: {
25
+ ...genericPreviewBlock.attributes,
26
+ className:
27
+ styleClassName +
28
+ ' block-editor-block-styles__block-preview-container',
29
+ },
30
+ };
31
+ }, [ genericPreviewBlock, styleClassName ] );
32
+
33
+ return (
34
+ <InserterPreviewPanel item={ previewBlocks } isStylePreview={ true } />
35
+ );
36
+ }
@@ -1,67 +1,75 @@
1
- .block-editor-block-styles {
2
- display: flex;
3
- flex-wrap: wrap;
4
- justify-content: space-between;
1
+ .block-editor-block-styles + .default-style-picker__default-switcher {
2
+ margin-top: $grid-unit-20;
5
3
  }
6
4
 
7
- .block-editor-block-styles__item {
8
- width: calc(50% - #{ $grid-unit-05 });
9
- margin: $grid-unit-05 0;
10
- flex-shrink: 0;
11
- cursor: pointer;
12
- overflow: hidden;
13
- border-radius: $radius-block-ui;
14
- padding: $grid-unit-05 * 1.5;
15
- display: flex;
16
- flex-direction: column;
17
-
18
- &:focus {
19
- box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
5
+ .block-editor-block-styles__preview-panel {
6
+ display: none;
7
+ position: absolute;
8
+ right: $grid-unit-20;
9
+ left: auto;
10
+ // Same layer as the sidebar from which it's triggered.
11
+ z-index: z-index(".interface-interface-skeleton__sidebar {greater than small}");
20
12
 
21
- // Windows High Contrast mode will show this outline, but not the box-shadow.
22
- outline: 2px solid transparent;
13
+ // Only show in narrow widths.
14
+ @include break-medium() {
15
+ display: block;
23
16
  }
24
17
 
25
- &:hover .block-editor-block-styles__item-preview {
26
- border-color: var(--wp-admin-theme-color);
18
+ // Overrides for InserterPreviewPanel.
19
+ .block-editor-inserter__preview-container {
20
+ left: auto;
21
+ right: auto;
22
+ top: auto;
23
+ position: static;
27
24
  }
28
25
 
29
- &.is-active {
30
- .block-editor-block-styles__item-label {
31
- font-weight: bold;
32
- }
33
-
34
- .block-editor-block-styles__item-preview {
35
- margin: 0;
36
- border: 2px solid $gray-900;
37
- }
26
+ .block-editor-block-card__title.block-editor-block-card__title {
27
+ margin: 0;
38
28
  }
39
-
40
- .block-editor-block-preview__container {
41
- cursor: inherit;
29
+ .block-editor-block-icon {
30
+ display: none;
42
31
  }
43
32
  }
44
33
 
45
- // Show a little preview thumbnail for style variations.
46
- .block-editor-block-styles__item-preview {
47
- outline: $border-width solid transparent; // Shown in Windows High Contrast mode.
48
- padding: 0;
49
- margin: 2px;
50
- border-radius: $radius-block-ui;
34
+ .block-editor-block-styles__variants {
51
35
  display: flex;
52
- overflow: hidden;
53
- background: $white;
54
- align-items: center;
55
- flex-grow: 1;
56
- min-height: 80px;
57
- max-height: 160px;
58
- }
36
+ flex-wrap: wrap;
37
+ justify-content: space-between;
38
+ gap: $grid-unit-10;
39
+
40
+ .block-editor-block-styles__item {
41
+ color: $gray-800;
42
+ box-shadow: inset 0 0 0 1px $gray-400;
43
+ display: inline-block;
44
+ width: calc(50% - #{$grid-unit-05});
45
+
46
+ &:focus,
47
+ &:hover {
48
+ color: var(--wp-admin-theme-color);
49
+ box-shadow: inset 0 0 0 2px var(--wp-admin-theme-color);
50
+ }
51
+
52
+ &.is-active,
53
+ &.is-active:hover {
54
+ background-color: $gray-800;
55
+ box-shadow: none;
56
+ }
57
+
58
+ &.is-active .block-editor-block-styles__item-text,
59
+ &.is-active:hover .block-editor-block-styles__item-text {
60
+ color: $white;
61
+ }
59
62
 
60
- .block-editor-block-switcher__styles__menugroup {
61
- position: relative;
63
+ &.is-active:focus {
64
+ box-shadow: inset 0 0 0 1px $white, 0 0 0 2px var(--wp-admin-theme-color);
65
+ }
66
+ }
62
67
  }
63
68
 
64
- .block-editor-block-styles__item-label {
65
- text-align: center;
66
- padding: 4px 0;
69
+ // To prevent overflow in the preview container,
70
+ // ensure that block contents' margin and padding
71
+ // do not add to the block container's width.
72
+ .block-editor-block-styles__block-preview-container,
73
+ .block-editor-block-styles__block-preview-container * {
74
+ box-sizing: border-box !important;
67
75
  }
@@ -1,7 +1,12 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { getActiveStyle, replaceActiveStyle } from '../utils';
4
+ import {
5
+ getActiveStyle,
6
+ getDefaultStyle,
7
+ getRenderedStyles,
8
+ replaceActiveStyle,
9
+ } from '../utils';
5
10
 
6
11
  describe( 'getActiveStyle', () => {
7
12
  it( 'Should return the undefined if no active style', () => {
@@ -74,3 +79,57 @@ describe( 'replaceActiveStyle', () => {
74
79
  );
75
80
  } );
76
81
  } );
82
+
83
+ describe( 'getRenderedStyles', () => {
84
+ it( 'Should return an empty array if styles is falsy', () => {
85
+ expect( getRenderedStyles( null ) ).toEqual( [] );
86
+ } );
87
+
88
+ it( 'Should return an empty array if styles array is empty', () => {
89
+ expect( getRenderedStyles( [] ) ).toEqual( [] );
90
+ } );
91
+
92
+ it( 'Should return styles collection if there is a default', () => {
93
+ const styles = [
94
+ { name: 'hazlenut' },
95
+ { name: 'cashew', isDefault: true },
96
+ ];
97
+
98
+ expect( getRenderedStyles( styles ) ).toEqual( styles );
99
+ } );
100
+
101
+ it( 'Should add a default item to the styles collection if there is no default', () => {
102
+ const styles = [ { name: 'pistachio' }, { name: 'peanut' } ];
103
+ const defaultStyle = {
104
+ name: 'default',
105
+ label: 'Default',
106
+ isDefault: true,
107
+ };
108
+
109
+ expect( getRenderedStyles( styles ) ).toEqual( [
110
+ defaultStyle,
111
+ ...styles,
112
+ ] );
113
+ } );
114
+ } );
115
+
116
+ describe( 'getDefaultStyle', () => {
117
+ it( 'Should return default style object', () => {
118
+ const styles = [
119
+ { name: 'trout' },
120
+ { name: 'bream', isDefault: true },
121
+ ];
122
+
123
+ expect( getDefaultStyle( styles ) ).toEqual( styles[ 1 ] );
124
+ } );
125
+
126
+ it( 'Should return `undefined` if there is no default', () => {
127
+ const styles = [ { name: 'snapper' }, { name: 'perch' } ];
128
+
129
+ expect( getDefaultStyle( styles ) ).toBeUndefined();
130
+ } );
131
+
132
+ it( 'Should return `undefined` if `styles` argument is no passed', () => {
133
+ expect( getDefaultStyle() ).toBeUndefined();
134
+ } );
135
+ } );
@@ -0,0 +1,99 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useDispatch, useSelect } from '@wordpress/data';
5
+ import {
6
+ cloneBlock,
7
+ getBlockType,
8
+ getBlockFromExample,
9
+ store as blocksStore,
10
+ } from '@wordpress/blocks';
11
+ import { useMemo } from '@wordpress/element';
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+ import { getActiveStyle, getRenderedStyles, replaceActiveStyle } from './utils';
17
+ import { store as blockEditorStore } from '../../store';
18
+
19
+ /**
20
+ *
21
+ * @param {WPBlock} block Block object.
22
+ * @param {WPBlockType} type Block type settings.
23
+ * @return {WPBlock} A generic block ready for styles preview.
24
+ */
25
+ function useGenericPreviewBlock( block, type ) {
26
+ return useMemo( () => {
27
+ const example = type?.example;
28
+ const blockName = type?.name;
29
+
30
+ if ( example && blockName ) {
31
+ return getBlockFromExample( blockName, {
32
+ attributes: example.attributes,
33
+ innerBlocks: example.innerBlocks,
34
+ } );
35
+ }
36
+
37
+ if ( block ) {
38
+ return cloneBlock( block );
39
+ }
40
+ }, [ type?.example ? block?.name : block, type ] );
41
+ }
42
+
43
+ /**
44
+ * @typedef useStylesForBlocksArguments
45
+ * @property {string} clientId Block client ID.
46
+ * @property {() => void} onSwitch Block style switch callback function.
47
+ */
48
+
49
+ /**
50
+ *
51
+ * @param {useStylesForBlocksArguments} useStylesForBlocks arguments.
52
+ * @return {Object} Results of the select methods.
53
+ */
54
+ export default function useStylesForBlocks( { clientId, onSwitch } ) {
55
+ const selector = ( select ) => {
56
+ const { getBlock } = select( blockEditorStore );
57
+ const block = getBlock( clientId );
58
+
59
+ if ( ! block ) {
60
+ return {};
61
+ }
62
+ const blockType = getBlockType( block.name );
63
+ const { getBlockStyles } = select( blocksStore );
64
+
65
+ return {
66
+ block,
67
+ blockType,
68
+ styles: getBlockStyles( block.name ),
69
+ className: block.attributes.className || '',
70
+ };
71
+ };
72
+ const { styles, block, blockType, className } = useSelect( selector, [
73
+ clientId,
74
+ ] );
75
+ const { updateBlockAttributes } = useDispatch( blockEditorStore );
76
+ const stylesToRender = getRenderedStyles( styles );
77
+ const activeStyle = getActiveStyle( stylesToRender, className );
78
+ const genericPreviewBlock = useGenericPreviewBlock( block, blockType );
79
+
80
+ const onSelect = ( style ) => {
81
+ const styleClassName = replaceActiveStyle(
82
+ className,
83
+ activeStyle,
84
+ style
85
+ );
86
+ updateBlockAttributes( clientId, {
87
+ className: styleClassName,
88
+ } );
89
+ onSwitch();
90
+ };
91
+
92
+ return {
93
+ onSelect,
94
+ stylesToRender,
95
+ activeStyle,
96
+ genericPreviewBlock,
97
+ className,
98
+ };
99
+ }