Skip to content

Commit

Permalink
Rename syntax to -- and data-wp-interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jun 5, 2023
1 parent 8cb572c commit 36d9df6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/interactivity/src/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const directivePrefix = 'data-wp-';
export const directivePrefix = 'wp';
28 changes: 23 additions & 5 deletions packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default () => {
);
} );

// data-wp-effect.[name]
// data-wp-effect--[name]
directive( 'effect', ( { directives: { effect }, context, evaluate } ) => {
const contextValue = useContext( context );
Object.values( effect ).forEach( ( path ) => {
Expand All @@ -74,7 +74,7 @@ export default () => {
} );
} );

// data-wp-init.[name]
// data-wp-init--[name]
directive( 'init', ( { directives: { init }, context, evaluate } ) => {
const contextValue = useContext( context );
Object.values( init ).forEach( ( path ) => {
Expand All @@ -84,7 +84,7 @@ export default () => {
} );
} );

// data-wp-on.[event]
// data-wp-on--[event]
directive( 'on', ( { directives: { on }, element, evaluate, context } ) => {
const contextValue = useContext( context );
Object.entries( on ).forEach( ( [ name, path ] ) => {
Expand All @@ -94,7 +94,7 @@ export default () => {
} );
} );

// data-wp-class.[classname]
// data-wp-class--[classname]
directive(
'class',
( {
Expand Down Expand Up @@ -139,7 +139,7 @@ export default () => {
}
);

// data-wp-bind.[attribute]
// data-wp-bind--[attribute]
directive(
'bind',
( { directives: { bind }, element, context, evaluate } ) => {
Expand Down Expand Up @@ -175,6 +175,24 @@ export default () => {
}
);

// data-wp-text
directive(
'text',
( {
directives: {
text: { default: text },
},
element,
evaluate,
context,
} ) => {
const contextValue = useContext( context );
element.props.children = evaluate( text, {
context: contextValue,
} );
}
);

// data-wp-ignore
directive(
'ignore',
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { directivePrefix } from './constants';

export const init = async () => {
document
.querySelectorAll( `[${ directivePrefix }island]` )
.querySelectorAll( `[data-${ directivePrefix }-interactive]` )
.forEach( ( node ) => {
if ( ! hydratedIslands.has( node ) ) {
const fragment = createRootFragment( node.parentNode, node );
Expand Down
25 changes: 21 additions & 4 deletions packages/interactivity/src/vdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ import { h } from 'preact';
*/
import { directivePrefix as p } from './constants';

const ignoreAttr = `${ p }ignore`;
const islandAttr = `${ p }island`;
const directiveParser = new RegExp( `${ p }([^.]+)\.?(.*)$` );
const ignoreAttr = `data-${ p }-ignore`;
const islandAttr = `data-${ p }-interactive`;
const fullPrefix = `data-${ p }-`;

// Regular expression for directive parsing.
const directiveParser = new RegExp(
`^data-${ p }-` + // ${p} must be a prefix string, like 'wp'.
// Match alphanumeric characters including hyphen-separated
// segments. It excludes underscore intentionally to prevent confusion.
// E.g., "custom-directive".
'([a-z0-9]+(?:-[a-z0-9]+)*)' +
// (Optional) Match '--' followed by any alphanumeric charachters. It
// excludes underscore intentionally to prevent confusion, but it can
// contain multiple hyphens. E.g., "--custom-prefix--with-more-info".
'(?:--([a-z0-9][a-z0-9-]+))?$',
'i' // Case insensitive.
);

export const hydratedIslands = new WeakSet();

Expand Down Expand Up @@ -44,7 +58,10 @@ export function toVdom( root ) {

for ( let i = 0; i < attributes.length; i++ ) {
const n = attributes[ i ].name;
if ( n[ p.length ] && n.slice( 0, p.length ) === p ) {
if (
n[ fullPrefix.length ] &&
n.slice( 0, fullPrefix.length ) === fullPrefix
) {
if ( n === ignoreAttr ) {
ignore = true;
} else if ( n === islandAttr ) {
Expand Down

0 comments on commit 36d9df6

Please sign in to comment.