Skip to content

Commit 253e8c4

Browse files
committed
merge theme and wp default presets if defaultSizesEnabled is true
1 parent 9890d19 commit 253e8c4

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/hooks/use-preset-controls.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import { __ } from '@wordpress/i18n'
77
const PRESET_MAPPING = {
88
fontSizes: {
99
settings: [ 'typography', 'fontSizes' ],
10+
defaultEnabled: [ 'typography', 'defaultFontSizes' ],
1011
prefix: 'font-size',
1112
},
1213
spacingSizes: {
1314
settings: [ 'spacing', 'spacingSizes' ],
15+
defaultEnabled: [ 'typography', 'defaultSpacingSizes' ],
1416
prefix: 'spacing',
1517
},
1618
blockHeights: {
1719
settings: [ 'blockHeights' ],
20+
defaultEnabled: [],
1821
prefix: 'block-height',
1922
},
2023
borderRadius: {
2124
settings: [ 'borderRadius' ],
25+
defaultEnabled: [],
2226
prefix: 'border-radius',
2327
},
2428
}
@@ -31,7 +35,26 @@ const nonePreset = {
3135

3236
export const usePresetControls = property => {
3337
// Get the theme presets for the property
34-
const [ themePresets ] = useSettings( PRESET_MAPPING[ property ].settings.join( '.' ) )
38+
const [
39+
themePresets,
40+
41+
/**
42+
* Dev note:
43+
* Starting from theme.json version 3, settings such as `typography.defaultFontSizes` and `spacing.defaultSpacingSizes`
44+
* must be set to `false` to override the default presets. If these settings are not found in the theme.json, they default to `true`.
45+
* Otherwise, themes that use the same slugs as the defaults will continue to use the default presets.
46+
* Therefore, we also need to get the default presets if the `defaultSizesEnabled` is `true` and merge it with the theme presets.
47+
*
48+
* https://make.wordpress.org/core/2024/06/19/theme-json-version-3/#:~:text=Breaking%20changes%20in%20version%203
49+
* */
50+
wpDefaultPresets,
51+
defaultSizesEnabled,
52+
] = useSettings(
53+
PRESET_MAPPING[ property ].settings.join( '.' ),
54+
[ ...PRESET_MAPPING[ property ].settings, 'default' ].join( '.' ),
55+
PRESET_MAPPING[ property ].defaultEnabled.join( '.' )
56+
)
57+
3558
// Get all custom presets
3659
const { allCustomPresets } = useSelect( select => {
3760
const _customPresetControls = select( 'stackable/global-preset-controls.custom' )?.getCustomPresetControls()
@@ -42,7 +65,10 @@ export const usePresetControls = property => {
4265

4366
// Get the theme/default presets if the user have one, else return the stackable presets
4467
const basePresets = hasThemePresets
45-
? themePresets
68+
? ( wpDefaultPresets && defaultSizesEnabled !== false
69+
? [ ...themePresets, ...wpDefaultPresets ] // merge theme and default preset sizes
70+
: themePresets
71+
)
4672
: PRESET_MAPPING[ property ].settings.reduce( ( acc, key ) => acc?.[ key ], DEFAULT_PRESETS.settings )
4773

4874
// Returns the base presets overriden by the custom presets

0 commit comments

Comments
 (0)