@@ -33,7 +33,6 @@ import { getGapCSSValue } from '../../hooks/gap';
3333import { store as blockEditorStore } from '../../store' ;
3434import { LAYOUT_DEFINITIONS } from '../../layouts/definitions' ;
3535import { getValueFromObjectPath , setImmutably } from '../../utils/object' ;
36- import BlockContext from '../block-context' ;
3736import { unlock } from '../../lock-unlock' ;
3837import { setThemeFileUris } from './theme-file-uri-utils' ;
3938
@@ -310,15 +309,15 @@ const getFeatureDeclarations = ( selectors, styles ) => {
310309 *
311310 * @param {Object } tree A theme.json tree containing layout definitions.
312311 *
313- * @param {boolean } isTemplate Whether the entity being edited is a full template or a pattern .
312+ * @param {boolean } disableRootPadding Whether to force disable the root padding styles .
314313 * @return {Array } An array of style declarations.
315314 */
316315export function getStylesDeclarations (
317316 blockStyles = { } ,
318317 selector = '' ,
319318 useRootPaddingAlign ,
320319 tree = { } ,
321- isTemplate = true
320+ disableRootPadding = false
322321) {
323322 const isRoot = ROOT_BLOCK_SELECTOR === selector ;
324323 const output = Object . entries ( STYLE_PROPERTY ) . reduce (
@@ -394,7 +393,7 @@ export function getStylesDeclarations(
394393 // Don't output padding properties if padding variables are set or if we're not editing a full template.
395394 if (
396395 isRoot &&
397- ( useRootPaddingAlign || ! isTemplate ) &&
396+ ( useRootPaddingAlign || disableRootPadding ) &&
398397 rule . key . startsWith ( 'padding' )
399398 ) {
400399 return ;
@@ -772,7 +771,7 @@ export const toStyles = (
772771 hasBlockGapSupport ,
773772 hasFallbackGapSupport ,
774773 disableLayoutStyles = false ,
775- isTemplate = true ,
774+ disableRootPadding = false ,
776775 styleOptions = undefined
777776) => {
778777 // These allow opting out of certain sets of styles.
@@ -817,7 +816,11 @@ export const toStyles = (
817816 ruleset += ':where(body) {margin: 0;' ;
818817
819818 // Root padding styles should only be output for full templates, not patterns or template parts.
820- if ( options . rootPadding && useRootPaddingAlign && isTemplate ) {
819+ if (
820+ options . rootPadding &&
821+ useRootPaddingAlign &&
822+ ! disableRootPadding
823+ ) {
821824 /*
822825 * These rules reproduce the ones from https://github.com/WordPress/gutenberg/blob/79103f124925d1f457f627e154f52a56228ed5ad/lib/class-wp-theme-json-gutenberg.php#L2508
823826 * almost exactly, but for the selectors that target block wrappers in the front end. This code only runs in the editor, so it doesn't need those selectors.
@@ -949,7 +952,7 @@ export const toStyles = (
949952 selector ,
950953 useRootPaddingAlign ,
951954 tree ,
952- isTemplate
955+ disableRootPadding
953956 ) ;
954957 if ( declarations ?. length ) {
955958 ruleset += `:where(${ selector } ){${ declarations . join (
@@ -1208,10 +1211,15 @@ export function processCSSNesting( css, blockSelector ) {
12081211 * The use case for a custom config is to generate bespoke styles
12091212 * and settings for previews, or other out-of-editor experiences.
12101213 *
1211- * @param {Object } mergedConfig Global styles configuration.
1214+ * @param {Object } mergedConfig Global styles configuration.
1215+ * @param {boolean } disableRootPadding Disable root padding styles.
1216+ *
12121217 * @return {Array } Array of stylesheets and settings.
12131218 */
1214- export function useGlobalStylesOutputWithConfig ( mergedConfig = { } ) {
1219+ export function useGlobalStylesOutputWithConfig (
1220+ mergedConfig = { } ,
1221+ disableRootPadding
1222+ ) {
12151223 const [ blockGap ] = useGlobalSetting ( 'spacing.blockGap' ) ;
12161224 mergedConfig = setThemeFileUris (
12171225 mergedConfig ,
@@ -1224,10 +1232,6 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
12241232 return ! ! getSettings ( ) . disableLayoutStyles ;
12251233 } ) ;
12261234
1227- const blockContext = useContext ( BlockContext ) ;
1228-
1229- const isTemplate = blockContext ?. templateSlug !== undefined ;
1230-
12311235 const { getBlockStyles } = useSelect ( blocksStore ) ;
12321236
12331237 return useMemo ( ( ) => {
@@ -1252,7 +1256,7 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
12521256 hasBlockGapSupport ,
12531257 hasFallbackGapSupport ,
12541258 disableLayoutStyles ,
1255- isTemplate
1259+ disableRootPadding
12561260 ) ;
12571261 const svgs = toSvgFilters ( updatedConfig , blockSelectors ) ;
12581262
@@ -1299,17 +1303,19 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) {
12991303 hasFallbackGapSupport ,
13001304 mergedConfig ,
13011305 disableLayoutStyles ,
1302- isTemplate ,
1306+ disableRootPadding ,
13031307 getBlockStyles ,
13041308 ] ) ;
13051309}
13061310
13071311/**
13081312 * Returns the global styles output based on the current state of global styles config loaded in the editor context.
13091313 *
1314+ * @param {boolean } disableRootPadding Disable root padding styles.
1315+ *
13101316 * @return {Array } Array of stylesheets and settings.
13111317 */
1312- export function useGlobalStylesOutput ( ) {
1318+ export function useGlobalStylesOutput ( disableRootPadding = false ) {
13131319 const { merged : mergedConfig } = useContext ( GlobalStylesContext ) ;
1314- return useGlobalStylesOutputWithConfig ( mergedConfig ) ;
1320+ return useGlobalStylesOutputWithConfig ( mergedConfig , disableRootPadding ) ;
13151321}
0 commit comments