2023-09-05 12:37:38 +00:00
|
|
|
import { useContext, useMemo, useEffect, useLayoutEffect } from 'preact/hooks';
|
2023-02-20 16:48:33 +00:00
|
|
|
import { deepSignal, peek } from 'deepsignal';
|
2023-06-27 10:22:12 +00:00
|
|
|
import { useSignalEffect } from './utils';
|
2023-01-26 11:39:25 +00:00
|
|
|
import { directive } from './hooks';
|
2023-08-03 08:55:13 +00:00
|
|
|
import { prefetch, navigate } from './router';
|
2023-01-26 11:39:25 +00:00
|
|
|
|
2023-02-20 16:48:33 +00:00
|
|
|
const isObject = ( item ) =>
|
|
|
|
item && typeof item === 'object' && ! Array.isArray( item );
|
|
|
|
|
|
|
|
const mergeDeepSignals = ( target, source ) => {
|
|
|
|
for ( const k in source ) {
|
|
|
|
if ( typeof peek( target, k ) === 'undefined' ) {
|
|
|
|
target[ `$${ k }` ] = source[ `$${ k }` ];
|
|
|
|
} else if (
|
|
|
|
isObject( peek( target, k ) ) &&
|
|
|
|
isObject( peek( source, k ) )
|
|
|
|
) {
|
|
|
|
mergeDeepSignals(
|
|
|
|
target[ `$${ k }` ].peek(),
|
|
|
|
source[ `$${ k }` ].peek()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-26 11:39:25 +00:00
|
|
|
export default () => {
|
2023-06-27 10:22:12 +00:00
|
|
|
// data-wc-context
|
2023-01-26 11:39:25 +00:00
|
|
|
directive(
|
|
|
|
'context',
|
|
|
|
( {
|
|
|
|
directives: {
|
|
|
|
context: { default: context },
|
|
|
|
},
|
|
|
|
props: { children },
|
2023-02-20 16:48:33 +00:00
|
|
|
context: inherited,
|
2023-01-26 11:39:25 +00:00
|
|
|
} ) => {
|
2023-02-20 16:48:33 +00:00
|
|
|
const { Provider } = inherited;
|
|
|
|
const inheritedValue = useContext( inherited );
|
|
|
|
const value = useMemo( () => {
|
|
|
|
const localValue = deepSignal( context );
|
|
|
|
mergeDeepSignals( localValue, inheritedValue );
|
|
|
|
return localValue;
|
|
|
|
}, [ context, inheritedValue ] );
|
|
|
|
|
|
|
|
return <Provider value={ value }>{ children }</Provider>;
|
2023-06-27 10:22:12 +00:00
|
|
|
},
|
|
|
|
{ priority: 5 }
|
2023-01-26 11:39:25 +00:00
|
|
|
);
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
// data-wc-effect--[name]
|
2023-01-26 11:39:25 +00:00
|
|
|
directive( 'effect', ( { directives: { effect }, context, evaluate } ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
Object.values( effect ).forEach( ( path ) => {
|
|
|
|
useSignalEffect( () => {
|
2023-06-27 10:22:12 +00:00
|
|
|
return evaluate( path, { context: contextValue } );
|
2023-01-26 11:39:25 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2023-09-05 12:37:38 +00:00
|
|
|
// data-wc-layout-init--[name]
|
|
|
|
directive(
|
|
|
|
'layout-init',
|
|
|
|
( {
|
|
|
|
directives: { 'layout-init': layoutInit },
|
|
|
|
context,
|
|
|
|
evaluate,
|
|
|
|
} ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
Object.values( layoutInit ).forEach( ( path ) => {
|
|
|
|
useLayoutEffect( () => {
|
|
|
|
return evaluate( path, { context: contextValue } );
|
|
|
|
}, [] );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-10-27 01:59:57 +00:00
|
|
|
// data-wc-init--[name]
|
|
|
|
directive( 'init', ( { directives: { init }, context, evaluate } ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
Object.values( init ).forEach( ( path ) => {
|
|
|
|
useEffect( () => {
|
|
|
|
return evaluate( path, { context: contextValue } );
|
|
|
|
}, [] );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
// data-wc-on--[event]
|
2023-01-26 11:39:25 +00:00
|
|
|
directive( 'on', ( { directives: { on }, element, evaluate, context } ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
Object.entries( on ).forEach( ( [ name, path ] ) => {
|
|
|
|
element.props[ `on${ name }` ] = ( event ) => {
|
|
|
|
evaluate( path, { event, context: contextValue } );
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
// data-wc-class--[classname]
|
2023-01-26 11:39:25 +00:00
|
|
|
directive(
|
|
|
|
'class',
|
|
|
|
( {
|
|
|
|
directives: { class: className },
|
|
|
|
element,
|
|
|
|
evaluate,
|
|
|
|
context,
|
|
|
|
} ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
Object.keys( className )
|
|
|
|
.filter( ( n ) => n !== 'default' )
|
|
|
|
.forEach( ( name ) => {
|
|
|
|
const result = evaluate( className[ name ], {
|
|
|
|
className: name,
|
|
|
|
context: contextValue,
|
|
|
|
} );
|
2023-02-28 16:33:33 +00:00
|
|
|
const currentClass = element.props.class || '';
|
|
|
|
const classFinder = new RegExp(
|
|
|
|
`(^|\\s)${ name }(\\s|$)`,
|
|
|
|
'g'
|
|
|
|
);
|
2023-01-26 11:39:25 +00:00
|
|
|
if ( ! result )
|
2023-02-28 16:33:33 +00:00
|
|
|
element.props.class = currentClass
|
|
|
|
.replace( classFinder, ' ' )
|
2023-01-26 11:39:25 +00:00
|
|
|
.trim();
|
2023-02-28 16:33:33 +00:00
|
|
|
else if ( ! classFinder.test( currentClass ) )
|
|
|
|
element.props.class = currentClass
|
|
|
|
? `${ currentClass } ${ name }`
|
|
|
|
: name;
|
2023-01-26 11:39:25 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
// This seems necessary because Preact doesn't change the class names
|
|
|
|
// on the hydration, so we have to do it manually. It doesn't need
|
|
|
|
// deps because it only needs to do it the first time.
|
|
|
|
if ( ! result ) {
|
|
|
|
element.ref.current.classList.remove( name );
|
|
|
|
} else {
|
|
|
|
element.ref.current.classList.add( name );
|
|
|
|
}
|
|
|
|
}, [] );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
// data-wc-bind--[attribute]
|
2023-01-26 11:39:25 +00:00
|
|
|
directive(
|
|
|
|
'bind',
|
|
|
|
( { directives: { bind }, element, context, evaluate } ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
Object.entries( bind )
|
|
|
|
.filter( ( n ) => n !== 'default' )
|
|
|
|
.forEach( ( [ attribute, path ] ) => {
|
2023-06-27 10:22:12 +00:00
|
|
|
const result = evaluate( path, {
|
2023-01-26 11:39:25 +00:00
|
|
|
context: contextValue,
|
|
|
|
} );
|
2023-06-27 10:22:12 +00:00
|
|
|
element.props[ attribute ] = result;
|
|
|
|
|
|
|
|
// This seems necessary because Preact doesn't change the attributes
|
|
|
|
// on the hydration, so we have to do it manually. It doesn't need
|
|
|
|
// deps because it only needs to do it the first time.
|
|
|
|
useEffect( () => {
|
|
|
|
// aria- and data- attributes have no boolean representation.
|
|
|
|
// A `false` value is different from the attribute not being
|
|
|
|
// present, so we can't remove it.
|
|
|
|
// We follow Preact's logic: https://github.com/preactjs/preact/blob/ea49f7a0f9d1ff2c98c0bdd66aa0cbc583055246/src/diff/props.js#L131C24-L136
|
|
|
|
if ( result === false && attribute[ 4 ] !== '-' ) {
|
|
|
|
element.ref.current.removeAttribute( attribute );
|
|
|
|
} else {
|
|
|
|
element.ref.current.setAttribute(
|
|
|
|
attribute,
|
|
|
|
result === true && attribute[ 4 ] !== '-'
|
|
|
|
? ''
|
|
|
|
: result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [] );
|
2023-01-26 11:39:25 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-08-03 08:55:13 +00:00
|
|
|
// data-wc-navigation-link
|
2023-01-26 11:39:25 +00:00
|
|
|
directive(
|
2023-08-03 08:55:13 +00:00
|
|
|
'navigation-link',
|
2023-01-26 11:39:25 +00:00
|
|
|
( {
|
|
|
|
directives: {
|
2023-08-03 08:55:13 +00:00
|
|
|
'navigation-link': { default: link },
|
2023-01-26 11:39:25 +00:00
|
|
|
},
|
|
|
|
props: { href },
|
|
|
|
element,
|
|
|
|
} ) => {
|
|
|
|
useEffect( () => {
|
|
|
|
// Prefetch the page if it is in the directive options.
|
2023-08-03 08:55:13 +00:00
|
|
|
if ( link?.prefetch ) {
|
2023-01-26 11:39:25 +00:00
|
|
|
prefetch( href );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Don't do anything if it's falsy.
|
2023-08-03 08:55:13 +00:00
|
|
|
if ( link !== false ) {
|
2023-01-26 11:39:25 +00:00
|
|
|
element.props.onclick = async ( event ) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// Fetch the page (or return it from cache).
|
|
|
|
await navigate( href );
|
|
|
|
|
|
|
|
// Update the scroll, depending on the option. True by default.
|
|
|
|
if ( link?.scroll === 'smooth' ) {
|
|
|
|
window.scrollTo( {
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
behavior: 'smooth',
|
|
|
|
} );
|
|
|
|
} else if ( link?.scroll !== false ) {
|
|
|
|
window.scrollTo( 0, 0 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-06-27 10:22:12 +00:00
|
|
|
|
|
|
|
// data-wc-show
|
|
|
|
directive(
|
|
|
|
'show',
|
|
|
|
( {
|
|
|
|
directives: {
|
|
|
|
show: { default: show },
|
|
|
|
},
|
|
|
|
element,
|
|
|
|
evaluate,
|
|
|
|
context,
|
|
|
|
} ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
|
|
|
|
if ( ! evaluate( show, { context: contextValue } ) )
|
|
|
|
element.props.children = (
|
|
|
|
<template>{ element.props.children }</template>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// data-wc-ignore
|
|
|
|
directive(
|
|
|
|
'ignore',
|
|
|
|
( {
|
|
|
|
element: {
|
|
|
|
type: Type,
|
|
|
|
props: { innerHTML, ...rest },
|
|
|
|
},
|
|
|
|
} ) => {
|
|
|
|
// Preserve the initial inner HTML.
|
|
|
|
const cached = useMemo( () => innerHTML, [] );
|
|
|
|
return (
|
|
|
|
<Type
|
|
|
|
dangerouslySetInnerHTML={ { __html: cached } }
|
|
|
|
{ ...rest }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// data-wc-text
|
|
|
|
directive(
|
|
|
|
'text',
|
|
|
|
( {
|
|
|
|
directives: {
|
|
|
|
text: { default: text },
|
|
|
|
},
|
|
|
|
element,
|
|
|
|
evaluate,
|
|
|
|
context,
|
|
|
|
} ) => {
|
|
|
|
const contextValue = useContext( context );
|
|
|
|
element.props.children = evaluate( text, {
|
|
|
|
context: contextValue,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
2023-01-26 11:39:25 +00:00
|
|
|
};
|