1

I have a shadow-root component and I simply don't want svelte-kit to add those random classes to that specific element or its child elements. It would be nice if there was something in the settings like: document.getElementById("<id>").cssHash == false

Or any possible hacks to achieve this?

enter image description here

9
  • Those classes are how Svelte can add CSS that is scoped to that specific component. It adds that class to each selector in a component's <style> block. Commented Mar 17, 2023 at 14:05
  • I know that @M.Desjardins Sveltes uses this for scoped CSS but what I want is to disable it on that certain element only. Or any hacks if that's not something possible in svelte Commented Mar 17, 2023 at 14:06
  • 1
    @AbhaySalvi you can, but you need to create a global module css file and import that file and use it like this vitejs.dev/guide/features.html#css-modules or write inline css code. and to select the same element you can use bind:this. unfortunately I don't know another way simple than this. hope it helps Commented Mar 17, 2023 at 14:08
  • 1
    What is the point?
    – brunnerh
    Commented Mar 17, 2023 at 14:23
  • 1
    Still don't see this as a valid goal. Adding classes should not conflict in any way with the existing Svelte classes. I just cannot recommend this. If the internal Svelte implementation changes things might break, too.
    – brunnerh
    Commented Mar 17, 2023 at 14:42

1 Answer 1

2

You can use a use action (tutorial | docs) for this:

let noClass = node => {
    node.classList.forEach(e => {if (e.startsWith('svelte-')) node.classList.remove(e)})
}

Then you use the directive on elements:

<div use:noClass>

et voila! Full example: https://svelte.dev/repl/31b4e0e04ec84ac6995f2a5f5c1f9cd5?version=3.57.0

2
  • 1
    Note: The Svelte REPL uses svelte- as a prefix, but your app seems to use s-, so you need to adjust the code, but it should still work.
    – lxhom
    Commented Mar 18, 2023 at 0:17
  • Yeah, that's the only hack it seems for me to work. Thanks! Commented Mar 19, 2023 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.