svelte-css-vars

:sparkles: Ever wanted to have reactive css variables in svelte? What if I told you there's a way?

Github stars Tracking Chart

svelte-css-vars

Ever wanted to have reactive css variables in svelte? What if I told you there's a way?

svelte-css-vars exposes a svelte action which receives an object of properties that should be treated as css custom properties. By defining this object inside a $:{} reactive block, svelte-css-vars can update those css properties on the fly whenever some of its values change.

Usage

import cssVars from 'svelte-css-vars';

After importing the module, just define the reactive object of your custom properties:

let some_state_variable = true;

$: styleVars = {
  titleColor: some_state_variable ? 'red' : 'blue',
};

And then use the action, passing the object, on the desired element:

<style>
  div {
    color: var(--titleColor);
  }
</style>

<div use:cssVars="{styleVars}">
  <!-- anything here will have access to var(--titleColor) -->
</div>

Example

repl

REPL

Main metrics

Overview
Name With Ownerkaisermann/svelte-css-vars
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 4)
Platform
License:MIT License
所有者活动
Created At2019-08-15 13:17:58
Pushed At2023-01-04 07:16:25
Last Commit At2021-06-17 13:09:20
Release Count2
Last Release Namev0.0.2 (Posted on 2021-03-29 17:05:12)
First Release Namev0.0.1 (Posted on 2019-08-15 11:10:34)
用户参与
Stargazers Count239
Watchers Count6
Fork Count3
Commits Count15
Has Issues Enabled
Issues Count3
Issue Open Count1
Pull Requests Count2
Pull Requests Open Count14
Pull Requests Close Count2
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private