Skip to content

Svelte action bug #16576

@alvissraghnall

Description

@alvissraghnall

Describe the bug

I'm solving this issue using a custom action that set the class(es) I need on the body.

classList.ts

import type { Action } from "svelte/action";

export const classList: Action<Element, string | string[]> = (node, classes) => {
  const tokens = Array.isArray(classes) ? classes : [classes];
 node.classList.add(...tokens);

 return {
   destroy() {
      node.classList.remove(...tokens);
    },
 };
};

+page.svelte

<script lang="ts">
   import { classList } from "$lib/actions/classList";
</script>

<svelte:body use:classList={showContent ? 'overflow-hidden' : 'overflow-initial'} />

Via: https://github.com/sveltejs/svelte/issues/3105#issuecomment-1868458487

However, when I use it across multiple components — say, HappyModal & SoftModal, it gets registered twice, and when one modal is clicked open, it doesn't trigger update on document body's style, because the other modal is closed and has assigned 'overflow-initial'.

Reproduction

jjk

Logs

System Info

System:
    OS: Linux 6.2 Ubuntu 24.04.2 LTS 24.04.2 LTS (Noble Numbat)
    Memory: 2003.96 MB / 7.66 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 22.15.1 - ~/.nvm/versions/node/v22.15.1/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.15.1/bin/npm
    bun: 1.2.13 - ~/.bun/bin/bun
  npmPackages:
    svelte: ^5.30.1 => 5.31.1

Severity

annoyance

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