Skip to content

Make clsx() function available #16873

@webJose

Description

@webJose

Describe the problem

I'm improving my router library. Now my router can provide hash routing to Sveltekit projects (configured for path routing), so hash routing on top of Sveltekit's path routing.

Since Sveltekit can use regular anchor elements, and because there is at least one styling library (Bulma) that applies the "active" look not to the anchor element, but to the parent <li> element, I now want to expose the active feature as an attachment.

My issue: This is my TS type for the information required for this "active" feature:

type ActiveState = {
    key?: string | undefined;
    class?: ClassValue | undefined; // <------------------------   HERE!!!
    style?: HTMLAnchorAttributes["style"] | Record<string, string>;
    ariaCurrent?: HTMLAnchorAttributes["aria-current"];
}

I have defined the class property as accepting all values accepted by clsx because my <Link> component simply adds this property value to the array inside the anchor element: <a ... class={[...., activeState?.class]} ...>. Well, in an attachment, I have to do this imperatively with el.classList.

Describe the proposed solution

Allow me to import the clsx() function that is built-in in Svelte, so I can do:

el.classList.add(clsx(activeState?.class));

The alternative would be to explicitly install the clsx NPM package, which would be redundant.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions