Skip to content

Spreadable actions: the use* attributeΒ #13449

@fcrozatier

Description

@fcrozatier

Describe the problem

Spreadable actions would be great for ergonomics in particular with components.

Describe the proposed solution

Removing the use:* directive in favor of a use* attribute in a move similar to the on:* directive -> on* attribute would allow spreadable actions.

  • It could be added to the HTMLAttribute interface something like
export interface HTMLAttributes<T extends EventTarget> extends AriaAttributes, DOMAttributes<T> {
  [actions: `use${string}`]: Action<T>
}
  • This way we can grab actions in a component rest attribute without having to manage actions in a particular way. Which makes our components future proof as we can later add more actions without reopening the component file + adding a use: directive
// Button.svelte
<script lang="ts">
  // Action-ability is already in the type, nothing to do here
  export let {children, ...rest}: HTMLButtonAttributes = $props();
</script>

<button {...rest}>{@render children()}</button>
// +page.svelte
<Button useclickoutside={(node /* typed as HTMLButton */) => {...}}>click me</Button>
  • If the action takes params they can be passed in the use*params attribute
// +page.svelte
<Button useclickoutside={(node, options = {message: "nope"}) => {...}} useclickoutsideparams={{message: "clicked outside"}}>click me</Button>
  • No dash (unlike data- attributes) to avoid double quotes everywhere

Importance

would make my life easier

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