Skip to content

Automatically update your styles when a svelte variable changes.

Notifications You must be signed in to change notification settings

srmullen/svelte-reactive-css-preprocess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

svelte-reactive-css-preprocess

npm package

Have you ever wished you could use your svelte variables in your component's styles. Now you can!

Installation

npm install --save-dev svelte-reactive-css-preprocess

Usage

In your svelte config

import reactiveCSSPreprocessor from 'svelte-reactive-css-preprocess';

svelte({
  preprocess: [
    reactiveCSSPreprocessor()
  ]
})

If you're using svelte-preprocess you need to run svelte-reactive-css-preprocess after all tasks for `svelte-preproccess complete. To do that use 'svelte-sequential-preprocessor'.

npm install --save-dev svelte-sequential-preprocessor.

import reactiveCSSPreprocessor from 'svelte-reactive-css-preprocess';
import sveltePreprocess from 'svelte-preprocess';
import seqPreprocess from 'svelte-sequential-preprocessor';

svelte({
  preprocess: seqPreprocess([
    sveltePreprocess({
      defaults: {
        style: "postcss",
      },
      postcss: true
    }),
    reactiveCSSPreprocess()
  ])
})

Now in your component's style you can reference the reactive variables using css variable syntax.

<script>
  // Create some variables that hold information about the component's style.
  export let size = 200;
  export let color = '#f00';

  $: sizepx = `${size}px`;
</script>

<div class="square"></div>

<style>
  .square {
    /* Reference the Svelte variables using the var(--varname) syntax */
    width: var(--sizepx);
    height: var(--sizepx);
    background-color: var(--color);
  }
</style>

Now your styles update when your variables do!

How it works

The preprocessor reads through the variables in each component's script and style tags. If a variable name appears in both the script and styles then a css variables that is scoped to the component is created and added to the :root pseudo-selector. In the component the css variables are replaced with the scoped variables. Variable scoping works similarly to how Svelte handles css scoping. The style tag for the above example would end up looking something like this...

<style>
  :root {
    --sizepx-1l2ucck: inherit;
    --color-1l2ucck: inherit;
  }

  .square {
      width: var(--sizepx-1l2ucck);
      height: var(--sizepx-1l2ucck);
      background-color: var(--color-1l2ucck);
  }
</style>

In the script tag code is injected that handles updating the scoped css variables using Svelte's reactivity.

About

Automatically update your styles when a svelte variable changes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published