diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js
index 2605fb75e14ef4..31e083f09ba2bb 100644
--- a/packages/block-library/src/navigation/edit/index.js
+++ b/packages/block-library/src/navigation/edit/index.js
@@ -75,6 +75,7 @@ import AccessibleMenuDescription from './accessible-menu-description';
import { unlock } from '../../lock-unlock';
import { useToolsPanelDropdownMenuProps } from '../../utils/hooks';
import { isWithinNavigationOverlay } from '../../utils/is-within-overlay';
+import useLayoutCustomProperties from './use-layout-custom-properties';
import {
DEFAULT_BLOCK,
NAVIGATION_OVERLAY_TEMPLATE_PART_AREA,
@@ -280,6 +281,11 @@ function Navigation( {
} = attributes;
const ref = attributes.ref;
+ useLayoutCustomProperties( {
+ clientId,
+ layout: attributes.layout,
+ style: attributes.style,
+ } );
const setRef = useCallback(
( postId ) => {
diff --git a/packages/block-library/src/navigation/edit/test/layout-custom-properties.js b/packages/block-library/src/navigation/edit/test/layout-custom-properties.js
new file mode 100644
index 00000000000000..59455da9757b6d
--- /dev/null
+++ b/packages/block-library/src/navigation/edit/test/layout-custom-properties.js
@@ -0,0 +1,138 @@
+/**
+ * External dependencies
+ */
+import { renderHook } from '@testing-library/react';
+
+/**
+ * WordPress dependencies
+ */
+import { useSettings, useStyleOverride } from '@wordpress/block-editor';
+
+/**
+ * Internal dependencies
+ */
+import useLayoutCustomProperties from '../use-layout-custom-properties';
+
+const mockGetResponsiveMediaQueries = jest.fn();
+
+jest.mock( '@wordpress/block-editor', () => ( {
+ useSettings: jest.fn(),
+ useStyleOverride: jest.fn(),
+} ) );
+
+jest.mock( '@wordpress/global-styles-engine', () => ( {
+ privateApis: {},
+} ) );
+
+jest.mock( '../../../lock-unlock', () => ( {
+ unlock: () => ( {
+ getResponsiveMediaQueries: ( ...args ) =>
+ mockGetResponsiveMediaQueries( ...args ),
+ } ),
+} ) );
+
+describe( 'Navigation layout custom properties', () => {
+ beforeEach( () => {
+ jest.clearAllMocks();
+ useSettings.mockReturnValue( [ { mobile: 480, tablet: 782 } ] );
+ mockGetResponsiveMediaQueries.mockReturnValue( {
+ '@tablet': '@media (480px < width <= 782px)',
+ '@mobile': '@media (width <= 480px)',
+ } );
+ } );
+
+ describe( 'vertical layouts', () => {
+ it.each( [
+ [ 'left', 'initial', 'flex-start' ],
+ [ 'center', 'center', 'center' ],
+ [ 'right', 'flex-end', 'flex-end' ],
+ [ 'space-between', 'space-between', 'flex-start' ],
+ ] )(
+ 'reproduces the vertical %s class cascade',
+ ( justifyContent, justify, align ) => {
+ renderHook( () =>
+ useLayoutCustomProperties( {
+ clientId: 'test',
+ style: {
+ '@mobile': {
+ layout: {
+ orientation: 'vertical',
+ justifyContent,
+ flexWrap: 'nowrap',
+ },
+ },
+ },
+ } )
+ );
+
+ const { css } = useStyleOverride.mock.calls.at( -1 )[ 0 ];
+ expect( css ).toContain(
+ '--navigation-layout-direction: column;'
+ );
+ expect( css ).toContain( '--navigation-layout-wrap: nowrap;' );
+ expect( css ).toContain(
+ `--navigation-layout-justify: ${ justify };`
+ );
+ expect( css ).toContain(
+ `--navigation-layout-align: ${ align };`
+ );
+ }
+ );
+ } );
+
+ describe( 'responsive styles', () => {
+ it( 'merges viewport overrides with the base layout', () => {
+ renderHook( () =>
+ useLayoutCustomProperties( {
+ clientId: 'test',
+ layout: {
+ justifyContent: 'right',
+ flexWrap: 'nowrap',
+ },
+ style: {
+ '@mobile': {
+ layout: { orientation: 'vertical' },
+ },
+ },
+ } )
+ );
+
+ expect( useStyleOverride ).toHaveBeenCalledWith( {
+ css: '@media (width <= 480px){#block-test {--navigation-layout-justification-setting: flex-end;--navigation-layout-direction: column;--navigation-layout-wrap: nowrap;--navigation-layout-justify: flex-end;--navigation-layout-align: flex-end;}}',
+ } );
+ } );
+
+ it( 'uses default values for explicit viewport resets', () => {
+ renderHook( () =>
+ useLayoutCustomProperties( {
+ clientId: 'test',
+ layout: { justifyContent: 'right' },
+ style: {
+ '@tablet': {
+ layout: { justifyContent: null },
+ },
+ },
+ } )
+ );
+
+ expect( useStyleOverride ).toHaveBeenCalledWith( {
+ css: expect.stringContaining(
+ '--navigation-layout-justify: flex-start;'
+ ),
+ } );
+ } );
+
+ it( 'does not emit styles without a viewport layout override', () => {
+ renderHook( () =>
+ useLayoutCustomProperties( {
+ clientId: 'test',
+ style: {
+ '@mobile': { spacing: { blockGap: '10px' } },
+ },
+ } )
+ );
+
+ expect( useStyleOverride ).toHaveBeenCalledWith( { css: '' } );
+ } );
+ } );
+} );
diff --git a/packages/block-library/src/navigation/edit/use-layout-custom-properties.js b/packages/block-library/src/navigation/edit/use-layout-custom-properties.js
new file mode 100644
index 00000000000000..cf718efe155bba
--- /dev/null
+++ b/packages/block-library/src/navigation/edit/use-layout-custom-properties.js
@@ -0,0 +1,84 @@
+/**
+ * WordPress dependencies
+ */
+import { useSettings, useStyleOverride } from '@wordpress/block-editor';
+import { privateApis as globalStylesEnginePrivateApis } from '@wordpress/global-styles-engine';
+
+/**
+ * Internal dependencies
+ */
+import { unlock } from '../../lock-unlock';
+
+const { getResponsiveMediaQueries } = unlock( globalStylesEnginePrivateApis );
+
+const JUSTIFICATION_VALUES = {
+ left: 'flex-start',
+ center: 'center',
+ right: 'flex-end',
+ 'space-between': 'space-between',
+};
+
+/**
+ * Adds responsive Navigation layout custom properties to the editor canvas.
+ *
+ * @param {Object} options Options.
+ * @param {string} options.clientId Block client ID.
+ * @param {Object} options.layout Base layout configuration.
+ * @param {Object} options.style Block style attribute.
+ */
+export default function useLayoutCustomProperties( {
+ clientId,
+ layout,
+ style,
+} ) {
+ const [ viewportSettings ] = useSettings( 'viewport' );
+ const selector = `#block-${ clientId }`;
+ const css = Object.entries( getResponsiveMediaQueries( viewportSettings ) )
+ .map( ( [ viewport, mediaQuery ] ) => {
+ const viewportLayout = style?.[ viewport ]?.layout;
+ if (
+ ! viewportLayout ||
+ typeof viewportLayout !== 'object' ||
+ Array.isArray( viewportLayout ) ||
+ ! Object.keys( viewportLayout ).length
+ ) {
+ return '';
+ }
+
+ const effectiveLayout = { ...layout, ...viewportLayout };
+ const justifyContent = Object.hasOwn(
+ JUSTIFICATION_VALUES,
+ effectiveLayout.justifyContent
+ )
+ ? effectiveLayout.justifyContent
+ : 'left';
+ const justification = JUSTIFICATION_VALUES[ justifyContent ];
+ const isVertical = effectiveLayout.orientation === 'vertical';
+ let align = 'center';
+ if ( isVertical ) {
+ align = [ 'center', 'right' ].includes( justifyContent )
+ ? justification
+ : 'flex-start';
+ }
+ const justify =
+ isVertical && justifyContent === 'left'
+ ? 'initial'
+ : justification;
+ const declarations = {
+ '--navigation-layout-justification-setting': justification,
+ '--navigation-layout-direction': isVertical ? 'column' : 'row',
+ '--navigation-layout-wrap':
+ effectiveLayout.flexWrap === 'nowrap' ? 'nowrap' : 'wrap',
+ '--navigation-layout-justify': justify,
+ '--navigation-layout-align': align,
+ };
+ const cssDeclarations = Object.entries( declarations )
+ .map( ( [ property, value ] ) => `${ property }: ${ value };` )
+ .join( '' );
+
+ return `${ mediaQuery }{${ selector } {${ cssDeclarations }}}`;
+ } )
+ .join( '' );
+
+ useStyleOverride( { css } );
+}
diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php
index eb960636b9f250..155184c2eaa4a0 100644
--- a/packages/block-library/src/navigation/index.php
+++ b/packages/block-library/src/navigation/index.php
@@ -40,6 +40,45 @@ function block_core_navigation_get_submenu_visibility( $attributes ) {
return $submenu_visibility ?? 'hover';
}
+/**
+ * Returns the custom properties used by the Navigation block for a layout.
+ *
+ * @since 7.1.0
+ *
+ * @param array $layout Layout configuration.
+ * @return array Navigation layout custom property declarations.
+ */
+function block_core_navigation_get_layout_custom_property_declarations( $layout ) {
+ $justification_values = array(
+ 'left' => 'flex-start',
+ 'center' => 'center',
+ 'right' => 'flex-end',
+ 'space-between' => 'space-between',
+ );
+ $justify_content = is_array( $layout ) ? ( $layout['justifyContent'] ?? 'left' ) : 'left';
+ if ( ! is_string( $justify_content ) || ! isset( $justification_values[ $justify_content ] ) ) {
+ $justify_content = 'left';
+ }
+
+ $justification = $justification_values[ $justify_content ];
+ $is_vertical = is_array( $layout ) && 'vertical' === ( $layout['orientation'] ?? null );
+ $align = 'center';
+ $justify = $justification;
+
+ if ( $is_vertical ) {
+ $align = in_array( $justify_content, array( 'center', 'right' ), true ) ? $justification : 'flex-start';
+ $justify = 'left' === $justify_content ? 'initial' : $justification;
+ }
+
+ return array(
+ '--navigation-layout-justification-setting' => $justification,
+ '--navigation-layout-direction' => $is_vertical ? 'column' : 'row',
+ '--navigation-layout-wrap' => is_array( $layout ) && 'nowrap' === ( $layout['flexWrap'] ?? null ) ? 'nowrap' : 'wrap',
+ '--navigation-layout-justify' => $justify,
+ '--navigation-layout-align' => $align,
+ );
+}
+
/**
* Helper functions used to render the navigation block.
*
@@ -1558,13 +1597,18 @@ function register_block_core_navigation() {
add_action( 'init', 'register_block_core_navigation' );
/**
- * Adds the Navigation block state class to inner list containers.
+ * Adds Navigation block support classes to inner list containers.
*
* State block support adds the generated `wp-states-*` class to the outer
* block wrapper. The Navigation block renders its menu items inside an inner
* `wp-block-navigation__container` list, so the same state class is also needed
* there for state styles to apply directly to the menu list.
*
+ * Navigation also uses layout classes on its outer wrapper to define custom
+ * properties consumed by its inner containers. Viewport layout styles cannot
+ * change those classes, so equivalent custom properties and a scoping class
+ * are generated for each configured viewport layout.
+ *
* Currently this is required as a workaround because of how difficult it is for nav
* child blocks to inherit styles through the complex responsive nav block html. The
* bug in https://github.com/WordPress/gutenberg/issues/62690 also prevents inheritance.
@@ -1575,25 +1619,83 @@ function register_block_core_navigation() {
* @param array $block The full block, including name and attributes.
* @return string The updated block content.
*/
-function block_core_navigation_add_state_class_to_container( $block_content, $block ) {
+function block_core_navigation_add_support_classes_to_container( $block_content, $block ) {
if ( 'core/navigation' !== ( $block['blockName'] ?? null ) || empty( $block_content ) ) {
return $block_content;
}
+ $attributes = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
+ $style = is_array( $attributes['style'] ?? null ) ? $attributes['style'] : array();
+ if (
+ defined( 'IS_GUTENBERG_PLUGIN' ) &&
+ IS_GUTENBERG_PLUGIN &&
+ function_exists( 'gutenberg_resolve_style_state_aliases' )
+ ) {
+ $style = gutenberg_resolve_style_state_aliases( $style, 'core/navigation' );
+ }
+
+ $global_settings = wp_get_global_settings();
+ $viewport_settings = $global_settings['viewport'] ?? null;
+ $responsive_media_queries = array();
+ if ( method_exists( 'WP_Theme_JSON_Gutenberg', 'get_viewport_media_queries' ) ) {
+ $responsive_media_queries = WP_Theme_JSON_Gutenberg::get_viewport_media_queries( $viewport_settings );
+ } elseif ( method_exists( 'WP_Theme_JSON', 'get_viewport_media_queries' ) ) {
+ $responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );
+ }
+
+ $styles = array();
+ $base_layout = is_array( $attributes['layout'] ?? null ) ? $attributes['layout'] : array();
+ foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
+ $viewport_style = is_array( $style[ $breakpoint ] ?? null ) ? $style[ $breakpoint ] : array();
+ $viewport_layout = is_array( $viewport_style['layout'] ?? null ) ? $viewport_style['layout'] : array();
+ if ( empty( $viewport_layout ) ) {
+ continue;
+ }
+
+ $styles[] = array(
+ 'declarations' => block_core_navigation_get_layout_custom_property_declarations(
+ array_replace( $base_layout, $viewport_layout )
+ ),
+ 'rules_group' => $media_query,
+ );
+ }
+
$processor = new WP_HTML_Tag_Processor( $block_content );
if ( ! $processor->next_tag() ) {
return $block_content;
}
$class_attribute = $processor->get_attribute( 'class' );
- if ( ! is_string( $class_attribute ) || ! preg_match( '/\bwp-states-[a-f0-9]{8}\b/', $class_attribute, $matches ) ) {
+ $state_class = null;
+ if ( is_string( $class_attribute ) && preg_match( '/\bwp-states-[a-f0-9]{8}\b/', $class_attribute, $matches ) ) {
+ $state_class = $matches[0];
+ }
+
+ $layout_class = null;
+ if ( ! empty( $styles ) ) {
+ $layout_class = wp_unique_id( 'wp-block-navigation-' );
+ // The inner selector includes both Navigation classes so it overrides the
+ // default layout custom properties set by `.wp-block-navigation.items-*`.
+ $selector = ".wp-block-navigation.{$layout_class},.wp-block-navigation.wp-block-navigation__container.{$layout_class}";
+ foreach ( $styles as &$style_rule ) {
+ $style_rule['selector'] = $selector;
+ }
+ unset( $style_rule );
+
+ $processor->add_class( $layout_class );
+ wp_style_engine_get_stylesheet_from_css_rules(
+ $styles,
+ array( 'context' => 'block-supports' )
+ );
+ }
+
+ if ( null === $state_class && null === $layout_class ) {
return $block_content;
}
- $state_class = $matches[0];
while ( $processor->next_tag() ) {
// Custom overlay content can include nested Navigation blocks.
- // Avoid applying the outer Navigation state class to an inner nav block.
+ // Avoid applying the outer Navigation classes to an inner nav block.
if ( $processor->has_class( 'wp-block-navigation' ) && ! $processor->has_class( 'wp-block-navigation__container' ) ) {
break;
}
@@ -1602,6 +1704,14 @@ function block_core_navigation_add_state_class_to_container( $block_content, $bl
continue;
}
+ if ( null !== $layout_class ) {
+ $processor->add_class( $layout_class );
+ }
+
+ if ( null === $state_class ) {
+ continue;
+ }
+
$class_attribute = $processor->get_attribute( 'class' );
if ( is_string( $class_attribute ) && preg_match( '/\bwp-states-[a-f0-9]{8}\b/', $class_attribute ) ) {
continue;
@@ -1613,7 +1723,7 @@ function block_core_navigation_add_state_class_to_container( $block_content, $bl
return $processor->get_updated_html();
}
-add_filter( 'render_block', 'block_core_navigation_add_state_class_to_container', 11, 2 );
+add_filter( 'render_block', 'block_core_navigation_add_support_classes_to_container', 11, 2 );
/**
* Filter that changes the parsed attribute values of navigation blocks contain typographic presets to contain the values directly.
diff --git a/phpunit/blocks/render-block-navigation-test.php b/phpunit/blocks/render-block-navigation-test.php
index b62a8a9c80d352..c16d179a059ecf 100644
--- a/phpunit/blocks/render-block-navigation-test.php
+++ b/phpunit/blocks/render-block-navigation-test.php
@@ -140,13 +140,13 @@ public function test_block_core_navigation_set_overlay_image_fetch_priority_no_i
/**
* Test that original markup is returned when there are no state classes.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_no_state_class_preserves_block_markup() {
$block_content = ' ';
$block = array( 'blockName' => 'core/navigation' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame( $block_content, $actual );
}
@@ -154,13 +154,13 @@ public function test_no_state_class_preserves_block_markup() {
/**
* Test that Navigation state classes are copied to inner list containers.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_state_class_is_added_to_inner_list_container() {
$block_content = ' ';
$block = array( 'blockName' => 'core/navigation' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame(
' ',
@@ -171,13 +171,13 @@ public function test_state_class_is_added_to_inner_list_container() {
/**
* Test that non-Navigation blocks are unchanged.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_state_class_is_not_added_to_non_navigation_blocks() {
$block_content = ' ';
$block = array( 'blockName' => 'core/group' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame( $block_content, $actual );
}
@@ -185,13 +185,13 @@ public function test_state_class_is_not_added_to_non_navigation_blocks() {
/**
* Test that existing inner state classes are preserved.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_existing_inner_state_class_is_preserved() {
$block_content = ' ';
$block = array( 'blockName' => 'core/navigation' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame( $block_content, $actual );
}
@@ -202,13 +202,13 @@ public function test_existing_inner_state_class_is_preserved() {
* Navigation can emit more than one list container when listable menu items
* are separated by non-listable blocks.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_state_class_is_added_to_multiple_inner_list_containers() {
$block_content = 'Separator
';
$block = array( 'blockName' => 'core/navigation' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame(
'Separator
',
@@ -219,13 +219,13 @@ public function test_state_class_is_added_to_multiple_inner_list_containers() {
/**
* Test that Navigation state classes are not copied to nested Navigation blocks.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_state_class_is_not_added_to_nested_navigation_containers() {
$block_content = ' ';
$block = array( 'blockName' => 'core/navigation' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame(
' ',
@@ -236,17 +236,79 @@ public function test_state_class_is_not_added_to_nested_navigation_containers()
/**
* Test that nested Navigation blocks preserve their own state classes.
*
- * @covers ::gutenberg_block_core_navigation_add_state_class_to_container
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
*/
public function test_nested_navigation_preserves_its_own_state_class() {
$block_content = ' ';
$block = array( 'blockName' => 'core/navigation' );
- $actual = gutenberg_block_core_navigation_add_state_class_to_container( $block_content, $block );
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container( $block_content, $block );
$this->assertSame(
' ',
$actual
);
}
+
+ /**
+ * Test that layout values are converted to the Navigation custom properties.
+ *
+ * @covers ::gutenberg_block_core_navigation_get_layout_custom_property_declarations
+ */
+ public function test_get_layout_custom_property_declarations() {
+ $actual = gutenberg_block_core_navigation_get_layout_custom_property_declarations(
+ array(
+ 'orientation' => 'vertical',
+ 'justifyContent' => 'right',
+ 'flexWrap' => 'nowrap',
+ )
+ );
+
+ $this->assertSame(
+ array(
+ '--navigation-layout-justification-setting' => 'flex-end',
+ '--navigation-layout-direction' => 'column',
+ '--navigation-layout-wrap' => 'nowrap',
+ '--navigation-layout-justify' => 'flex-end',
+ '--navigation-layout-align' => 'flex-end',
+ ),
+ $actual
+ );
+ }
+
+ /**
+ * Test responsive layout styles and classes on local Navigation containers.
+ *
+ * @covers ::gutenberg_block_core_navigation_add_support_classes_to_container
+ */
+ public function test_add_layout_custom_properties_updates_navigation_containers_only() {
+ $block_content = 'Separator
';
+ $attributes = array(
+ 'layout' => array(
+ 'justifyContent' => 'right',
+ 'flexWrap' => 'nowrap',
+ ),
+ 'style' => array(
+ '@mobile' => array(
+ 'layout' => array( 'orientation' => 'vertical' ),
+ ),
+ ),
+ );
+
+ $actual = gutenberg_block_core_navigation_add_support_classes_to_container(
+ $block_content,
+ array(
+ 'blockName' => 'core/navigation',
+ 'attrs' => $attributes,
+ )
+ );
+ $this->assertMatchesRegularExpression( '/\bwp-block-navigation-(\d+)\b/', $actual );
+ preg_match( '/\bwp-block-navigation-(\d+)\b/', $actual, $matches );
+ $responsive_class = $matches[0];
+
+ $this->assertSame(
+ 'Separator
',
+ $actual
+ );
+ }
}