Skip to content

reactive actions #15243

Closed
Closed
@pjebs

Description

@pjebs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions