Repository navigation
Try fixing responsive layout in Nav block #80305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
packages/block-library/src/navigation/edit/test/layout-custom-properties.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: '' } ); | ||
| } ); | ||
| } ); | ||
| } ); |
84 changes: 84 additions & 0 deletions
84
packages/block-library/src/navigation/edit/use-layout-custom-properties.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 } ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,138 @@ 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, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds responsive layout custom property styles to a Navigation block. | ||
| * | ||
| * Navigation uses layout classes on its outer wrapper to define custom | ||
| * properties consumed by its inner and overlay containers. Viewport layout | ||
| * styles cannot change those classes, so equivalent custom properties are | ||
| * generated inside each configured viewport media query. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @param string $block_content Rendered Navigation block markup. | ||
| * @param array $attributes Navigation block attributes. | ||
| * @return string Updated Navigation block markup. | ||
| */ | ||
| function block_core_navigation_add_layout_custom_properties( $block_content, $attributes ) { | ||
| if ( empty( $block_content ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $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(); | ||
| foreach ( array( 'WP_Theme_JSON_Gutenberg', 'WP_Theme_JSON' ) as $theme_json_class_name ) { | ||
| if ( method_exists( $theme_json_class_name, 'get_viewport_media_queries' ) ) { | ||
| $responsive_media_queries = $theme_json_class_name::get_viewport_media_queries( $viewport_settings ); | ||
| break; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I see what it's doing here, but it seems like a harder to read way of doing it vs. something like: 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 );
}Also more lines of code. |
||
|
|
||
| $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, | ||
| ); | ||
| } | ||
| if ( empty( $styles ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $unique_classname = 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.{$unique_classname},.wp-block-navigation.wp-block-navigation__container.{$unique_classname}"; | ||
| foreach ( $styles as &$style_rule ) { | ||
| $style_rule['selector'] = $selector; | ||
| } | ||
| unset( $style_rule ); | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $block_content ); | ||
| if ( ! $processor->next_tag() ) { | ||
| return $block_content; | ||
| } | ||
| $processor->add_class( $unique_classname ); | ||
|
|
||
| while ( $processor->next_tag() ) { | ||
| // Custom overlay content can include nested Navigation blocks. | ||
| // Avoid applying the outer Navigation class to an inner nav block. | ||
| if ( $processor->has_class( 'wp-block-navigation' ) && ! $processor->has_class( 'wp-block-navigation__container' ) ) { | ||
| break; | ||
| } | ||
|
|
||
| if ( $processor->has_class( 'wp-block-navigation__container' ) ) { | ||
| $processor->add_class( $unique_classname ); | ||
| } | ||
| } | ||
|
|
||
| wp_style_engine_get_stylesheet_from_css_rules( | ||
| $styles, | ||
| array( 'context' => 'block-supports' ) | ||
| ); | ||
|
|
||
| return $processor->get_updated_html(); | ||
| } | ||
|
|
||
| /** | ||
| * Helper functions used to render the navigation block. | ||
| * | ||
|
|
@@ -1006,12 +1138,14 @@ public static function render( $attributes, $content, $block ) { | |
| $is_within_overlay = $attributes['_isWithinOverlayTemplatePart'] ?? false; | ||
| $tag_name = $is_within_overlay ? 'div' : 'nav'; | ||
|
|
||
| return sprintf( | ||
| $navigation_markup = sprintf( | ||
| '<%1$s %2$s>%3$s</%1$s>', | ||
| $tag_name, | ||
| static::get_nav_attributes( $attributes, $inner_blocks ), | ||
| static::get_inner_block_markup( $attributes, $inner_blocks ) | ||
| ); | ||
|
|
||
| return block_core_navigation_add_layout_custom_properties( $navigation_markup, $attributes ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really minor, but #79072 uses a filter for a similar situation (
block_core_navigation_add_state_class_to_container).I wonder if we could consolidate the two together (just one
block_core_navigation_process_style_statefunction). Or alternatively keep it seperate, but make the two work similarly, both use filters or both called as part of the render.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried consolidating both functions. There's a fair bit of custom logic in each so it's not a huge economy in terms of lines of code but it means everything state-related is now in the same function and the same filter.