From 22c3f1af60f01c373bd0c4ad92c1f5e5c13661a8 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Tue, 27 Sep 2022 17:07:07 +0200 Subject: [PATCH 01/27] First WordPress directives Co-authored-by: Mario Santos Co-authored-by: David Arenas --- packages/block-library/package.json | 1 + .../block-library/src/navigation/block.json | 2 +- .../src/navigation/directives.js | 39 +++++ .../block-library/src/navigation/index.php | 10 +- .../block-library/src/navigation/utils.js | 24 ++++ packages/block-library/src/navigation/vdom.js | 43 ++++++ packages/block-library/src/navigation/view.js | 134 +++++++++--------- 7 files changed, 180 insertions(+), 73 deletions(-) create mode 100644 packages/block-library/src/navigation/directives.js create mode 100644 packages/block-library/src/navigation/utils.js create mode 100644 packages/block-library/src/navigation/vdom.js diff --git a/packages/block-library/package.json b/packages/block-library/package.json index b99743fd0dcbaf..e2ea3a3e8bec75 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -64,6 +64,7 @@ "lodash": "^4.17.21", "memize": "^1.1.0", "micromodal": "^0.4.10", + "preact": "^10.11.0", "remove-accents": "^0.4.2" }, "peerDependencies": { diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index a8c65b5a88b75b..781251b101aa8d 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -130,7 +130,7 @@ } } }, - "viewScript": [ "file:./view.min.js", "file:./view-modal.min.js" ], + "viewScript": [ "file:./view.min.js" ], "editorStyle": "wp-block-navigation-editor", "style": "wp-block-navigation" } diff --git a/packages/block-library/src/navigation/directives.js b/packages/block-library/src/navigation/directives.js new file mode 100644 index 00000000000000..bd50c663f39e79 --- /dev/null +++ b/packages/block-library/src/navigation/directives.js @@ -0,0 +1,39 @@ +/** + * External dependencies + */ +import { h, options } from 'preact'; + +// WordPress Directives. +const directives = {}; + +// Expose function to add directives. +export const directive = ( name, cb ) => { + directives[ name ] = cb; +}; + +const WpDirective = ( props ) => { + for ( const d in props.wp ) { + directives[ d ]?.( props ); + } + props._wrapped = true; + const { wp, tag, children, ...rest } = props; + return h( tag, rest, children ); +}; + +const old = options.vnode; + +options.vnode = ( vnode ) => { + const wp = vnode.props.wp; + const wrapped = vnode.props._wrapped; + + if ( wp ) { + if ( ! wrapped ) { + vnode.props.tag = vnode.type; + vnode.type = WpDirective; + } + } else if ( wrapped ) { + delete vnode.props._wrapped; + } + + if ( old ) old( vnode ); +}; diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 5173f96040b4d0..4eeab9dd91bea8 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -632,11 +632,11 @@ function render_block_core_navigation( $attributes, $content, $block ) { $toggle_aria_label_close = $should_display_icon_label ? 'aria-label="' . __( 'Close menu' ) . '"' : ''; // Close button label. $responsive_container_markup = sprintf( - ' + '
-
-
- +
+