Skip to content

Commit

Permalink
More WIP typing
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Feb 8, 2024
1 parent bfc90f5 commit e6c6506
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
34 changes: 20 additions & 14 deletions packages/interactivity/src/directives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const isObject = ( item: any ): boolean =>
const mergeDeepSignals = (
target: DeepSignalObject< any >,
source: DeepSignalObject< any >,
overwrite: boolean = false
overwrite?: boolean
) => {
for ( const k in source ) {
if ( isObject( peek( target, k ) ) && isObject( peek( source, k ) ) ) {
Expand Down Expand Up @@ -123,33 +123,39 @@ const getGlobalEventDirective =
} );
};

export default (): void => {
export default () => {
// data-wp-context
directive(
'context',
( {
directives: { context },
props: { children },
context: inheritedContext,
}: DirectiveArgs ): Element => {
} ) => {
const { Provider } = inheritedContext;
const inheritedValue = useContext( inheritedContext );
const currentValue = useRef( deepSignal( {} ) );
const passedValues = context.map( ( { value } ) => value );
const defaultEntry = context.find(
( { suffix } ) => suffix === 'default'
);

currentValue.current = useMemo( () => {
const newValue = context
.map( ( c ) => deepSignal( { [ c.namespace ]: c.value } ) )
.reduceRight( mergeDeepSignals );

if ( ! defaultEntry ) return null;
const { namespace, value } = defaultEntry;
const newValue = deepSignal( { [ namespace ]: value } );
mergeDeepSignals( newValue, inheritedValue );
mergeDeepSignals( currentValue.current, newValue, true );
return currentValue.current;
}, [ inheritedValue, ...passedValues ] );
}, [ inheritedValue, defaultEntry ] );

return (
<Provider value={ currentValue.current }>{ children }</Provider>
);
if ( currentValue.current ) {
return (
<Provider value={ currentValue.current }>
{ children }
</Provider>
);
}
return undefined;
},
{ priority: 5 }
);
Expand Down Expand Up @@ -220,9 +226,9 @@ export default (): void => {
* need deps because it only needs to do it the first time.
*/
if ( ! result ) {
element.ref.current.classList.remove( name );
element.ref!.current.classList.remove( name );
} else {
element.ref.current.classList.add( name );
element.ref!.current.classList.add( name );
}
} );
} );
Expand Down
3 changes: 1 addition & 2 deletions packages/interactivity/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type {
DirectiveCallback,
DirectiveOptions,
DirectivesProps,
GetEvaluate,
GetPriorityLevels,
Scope,
} from './types';
Expand Down Expand Up @@ -205,7 +204,7 @@ const resolve = ( path, namespace ) => {
};

// Generate the evaluate function.
export const getEvaluate: GetEvaluate =
export const getEvaluate: any =
( { scope } ) =>
( entry, ...args ) => {
let { value: path, namespace } = entry;
Expand Down
4 changes: 2 additions & 2 deletions packages/interactivity/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface DirectiveArgs {
/**
* Virtual node representing the element.
*/
element?: Element;
element?: any;
/**
* The inherited context.
*/
Expand All @@ -54,7 +54,7 @@ export interface DirectiveArgs {
}

export interface DirectiveCallback {
( args: DirectiveArgs ): Element | void;
( args: DirectiveArgs ): VNode | void;
}

export interface DirectiveOptions {
Expand Down

0 comments on commit e6c6506

Please sign in to comment.