npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-pass-class

v0.0.16

Published

> This is current experimental and totally not ready for any production. > At current stage, this package is only a proof of concept.

Downloads

24

Readme

svelte-pass-class

This is current experimental and totally not ready for any production. At current stage, this package is only a proof of concept.

This package is meant to provide the ability to pass the style classes into the child component. It is done by using the svelte preprocessor.

Installation

npm i -D svelte-pass-class
// svelte.config.js
import sveltePassClass from 'svelte-pass-class';

const config = {
  // ...other settings

	preprocess: [
    sveltePassClass,
    // ...other preprocessor
  ],

  // ...other settings
};

export default config;

Usage

This preprocessor introduced 2 pseudo-class, :export and :let.

:export

Syntax: :export(.<Class name>) { /* style */ }

The :export tells the preprocessor the class inside the parenthesis may be passed in from the parent component, so don't directly apply the style to the element.

Example

<div class="a">Hello</div>

<style>
  :export(.a) {
    color: red;
  }
</style>

will be compiled to

<script>
  export let __spc_class_swap_table__ = {};
</script>

<div class="{__spc_class_swap_table__["a"] ?? "a"}">Hello</div>

<style>
  .a {
    color: red;
  }
</style>

:let

Syntax: :let(<Component name>).<Class name> { /* style */ }

The :let tells the preprocessor to apply the styles to the class field for all components it specified. Such as :let(Foo).bar { /* style */ } will apply the style to the bar field of all the Foo component.

Example

<script>
  import Foo from './Foo.svelte';
</script>

<Foo />
<Foo />

<style>
  :let(Foo).bar {
    color: red;
  }
</style>

will be compiled to

<script>
  import Foo from './Foo.svelte';
</script>

<Foo __spc_class_swap_table__={{"bar":"spc-xxxxxxxxxx"}} />
<Foo __spc_class_swap_table__={{"bar":"spc-xxxxxxxxxx"}} />

<style>
  :global(.spc-xxxxxxxxxx) {
    color: red;
  }
</style>

More Applies

Except for using :let to apply the styles, you can also use the class attribute to apply it. Using :let will have a lower priority than using class attribute, because the class attribute is closer to the element visually.

Component

For components, you have only 1 way to apply with class attribute, that is class:bar="name" where bar is the exported class name from the component and name is the class name you want to apply.

To reduce the verbosity, you can also use class:bar as the shorthand for class:bar="bar".

<script>
  import Foo from './Foo.svelte';
</script>

<Foo class:bar          />
<Foo class:bar="foobar" />

<style>
  .bar {
    color: red;
  }

  .foobar {
    color: blue;
  }
</style>

will be compiled to

<script>
  import Foo from './Foo.svelte';
</script>

<Foo __spc_class_swap_table__={{"bar":"spc-xxxxxxxxxx"}} />
<Foo __spc_class_swap_table__={{"bar":"spc-oooooooooo"}} />

<style>
  :global(.spc-xxxxxxxxxx), .bar {
    color: red;
  }

  :global(.spc-oooooooooo), .foobar {
    color: blue;
  }

Element

For elements, you can use the class directly to apply the style. And also the method mentioned above with little difference.

The difference is that the class:bar={true} in the above example is using {} instead of "", this is because it's the native svelte syntax, but the preprocessor will still recognize it.

Example 1

In this example, you'll find out that before and after is the same, that is because the preprocessor only do the transformation when the class is exported.

<div class="bar" />
<div class:bar={true} />

<style>
  .bar {
    color: red;
  }
</style>

will be compiled to

<div class="bar" />
<div class:bar={true} />

<style>
  .bar {
    color: red;
  }
</style>

Example 2

This example shows that the class:bar will be transformed.

<div class="bar" />
<div class:bar={true} />

<style>
  :export(.bar) {
    color: red;
  }
</style>

will be compiled to

<script>
  export let __spc_class_swap_table__ = {};

  $: __random_uuid__ = (true) ? (__spc_class_swap_table__["bar"] ?? "bar") : "";
</script>

<div class="{__spc_class_swap_table__["bar"] ?? "bar"}" />
<div class="{__random_uuid__}" />

<style>
  .bar {
    color: red;
  }
</style>

Forwarding

If the exported class is passed into the child component, the preprocessor will forward the class to the child component.

<script>
  import Child from './Child.svelte';
</script>

<Child class:bar />

<style>
  :export(.bar) {
    color: red;
  }
</style>

will be compiled to

<script>
  import Child from './Child.svelte';

  export let __spc_class_swap_table__ = {};
</script>

<Child __spc_class_swap_table__={{"bar": __spc_class_swap_table__["bar"] ?? "bar"}} />

<style>
  .bar {
    color: red;
  }
</style>