tsparticles / svelte

Svelte tsParticles official plugin
MIT License
41 stars 4 forks source link

Add class props #7

Closed Unarray closed 1 year ago

Unarray commented 1 year ago

Currently, we cannot pass classes to the Particle component which makes for example the positioning of our particle container difficult.

To solve this problem, I suggest this changes:

  <svelte:options accessors={true} />

  <script lang="ts">
    /* Current imports */

+   let clazz = "";
+   export { clazz as class };
    /* Current props */

    /* Current script */
  </script>

+ <div {id} class={clazz} />
Final code ```html
```

Now we can do :

<Particles
  id="tsparticles"
  class="hello word foo bar"
  options="{particlesConfig}"
  on:particlesLoaded="{onParticlesLoaded}"
  particlesInit="{particlesInit}"
/>
matteobruni commented 1 year ago

Hi @Unarray,

This seems interesting, I thought that the id would be enough for customizing the component, but the class could be helpful too.

Could you open a PR with the suggested code?

Unarray commented 1 year ago

Yes, I can open a PR (I'll do it in one or two hours). The advantage of classes is that you can have the same style without duplicating it several times unlike IDs. Let's say we want a 5px margin on each of our particles. So we have to duplicate as many times as we have particles unlike classes.

matteobruni commented 1 year ago

I never recommend to have more than one particles instance per page, the performance could drop very fast. But more particles can be shown in different pages and they could have the same format, it makes sense to have a class.