-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Navigation Overlay customisation via Template Parts #55657
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!-- wp:group {"lock":{"move":true,"remove":true},"metadata":{"name":"<?php echo __( 'Overlay' ); ?>"},"style":{"dimensions":{"minHeight":"100vh"},"spacing":{"padding":{"top":"clamp(1rem, calc(var(\u002d\u002dwp\u002dadmin\u002d\u002dadmin\u002dbar\u002d\u002dheight, 1rem) + var(\u002d\u002dwp\u002d\u002dstyle\u002d\u002droot\u002d\u002dpadding\u002dtop, 1rem)), 20rem)","bottom":"clamp(1rem, var(\u002d\u002dwp\u002d\u002dstyle\u002d\u002droot\u002d\u002dpadding\u002dbottom, 1rem), 20rem)","left":"clamp(1rem, var(\u002d\u002dwp\u002d\u002dstyle\u002d\u002droot\u002d\u002dpadding\u002dleft), 20rem)","right":"clamp(1rem, var(\u002d\u002dwp\u002d\u002dstyle\u002d\u002droot\u002d\u002dpadding\u002dright), 20rem)"}}},"layout":{"type":"constrained","justifyContent":"center"}} --> | ||
| <div class="wp-block-group" style="min-height:100vh;padding-top:clamp(1rem, calc(var(--wp-admin--admin-bar--height, 1rem) + var(--wp--style--root--padding-top, 1rem)), 20rem);padding-right:clamp(1rem, var(--wp--style--root--padding-right), 20rem);padding-bottom:clamp(1rem, var(--wp--style--root--padding-bottom, 1rem), 20rem);padding-left:clamp(1rem, var(--wp--style--root--padding-left), 20rem)"><!-- wp:group {"metadata":{"name":"Close"},"align":"wide","layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"right"}} --> | ||
| <div class="wp-block-group alignwide"><!-- wp:navigation-overlay-close {"lock":{"move":false,"remove":true},"metadata":{},"className":"wp-block-navigation-overlay-close"} /--></div> | ||
| <!-- /wp:group --> | ||
|
|
||
| <!-- wp:group {"metadata":{"name":"<?php echo __( 'Contents' ); ?>"},"align":"wide","style":{"spacing":{"blockGap":"var:preset|spacing|30"}},"layout":{"type":"flex","orientation":"vertical","justifyContent":"right"}} --> | ||
| <div class="wp-block-group alignwide"><!-- wp:navigation {"overlayMenu":"never","align":"wide","layout":{"type":"flex","orientation":"vertical","justifyContent":"right"}} /--></div> | ||
| <!-- /wp:group --></div> | ||
| <!-- /wp:group --> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| { | ||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||
| "apiVersion": 3, | ||
| "name": "core/navigation-overlay-close", | ||
| "title": "Navigation Overlay Close", | ||
| "category": "design", | ||
| "description": "Add a Close button to your Navigation Overlay.", | ||
| "textdomain": "default", | ||
| "icon": "dismiss", | ||
| "attributes": { | ||
| "hasIcon": { | ||
| "type": "boolean", | ||
| "default": true | ||
| } | ||
| }, | ||
| "usesContext": [ | ||
| "textColor", | ||
| "customTextColor", | ||
| "backgroundColor", | ||
| "customBackgroundColor" | ||
| ], | ||
| "supports": { | ||
| "multiple": false, | ||
| "reusable": false, | ||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "html": false, | ||
| "dimensions": { | ||
| "width": true, | ||
| "height": true | ||
| }, | ||
| "color": { | ||
| "link": false, | ||
| "text": true, | ||
| "background": true | ||
| }, | ||
| "interactivity": true, | ||
| "anchor": true, | ||
| "spacing": { | ||
| "margin": true, | ||
| "padding": true, | ||
| "units": [ "px", "em", "rem", "vh", "vw" ], | ||
| "__experimentalDefaultControls": { | ||
| "margin": true, | ||
| "padding": true | ||
| } | ||
| } | ||
| }, | ||
| "editorStyle": "wp-block-navigation-overlay-close-editor", | ||
| "style": "wp-block-navigation-overlay-close" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Button, Icon, ToggleControl, PanelBody } from '@wordpress/components'; | ||
| import { close } from '@wordpress/icons'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; | ||
| import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { unlock } from '../lock-unlock'; | ||
|
|
||
| const { useHistory } = unlock( routerPrivateApis ); | ||
|
|
||
| export default function Edit( { attributes, isSelected, setAttributes } ) { | ||
| const blockProps = useBlockProps(); | ||
| const history = useHistory(); | ||
| const { hasIcon } = attributes; | ||
|
|
||
| const closeText = __( 'Close' ); | ||
|
|
||
| const onClick = () => { | ||
| if ( isSelected ) { | ||
| // Exit navigation overlay edit mode. | ||
| history.back(); | ||
|
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. There are more routes available, so this doesn't work. Example repro:
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. Also, we should still have the Back button navigation in the central header area as on other templates.
Contributor
Author
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.
That's turned into a wider problem. I agree we need it but it will need to be a seperate PR. |
||
| } | ||
| }; | ||
|
|
||
| blockProps.onClick = onClick; | ||
|
|
||
| return ( | ||
| <> | ||
| <InspectorControls> | ||
| <PanelBody title={ __( 'Display' ) }> | ||
| <ToggleControl | ||
| __nextHasNoMarginBottom | ||
| label={ __( 'Use icon' ) } | ||
| help={ __( | ||
| 'Configure whether the button use an icon or text.' | ||
| ) } | ||
| onChange={ ( value ) => | ||
| setAttributes( { hasIcon: value } ) | ||
| } | ||
| checked={ hasIcon } | ||
| /> | ||
| </PanelBody> | ||
| </InspectorControls> | ||
| <Button { ...blockProps } aria-label={ hasIcon && closeText }> | ||
| { hasIcon ? <Icon icon={ close } /> : closeText } | ||
| </Button> | ||
| </> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import initBlock from '../utils/init-block'; | ||
| import metadata from './block.json'; | ||
| import edit from './edit'; | ||
|
|
||
| const { name } = metadata; | ||
|
|
||
| export { metadata, name }; | ||
|
|
||
| export const settings = { | ||
| edit, | ||
| }; | ||
|
|
||
| export const init = () => initBlock( { name, metadata, settings } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
|
|
||
| function render_block_core_navigation_overlay_close( $attributes ) { | ||
|
Check failure on line 4 in packages/block-library/src/navigation-overlay-close/index.php
|
||
|
|
||
| // This icon is duplicated in the Navigation Block itself. | ||
| // See WP_Navigation_Block_Renderer::get_responsive_container_markup(). | ||
| // Changes to this icon should be reflected there as well. | ||
| $close_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg>'; | ||
|
|
||
| $hasIcon = ! empty( $attributes['hasIcon'] ); | ||
|
|
||
| $wrapper_attributes = get_block_wrapper_attributes( | ||
| array_filter( // Removes any empty attributes. | ||
| // Attributes | ||
| array( | ||
| // This directive is duplicated in the Navigation Block itself. | ||
| // See WP_Navigation_Block_Renderer::get_responsive_container_markup(). | ||
| // Changes to this directive should be reflected there as well. | ||
| 'data-wp-on--click' => 'actions.closeMenuOnClick', | ||
| 'aria-label' => $hasIcon ? __( 'Close menu' ) : false, | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| $content = $hasIcon ? $close_icon : __( 'Close menu' ); | ||
|
|
||
| return sprintf( | ||
| '<button %1$s>%2$s</button>', | ||
| $wrapper_attributes, | ||
| $content, | ||
| ); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Registers the `core/navigation-overlay-close` block on server. | ||
| */ | ||
| function register_block_core_navigation_overlay_close() { | ||
|
Check failure on line 40 in packages/block-library/src/navigation-overlay-close/index.php
|
||
| register_block_type_from_metadata( | ||
| __DIR__ . '/navigation-overlay-close', | ||
| array( | ||
| 'render_callback' => 'render_block_core_navigation_overlay_close', | ||
| ) | ||
| ); | ||
| } | ||
| add_action( 'init', 'register_block_core_navigation_overlay_close' ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { init } from './'; | ||
|
|
||
| export default init(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
|
|
||
| // Size of burger and close icons. | ||
| $navigation-icon-size: 24px; | ||
|
|
||
| // Menu and close buttons. | ||
| .wp-block-navigation-overlay-close { | ||
| height: auto; // remove default height applied to button component | ||
| vertical-align: middle; | ||
| cursor: pointer; | ||
| border: none; | ||
| margin: 0; | ||
| padding: 0; | ||
| text-transform: inherit; | ||
| z-index: 2; // Needs to be above the modal z index itself. | ||
| background-color: inherit; // remove user agent stylesheet default. | ||
| color: inherit; // remove user agent stylesheet default. | ||
|
|
||
| // When set to collapse into a text button, it should inherit the parent font. | ||
| // This needs specificity to override inherited properties by the button element and component. | ||
| &.wp-block-navigation-overlay-close { | ||
| font-family: inherit; | ||
| font-weight: inherit; | ||
| font-size: inherit; | ||
| } | ||
|
|
||
| svg { | ||
| fill: currentColor; | ||
| pointer-events: none; | ||
| display: block; | ||
| width: $navigation-icon-size; | ||
| height: $navigation-icon-size; | ||
| } | ||
| } | ||
|
|
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.
Don't worry - we can make a separate PR for this block if/when we decide to merge this branch.
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.
@WordPress/interactivity-api This block is going to effectively be the same as the existing button which toggles the overlay. It's required because when editing the overlay within the editor, users need to be able to:
I'd really appreciate any advice/help on how best to transfer the interactivity directives over the this block 🙏
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.
We've been doing so with the
WP_HTML_Tag_Processorquite successfully so far.We basically use the
WP_HTML_Tag_Processoron the render callback of the block, search for the inner block, and add the necessary directives. Something like this: