-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Experiment: Replicate Navigation block using directives #44509
Closed
luisherranz
wants to merge
28
commits into
trunk
from
experiment/navigation-block-with-wordpress-directives
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
22c3f1a
First WordPress directives
luisherranz 4af14d6
Fix return of components
luisherranz 9195ddf
Delete not used view-modal file
luisherranz a28ccfd
Merge remote-tracking branch 'origin/trunk' into experiment/navigatio…
luisherranz 1ee977c
Refactor directives logic to match last version in BHE
luisherranz 5a5b451
Preliminary support for prefix/suffix in directives
luisherranz cd01ffa
Update package.lock
luisherranz eabeed9
Remove support for modifiers
luisherranz ced9cbf
Move rename and value to the vdom file
luisherranz 2d8dc0c
Add wp-class directive
luisherranz 546b5b8
Remove camelcase in directive suffix
luisherranz 7807a98
Add first directives to open and close the menu
luisherranz 4534457
Pass ref, use it to set focus of first element
luisherranz e80249f
Try new API
luisherranz 7d868f9
Remove wp-log
luisherranz d85c56b
Fix variable typo
luisherranz 9309a91
Rename directive props to directive args
luisherranz 265763e
Switch to signals
luisherranz 1b244bb
Don't enqueue view-modal
luisherranz fa5c532
Add preliminary support for components
luisherranz 221ea02
Rename directives file
luisherranz 21d937e
Move context to wpx file
luisherranz d0db1e8
Add wp-bind:attr directive
luisherranz 42e4612
Deep signal: Add all the supported arrays
luisherranz 62e46a4
Move stuff to utils
luisherranz d54ab56
Refactor to wpx folder
luisherranz 4c82fbd
Move function to JS file to avoid eval
luisherranz b2e0e6a
Namespace navigation context
luisherranz 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,74 +1,34 @@ | ||
// Open on click functionality. | ||
function closeSubmenus( element ) { | ||
element | ||
.querySelectorAll( '[aria-expanded="true"]' ) | ||
.forEach( function ( toggle ) { | ||
toggle.setAttribute( 'aria-expanded', 'false' ); | ||
} ); | ||
} | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import wpx from './wpx'; | ||
|
||
function toggleSubmenuOnClick( event ) { | ||
const buttonToggle = event.target.closest( '[aria-expanded]' ); | ||
const isSubmenuOpen = buttonToggle.getAttribute( 'aria-expanded' ); | ||
|
||
if ( isSubmenuOpen === 'true' ) { | ||
closeSubmenus( buttonToggle.closest( '.wp-block-navigation-item' ) ); | ||
} else { | ||
// Close all sibling submenus. | ||
const parentElement = buttonToggle.closest( | ||
'.wp-block-navigation-item' | ||
); | ||
const navigationParent = buttonToggle.closest( | ||
'.wp-block-navigation__submenu-container, .wp-block-navigation__container, .wp-block-page-list' | ||
); | ||
navigationParent | ||
.querySelectorAll( '.wp-block-navigation-item' ) | ||
.forEach( function ( child ) { | ||
if ( child !== parentElement ) { | ||
closeSubmenus( child ); | ||
wpx( { | ||
core: { | ||
navigation: { | ||
openMenu: ( { context: { navigation } } ) => { | ||
navigation.open = true; | ||
navigation.previousElementWithFocus = | ||
window.document.activeElement; | ||
}, | ||
closeMenu: ( { context: { navigation } } ) => { | ||
navigation.open = false; | ||
}, | ||
isMenuOpen: ( { context: { navigation } } ) => navigation.open, | ||
isMenuClosed: ( { context: { navigation } } ) => ! navigation.open, | ||
addFirstElementToContext: ( { context: { navigation }, ref } ) => { | ||
navigation.firstMenuElement = ref; | ||
}, | ||
focusFirstElement: async ( { context: { navigation }, tick } ) => { | ||
if ( navigation.open ) { | ||
await tick(); // We need to wait until the DOM is updated. | ||
navigation.firstMenuElement.focus(); | ||
} | ||
} ); | ||
// Open submenu. | ||
buttonToggle.setAttribute( 'aria-expanded', 'true' ); | ||
} | ||
} | ||
|
||
// Necessary for some themes such as TT1 Blocks, where | ||
// scripts could be loaded before the body. | ||
window.addEventListener( 'load', () => { | ||
const submenuButtons = document.querySelectorAll( | ||
'.wp-block-navigation-submenu__toggle' | ||
); | ||
|
||
submenuButtons.forEach( function ( button ) { | ||
button.addEventListener( 'click', toggleSubmenuOnClick ); | ||
} ); | ||
|
||
// Close on click outside. | ||
document.addEventListener( 'click', function ( event ) { | ||
const navigationBlocks = document.querySelectorAll( | ||
'.wp-block-navigation' | ||
); | ||
navigationBlocks.forEach( function ( block ) { | ||
if ( ! block.contains( event.target ) ) { | ||
closeSubmenus( block ); | ||
} | ||
} ); | ||
} ); | ||
// Close on focus outside or escape key. | ||
document.addEventListener( 'keyup', function ( event ) { | ||
const submenuBlocks = document.querySelectorAll( | ||
'.wp-block-navigation-item.has-child' | ||
); | ||
submenuBlocks.forEach( function ( block ) { | ||
if ( ! block.contains( event.target ) ) { | ||
closeSubmenus( block ); | ||
} else if ( event.key === 'Escape' ) { | ||
const toggle = block.querySelector( '[aria-expanded="true"]' ); | ||
closeSubmenus( block ); | ||
// Focus the submenu trigger so focus does not get trapped in the closed submenu. | ||
toggle?.focus(); | ||
} | ||
} ); | ||
} ); | ||
}, | ||
focusLastFocusedElement: ( { context: { navigation } } ) => { | ||
if ( ! navigation.open && navigation.previousElementWithFocus ) | ||
navigation.previousElementWithFocus.focus(); | ||
}, | ||
}, | ||
}, | ||
} ); |
This file contains 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,23 @@ | ||
// eslint-disable-next-line eslint-comments/disable-enable-pair | ||
/* eslint-disable jsdoc/check-tag-names */ | ||
/** @jsx h */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { h } from 'preact'; | ||
import { useMemo } from 'preact/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { deepSignal } from './deep-signal'; | ||
import { component } from './hooks'; | ||
|
||
export default () => { | ||
const WpContext = ( { children, data, context: { Provider } } ) => { | ||
const signals = useMemo( () => deepSignal( JSON.parse( data ) ), [] ); | ||
return <Provider value={ signals }>{ children }</Provider>; | ||
}; | ||
component( 'wp-context', WpContext ); | ||
}; |
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.
This won't be needed once this is merged: preactjs/signals#290