@wordpress/block-library 8.14.0 → 8.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/build/avatar/edit.js +1 -0
- package/build/avatar/edit.js.map +1 -1
- package/build/avatar/index.js +3 -0
- package/build/avatar/index.js.map +1 -1
- package/build/block/edit.js +2 -30
- package/build/block/edit.js.map +1 -1
- package/build/block/index.js +2 -1
- package/build/block/index.js.map +1 -1
- package/build/buttons/edit.js +2 -2
- package/build/buttons/edit.js.map +1 -1
- package/build/column/edit.native.js +1 -4
- package/build/column/edit.native.js.map +1 -1
- package/build/columns/edit.js +1 -0
- package/build/columns/edit.js.map +1 -1
- package/build/comment-author-avatar/edit.js +1 -0
- package/build/comment-author-avatar/edit.js.map +1 -1
- package/build/cover/deprecated.js +4 -2
- package/build/cover/deprecated.js.map +1 -1
- package/build/cover/index.js +2 -1
- package/build/cover/index.js.map +1 -1
- package/build/embed/embed-placeholder.native.js +37 -13
- package/build/embed/embed-placeholder.native.js.map +1 -1
- package/build/file/index.js +1 -2
- package/build/file/index.js.map +1 -1
- package/build/file/inspector.js +1 -0
- package/build/file/inspector.js.map +1 -1
- package/build/file/view-interactivity.js +23 -0
- package/build/file/view-interactivity.js.map +1 -0
- package/build/file/view.js +1 -15
- package/build/file/view.js.map +1 -1
- package/build/footnotes/edit.js +31 -1
- package/build/footnotes/edit.js.map +1 -1
- package/build/footnotes/format.js +119 -23
- package/build/footnotes/format.js.map +1 -1
- package/build/footnotes/index.js +45 -4
- package/build/footnotes/index.js.map +1 -1
- package/build/gallery/edit.js +8 -6
- package/build/gallery/edit.js.map +1 -1
- package/build/image/deprecated.js +106 -2
- package/build/image/deprecated.js.map +1 -1
- package/build/image/image.js +2 -2
- package/build/image/image.js.map +1 -1
- package/build/image/index.js +2 -1
- package/build/image/index.js.map +1 -1
- package/build/index.js +3 -1
- package/build/index.js.map +1 -1
- package/build/latest-comments/edit.js +1 -0
- package/build/latest-comments/edit.js.map +1 -1
- package/build/latest-posts/edit.js +2 -0
- package/build/latest-posts/edit.js.map +1 -1
- package/build/list/edit.js +4 -4
- package/build/list/edit.js.map +1 -1
- package/build/list-item/hooks/use-merge.js +10 -1
- package/build/list-item/hooks/use-merge.js.map +1 -1
- package/build/media-text/media-container.native.js +2 -1
- package/build/media-text/media-container.native.js.map +1 -1
- package/build/missing/edit.native.js +7 -5
- package/build/missing/edit.native.js.map +1 -1
- package/build/navigation/constants.js +10 -4
- package/build/navigation/constants.js.map +1 -1
- package/build/navigation/edit/index.js +16 -2
- package/build/navigation/edit/index.js.map +1 -1
- package/build/navigation/edit/inner-blocks.js +2 -2
- package/build/navigation/edit/inner-blocks.js.map +1 -1
- package/build/navigation/edit/menu-inspector-controls.js +1 -1
- package/build/navigation/edit/menu-inspector-controls.js.map +1 -1
- package/build/navigation/edit/navigation-menu-selector.js +4 -4
- package/build/navigation/edit/navigation-menu-selector.js.map +1 -1
- package/build/navigation/edit/unsaved-inner-blocks.js +2 -2
- package/build/navigation/edit/unsaved-inner-blocks.js.map +1 -1
- package/build/navigation/edit/use-convert-classic-menu-to-block-menu.js +10 -15
- package/build/navigation/edit/use-convert-classic-menu-to-block-menu.js.map +1 -1
- package/build/navigation/index.js +2 -3
- package/build/navigation/index.js.map +1 -1
- package/build/navigation/use-navigation-menu.js +33 -45
- package/build/navigation/use-navigation-menu.js.map +1 -1
- package/build/navigation/view-interactivity.js +185 -0
- package/build/navigation/view-interactivity.js.map +1 -0
- package/build/navigation/view-modal.js +125 -0
- package/build/navigation/view-modal.js.map +1 -0
- package/build/navigation/view.js +71 -163
- package/build/navigation/view.js.map +1 -1
- package/build/navigation-link/edit.js +12 -7
- package/build/navigation-link/edit.js.map +1 -1
- package/build/navigation-submenu/edit.js +2 -2
- package/build/navigation-submenu/edit.js.map +1 -1
- package/build/pattern/edit.js +28 -4
- package/build/pattern/edit.js.map +1 -1
- package/build/preformatted/index.js +4 -0
- package/build/preformatted/index.js.map +1 -1
- package/build/query-pagination/edit.js +1 -1
- package/build/query-pagination/edit.js.map +1 -1
- package/build/query-title/edit.js +43 -1
- package/build/query-title/edit.js.map +1 -1
- package/build/quote/transforms.js +8 -0
- package/build/quote/transforms.js.map +1 -1
- package/build/rss/edit.js +3 -0
- package/build/rss/edit.js.map +1 -1
- package/build/search/edit.js +4 -3
- package/build/search/edit.js.map +1 -1
- package/build/search/index.js +1 -0
- package/build/search/index.js.map +1 -1
- package/build/search/view.js +166 -62
- package/build/search/view.js.map +1 -1
- package/build/site-logo/edit.js +1 -0
- package/build/site-logo/edit.js.map +1 -1
- package/build/social-link/icons/index.js +13 -0
- package/build/social-link/icons/index.js.map +1 -1
- package/build/social-link/icons/threads.js +25 -0
- package/build/social-link/icons/threads.js.map +1 -0
- package/build/social-link/variations.js +7 -0
- package/build/social-link/variations.js.map +1 -1
- package/build/tag-cloud/edit.js +1 -0
- package/build/tag-cloud/edit.js.map +1 -1
- package/build/template-part/edit/import-controls.js +1 -1
- package/build/template-part/edit/import-controls.js.map +1 -1
- package/build/template-part/edit/index.js +1 -1
- package/build/template-part/edit/index.js.map +1 -1
- package/build/text-columns/edit.js +1 -0
- package/build/text-columns/edit.js.map +1 -1
- package/build-module/avatar/edit.js +1 -0
- package/build-module/avatar/edit.js.map +1 -1
- package/build-module/avatar/index.js +3 -0
- package/build-module/avatar/index.js.map +1 -1
- package/build-module/block/edit.js +4 -29
- package/build-module/block/edit.js.map +1 -1
- package/build-module/block/index.js +2 -1
- package/build-module/block/index.js.map +1 -1
- package/build-module/buttons/edit.js +2 -2
- package/build-module/buttons/edit.js.map +1 -1
- package/build-module/column/edit.native.js +1 -4
- package/build-module/column/edit.native.js.map +1 -1
- package/build-module/columns/edit.js +1 -0
- package/build-module/columns/edit.js.map +1 -1
- package/build-module/comment-author-avatar/edit.js +1 -0
- package/build-module/comment-author-avatar/edit.js.map +1 -1
- package/build-module/cover/deprecated.js +4 -2
- package/build-module/cover/deprecated.js.map +1 -1
- package/build-module/cover/index.js +2 -1
- package/build-module/cover/index.js.map +1 -1
- package/build-module/embed/embed-placeholder.native.js +37 -14
- package/build-module/embed/embed-placeholder.native.js.map +1 -1
- package/build-module/file/index.js +1 -2
- package/build-module/file/index.js.map +1 -1
- package/build-module/file/inspector.js +1 -0
- package/build-module/file/inspector.js.map +1 -1
- package/build-module/file/view-interactivity.js +19 -0
- package/build-module/file/view-interactivity.js.map +1 -0
- package/build-module/file/view.js +2 -15
- package/build-module/file/view.js.map +1 -1
- package/build-module/footnotes/edit.js +29 -2
- package/build-module/footnotes/edit.js.map +1 -1
- package/build-module/footnotes/format.js +120 -25
- package/build-module/footnotes/format.js.map +1 -1
- package/build-module/footnotes/index.js +45 -4
- package/build-module/footnotes/index.js.map +1 -1
- package/build-module/gallery/edit.js +8 -6
- package/build-module/gallery/edit.js.map +1 -1
- package/build-module/image/deprecated.js +107 -3
- package/build-module/image/deprecated.js.map +1 -1
- package/build-module/image/image.js +2 -2
- package/build-module/image/image.js.map +1 -1
- package/build-module/image/index.js +2 -1
- package/build-module/image/index.js.map +1 -1
- package/build-module/index.js +3 -1
- package/build-module/index.js.map +1 -1
- package/build-module/latest-comments/edit.js +1 -0
- package/build-module/latest-comments/edit.js.map +1 -1
- package/build-module/latest-posts/edit.js +2 -0
- package/build-module/latest-posts/edit.js.map +1 -1
- package/build-module/list/edit.js +4 -4
- package/build-module/list/edit.js.map +1 -1
- package/build-module/list-item/hooks/use-merge.js +10 -1
- package/build-module/list-item/hooks/use-merge.js.map +1 -1
- package/build-module/media-text/media-container.native.js +2 -1
- package/build-module/media-text/media-container.native.js.map +1 -1
- package/build-module/missing/edit.native.js +8 -6
- package/build-module/missing/edit.native.js.map +1 -1
- package/build-module/navigation/constants.js +8 -3
- package/build-module/navigation/constants.js.map +1 -1
- package/build-module/navigation/edit/index.js +17 -3
- package/build-module/navigation/edit/index.js.map +1 -1
- package/build-module/navigation/edit/inner-blocks.js +2 -2
- package/build-module/navigation/edit/inner-blocks.js.map +1 -1
- package/build-module/navigation/edit/menu-inspector-controls.js +1 -1
- package/build-module/navigation/edit/menu-inspector-controls.js.map +1 -1
- package/build-module/navigation/edit/navigation-menu-selector.js +4 -4
- package/build-module/navigation/edit/navigation-menu-selector.js.map +1 -1
- package/build-module/navigation/edit/unsaved-inner-blocks.js +2 -2
- package/build-module/navigation/edit/unsaved-inner-blocks.js.map +1 -1
- package/build-module/navigation/edit/use-convert-classic-menu-to-block-menu.js +10 -14
- package/build-module/navigation/edit/use-convert-classic-menu-to-block-menu.js.map +1 -1
- package/build-module/navigation/index.js +2 -3
- package/build-module/navigation/index.js.map +1 -1
- package/build-module/navigation/use-navigation-menu.js +35 -47
- package/build-module/navigation/use-navigation-menu.js.map +1 -1
- package/build-module/navigation/view-interactivity.js +182 -0
- package/build-module/navigation/view-interactivity.js.map +1 -0
- package/build-module/navigation/view-modal.js +120 -0
- package/build-module/navigation/view-modal.js.map +1 -0
- package/build-module/navigation/view.js +72 -163
- package/build-module/navigation/view.js.map +1 -1
- package/build-module/navigation-link/edit.js +12 -7
- package/build-module/navigation-link/edit.js.map +1 -1
- package/build-module/navigation-submenu/edit.js +2 -2
- package/build-module/navigation-submenu/edit.js.map +1 -1
- package/build-module/pattern/edit.js +27 -4
- package/build-module/pattern/edit.js.map +1 -1
- package/build-module/preformatted/index.js +4 -0
- package/build-module/preformatted/index.js.map +1 -1
- package/build-module/query-pagination/edit.js +1 -1
- package/build-module/query-pagination/edit.js.map +1 -1
- package/build-module/query-title/edit.js +44 -3
- package/build-module/query-title/edit.js.map +1 -1
- package/build-module/quote/transforms.js +8 -0
- package/build-module/quote/transforms.js.map +1 -1
- package/build-module/rss/edit.js +3 -0
- package/build-module/rss/edit.js.map +1 -1
- package/build-module/search/edit.js +4 -3
- package/build-module/search/edit.js.map +1 -1
- package/build-module/search/index.js +1 -0
- package/build-module/search/index.js.map +1 -1
- package/build-module/search/view.js +166 -62
- package/build-module/search/view.js.map +1 -1
- package/build-module/site-logo/edit.js +1 -0
- package/build-module/site-logo/edit.js.map +1 -1
- package/build-module/social-link/icons/index.js +1 -0
- package/build-module/social-link/icons/index.js.map +1 -1
- package/build-module/social-link/icons/threads.js +15 -0
- package/build-module/social-link/icons/threads.js.map +1 -0
- package/build-module/social-link/variations.js +8 -1
- package/build-module/social-link/variations.js.map +1 -1
- package/build-module/tag-cloud/edit.js +1 -0
- package/build-module/tag-cloud/edit.js.map +1 -1
- package/build-module/template-part/edit/import-controls.js +2 -2
- package/build-module/template-part/edit/import-controls.js.map +1 -1
- package/build-module/template-part/edit/index.js +1 -1
- package/build-module/template-part/edit/index.js.map +1 -1
- package/build-module/text-columns/edit.js +1 -0
- package/build-module/text-columns/edit.js.map +1 -1
- package/build-style/details/style-rtl.css +4 -2
- package/build-style/details/style.css +4 -2
- package/build-style/footnotes/style-rtl.css +4 -3
- package/build-style/footnotes/style.css +4 -3
- package/build-style/navigation/style-rtl.css +14 -2
- package/build-style/navigation/style.css +14 -2
- package/build-style/preformatted/style-rtl.css +2 -1
- package/build-style/preformatted/style.css +2 -1
- package/build-style/query-pagination/style-rtl.css +4 -2
- package/build-style/query-pagination/style.css +4 -2
- package/build-style/social-links/style-rtl.css +7 -0
- package/build-style/social-links/style.css +7 -0
- package/build-style/style-rtl.css +36 -10
- package/build-style/style.css +36 -10
- package/build-style/video/style-rtl.css +1 -0
- package/build-style/video/style.css +1 -0
- package/package.json +33 -32
- package/src/audio/test/__snapshots__/edit.native.js.snap +118 -33
- package/src/avatar/block.json +3 -0
- package/src/avatar/edit.js +1 -0
- package/src/block/block.json +2 -1
- package/src/block/edit.js +1 -39
- package/src/block/editor.native.scss +2 -2
- package/src/buttons/edit.js +2 -2
- package/src/buttons/test/edit.native.js +4 -0
- package/src/column/edit.native.js +4 -10
- package/src/column/editor.native.scss +0 -4
- package/src/columns/edit.js +1 -0
- package/src/columns/test/edit.native.js +5 -0
- package/src/comment-author-avatar/edit.js +1 -0
- package/src/comment-template/index.php +7 -2
- package/src/cover/block.json +2 -1
- package/src/cover/deprecated.js +2 -0
- package/src/cover/test/edit.native.js +8 -0
- package/src/embed/embed-placeholder.native.js +80 -47
- package/src/embed/styles.native.scss +54 -18
- package/src/embed/test/index.native.js +13 -5
- package/src/file/block.json +1 -2
- package/src/file/index.php +20 -2
- package/src/file/inspector.js +1 -0
- package/src/file/test/__snapshots__/edit.native.js.snap +119 -33
- package/src/file/view-interactivity.js +18 -0
- package/src/file/view.js +5 -14
- package/src/footnotes/block.json +44 -2
- package/src/footnotes/edit.js +33 -2
- package/src/footnotes/format.js +90 -25
- package/src/footnotes/index.js +0 -1
- package/src/footnotes/index.php +207 -0
- package/src/footnotes/style.scss +6 -3
- package/src/gallery/edit.js +42 -38
- package/src/gallery/test/index.native.js +31 -18
- package/src/heading/test/index.native.js +4 -0
- package/src/home-link/index.php +15 -2
- package/src/image/block.json +2 -1
- package/src/image/deprecated.js +109 -3
- package/src/image/image.js +2 -2
- package/src/image/index.php +45 -8
- package/src/image/test/edit.native.js +1 -2
- package/src/index.js +5 -1
- package/src/latest-comments/edit.js +1 -0
- package/src/latest-posts/edit.js +2 -0
- package/src/list/edit.js +6 -4
- package/src/list/test/edit.native.js +134 -33
- package/src/list-item/hooks/use-merge.js +12 -5
- package/src/media-text/media-container.native.js +1 -0
- package/src/missing/edit.native.js +12 -10
- package/src/missing/style.native.scss +19 -12
- package/src/missing/test/__snapshots__/edit.native.js.snap +42 -13
- package/src/navigation/block.json +2 -3
- package/src/navigation/constants.js +12 -6
- package/src/navigation/edit/index.js +29 -1
- package/src/navigation/edit/inner-blocks.js +2 -2
- package/src/navigation/edit/menu-inspector-controls.js +1 -1
- package/src/navigation/edit/navigation-menu-selector.js +8 -4
- package/src/navigation/edit/unsaved-inner-blocks.js +2 -2
- package/src/navigation/edit/use-convert-classic-menu-to-block-menu.js +17 -21
- package/src/navigation/index.php +50 -18
- package/src/navigation/style.scss +27 -8
- package/src/navigation/use-navigation-menu.js +39 -63
- package/src/navigation/view-interactivity.js +196 -0
- package/src/navigation/view-modal.js +127 -0
- package/src/navigation/view.js +96 -185
- package/src/navigation-link/edit.js +61 -47
- package/src/navigation-submenu/edit.js +2 -2
- package/src/paragraph/test/__snapshots__/edit.native.js.snap +1 -0
- package/src/paragraph/test/edit.native.js +81 -35
- package/src/pattern/edit.js +21 -0
- package/src/pattern/index.php +13 -1
- package/src/post-template/index.php +6 -2
- package/src/post-title/index.php +8 -3
- package/src/preformatted/block.json +4 -0
- package/src/preformatted/style.scss +4 -1
- package/src/preformatted/test/__snapshots__/edit.native.js.snap +2 -0
- package/src/pullquote/test/edit.native.js +12 -4
- package/src/query-pagination/edit.js +17 -14
- package/src/query-title/edit.js +48 -6
- package/src/quote/test/__snapshots__/transforms.native.js.snap +10 -0
- package/src/quote/test/edit.native.js +12 -4
- package/src/quote/test/transforms.native.js +5 -1
- package/src/quote/transforms.js +13 -0
- package/src/rss/edit.js +3 -0
- package/src/search/block.json +1 -0
- package/src/search/edit.js +4 -3
- package/src/search/index.php +26 -4
- package/src/search/test/__snapshots__/edit.native.js.snap +70 -0
- package/src/search/view.js +171 -67
- package/src/site-logo/edit.js +1 -0
- package/src/social-link/icons/index.js +1 -0
- package/src/social-link/icons/threads.js +10 -0
- package/src/social-link/index.php +4 -0
- package/src/social-link/socials-with-bg.scss +5 -0
- package/src/social-link/socials-without-bg.scss +4 -0
- package/src/social-link/variations.js +7 -0
- package/src/tag-cloud/edit.js +1 -0
- package/src/template-part/edit/import-controls.js +2 -2
- package/src/template-part/edit/index.js +1 -1
- package/src/template-part/index.php +7 -16
- package/src/text-columns/edit.js +1 -0
- package/src/video/style.scss +1 -0
- package/build/gallery/shared-icon.native.js +0 -38
- package/build/gallery/shared-icon.native.js.map +0 -1
- package/build-module/gallery/shared-icon.native.js +0 -24
- package/build-module/gallery/shared-icon.native.js.map +0 -1
- package/src/gallery/shared-icon.native.js +0 -23
|
@@ -9,7 +9,6 @@ import { __, sprintf } from '@wordpress/i18n';
|
|
|
9
9
|
/**
|
|
10
10
|
* Internal dependencies
|
|
11
11
|
*/
|
|
12
|
-
import useCreateNavigationMenu from './use-create-navigation-menu';
|
|
13
12
|
import menuItemsToBlocks from '../menu-items-to-blocks';
|
|
14
13
|
|
|
15
14
|
export const CLASSIC_MENU_CONVERSION_SUCCESS = 'success';
|
|
@@ -21,15 +20,10 @@ export const CLASSIC_MENU_CONVERSION_IDLE = 'idle';
|
|
|
21
20
|
// do not import the same classic menu twice.
|
|
22
21
|
let classicMenuBeingConvertedId = null;
|
|
23
22
|
|
|
24
|
-
function useConvertClassicToBlockMenu(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*/
|
|
29
|
-
const { create: createNavigationMenu } = useCreateNavigationMenu(
|
|
30
|
-
clientId,
|
|
31
|
-
'draft'
|
|
32
|
-
);
|
|
23
|
+
function useConvertClassicToBlockMenu(
|
|
24
|
+
createNavigationMenu,
|
|
25
|
+
{ throwOnError = false } = {}
|
|
26
|
+
) {
|
|
33
27
|
const registry = useRegistry();
|
|
34
28
|
const { editEntityRecord } = useDispatch( coreStore );
|
|
35
29
|
|
|
@@ -158,19 +152,21 @@ function useConvertClassicToBlockMenu( clientId ) {
|
|
|
158
152
|
classicMenuBeingConvertedId = null;
|
|
159
153
|
|
|
160
154
|
// Rethrow error for debugging.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
155
|
+
if ( throwOnError ) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
sprintf(
|
|
158
|
+
// translators: %s: the name of a menu (e.g. Header navigation).
|
|
159
|
+
__( `Unable to create Navigation Menu "%s".` ),
|
|
160
|
+
menuName
|
|
161
|
+
),
|
|
162
|
+
{
|
|
163
|
+
cause: err,
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
}
|
|
171
167
|
} );
|
|
172
168
|
},
|
|
173
|
-
[ convertClassicMenuToBlockMenu ]
|
|
169
|
+
[ convertClassicMenuToBlockMenu, throwOnError ]
|
|
174
170
|
);
|
|
175
171
|
|
|
176
172
|
return {
|
package/src/navigation/index.php
CHANGED
|
@@ -65,6 +65,9 @@ if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
|
|
|
65
65
|
|
|
66
66
|
return $menu_items_by_parent_id;
|
|
67
67
|
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if ( gutenberg_should_block_use_interactivity_api( 'core/navigation' ) ) {
|
|
68
71
|
|
|
69
72
|
/**
|
|
70
73
|
* Add Interactivity API directives to the navigation-submenu and page-list blocks markup using the Tag Processor
|
|
@@ -138,8 +141,23 @@ if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
|
|
|
138
141
|
}
|
|
139
142
|
return $w->get_updated_html();
|
|
140
143
|
};
|
|
141
|
-
}
|
|
142
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Replaces view script for the Navigation block with version using Interactivity API.
|
|
147
|
+
*
|
|
148
|
+
* @param array $metadata Block metadata as read in via block.json.
|
|
149
|
+
*
|
|
150
|
+
* @return array Filtered block type metadata.
|
|
151
|
+
*/
|
|
152
|
+
function gutenberg_block_core_navigation_update_interactive_view_script( $metadata ) {
|
|
153
|
+
if ( 'core/navigation' === $metadata['name'] ) {
|
|
154
|
+
$metadata['viewScript'] = array( 'file:./view-interactivity.min.js' );
|
|
155
|
+
$metadata['supports']['interactivity'] = true;
|
|
156
|
+
}
|
|
157
|
+
return $metadata;
|
|
158
|
+
}
|
|
159
|
+
add_filter( 'block_type_metadata', 'gutenberg_block_core_navigation_update_interactive_view_script', 10, 1 );
|
|
160
|
+
}
|
|
143
161
|
|
|
144
162
|
|
|
145
163
|
/**
|
|
@@ -653,24 +671,38 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
|
|
653
671
|
$inner_blocks_html .= '</ul>';
|
|
654
672
|
}
|
|
655
673
|
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
$script_handles = $block->block_type->view_script_handles;
|
|
674
|
+
$needed_script_map = array(
|
|
675
|
+
'wp-block-navigation-view' => ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ),
|
|
676
|
+
'wp-block-navigation-view-2' => $is_responsive_menu,
|
|
677
|
+
);
|
|
661
678
|
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
679
|
+
$should_load_view_script = false;
|
|
680
|
+
if ( gutenberg_should_block_use_interactivity_api( 'core/navigation' ) ) {
|
|
681
|
+
// TODO: The script is still loaded even when it isn't needed when the Interactivity API is used.
|
|
682
|
+
$should_load_view_script = count( array_filter( $needed_script_map ) ) > 0;
|
|
683
|
+
} else {
|
|
684
|
+
foreach ( $needed_script_map as $view_script_handle => $is_view_script_needed ) {
|
|
685
|
+
|
|
686
|
+
// If the script already exists, there is no point in removing it from viewScript.
|
|
687
|
+
if ( wp_script_is( $view_script_handle ) ) {
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
$script_handles = $block->block_type->view_script_handles;
|
|
692
|
+
|
|
693
|
+
// If the script is not needed, and it is still in the `view_script_handles`, remove it.
|
|
694
|
+
if ( ! $is_view_script_needed && in_array( $view_script_handle, $script_handles, true ) ) {
|
|
695
|
+
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_script_handle ) );
|
|
696
|
+
}
|
|
697
|
+
// If the script is needed, but it was previously removed, add it again.
|
|
698
|
+
if ( $is_view_script_needed && ! in_array( $view_script_handle, $script_handles, true ) ) {
|
|
699
|
+
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_script_handle ) );
|
|
700
|
+
}
|
|
669
701
|
}
|
|
670
702
|
}
|
|
671
703
|
|
|
672
704
|
// Add directives to the submenu if needed.
|
|
673
|
-
if (
|
|
705
|
+
if ( gutenberg_should_block_use_interactivity_api( 'core/navigation' ) && $has_submenus && $should_load_view_script ) {
|
|
674
706
|
$w = new WP_HTML_Tag_Processor( $inner_blocks_html );
|
|
675
707
|
$inner_blocks_html = block_core_navigation_add_directives_to_submenu( $w, $attributes );
|
|
676
708
|
}
|
|
@@ -718,7 +750,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
|
|
718
750
|
$responsive_container_directives = '';
|
|
719
751
|
$responsive_dialog_directives = '';
|
|
720
752
|
$close_button_directives = '';
|
|
721
|
-
if (
|
|
753
|
+
if ( gutenberg_should_block_use_interactivity_api( 'core/navigation' ) && $should_load_view_script ) {
|
|
722
754
|
$nav_element_directives = '
|
|
723
755
|
data-wp-interactive
|
|
724
756
|
data-wp-context=\'{ "core": { "navigation": { "overlayOpenedBy": {}, "type": "overlay", "roleAttribute": "" } } }\'
|
|
@@ -746,11 +778,11 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
|
|
746
778
|
}
|
|
747
779
|
|
|
748
780
|
$responsive_container_markup = sprintf(
|
|
749
|
-
'<button aria-haspopup="true" %3$s class="%6$s" %11$s>%9$s</button>
|
|
781
|
+
'<button aria-haspopup="true" %3$s class="%6$s" data-micromodal-trigger="%1$s" %11$s>%9$s</button>
|
|
750
782
|
<div class="%5$s" style="%7$s" id="%1$s" %12$s>
|
|
751
|
-
<div class="wp-block-navigation__responsive-close" tabindex="-1">
|
|
783
|
+
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
|
|
752
784
|
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s" %13$s>
|
|
753
|
-
<button %4$s class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
|
|
785
|
+
<button %4$s data-micromodal-close class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
|
|
754
786
|
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
|
|
755
787
|
%2$s
|
|
756
788
|
</div>
|
|
@@ -31,6 +31,7 @@ $navigation-icon-size: 24px;
|
|
|
31
31
|
|
|
32
32
|
// Menu item container.
|
|
33
33
|
.wp-block-navigation-item {
|
|
34
|
+
background-color: inherit;
|
|
34
35
|
display: flex;
|
|
35
36
|
align-items: center;
|
|
36
37
|
position: relative;
|
|
@@ -398,15 +399,26 @@ button.wp-block-navigation-item__content {
|
|
|
398
399
|
}
|
|
399
400
|
|
|
400
401
|
// Default background and font color.
|
|
401
|
-
.wp-block-navigation:not(.has-background) {
|
|
402
|
+
.wp-block-navigation:not(.has-background) .wp-block-navigation__submenu-container {
|
|
403
|
+
// Set a background color for submenus so that they're not transparent.
|
|
404
|
+
// NOTE TO DEVS - if refactoring this code, please double-check that
|
|
405
|
+
// submenus have a default background color, this feature has regressed
|
|
406
|
+
// several times, so care needs to be taken.
|
|
407
|
+
background-color: #fff;
|
|
408
|
+
border: 1px solid rgba(0, 0, 0, 0.15);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// If we do have a background color selected, inherit it from the navigation block
|
|
412
|
+
.wp-block-navigation.has-background {
|
|
402
413
|
.wp-block-navigation__submenu-container {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
414
|
+
background-color: inherit;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
.wp-block-navigation:not(.has-text-color) {
|
|
419
|
+
.wp-block-navigation__submenu-container {
|
|
420
|
+
// Set a default color for submenu text if none is selected
|
|
408
421
|
color: #000;
|
|
409
|
-
border: 1px solid rgba(0, 0, 0, 0.15);
|
|
410
422
|
}
|
|
411
423
|
}
|
|
412
424
|
|
|
@@ -458,7 +470,8 @@ button.wp-block-navigation-item__content {
|
|
|
458
470
|
right: 0;
|
|
459
471
|
bottom: 0;
|
|
460
472
|
|
|
461
|
-
|
|
473
|
+
// Low specificity so that themes can override.
|
|
474
|
+
& :where(.wp-block-navigation-item a) {
|
|
462
475
|
color: inherit;
|
|
463
476
|
}
|
|
464
477
|
|
|
@@ -579,6 +592,7 @@ button.wp-block-navigation-item__content {
|
|
|
579
592
|
// Remove background colors for items inside the overlay menu.
|
|
580
593
|
// Has to be !important to override global styles.
|
|
581
594
|
.wp-block-navigation-item .wp-block-navigation__submenu-container,
|
|
595
|
+
.wp-block-navigation__container,
|
|
582
596
|
.wp-block-navigation-item,
|
|
583
597
|
.wp-block-page-list {
|
|
584
598
|
color: inherit !important;
|
|
@@ -620,9 +634,14 @@ button.wp-block-navigation-item__content {
|
|
|
620
634
|
.wp-block-navigation:not(.has-background)
|
|
621
635
|
.wp-block-navigation__responsive-container.is-menu-open {
|
|
622
636
|
background-color: #fff;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
.wp-block-navigation:not(.has-text-color)
|
|
640
|
+
.wp-block-navigation__responsive-container.is-menu-open {
|
|
623
641
|
color: #000;
|
|
624
642
|
}
|
|
625
643
|
|
|
644
|
+
|
|
626
645
|
// Overlay menu toggle button label
|
|
627
646
|
.wp-block-navigation__toggle_button_label {
|
|
628
647
|
font-size: 1rem;
|
|
@@ -4,85 +4,61 @@
|
|
|
4
4
|
import {
|
|
5
5
|
store as coreStore,
|
|
6
6
|
useResourcePermissions,
|
|
7
|
+
useEntityRecords,
|
|
7
8
|
} from '@wordpress/core-data';
|
|
8
9
|
import { useSelect } from '@wordpress/data';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Internal dependencies
|
|
12
13
|
*/
|
|
13
|
-
import {
|
|
14
|
+
import { PRELOADED_NAVIGATION_MENUS_QUERY } from './constants';
|
|
14
15
|
|
|
15
16
|
export default function useNavigationMenu( ref ) {
|
|
16
17
|
const permissions = useResourcePermissions( 'navigation', ref );
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
const {
|
|
20
|
+
navigationMenu,
|
|
21
|
+
isNavigationMenuResolved,
|
|
22
|
+
isNavigationMenuMissing,
|
|
23
|
+
} = useSelect(
|
|
19
24
|
( select ) => {
|
|
20
|
-
|
|
21
|
-
canCreate,
|
|
22
|
-
canUpdate,
|
|
23
|
-
canDelete,
|
|
24
|
-
isResolving,
|
|
25
|
-
hasResolved,
|
|
26
|
-
} = permissions;
|
|
27
|
-
|
|
28
|
-
const {
|
|
29
|
-
navigationMenus,
|
|
30
|
-
isResolvingNavigationMenus,
|
|
31
|
-
hasResolvedNavigationMenus,
|
|
32
|
-
} = selectNavigationMenus( select );
|
|
33
|
-
|
|
34
|
-
const {
|
|
35
|
-
navigationMenu,
|
|
36
|
-
isNavigationMenuResolved,
|
|
37
|
-
isNavigationMenuMissing,
|
|
38
|
-
} = selectExistingMenu( select, ref );
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
navigationMenus,
|
|
42
|
-
isResolvingNavigationMenus,
|
|
43
|
-
hasResolvedNavigationMenus,
|
|
44
|
-
|
|
45
|
-
navigationMenu,
|
|
46
|
-
isNavigationMenuResolved,
|
|
47
|
-
isNavigationMenuMissing,
|
|
48
|
-
|
|
49
|
-
canSwitchNavigationMenu: ref
|
|
50
|
-
? navigationMenus?.length > 1
|
|
51
|
-
: navigationMenus?.length > 0,
|
|
52
|
-
|
|
53
|
-
canUserCreateNavigationMenu: canCreate,
|
|
54
|
-
isResolvingCanUserCreateNavigationMenu: isResolving,
|
|
55
|
-
hasResolvedCanUserCreateNavigationMenu: hasResolved,
|
|
56
|
-
|
|
57
|
-
canUserUpdateNavigationMenu: canUpdate,
|
|
58
|
-
hasResolvedCanUserUpdateNavigationMenu: ref
|
|
59
|
-
? hasResolved
|
|
60
|
-
: undefined,
|
|
61
|
-
|
|
62
|
-
canUserDeleteNavigationMenu: canDelete,
|
|
63
|
-
hasResolvedCanUserDeleteNavigationMenu: ref
|
|
64
|
-
? hasResolved
|
|
65
|
-
: undefined,
|
|
66
|
-
};
|
|
25
|
+
return selectExistingMenu( select, ref );
|
|
67
26
|
},
|
|
68
|
-
[ ref
|
|
27
|
+
[ ref ]
|
|
69
28
|
);
|
|
70
|
-
}
|
|
71
29
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
30
|
+
const { canCreate, canUpdate, canDelete, isResolving, hasResolved } =
|
|
31
|
+
permissions;
|
|
32
|
+
|
|
33
|
+
const {
|
|
34
|
+
records: navigationMenus,
|
|
35
|
+
isResolving: isResolvingNavigationMenus,
|
|
36
|
+
hasResolved: hasResolvedNavigationMenus,
|
|
37
|
+
} = useEntityRecords(
|
|
38
|
+
'postType',
|
|
39
|
+
`wp_navigation`,
|
|
40
|
+
PRELOADED_NAVIGATION_MENUS_QUERY
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const canSwitchNavigationMenu = ref
|
|
44
|
+
? navigationMenus?.length > 1
|
|
45
|
+
: navigationMenus?.length > 0;
|
|
75
46
|
|
|
76
47
|
return {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
hasResolvedNavigationMenus
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
48
|
+
navigationMenu,
|
|
49
|
+
isNavigationMenuResolved,
|
|
50
|
+
isNavigationMenuMissing,
|
|
51
|
+
navigationMenus,
|
|
52
|
+
isResolvingNavigationMenus,
|
|
53
|
+
hasResolvedNavigationMenus,
|
|
54
|
+
canSwitchNavigationMenu,
|
|
55
|
+
canUserCreateNavigationMenu: canCreate,
|
|
56
|
+
isResolvingCanUserCreateNavigationMenu: isResolving,
|
|
57
|
+
hasResolvedCanUserCreateNavigationMenu: hasResolved,
|
|
58
|
+
canUserUpdateNavigationMenu: canUpdate,
|
|
59
|
+
hasResolvedCanUserUpdateNavigationMenu: ref ? hasResolved : undefined,
|
|
60
|
+
canUserDeleteNavigationMenu: canDelete,
|
|
61
|
+
hasResolvedCanUserDeleteNavigationMenu: ref ? hasResolved : undefined,
|
|
86
62
|
};
|
|
87
63
|
}
|
|
88
64
|
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { store as wpStore } from '@wordpress/interactivity';
|
|
5
|
+
|
|
6
|
+
const focusableSelectors = [
|
|
7
|
+
'a[href]',
|
|
8
|
+
'input:not([disabled]):not([type="hidden"]):not([aria-hidden])',
|
|
9
|
+
'select:not([disabled]):not([aria-hidden])',
|
|
10
|
+
'textarea:not([disabled]):not([aria-hidden])',
|
|
11
|
+
'button:not([disabled]):not([aria-hidden])',
|
|
12
|
+
'[contenteditable]',
|
|
13
|
+
'[tabindex]:not([tabindex^="-"])',
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const openMenu = ( store, menuOpenedOn ) => {
|
|
17
|
+
const { context, ref, selectors } = store;
|
|
18
|
+
selectors.core.navigation.menuOpenedBy( store )[ menuOpenedOn ] = true;
|
|
19
|
+
context.core.navigation.previousFocus = ref;
|
|
20
|
+
if ( context.core.navigation.type === 'overlay' ) {
|
|
21
|
+
// Add a `has-modal-open` class to the <html> root.
|
|
22
|
+
document.documentElement.classList.add( 'has-modal-open' );
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const closeMenu = ( store, menuClosedOn ) => {
|
|
27
|
+
const { context, selectors } = store;
|
|
28
|
+
selectors.core.navigation.menuOpenedBy( store )[ menuClosedOn ] = false;
|
|
29
|
+
// Check if the menu is still open or not.
|
|
30
|
+
if ( ! selectors.core.navigation.isMenuOpen( store ) ) {
|
|
31
|
+
if (
|
|
32
|
+
context.core.navigation.modal?.contains(
|
|
33
|
+
window.document.activeElement
|
|
34
|
+
)
|
|
35
|
+
) {
|
|
36
|
+
context.core.navigation.previousFocus.focus();
|
|
37
|
+
}
|
|
38
|
+
context.core.navigation.modal = null;
|
|
39
|
+
context.core.navigation.previousFocus = null;
|
|
40
|
+
if ( context.core.navigation.type === 'overlay' ) {
|
|
41
|
+
document.documentElement.classList.remove( 'has-modal-open' );
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
wpStore( {
|
|
47
|
+
effects: {
|
|
48
|
+
core: {
|
|
49
|
+
navigation: {
|
|
50
|
+
initMenu: ( store ) => {
|
|
51
|
+
const { context, selectors, ref } = store;
|
|
52
|
+
if ( selectors.core.navigation.isMenuOpen( store ) ) {
|
|
53
|
+
const focusableElements =
|
|
54
|
+
ref.querySelectorAll( focusableSelectors );
|
|
55
|
+
context.core.navigation.modal = ref;
|
|
56
|
+
context.core.navigation.firstFocusableElement =
|
|
57
|
+
focusableElements[ 0 ];
|
|
58
|
+
context.core.navigation.lastFocusableElement =
|
|
59
|
+
focusableElements[ focusableElements.length - 1 ];
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
focusFirstElement: ( store ) => {
|
|
63
|
+
const { selectors, ref } = store;
|
|
64
|
+
if ( selectors.core.navigation.isMenuOpen( store ) ) {
|
|
65
|
+
ref.querySelector(
|
|
66
|
+
'.wp-block-navigation-item > *:first-child'
|
|
67
|
+
).focus();
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
selectors: {
|
|
74
|
+
core: {
|
|
75
|
+
navigation: {
|
|
76
|
+
roleAttribute: ( store ) => {
|
|
77
|
+
const { context, selectors } = store;
|
|
78
|
+
return context.core.navigation.type === 'overlay' &&
|
|
79
|
+
selectors.core.navigation.isMenuOpen( store )
|
|
80
|
+
? 'dialog'
|
|
81
|
+
: '';
|
|
82
|
+
},
|
|
83
|
+
isMenuOpen: ( { context } ) =>
|
|
84
|
+
// The menu is opened if either `click`, `hover` or `focus` is true.
|
|
85
|
+
Object.values(
|
|
86
|
+
context.core.navigation[
|
|
87
|
+
context.core.navigation.type === 'overlay'
|
|
88
|
+
? 'overlayOpenedBy'
|
|
89
|
+
: 'submenuOpenedBy'
|
|
90
|
+
]
|
|
91
|
+
).filter( Boolean ).length > 0,
|
|
92
|
+
menuOpenedBy: ( { context } ) =>
|
|
93
|
+
context.core.navigation[
|
|
94
|
+
context.core.navigation.type === 'overlay'
|
|
95
|
+
? 'overlayOpenedBy'
|
|
96
|
+
: 'submenuOpenedBy'
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
actions: {
|
|
102
|
+
core: {
|
|
103
|
+
navigation: {
|
|
104
|
+
openMenuOnHover( store ) {
|
|
105
|
+
const { navigation } = store.context.core;
|
|
106
|
+
if (
|
|
107
|
+
navigation.type === 'submenu' &&
|
|
108
|
+
// Only open on hover if the overlay is closed.
|
|
109
|
+
Object.values(
|
|
110
|
+
navigation.overlayOpenedBy || {}
|
|
111
|
+
).filter( Boolean ).length === 0
|
|
112
|
+
)
|
|
113
|
+
openMenu( store, 'hover' );
|
|
114
|
+
},
|
|
115
|
+
closeMenuOnHover( store ) {
|
|
116
|
+
closeMenu( store, 'hover' );
|
|
117
|
+
},
|
|
118
|
+
openMenuOnClick( store ) {
|
|
119
|
+
openMenu( store, 'click' );
|
|
120
|
+
},
|
|
121
|
+
closeMenuOnClick( store ) {
|
|
122
|
+
closeMenu( store, 'click' );
|
|
123
|
+
closeMenu( store, 'focus' );
|
|
124
|
+
},
|
|
125
|
+
openMenuOnFocus( store ) {
|
|
126
|
+
openMenu( store, 'focus' );
|
|
127
|
+
},
|
|
128
|
+
toggleMenuOnClick: ( store ) => {
|
|
129
|
+
const { selectors } = store;
|
|
130
|
+
const menuOpenedBy =
|
|
131
|
+
selectors.core.navigation.menuOpenedBy( store );
|
|
132
|
+
if ( menuOpenedBy.click || menuOpenedBy.focus ) {
|
|
133
|
+
closeMenu( store, 'click' );
|
|
134
|
+
closeMenu( store, 'focus' );
|
|
135
|
+
} else {
|
|
136
|
+
openMenu( store, 'click' );
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
handleMenuKeydown: ( store ) => {
|
|
140
|
+
const { context, selectors, event } = store;
|
|
141
|
+
if (
|
|
142
|
+
selectors.core.navigation.menuOpenedBy( store ).click
|
|
143
|
+
) {
|
|
144
|
+
// If Escape close the menu.
|
|
145
|
+
if ( event?.key === 'Escape' ) {
|
|
146
|
+
closeMenu( store, 'click' );
|
|
147
|
+
closeMenu( store, 'focus' );
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Trap focus if it is an overlay (main menu).
|
|
152
|
+
if (
|
|
153
|
+
context.core.navigation.type === 'overlay' &&
|
|
154
|
+
event.key === 'Tab'
|
|
155
|
+
) {
|
|
156
|
+
// If shift + tab it change the direction.
|
|
157
|
+
if (
|
|
158
|
+
event.shiftKey &&
|
|
159
|
+
window.document.activeElement ===
|
|
160
|
+
context.core.navigation
|
|
161
|
+
.firstFocusableElement
|
|
162
|
+
) {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
context.core.navigation.lastFocusableElement.focus();
|
|
165
|
+
} else if (
|
|
166
|
+
! event.shiftKey &&
|
|
167
|
+
window.document.activeElement ===
|
|
168
|
+
context.core.navigation.lastFocusableElement
|
|
169
|
+
) {
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
context.core.navigation.firstFocusableElement.focus();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
handleMenuFocusout: ( store ) => {
|
|
177
|
+
const { context, event } = store;
|
|
178
|
+
// If focus is outside modal, and in the document, close menu
|
|
179
|
+
// event.target === The element losing focus
|
|
180
|
+
// event.relatedTarget === The element receiving focus (if any)
|
|
181
|
+
// When focusout is outsite the document,
|
|
182
|
+
// `window.document.activeElement` doesn't change.
|
|
183
|
+
if (
|
|
184
|
+
! context.core.navigation.modal?.contains(
|
|
185
|
+
event.relatedTarget
|
|
186
|
+
) &&
|
|
187
|
+
event.target !== window.document.activeElement
|
|
188
|
+
) {
|
|
189
|
+
closeMenu( store, 'click' );
|
|
190
|
+
closeMenu( store, 'focus' );
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
} );
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/*eslint-env browser*/
|
|
2
|
+
/**
|
|
3
|
+
* External dependencies
|
|
4
|
+
*/
|
|
5
|
+
import MicroModal from 'micromodal';
|
|
6
|
+
|
|
7
|
+
// Responsive navigation toggle.
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Toggles responsive navigation.
|
|
11
|
+
*
|
|
12
|
+
* @param {HTMLDivElement} modal
|
|
13
|
+
* @param {boolean} isHidden
|
|
14
|
+
*/
|
|
15
|
+
function navigationToggleModal( modal, isHidden ) {
|
|
16
|
+
const dialogContainer = modal.querySelector(
|
|
17
|
+
`.wp-block-navigation__responsive-dialog`
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
modal.classList.toggle( 'has-modal-open', ! isHidden );
|
|
21
|
+
dialogContainer.toggleAttribute( 'aria-modal', ! isHidden );
|
|
22
|
+
|
|
23
|
+
if ( isHidden ) {
|
|
24
|
+
dialogContainer.removeAttribute( 'role' );
|
|
25
|
+
dialogContainer.removeAttribute( 'aria-modal' );
|
|
26
|
+
} else {
|
|
27
|
+
dialogContainer.setAttribute( 'role', 'dialog' );
|
|
28
|
+
dialogContainer.setAttribute( 'aria-modal', 'true' );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Add a class to indicate the modal is open.
|
|
32
|
+
document.documentElement.classList.toggle( 'has-modal-open' );
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Checks whether the provided link is an anchor on the current page.
|
|
37
|
+
*
|
|
38
|
+
* @param {HTMLAnchorElement} node
|
|
39
|
+
* @return {boolean} Is anchor.
|
|
40
|
+
*/
|
|
41
|
+
function isLinkToAnchorOnCurrentPage( node ) {
|
|
42
|
+
return (
|
|
43
|
+
node.hash &&
|
|
44
|
+
node.protocol === window.location.protocol &&
|
|
45
|
+
node.host === window.location.host &&
|
|
46
|
+
node.pathname === window.location.pathname &&
|
|
47
|
+
node.search === window.location.search
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Handles effects after opening the modal.
|
|
53
|
+
*
|
|
54
|
+
* @param {HTMLDivElement} modal
|
|
55
|
+
*/
|
|
56
|
+
function onShow( modal ) {
|
|
57
|
+
navigationToggleModal( modal, false );
|
|
58
|
+
modal.addEventListener( 'click', handleAnchorLinkClicksInsideModal, {
|
|
59
|
+
passive: true,
|
|
60
|
+
} );
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Handles effects after closing the modal.
|
|
65
|
+
*
|
|
66
|
+
* @param {HTMLDivElement} modal
|
|
67
|
+
*/
|
|
68
|
+
function onClose( modal ) {
|
|
69
|
+
navigationToggleModal( modal, true );
|
|
70
|
+
modal.removeEventListener( 'click', handleAnchorLinkClicksInsideModal, {
|
|
71
|
+
passive: true,
|
|
72
|
+
} );
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Handle clicks to anchor links in modal using event delegation by closing modal automatically
|
|
77
|
+
*
|
|
78
|
+
* @param {UIEvent} event
|
|
79
|
+
*/
|
|
80
|
+
function handleAnchorLinkClicksInsideModal( event ) {
|
|
81
|
+
const link = event.target.closest( '.wp-block-navigation-item__content' );
|
|
82
|
+
if ( ! ( link instanceof HTMLAnchorElement ) ) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Ignore non-anchor links and anchor links which open on a new tab.
|
|
87
|
+
if (
|
|
88
|
+
! isLinkToAnchorOnCurrentPage( link ) ||
|
|
89
|
+
link.attributes?.target === '_blank'
|
|
90
|
+
) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Find the specific parent modal for this link
|
|
95
|
+
// since .close() won't work without an ID if there are
|
|
96
|
+
// multiple navigation menus in a post/page.
|
|
97
|
+
const modal = link.closest( '.wp-block-navigation__responsive-container' );
|
|
98
|
+
const modalId = modal?.getAttribute( 'id' );
|
|
99
|
+
if ( ! modalId ) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// check if modal exists and is open before trying to close it
|
|
104
|
+
// otherwise Micromodal will toggle the `has-modal-open` class
|
|
105
|
+
// on the html tag which prevents scrolling
|
|
106
|
+
if ( modalId && modal.classList.contains( 'has-modal-open' ) ) {
|
|
107
|
+
MicroModal.close( modalId );
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// MicroModal.init() does not support event delegation for the open trigger, so here MicroModal.show() is called manually.
|
|
112
|
+
document.addEventListener(
|
|
113
|
+
'click',
|
|
114
|
+
( event ) => {
|
|
115
|
+
/** @type {HTMLElement} */
|
|
116
|
+
const target = event.target;
|
|
117
|
+
|
|
118
|
+
if ( target.dataset.micromodalTrigger ) {
|
|
119
|
+
MicroModal.show( target.dataset.micromodalTrigger, {
|
|
120
|
+
onShow,
|
|
121
|
+
onClose,
|
|
122
|
+
openClass: 'is-menu-open',
|
|
123
|
+
} );
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
{ passive: true }
|
|
127
|
+
);
|