-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add Navigation Overlay close block (behind experiment) #73724
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 18 commits
0874d3a
a3a2aaa
a41e5da
509ba4d
65c6620
efd367b
f0ced15
f8da4fe
e66deed
65367c3
dbe8322
4f6867e
82c78bc
8eacbee
e26bf85
6728514
e2db952
f46c64a
9ec0ffd
741539f
3a6d9f0
733ca79
7fe8323
3f80654
a66d451
e10225a
75a34c7
48ab8c2
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -546,6 +546,15 @@ Add a page, link, or another item to your navigation. ([Source](https://github.c | |||||
| - **Supports:** interactivity (clientNavigation), typography (fontSize, lineHeight), ~~html~~, ~~renaming~~, ~~reusable~~ | ||||||
| - **Attributes:** description, id, isTopLevelLink, kind, label, opensInNewTab, rel, title, type, url | ||||||
|
|
||||||
| ## Navigation Overlay Close | ||||||
|
|
||||||
| A customizable button to close overlays. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/navigation-overlay-close)) | ||||||
|
|
||||||
| - **Name:** core/navigation-overlay-close | ||||||
| - **Category:** design | ||||||
| - **Supports:** color (background, text, ~~gradients~~), spacing (padding), typography (fontSize, lineHeight) | ||||||
|
||||||
| - **Supports:** color (background, text, ~~gradients~~), spacing (padding), typography (fontSize, lineHeight) | |
| - **Supports:** color (background, text, ~~gradients~~), spacing (padding), typography (fontSize, lineHeight, fontFamily, fontWeight, fontStyle, textTransform, textDecoration, letterSpacing) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||
| { | ||||||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||||||
|
Member
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. Should this have a
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. Interesting. I didn't think that would work at the entity level. I'll give that a try now.
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. It doesn't. I've used a filter to restrict insertion to when we're editing an overlay template part. It still shows up in the inserter but that's because we don't seem to have a way to filter that dynamically as it relies on We could consider hiding blocks that can't be inserted from the inserter. That would seem to make sense.
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. While we can't use Aside, maybe diff --git a/packages/block-library/src/navigation-overlay-close/block.json b/packages/block-library/src/navigation-overlay-close/block.json
index 96cdabca2a2..aade2aaf570 100644
--- a/packages/block-library/src/navigation-overlay-close/block.json
+++ b/packages/block-library/src/navigation-overlay-close/block.json
@@ -6,6 +6,7 @@
"category": "design",
"description": "A customizable button to close overlays.",
"keywords": [ "close", "overlay", "navigation", "menu" ],
+ "ancestor": [ "core/template-part" ],
"textdomain": "default",
"attributes": {
"displayMode": {
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. Turns out this won't work because when in focused template part editing mode we don't have a template part block. As a result with that attribute in place you can no longer add a Close block to the overlay template.
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.
You're right 😢
BTW this raises the question if we should make Gutenberg smart enough to understand that if we're currently in focused "x entity" editing mode, and a block includes
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. I think it should yes. Or we allow blocks to define the
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. I've filed an issue: #73827
One thing I've discussed with @mtias is to
Together, those changes should allow us to limit the block to the Navigation Overlay. (Note that @mtias is also modifying the behavior of the
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.
Draft PR: #73828
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. @ockham Thank you for your work here. As it's experimental, I'm going to go ahead and merge the block and then follow up to include your work when it lands as it looks to be exactly what we'll need here 🙇 |
||||||
| "apiVersion": 3, | ||||||
| "name": "core/navigation-overlay-close", | ||||||
| "title": "Navigation Overlay Close", | ||||||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| "category": "design", | ||||||
| "description": "A customizable button to close overlays.", | ||||||
| "keywords": [ "close", "overlay", "navigation", "menu" ], | ||||||
| "textdomain": "default", | ||||||
| "attributes": { | ||||||
| "displayMode": { | ||||||
| "type": "string", | ||||||
| "enum": [ "icon", "text", "both" ], | ||||||
| "default": "icon" | ||||||
| }, | ||||||
| "text": { | ||||||
| "type": "rich-text", | ||||||
| "source": "rich-text", | ||||||
| "selector": "span", | ||||||
| "default": "Close" | ||||||
| } | ||||||
| }, | ||||||
| "supports": { | ||||||
| "color": { | ||||||
| "gradients": false, | ||||||
| "__experimentalDefaultControls": { | ||||||
| "background": true, | ||||||
| "text": true | ||||||
| } | ||||||
| }, | ||||||
| "spacing": { | ||||||
| "padding": true, | ||||||
| "__experimentalDefaultControls": { | ||||||
| "padding": true | ||||||
| } | ||||||
| }, | ||||||
| "typography": { | ||||||
| "fontSize": true, | ||||||
| "lineHeight": true, | ||||||
| "__experimentalFontFamily": true, | ||||||
| "__experimentalFontWeight": true, | ||||||
| "__experimentalFontStyle": true, | ||||||
| "__experimentalTextTransform": true, | ||||||
| "__experimentalTextDecoration": true, | ||||||
| "__experimentalLetterSpacing": true, | ||||||
| "__experimentalDefaultControls": { | ||||||
| "fontSize": 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,82 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| InspectorControls, | ||
| useBlockProps, | ||
| RichText, | ||
| } from '@wordpress/block-editor'; | ||
| import { | ||
| PanelBody, | ||
| __experimentalToggleGroupControl as ToggleGroupControl, | ||
| __experimentalToggleGroupControlOption as ToggleGroupControlOption, | ||
| Button, | ||
| } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { close as closeIcon } from '@wordpress/icons'; | ||
|
|
||
| export default function NavigationOverlayCloseEdit( { | ||
| attributes, | ||
| setAttributes, | ||
| } ) { | ||
| const { displayMode, text } = attributes; | ||
| const showIcon = displayMode === 'icon' || displayMode === 'both'; | ||
| const showText = displayMode === 'text' || displayMode === 'both'; | ||
|
|
||
| const blockProps = useBlockProps( { | ||
| className: 'wp-block-navigation-overlay-close', | ||
| } ); | ||
|
|
||
| return ( | ||
| <> | ||
| <InspectorControls> | ||
| <PanelBody title={ __( 'Display Settings' ) }> | ||
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <ToggleGroupControl | ||
| label={ __( 'Display Mode' ) } | ||
| value={ displayMode } | ||
| onChange={ ( value ) => | ||
| setAttributes( { displayMode: value } ) | ||
| } | ||
| isBlock | ||
| __next40pxDefaultSize | ||
| __nextHasNoMarginBottom | ||
| > | ||
| <ToggleGroupControlOption | ||
| value="icon" | ||
| label={ __( 'Icon' ) } | ||
| /> | ||
| <ToggleGroupControlOption | ||
| value="text" | ||
| label={ __( 'Text' ) } | ||
| /> | ||
| <ToggleGroupControlOption | ||
| value="both" | ||
| label={ __( 'Both' ) } | ||
| /> | ||
| </ToggleGroupControl> | ||
| </PanelBody> | ||
| </InspectorControls> | ||
| <Button | ||
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { ...blockProps } | ||
| icon={ showIcon ? closeIcon : undefined } | ||
| aria-label={ __( 'Close' ) } | ||
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __next40pxDefaultSize | ||
| > | ||
| { showText && ( | ||
| <RichText | ||
| identifier="text" | ||
| value={ text } | ||
| onChange={ ( value ) => | ||
| setAttributes( { text: value } ) | ||
| } | ||
| placeholder={ __( 'Close' ) } | ||
| allowedFormats={ [ 'core/bold', 'core/italic' ] } | ||
| withoutInteractiveFormatting | ||
| tagName="span" | ||
| className="wp-block-navigation-overlay-close__text" | ||
| /> | ||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) } | ||
| </Button> | ||
| </> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .block-editor-block-list__block[data-type="core/navigation-overlay-close"] { | ||
| .wp-block-navigation-overlay-close { | ||
| min-height: 44px; | ||
| min-width: 44px; | ||
|
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { SVG, Path } from '@wordpress/primitives'; | ||
|
|
||
| export default ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M18 4H6c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2Zm.5 14c0 .3-.2.5-.5.5H6c-.3 0-.5-.2-.5-.5V6c0-.3.2-.5.5-.5h12c.3 0 .5.2.5.5v12ZM15 8l-3 3-3-3-1.1 1.1 3 3-3 3L9 16.2l3-3 3 3 1.1-1.1-3-3 3-3L15 8Z" /> | ||
| </SVG> | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { addFilter } from '@wordpress/hooks'; | ||
| import { select } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import initBlock from '../utils/init-block'; | ||
| import edit from './edit'; | ||
| import metadata from './block.json'; | ||
| import save from './save'; | ||
| import icon from './icon'; | ||
|
|
||
| const { name } = metadata; | ||
|
|
||
| export { metadata, name }; | ||
|
|
||
| export const settings = { | ||
| icon, | ||
| edit, | ||
| save, | ||
| }; | ||
|
|
||
| function isWithinOverlay() { | ||
| // @wordpress/block-library should not depend on @wordpress/editor. | ||
| // Blocks can be loaded into a *non-post* block editor, so to avoid | ||
| // declaring @wordpress/editor as a dependency, we must access its | ||
| // store by string. | ||
| // eslint-disable-next-line @wordpress/data-no-store-string-literals | ||
| const { getCurrentPostType, getCurrentPostId } = select( 'core/editor' ); | ||
| const { getEditedEntityRecord } = select( coreStore ); | ||
|
|
||
| const postType = getCurrentPostType(); | ||
| const postId = getCurrentPostId(); | ||
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ( postType === 'wp_template_part' && postId ) { | ||
| const templatePartEntity = getEditedEntityRecord( | ||
| 'postType', | ||
| 'wp_template_part', | ||
| postId | ||
| ); | ||
|
|
||
| return templatePartEntity?.area === 'overlay'; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| export const init = () => { | ||
| addFilter( | ||
| 'blockEditor.__unstableCanInsertBlockType', | ||
| 'core/navigation-overlay-close/restrict-to-overlay-template-parts', | ||
| ( canInsert, blockType ) => { | ||
| if ( blockType.name !== 'core/navigation-overlay-close' ) { | ||
| return canInsert; | ||
| } | ||
|
|
||
| if ( ! canInsert ) { | ||
| return canInsert; | ||
| } | ||
|
|
||
| return isWithinOverlay(); | ||
| } | ||
| ); | ||
|
|
||
| return initBlock( { name, metadata, settings } ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
| /** | ||
| * Server-side registering of the `core/navigation-overlay-close` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Registers the navigation overlay close block. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| function register_block_core_navigation_overlay_close() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/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,40 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useBlockProps, RichText } from '@wordpress/block-editor'; | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| export default function NavigationOverlayCloseSave( { attributes } ) { | ||
| const { displayMode, text } = attributes; | ||
| const showIcon = displayMode === 'icon' || displayMode === 'both'; | ||
| const showText = displayMode === 'text' || displayMode === 'both'; | ||
|
|
||
| // eslint-disable-next-line react-compiler/react-compiler | ||
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const blockProps = useBlockProps.save( { | ||
| className: 'wp-block-navigation-overlay-close', | ||
| } ); | ||
|
|
||
| return ( | ||
| <button { ...blockProps } type="button" aria-label={ __( 'Close' ) }> | ||
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
getdave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { showIcon && ( | ||
| <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-1-6.1 6.2-6.1-6.2-1.1 1 6.1 6.3-6.5 6.7 1.1 1 6.5-6.6 6.5 6.6 1.1-1z" /> | ||
| </svg> | ||
| ) } | ||
| { showText && ( | ||
| <RichText.Content | ||
| tagName="span" | ||
| value={ text } | ||
| className="wp-block-navigation-overlay-close__text" | ||
| /> | ||
| ) } | ||
| </button> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| .wp-block-navigation-overlay-close { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| gap: 0.5em; | ||
| padding: 0.5em; | ||
| border: none; | ||
| background: transparent; | ||
| cursor: pointer; | ||
| text-decoration: none; | ||
| transition: opacity 0.2s; | ||
|
|
||
| &:hover { | ||
| opacity: 0.8; | ||
| } | ||
|
|
||
| &:focus { | ||
| outline: 2px solid currentColor; | ||
| outline-offset: 2px; | ||
| } | ||
|
|
||
| svg { | ||
| width: 1.5em; | ||
| height: 1.5em; | ||
| fill: currentColor; | ||
| } | ||
|
|
||
| span { | ||
| display: inline-block; | ||
| } | ||
| } | ||
|
|
||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||



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.
Maybe add a note about when it's allowed to be inserted (overlay template part)
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.
That would require updating the block description in
block.jsonas this is auto generated from that. Not sure 😬