Given that you have no control over the classes
function, one way would be to wrap the action with one of your own:
import classes from '....'
function classesWrap(node, useDefault = true) {
useDefault && classes(node);
return {
update(useDefault) {
useDefault && classes(node);
}
}
}
and then use this action instead: <div use:classesWrap={originalClasses}>
This works, kind of, when originalClasses
is false, it will not set the default ones, and when it is true it will set them. When it starts as false and turns true it will also apply them (thanks to the update
) However it seems that once an action is attached it cannot be removed again (which is logical as actions are supposed to run when the element gets mounted). This means that turning from true to false will not change anything, it will keep the default classes.
There are two (really bad) workarounds for this:
- add some extra code to the update function to remove the classes yourself:
update(useDefault) {
useDefault && classes(node);
!useDefault && node.classList.remove(...node.classList);
}
(but this removes all classes, also those you might have added yourself)
- a second option is to wrap the element with a
{#key}
block based on originalClasses
this will force a re-render whenever it changes.
{#key originalClasses}
<div use:classesWrap={originalClasses}>...</div>
{/key}
But this can be very expensive if this div has a lot of content, maybe for a one line element it might be ok.
(note: you don't need the update function anymore)
Of course this assumes that originalClasses
can and will change, if you are 100% sure it will never ever change, just use the wrapper without update or key blocks.