@wordpress/edit-site 4.11.0 → 4.13.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 (180) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/components/add-new-template/add-custom-template-modal.js +4 -2
  3. package/build/components/add-new-template/add-custom-template-modal.js.map +1 -1
  4. package/build/components/add-new-template/new-template.js +29 -4
  5. package/build/components/add-new-template/new-template.js.map +1 -1
  6. package/build/components/add-new-template/utils.js +152 -28
  7. package/build/components/add-new-template/utils.js.map +1 -1
  8. package/build/components/block-editor/resizable-editor.js +26 -12
  9. package/build/components/block-editor/resizable-editor.js.map +1 -1
  10. package/build/components/global-styles/border-panel.js +3 -3
  11. package/build/components/global-styles/border-panel.js.map +1 -1
  12. package/build/components/global-styles/dimensions-panel.js +98 -8
  13. package/build/components/global-styles/dimensions-panel.js.map +1 -1
  14. package/build/components/global-styles/hooks.js +4 -4
  15. package/build/components/global-styles/hooks.js.map +1 -1
  16. package/build/components/global-styles/screen-color-palette.js +1 -1
  17. package/build/components/global-styles/screen-color-palette.js.map +1 -1
  18. package/build/components/global-styles/screen-colors.js +51 -7
  19. package/build/components/global-styles/screen-colors.js.map +1 -1
  20. package/build/components/global-styles/screen-heading-color.js +157 -0
  21. package/build/components/global-styles/screen-heading-color.js.map +1 -0
  22. package/build/components/global-styles/screen-typography-element.js +4 -0
  23. package/build/components/global-styles/screen-typography-element.js.map +1 -1
  24. package/build/components/global-styles/screen-typography.js +5 -0
  25. package/build/components/global-styles/screen-typography.js.map +1 -1
  26. package/build/components/global-styles/typography-panel.js +82 -14
  27. package/build/components/global-styles/typography-panel.js.map +1 -1
  28. package/build/components/global-styles/typography-utils.js +217 -0
  29. package/build/components/global-styles/typography-utils.js.map +1 -0
  30. package/build/components/global-styles/ui.js +11 -0
  31. package/build/components/global-styles/ui.js.map +1 -1
  32. package/build/components/global-styles/use-global-styles-output.js +102 -49
  33. package/build/components/global-styles/use-global-styles-output.js.map +1 -1
  34. package/build/components/global-styles/utils.js +54 -5
  35. package/build/components/global-styles/utils.js.map +1 -1
  36. package/build/components/header/document-actions/index.js +1 -0
  37. package/build/components/header/document-actions/index.js.map +1 -1
  38. package/build/components/header/index.js +20 -6
  39. package/build/components/header/index.js.map +1 -1
  40. package/build/components/header/mode-switcher/index.js +0 -4
  41. package/build/components/header/mode-switcher/index.js.map +1 -1
  42. package/build/components/header/more-menu/index.js +13 -3
  43. package/build/components/header/more-menu/index.js.map +1 -1
  44. package/build/components/header/undo-redo/redo.js +2 -1
  45. package/build/components/header/undo-redo/redo.js.map +1 -1
  46. package/build/components/list/actions/index.js +1 -1
  47. package/build/components/list/actions/index.js.map +1 -1
  48. package/build/components/save-button/index.js +2 -3
  49. package/build/components/save-button/index.js.map +1 -1
  50. package/build/components/sidebar/default-sidebar.js +11 -1
  51. package/build/components/sidebar/default-sidebar.js.map +1 -1
  52. package/build/components/sidebar/navigation-menu-sidebar/navigation-menu.js +2 -2
  53. package/build/components/sidebar/navigation-menu-sidebar/navigation-menu.js.map +1 -1
  54. package/build/components/sidebar/plugin-sidebar/index.js +11 -1
  55. package/build/components/sidebar/plugin-sidebar/index.js.map +1 -1
  56. package/build/components/sidebar/template-card/template-actions.js +1 -1
  57. package/build/components/sidebar/template-card/template-actions.js.map +1 -1
  58. package/build/components/template-details/edit-template-title.js +1 -1
  59. package/build/components/template-details/edit-template-title.js.map +1 -1
  60. package/build/components/template-details/index.js +19 -9
  61. package/build/components/template-details/index.js.map +1 -1
  62. package/build/components/template-details/template-areas.js +1 -1
  63. package/build/components/template-details/template-areas.js.map +1 -1
  64. package/build/components/template-details/template-part-area-selector.js +47 -0
  65. package/build/components/template-details/template-part-area-selector.js.map +1 -0
  66. package/build/components/template-part-converter/convert-to-template-part.js +4 -1
  67. package/build/components/template-part-converter/convert-to-template-part.js.map +1 -1
  68. package/build/index.js +1 -1
  69. package/build/index.js.map +1 -1
  70. package/build-module/components/add-new-template/add-custom-template-modal.js +4 -2
  71. package/build-module/components/add-new-template/add-custom-template-modal.js.map +1 -1
  72. package/build-module/components/add-new-template/new-template.js +29 -6
  73. package/build-module/components/add-new-template/new-template.js.map +1 -1
  74. package/build-module/components/add-new-template/utils.js +147 -26
  75. package/build-module/components/add-new-template/utils.js.map +1 -1
  76. package/build-module/components/block-editor/resizable-editor.js +26 -12
  77. package/build-module/components/block-editor/resizable-editor.js.map +1 -1
  78. package/build-module/components/global-styles/border-panel.js +3 -3
  79. package/build-module/components/global-styles/border-panel.js.map +1 -1
  80. package/build-module/components/global-styles/dimensions-panel.js +96 -9
  81. package/build-module/components/global-styles/dimensions-panel.js.map +1 -1
  82. package/build-module/components/global-styles/hooks.js +4 -4
  83. package/build-module/components/global-styles/hooks.js.map +1 -1
  84. package/build-module/components/global-styles/screen-color-palette.js +1 -1
  85. package/build-module/components/global-styles/screen-color-palette.js.map +1 -1
  86. package/build-module/components/global-styles/screen-colors.js +51 -7
  87. package/build-module/components/global-styles/screen-colors.js.map +1 -1
  88. package/build-module/components/global-styles/screen-heading-color.js +143 -0
  89. package/build-module/components/global-styles/screen-heading-color.js.map +1 -0
  90. package/build-module/components/global-styles/screen-typography-element.js +4 -0
  91. package/build-module/components/global-styles/screen-typography-element.js.map +1 -1
  92. package/build-module/components/global-styles/screen-typography.js +5 -0
  93. package/build-module/components/global-styles/screen-typography.js.map +1 -1
  94. package/build-module/components/global-styles/typography-panel.js +83 -15
  95. package/build-module/components/global-styles/typography-panel.js.map +1 -1
  96. package/build-module/components/global-styles/typography-utils.js +204 -0
  97. package/build-module/components/global-styles/typography-utils.js.map +1 -0
  98. package/build-module/components/global-styles/ui.js +10 -0
  99. package/build-module/components/global-styles/ui.js.map +1 -1
  100. package/build-module/components/global-styles/use-global-styles-output.js +102 -53
  101. package/build-module/components/global-styles/use-global-styles-output.js.map +1 -1
  102. package/build-module/components/global-styles/utils.js +53 -5
  103. package/build-module/components/global-styles/utils.js.map +1 -1
  104. package/build-module/components/header/document-actions/index.js +1 -0
  105. package/build-module/components/header/document-actions/index.js.map +1 -1
  106. package/build-module/components/header/index.js +22 -8
  107. package/build-module/components/header/index.js.map +1 -1
  108. package/build-module/components/header/mode-switcher/index.js +0 -4
  109. package/build-module/components/header/mode-switcher/index.js.map +1 -1
  110. package/build-module/components/header/more-menu/index.js +13 -3
  111. package/build-module/components/header/more-menu/index.js.map +1 -1
  112. package/build-module/components/header/undo-redo/redo.js +3 -2
  113. package/build-module/components/header/undo-redo/redo.js.map +1 -1
  114. package/build-module/components/list/actions/index.js +1 -1
  115. package/build-module/components/list/actions/index.js.map +1 -1
  116. package/build-module/components/save-button/index.js +3 -4
  117. package/build-module/components/save-button/index.js.map +1 -1
  118. package/build-module/components/sidebar/default-sidebar.js +9 -1
  119. package/build-module/components/sidebar/default-sidebar.js.map +1 -1
  120. package/build-module/components/sidebar/navigation-menu-sidebar/navigation-menu.js +3 -3
  121. package/build-module/components/sidebar/navigation-menu-sidebar/navigation-menu.js.map +1 -1
  122. package/build-module/components/sidebar/plugin-sidebar/index.js +9 -1
  123. package/build-module/components/sidebar/plugin-sidebar/index.js.map +1 -1
  124. package/build-module/components/sidebar/template-card/template-actions.js +1 -1
  125. package/build-module/components/sidebar/template-card/template-actions.js.map +1 -1
  126. package/build-module/components/template-details/edit-template-title.js +1 -1
  127. package/build-module/components/template-details/edit-template-title.js.map +1 -1
  128. package/build-module/components/template-details/index.js +19 -10
  129. package/build-module/components/template-details/index.js.map +1 -1
  130. package/build-module/components/template-details/template-areas.js +1 -1
  131. package/build-module/components/template-details/template-areas.js.map +1 -1
  132. package/build-module/components/template-details/template-part-area-selector.js +35 -0
  133. package/build-module/components/template-details/template-part-area-selector.js.map +1 -0
  134. package/build-module/components/template-part-converter/convert-to-template-part.js +3 -1
  135. package/build-module/components/template-part-converter/convert-to-template-part.js.map +1 -1
  136. package/build-module/index.js +1 -1
  137. package/build-module/index.js.map +1 -1
  138. package/build-style/style-rtl.css +15 -8
  139. package/build-style/style.css +15 -8
  140. package/package.json +29 -29
  141. package/src/components/add-new-template/add-custom-template-modal.js +12 -3
  142. package/src/components/add-new-template/new-template.js +53 -24
  143. package/src/components/add-new-template/utils.js +145 -16
  144. package/src/components/block-editor/resizable-editor.js +28 -18
  145. package/src/components/editor/style.scss +1 -0
  146. package/src/components/global-styles/border-panel.js +3 -3
  147. package/src/components/global-styles/dimensions-panel.js +139 -33
  148. package/src/components/global-styles/hooks.js +4 -3
  149. package/src/components/global-styles/screen-color-palette.js +1 -1
  150. package/src/components/global-styles/screen-colors.js +46 -4
  151. package/src/components/global-styles/screen-heading-color.js +201 -0
  152. package/src/components/global-styles/screen-typography-element.js +4 -0
  153. package/src/components/global-styles/screen-typography.js +6 -0
  154. package/src/components/global-styles/style.scss +14 -6
  155. package/src/components/global-styles/test/typography-utils.js +130 -0
  156. package/src/components/global-styles/test/use-global-styles-output.js +79 -3
  157. package/src/components/global-styles/test/utils.js +68 -8
  158. package/src/components/global-styles/typography-panel.js +119 -48
  159. package/src/components/global-styles/typography-utils.js +228 -0
  160. package/src/components/global-styles/ui.js +13 -0
  161. package/src/components/global-styles/use-global-styles-output.js +119 -61
  162. package/src/components/global-styles/utils.js +39 -4
  163. package/src/components/header/document-actions/index.js +3 -0
  164. package/src/components/header/index.js +32 -4
  165. package/src/components/header/mode-switcher/index.js +0 -3
  166. package/src/components/header/more-menu/index.js +7 -2
  167. package/src/components/header/undo-redo/redo.js +6 -2
  168. package/src/components/list/actions/index.js +3 -1
  169. package/src/components/save-button/index.js +10 -13
  170. package/src/components/sidebar/default-sidebar.js +12 -0
  171. package/src/components/sidebar/navigation-menu-sidebar/navigation-menu.js +1 -5
  172. package/src/components/sidebar/plugin-sidebar/index.js +12 -0
  173. package/src/components/sidebar/template-card/template-actions.js +3 -1
  174. package/src/components/template-details/edit-template-title.js +7 -3
  175. package/src/components/template-details/index.js +23 -8
  176. package/src/components/template-details/style.scss +0 -5
  177. package/src/components/template-details/template-areas.js +3 -1
  178. package/src/components/template-details/template-part-area-selector.js +38 -0
  179. package/src/components/template-part-converter/convert-to-template-part.js +3 -1
  180. package/src/index.js +1 -1
@@ -0,0 +1,228 @@
1
+ /**
2
+ * The fluid utilities must match the backend equivalent.
3
+ * See: gutenberg_get_typography_font_size_value() in lib/block-supports/typography.php
4
+ * ---------------------------------------------------------------
5
+ */
6
+
7
+ /**
8
+ * Returns a font-size value based on a given font-size preset.
9
+ * Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values.
10
+ *
11
+ * @param {Object} preset
12
+ * @param {string} preset.size A default font size.
13
+ * @param {string} preset.name A font size name, displayed in the UI.
14
+ * @param {string} preset.slug A font size slug.
15
+ * @param {Object} preset.fluid
16
+ * @param {string|undefined} preset.fluid.max A maximum font size value.
17
+ * @param {string|undefined} preset.fluid.min A minimum font size value.
18
+ * @param {Object} typographySettings
19
+ * @param {boolean} typographySettings.fluid Whether fluid typography is enabled.
20
+ *
21
+ * @return {string} An font-size value
22
+ */
23
+ export function getTypographyFontSizeValue( preset, typographySettings ) {
24
+ const { size: defaultSize } = preset;
25
+
26
+ if ( true !== typographySettings?.fluid ) {
27
+ return defaultSize;
28
+ }
29
+
30
+ // Defaults.
31
+ const DEFAULT_MAXIMUM_VIEWPORT_WIDTH = '1600px';
32
+ const DEFAULT_MINIMUM_VIEWPORT_WIDTH = '768px';
33
+ const DEFAULT_MINIMUM_FONT_SIZE_FACTOR = 0.75;
34
+ const DEFAULT_MAXIMUM_FONT_SIZE_FACTOR = 1.5;
35
+ const DEFAULT_SCALE_FACTOR = 1;
36
+
37
+ // Font sizes.
38
+ // A font size has explicitly bypassed fluid calculations.
39
+ if ( false === preset?.fluid ) {
40
+ return defaultSize;
41
+ }
42
+
43
+ const fluidFontSizeSettings = preset?.fluid || {};
44
+
45
+ // Try to grab explicit min and max fluid font sizes.
46
+ let minimumFontSizeRaw = fluidFontSizeSettings?.min;
47
+ let maximumFontSizeRaw = fluidFontSizeSettings?.max;
48
+ const preferredSize = getTypographyValueAndUnit( defaultSize );
49
+
50
+ // Protect against unsupported units.
51
+ if ( ! preferredSize?.unit ) {
52
+ return defaultSize;
53
+ }
54
+
55
+ // If no fluid min or max font sizes are available, create some using min/max font size factors.
56
+ if ( ! minimumFontSizeRaw ) {
57
+ minimumFontSizeRaw =
58
+ preferredSize.value * DEFAULT_MINIMUM_FONT_SIZE_FACTOR +
59
+ preferredSize.unit;
60
+ }
61
+
62
+ if ( ! maximumFontSizeRaw ) {
63
+ maximumFontSizeRaw =
64
+ preferredSize.value * DEFAULT_MAXIMUM_FONT_SIZE_FACTOR +
65
+ preferredSize.unit;
66
+ }
67
+
68
+ const fluidFontSizeValue = getComputedFluidTypographyValue( {
69
+ maximumViewPortWidth: DEFAULT_MAXIMUM_VIEWPORT_WIDTH,
70
+ minimumViewPortWidth: DEFAULT_MINIMUM_VIEWPORT_WIDTH,
71
+ maximumFontSize: maximumFontSizeRaw,
72
+ minimumFontSize: minimumFontSizeRaw,
73
+ scaleFactor: DEFAULT_SCALE_FACTOR,
74
+ } );
75
+
76
+ if ( !! fluidFontSizeValue ) {
77
+ return fluidFontSizeValue;
78
+ }
79
+
80
+ return defaultSize;
81
+ }
82
+
83
+ /**
84
+ * Internal implementation of clamp() based on available min/max viewport width, and min/max font sizes.
85
+ *
86
+ * @param {Object} args
87
+ * @param {string} args.maximumViewPortWidth Maximum size up to which type will have fluidity.
88
+ * @param {string} args.minimumViewPortWidth Minimum viewport size from which type will have fluidity.
89
+ * @param {string} args.maximumFontSize Maximum font size for any clamp() calculation.
90
+ * @param {string} args.minimumFontSize Minimum font size for any clamp() calculation.
91
+ * @param {number} args.scaleFactor A scale factor to determine how fast a font scales within boundaries.
92
+ *
93
+ * @return {string|null} A font-size value using clamp().
94
+ */
95
+ export function getComputedFluidTypographyValue( {
96
+ maximumViewPortWidth,
97
+ minimumViewPortWidth,
98
+ maximumFontSize,
99
+ minimumFontSize,
100
+ scaleFactor,
101
+ } ) {
102
+ // Grab the minimum font size and normalize it in order to use the value for calculations.
103
+ const minimumFontSizeParsed = getTypographyValueAndUnit( minimumFontSize );
104
+
105
+ // We get a 'preferred' unit to keep units consistent when calculating,
106
+ // otherwise the result will not be accurate.
107
+ const fontSizeUnit = minimumFontSizeParsed?.unit || 'rem';
108
+
109
+ // Grab the maximum font size and normalize it in order to use the value for calculations.
110
+ const maximumFontSizeParsed = getTypographyValueAndUnit( maximumFontSize, {
111
+ coerceTo: fontSizeUnit,
112
+ } );
113
+
114
+ // Protect against unsupported units.
115
+ if ( ! minimumFontSizeParsed || ! maximumFontSizeParsed ) {
116
+ return null;
117
+ }
118
+
119
+ // Use rem for accessible fluid target font scaling.
120
+ const minimumFontSizeRem = getTypographyValueAndUnit( minimumFontSize, {
121
+ coerceTo: 'rem',
122
+ } );
123
+
124
+ // Viewport widths defined for fluid typography. Normalize units
125
+ const maximumViewPortWidthParsed = getTypographyValueAndUnit(
126
+ maximumViewPortWidth,
127
+ { coerceTo: fontSizeUnit }
128
+ );
129
+ const minumumViewPortWidthParsed = getTypographyValueAndUnit(
130
+ minimumViewPortWidth,
131
+ { coerceTo: fontSizeUnit }
132
+ );
133
+
134
+ // Protect against unsupported units.
135
+ if (
136
+ ! maximumViewPortWidthParsed ||
137
+ ! minumumViewPortWidthParsed ||
138
+ ! minimumFontSizeRem
139
+ ) {
140
+ return null;
141
+ }
142
+
143
+ // Build CSS rule.
144
+ // Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
145
+ const minViewPortWidthOffsetValue = roundToPrecision(
146
+ minumumViewPortWidthParsed.value / 100,
147
+ 3
148
+ );
149
+
150
+ const viewPortWidthOffset = minViewPortWidthOffsetValue + fontSizeUnit;
151
+ let linearFactor =
152
+ 100 *
153
+ ( ( maximumFontSizeParsed.value - minimumFontSizeParsed.value ) /
154
+ ( maximumViewPortWidthParsed.value -
155
+ minumumViewPortWidthParsed.value ) );
156
+ linearFactor = roundToPrecision( linearFactor, 3 ) || 1;
157
+ const linearFactorScaled = linearFactor * scaleFactor;
158
+ const fluidTargetFontSize = `${ minimumFontSizeRem.value }${ minimumFontSizeRem.unit } + ((1vw - ${ viewPortWidthOffset }) * ${ linearFactorScaled })`;
159
+
160
+ return `clamp(${ minimumFontSize }, ${ fluidTargetFontSize }, ${ maximumFontSize })`;
161
+ }
162
+
163
+ /**
164
+ *
165
+ * @param {string} rawValue Raw size value from theme.json.
166
+ * @param {Object|undefined} options Calculation options.
167
+ *
168
+ * @return {{ unit: string, value: number }|null} An object consisting of `'value'` and `'unit'` properties.
169
+ */
170
+ export function getTypographyValueAndUnit( rawValue, options = {} ) {
171
+ if ( ! rawValue ) {
172
+ return null;
173
+ }
174
+
175
+ const { coerceTo, rootSizeValue, acceptableUnits } = {
176
+ coerceTo: '',
177
+ // Default browser font size. Later we could inject some JS to compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
178
+ rootSizeValue: 16,
179
+ acceptableUnits: [ 'rem', 'px', 'em' ],
180
+ ...options,
181
+ };
182
+
183
+ const acceptableUnitsGroup = acceptableUnits?.join( '|' );
184
+ const regexUnits = new RegExp(
185
+ `^(\\d*\\.?\\d+)(${ acceptableUnitsGroup }){1,1}$`
186
+ );
187
+
188
+ const matches = rawValue.match( regexUnits );
189
+
190
+ // We need a number value and a unit.
191
+ if ( ! matches || matches.length < 3 ) {
192
+ return null;
193
+ }
194
+
195
+ let [ , value, unit ] = matches;
196
+
197
+ let returnValue = parseFloat( value );
198
+
199
+ if ( 'px' === coerceTo && ( 'em' === unit || 'rem' === unit ) ) {
200
+ returnValue = returnValue * rootSizeValue;
201
+ unit = coerceTo;
202
+ }
203
+
204
+ if ( 'px' === unit && ( 'em' === coerceTo || 'rem' === coerceTo ) ) {
205
+ returnValue = returnValue / rootSizeValue;
206
+ unit = coerceTo;
207
+ }
208
+
209
+ return {
210
+ value: returnValue,
211
+ unit,
212
+ };
213
+ }
214
+
215
+ /**
216
+ * Returns a value rounded to defined precision.
217
+ * Returns `undefined` if the value is not a valid finite number.
218
+ *
219
+ * @param {number} value Raw value.
220
+ * @param {number} digits The number of digits to appear after the decimal point
221
+ *
222
+ * @return {number|undefined} Value rounded to standard precision.
223
+ */
224
+ export function roundToPrecision( value, digits = 3 ) {
225
+ return Number.isFinite( value )
226
+ ? parseFloat( value.toFixed( digits ) )
227
+ : undefined;
228
+ }
@@ -20,6 +20,7 @@ import ScreenColorPalette from './screen-color-palette';
20
20
  import ScreenBackgroundColor from './screen-background-color';
21
21
  import ScreenTextColor from './screen-text-color';
22
22
  import ScreenLinkColor from './screen-link-color';
23
+ import ScreenHeadingColor from './screen-heading-color';
23
24
  import ScreenButtonColor from './screen-button-color';
24
25
  import ScreenLayout from './screen-layout';
25
26
  import ScreenStyleVariations from './screen-style-variations';
@@ -59,6 +60,12 @@ function ContextScreens( { name } ) {
59
60
  <ScreenTypographyElement name={ name } element="link" />
60
61
  </GlobalStylesNavigationScreen>
61
62
 
63
+ <GlobalStylesNavigationScreen
64
+ path={ parentMenu + '/typography/heading' }
65
+ >
66
+ <ScreenTypographyElement name={ name } element="heading" />
67
+ </GlobalStylesNavigationScreen>
68
+
62
69
  <GlobalStylesNavigationScreen
63
70
  path={ parentMenu + '/typography/button' }
64
71
  >
@@ -89,6 +96,12 @@ function ContextScreens( { name } ) {
89
96
  <ScreenLinkColor name={ name } />
90
97
  </GlobalStylesNavigationScreen>
91
98
 
99
+ <GlobalStylesNavigationScreen
100
+ path={ parentMenu + '/colors/heading' }
101
+ >
102
+ <ScreenHeadingColor name={ name } />
103
+ </GlobalStylesNavigationScreen>
104
+
92
105
  <GlobalStylesNavigationScreen
93
106
  path={ parentMenu + '/colors/button' }
94
107
  >
@@ -10,7 +10,6 @@ import {
10
10
  pickBy,
11
11
  reduce,
12
12
  set,
13
- startsWith,
14
13
  } from 'lodash';
15
14
 
16
15
  /**
@@ -21,11 +20,13 @@ import {
21
20
  __EXPERIMENTAL_ELEMENTS as ELEMENTS,
22
21
  getBlockTypes,
23
22
  } from '@wordpress/blocks';
24
- import { useEffect, useState, useContext } from '@wordpress/element';
23
+ import { useSelect } from '@wordpress/data';
24
+ import { useContext, useMemo } from '@wordpress/element';
25
25
  import { getCSSRules } from '@wordpress/style-engine';
26
26
  import {
27
27
  __unstablePresetDuotoneFilter as PresetDuotoneFilter,
28
28
  __experimentalGetGapCSSValue as getGapCSSValue,
29
+ store as blockEditorStore,
29
30
  } from '@wordpress/block-editor';
30
31
 
31
32
  /**
@@ -48,7 +49,8 @@ function compileStyleValue( uncompiledValue ) {
48
49
  const VARIABLE_REFERENCE_PREFIX = 'var:';
49
50
  const VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE = '|';
50
51
  const VARIABLE_PATH_SEPARATOR_TOKEN_STYLE = '--';
51
- if ( startsWith( uncompiledValue, VARIABLE_REFERENCE_PREFIX ) ) {
52
+
53
+ if ( uncompiledValue?.startsWith?.( VARIABLE_REFERENCE_PREFIX ) ) {
52
54
  const variable = uncompiledValue
53
55
  .slice( VARIABLE_REFERENCE_PREFIX.length )
54
56
  .split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )
@@ -62,10 +64,11 @@ function compileStyleValue( uncompiledValue ) {
62
64
  * Transform given preset tree into a set of style declarations.
63
65
  *
64
66
  * @param {Object} blockPresets
67
+ * @param {Object} mergedSettings Merged theme.json settings.
65
68
  *
66
- * @return {Array} An array of style declarations.
69
+ * @return {Array<Object>} An array of style declarations.
67
70
  */
68
- function getPresetsDeclarations( blockPresets = {} ) {
71
+ function getPresetsDeclarations( blockPresets = {}, mergedSettings ) {
69
72
  return reduce(
70
73
  PRESET_METADATA,
71
74
  ( declarations, { path, valueKey, valueFunc, cssVarInfix } ) => {
@@ -73,7 +76,7 @@ function getPresetsDeclarations( blockPresets = {} ) {
73
76
  [ 'default', 'theme', 'custom' ].forEach( ( origin ) => {
74
77
  if ( presetByOrigin[ origin ] ) {
75
78
  presetByOrigin[ origin ].forEach( ( value ) => {
76
- if ( valueKey ) {
79
+ if ( valueKey && ! valueFunc ) {
77
80
  declarations.push(
78
81
  `--wp--preset--${ cssVarInfix }--${ kebabCase(
79
82
  value.slug
@@ -86,7 +89,7 @@ function getPresetsDeclarations( blockPresets = {} ) {
86
89
  declarations.push(
87
90
  `--wp--preset--${ cssVarInfix }--${ kebabCase(
88
91
  value.slug
89
- ) }: ${ valueFunc( value ) }`
92
+ ) }: ${ valueFunc( value, mergedSettings ) }`
90
93
  );
91
94
  }
92
95
  } );
@@ -187,12 +190,15 @@ function flattenTree( input = {}, prefix, token ) {
187
190
  *
188
191
  * @param {boolean} useRootPaddingAlign Whether to use CSS custom properties in root selector.
189
192
  *
193
+ * @param {Object} tree A theme.json tree containing layout definitions.
194
+ *
190
195
  * @return {Array} An array of style declarations.
191
196
  */
192
- function getStylesDeclarations(
197
+ export function getStylesDeclarations(
193
198
  blockStyles = {},
194
199
  selector = '',
195
- useRootPaddingAlign
200
+ useRootPaddingAlign,
201
+ tree = {}
196
202
  ) {
197
203
  const isRoot = ROOT_BLOCK_SELECTOR === selector;
198
204
  const output = reduce(
@@ -208,6 +214,15 @@ function getStylesDeclarations(
208
214
 
209
215
  const styleValue = get( blockStyles, pathToValue );
210
216
 
217
+ // Root-level padding styles don't currently support strings with CSS shorthand values.
218
+ // This may change: https://github.com/WordPress/gutenberg/issues/40132.
219
+ if (
220
+ key === '--wp--style--root--padding' &&
221
+ ( typeof styleValue === 'string' || ! useRootPaddingAlign )
222
+ ) {
223
+ return declarations;
224
+ }
225
+
211
226
  if ( !! properties && typeof styleValue !== 'string' ) {
212
227
  Object.entries( properties ).forEach( ( entry ) => {
213
228
  const [ name, prop ] = entry;
@@ -227,13 +242,6 @@ function getStylesDeclarations(
227
242
  ) }`
228
243
  );
229
244
  } );
230
- } else if (
231
- key === '--wp--style--root--padding' &&
232
- typeof styleValue === 'string'
233
- ) {
234
- // Root-level padding styles don't currently support strings with CSS shorthand values.
235
- // This may change: https://github.com/WordPress/gutenberg/issues/40132.
236
- return declarations;
237
245
  } else if ( get( blockStyles, pathToValue, false ) ) {
238
246
  const cssProperty = key.startsWith( '--' )
239
247
  ? key
@@ -250,18 +258,34 @@ function getStylesDeclarations(
250
258
  []
251
259
  );
252
260
 
253
- if ( isRoot && useRootPaddingAlign ) {
254
- return output;
255
- }
256
-
257
261
  // The goal is to move everything to server side generated engine styles
258
262
  // This is temporary as we absorb more and more styles into the engine.
259
263
  const extraRules = getCSSRules( blockStyles );
260
264
  extraRules.forEach( ( rule ) => {
265
+ // Don't output padding properties if padding variables are set.
266
+ if (
267
+ isRoot &&
268
+ useRootPaddingAlign &&
269
+ rule.key.startsWith( 'padding' )
270
+ ) {
271
+ return;
272
+ }
261
273
  const cssProperty = rule.key.startsWith( '--' )
262
274
  ? rule.key
263
275
  : kebabCase( rule.key );
264
- output.push( `${ cssProperty }: ${ compileStyleValue( rule.value ) }` );
276
+
277
+ let ruleValue = rule.value;
278
+ if ( typeof ruleValue !== 'string' && ruleValue?.ref ) {
279
+ const refPath = ruleValue.ref.split( '.' );
280
+ ruleValue = get( tree, refPath );
281
+ // Presence of another ref indicates a reference to another dynamic value.
282
+ // Pointing to another dynamic value is not supported.
283
+ if ( ! ruleValue || !! ruleValue?.ref ) {
284
+ return;
285
+ }
286
+ }
287
+
288
+ output.push( `${ cssProperty }: ${ ruleValue }` );
265
289
  } );
266
290
 
267
291
  return output;
@@ -328,14 +352,28 @@ export function getLayoutStyles( {
328
352
  }
329
353
 
330
354
  if ( declarations.length ) {
331
- const combinedSelector =
332
- selector === ROOT_BLOCK_SELECTOR
333
- ? `${ selector } .${ className }${
334
- spacingStyle?.selector || ''
335
- }`
336
- : `${ selector }.${ className }${
337
- spacingStyle?.selector || ''
338
- }`;
355
+ let combinedSelector = '';
356
+
357
+ if ( ! hasBlockGapSupport ) {
358
+ // For fallback gap styles, use lower specificity, to ensure styles do not unintentionally override theme styles.
359
+ combinedSelector =
360
+ selector === ROOT_BLOCK_SELECTOR
361
+ ? `:where(.${ className }${
362
+ spacingStyle?.selector || ''
363
+ })`
364
+ : `:where(${ selector }.${ className }${
365
+ spacingStyle?.selector || ''
366
+ })`;
367
+ } else {
368
+ combinedSelector =
369
+ selector === ROOT_BLOCK_SELECTOR
370
+ ? `${ selector } .${ className }${
371
+ spacingStyle?.selector || ''
372
+ }`
373
+ : `${ selector }.${ className }${
374
+ spacingStyle?.selector || ''
375
+ }`;
376
+ }
339
377
  ruleset += `${ combinedSelector } { ${ declarations.join(
340
378
  '; '
341
379
  ) }; }`;
@@ -418,11 +456,12 @@ export const getNodesWithStyles = ( tree, blockSelectors ) => {
418
456
  selector: ROOT_BLOCK_SELECTOR,
419
457
  } );
420
458
  }
421
- forEach( tree.styles?.elements, ( value, key ) => {
422
- if ( !! value && !! ELEMENTS[ key ] ) {
459
+
460
+ forEach( ELEMENTS, ( selector, name ) => {
461
+ if ( !! tree.styles?.elements[ name ] ) {
423
462
  nodes.push( {
424
- styles: value,
425
- selector: ELEMENTS[ key ],
463
+ styles: tree.styles?.elements[ name ],
464
+ selector,
426
465
  } );
427
466
  }
428
467
  } );
@@ -515,10 +554,9 @@ export const getNodesWithSettings = ( tree, blockSelectors ) => {
515
554
 
516
555
  export const toCustomProperties = ( tree, blockSelectors ) => {
517
556
  const settings = getNodesWithSettings( tree, blockSelectors );
518
-
519
557
  let ruleset = '';
520
558
  settings.forEach( ( { presets, custom, selector } ) => {
521
- const declarations = getPresetsDeclarations( presets );
559
+ const declarations = getPresetsDeclarations( presets, tree?.settings );
522
560
  const customProps = flattenTree( custom, '--wp--custom--', '--' );
523
561
  if ( customProps.length > 0 ) {
524
562
  declarations.push( ...customProps );
@@ -536,11 +574,13 @@ export const toStyles = (
536
574
  tree,
537
575
  blockSelectors,
538
576
  hasBlockGapSupport,
539
- hasFallbackGapSupport
577
+ hasFallbackGapSupport,
578
+ disableLayoutStyles = false
540
579
  ) => {
541
580
  const nodesWithStyles = getNodesWithStyles( tree, blockSelectors );
542
581
  const nodesWithSettings = getNodesWithSettings( tree, blockSelectors );
543
582
  const useRootPaddingAlign = tree?.settings?.useRootPaddingAwareAlignments;
583
+ const { contentSize, wideSize } = tree?.settings?.layout || {};
544
584
 
545
585
  /*
546
586
  * Reset default browser margin on the root body element.
@@ -550,13 +590,23 @@ export const toStyles = (
550
590
  * user-generated values take precedence in the CSS cascade.
551
591
  * @link https://github.com/WordPress/gutenberg/issues/36147.
552
592
  */
553
- let ruleset = 'body {margin: 0;}';
593
+ let ruleset = 'body {margin: 0;';
594
+
595
+ if ( contentSize ) {
596
+ ruleset += ` --wp--style--global--content-size: ${ contentSize };`;
597
+ }
598
+
599
+ if ( wideSize ) {
600
+ ruleset += ` --wp--style--global--wide-size: ${ wideSize };`;
601
+ }
554
602
 
555
603
  if ( useRootPaddingAlign ) {
556
- ruleset =
557
- 'body { margin: 0; padding-right: 0; padding-left: 0; padding-top: var(--wp--style--root--padding-top); padding-bottom: var(--wp--style--root--padding-bottom) } .has-global-padding { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); } .has-global-padding > .alignfull { margin-right: calc(var(--wp--style--root--padding-right) * -1); margin-left: calc(var(--wp--style--root--padding-left) * -1); } .has-global-padding > .alignfull > :where([class*="wp-block-"]:not(.alignfull):not([class*="__"]),p,h1,h2,h3,h4,h5,h6,ul,ol) { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); }';
604
+ ruleset +=
605
+ 'padding-right: 0; padding-left: 0; padding-top: var(--wp--style--root--padding-top); padding-bottom: var(--wp--style--root--padding-bottom) } .has-global-padding { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); } .has-global-padding > .alignfull { margin-right: calc(var(--wp--style--root--padding-right) * -1); margin-left: calc(var(--wp--style--root--padding-left) * -1); } .has-global-padding > .alignfull > :where([class*="wp-block-"]:not(.alignfull):not([class*="__"]),p,h1,h2,h3,h4,h5,h6,ul,ol) { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left);';
558
606
  }
559
607
 
608
+ ruleset += '}';
609
+
560
610
  nodesWithStyles.forEach(
561
611
  ( {
562
612
  selector,
@@ -601,18 +651,20 @@ export const toStyles = (
601
651
  if ( duotoneSelector ) {
602
652
  const duotoneDeclarations =
603
653
  getStylesDeclarations( duotoneStyles );
604
- if ( duotoneDeclarations.length === 0 ) {
605
- return;
654
+ if ( duotoneDeclarations.length > 0 ) {
655
+ ruleset =
656
+ ruleset +
657
+ `${ duotoneSelector }{${ duotoneDeclarations.join(
658
+ ';'
659
+ ) };}`;
606
660
  }
607
- ruleset =
608
- ruleset +
609
- `${ duotoneSelector }{${ duotoneDeclarations.join(
610
- ';'
611
- ) };}`;
612
661
  }
613
662
 
614
663
  // Process blockGap and layout styles.
615
- if ( ROOT_BLOCK_SELECTOR === selector || hasLayoutSupport ) {
664
+ if (
665
+ ! disableLayoutStyles &&
666
+ ( ROOT_BLOCK_SELECTOR === selector || hasLayoutSupport )
667
+ ) {
616
668
  ruleset += getLayoutStyles( {
617
669
  tree,
618
670
  style: styles,
@@ -627,7 +679,8 @@ export const toStyles = (
627
679
  const declarations = getStylesDeclarations(
628
680
  styles,
629
681
  selector,
630
- useRootPaddingAlign
682
+ useRootPaddingAlign,
683
+ tree
631
684
  );
632
685
  if ( declarations?.length ) {
633
686
  ruleset =
@@ -762,17 +815,18 @@ export const getBlockSelectors = ( blockTypes ) => {
762
815
  };
763
816
 
764
817
  export function useGlobalStylesOutput() {
765
- const [ stylesheets, setStylesheets ] = useState( [] );
766
- const [ settings, setSettings ] = useState( {} );
767
- const [ svgFilters, setSvgFilters ] = useState( {} );
768
818
  const { merged: mergedConfig } = useContext( GlobalStylesContext );
769
819
  const [ blockGap ] = useSetting( 'spacing.blockGap' );
770
820
  const hasBlockGapSupport = blockGap !== null;
771
821
  const hasFallbackGapSupport = ! hasBlockGapSupport; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback styles support.
822
+ const disableLayoutStyles = useSelect( ( select ) => {
823
+ const { getSettings } = select( blockEditorStore );
824
+ return !! getSettings().disableLayoutStyles;
825
+ } );
772
826
 
773
- useEffect( () => {
827
+ return useMemo( () => {
774
828
  if ( ! mergedConfig?.styles || ! mergedConfig?.settings ) {
775
- return;
829
+ return [];
776
830
  }
777
831
 
778
832
  const blockSelectors = getBlockSelectors( getBlockTypes() );
@@ -784,10 +838,11 @@ export function useGlobalStylesOutput() {
784
838
  mergedConfig,
785
839
  blockSelectors,
786
840
  hasBlockGapSupport,
787
- hasFallbackGapSupport
841
+ hasFallbackGapSupport,
842
+ disableLayoutStyles
788
843
  );
789
844
  const filters = toSvgFilters( mergedConfig, blockSelectors );
790
- setStylesheets( [
845
+ const stylesheets = [
791
846
  {
792
847
  css: customProperties,
793
848
  isGlobalStyles: true,
@@ -796,10 +851,13 @@ export function useGlobalStylesOutput() {
796
851
  css: globalStyles,
797
852
  isGlobalStyles: true,
798
853
  },
799
- ] );
800
- setSettings( mergedConfig.settings );
801
- setSvgFilters( filters );
802
- }, [ hasBlockGapSupport, hasFallbackGapSupport, mergedConfig ] );
803
-
804
- return [ stylesheets, settings, svgFilters, hasBlockGapSupport ];
854
+ ];
855
+
856
+ return [ stylesheets, mergedConfig.settings, filters ];
857
+ }, [
858
+ hasBlockGapSupport,
859
+ hasFallbackGapSupport,
860
+ mergedConfig,
861
+ disableLayoutStyles,
862
+ ] );
805
863
  }