Skip to content

Commit

Permalink
Move to only one pass
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed May 6, 2024
1 parent 656a2d6 commit d57523d
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,17 @@ const cssStringToObject = ( val ) => {
*/
const getGlobalEventDirective = ( type ) => {
return ( { directives, evaluate } ) => {
const events = new Map();
directives[ `on-${ type }` ]
.filter( ( { suffix } ) => suffix !== 'default' )
.forEach( ( entry ) => {
const event = entry.suffix.split( '--' )[ 0 ];
if ( ! events.has( event ) ) events.set( event, new Set() );
events.get( event ).add( entry );
} );
events.forEach( ( entries, eventType ) => {
entries.forEach( ( entry ) => {
const event = entry.suffix.split( '--', 1 )[ 0 ];
useInit( () => {
const cb = ( event ) => evaluate( entry, event );
const cb = () => evaluate( entry, event );
const globalVar = type === 'window' ? window : document;
globalVar.addEventListener( eventType, cb );
return () => globalVar.removeEventListener( eventType, cb );
globalVar.addEventListener( event, cb );
return () => globalVar.removeEventListener( event, cb );
}, [] );
} );
} );
};
};

Expand Down Expand Up @@ -281,7 +274,9 @@ export default () => {
on.filter( ( { suffix } ) => suffix !== 'default' ).forEach(
( entry ) => {
const event = entry.suffix.split( '--' )[ 0 ];
if ( ! events.has( event ) ) events.set( event, new Set() );
if ( ! events.has( event ) ) {
events.set( event, new Set() );
}
events.get( event ).add( entry );
}
);
Expand Down Expand Up @@ -314,14 +309,15 @@ export default () => {
`(^|\\s)${ className }(\\s|$)`,
'g'
);
if ( ! result )
if ( ! result ) {
element.props.class = currentClass
.replace( classFinder, ' ' )
.trim();
else if ( ! classFinder.test( currentClass ) )
} else if ( ! classFinder.test( currentClass ) ) {
element.props.class = currentClass
? `${ currentClass } ${ className }`
: className;
}

useInit( () => {
/*
Expand All @@ -347,12 +343,16 @@ export default () => {
const styleProp = entry.suffix;
const result = evaluate( entry );
element.props.style = element.props.style || {};
if ( typeof element.props.style === 'string' )
if ( typeof element.props.style === 'string' ) {
element.props.style = cssStringToObject(
element.props.style
);
if ( ! result ) delete element.props.style[ styleProp ];
else element.props.style[ styleProp ] = result;
}
if ( ! result ) {
delete element.props.style[ styleProp ];
} else {
element.props.style[ styleProp ] = result;
}

useInit( () => {
/*
Expand Down Expand Up @@ -391,8 +391,9 @@ export default () => {
* logic: https://github.com/preactjs/preact/blob/ea49f7a0f9d1ff2c98c0bdd66aa0cbc583055246/src/diff/props.js#L110-L129
*/
if ( attribute === 'style' ) {
if ( typeof result === 'string' )
if ( typeof result === 'string' ) {
el.style.cssText = result;
}
return;
} else if (
attribute !== 'width' &&
Expand Down Expand Up @@ -492,7 +493,9 @@ export default () => {
element,
evaluate,
} ) => {
if ( element.type !== 'template' ) return;
if ( element.type !== 'template' ) {
return;
}

const { Provider } = inheritedContext;
const inheritedValue = useContext( inheritedContext );
Expand Down

0 comments on commit d57523d

Please sign in to comment.