Description
Describe the problem
Can we reconsider the restriction against the reactivity of actions from argument changes. Seems to go against the philosophy of Svelte and is effecting a library I am building.
The docs: https://svelte.dev/docs/svelte/use state:
<script lang="ts">
import type { Action } from 'svelte/action';
const myaction: Action = (node, data) => {
// ...
};
</script>
<div use:myaction={data}>...</div>
The action is only called once — it will not run again if the argument changes.
Prior to the $effect rune, actions could return an object with update and destroy methods, where update would be called with the latest value of the argument if it changed. Using effects is preferred.
I am trying to convert y-direction mouse wheel scrolls into the x-direction on desktop sites, but not have any effect on mobile devices. On Mobile sites, I pass disable=true
. When the screen resizes to small I consider it a "mobile device"
Simplified example below:
const scrollEvents: Action = (node: HTMLElement, disable: boolean) => {
let wheelHandler = (event) => {
// Wheel Inversion
if (disable) {
return;
}
event.preventDefault();
node.scrollLeft += (event.deltaY+ event.deltaX);
}
$effect(() => {
node.addEventListener("wheel", wheelHandler);
return () => {
node.removeEventListener("wheel", wheelHandler);
};
});
}
<span use:scrollEvents={isMobile} > <!------ state changes on `window.onresize` or `ResizeObserver`
Describe the proposed solution
Make actions reactive upon argument changes
Importance
nice to have