Skip to content

Commit f10a4ed

Browse files
committed
Add repl image and explicit link
1 parent 4200cdb commit f10a4ed

File tree

3 files changed

+39
-88
lines changed

3 files changed

+39
-88
lines changed

README.md

Lines changed: 0 additions & 88 deletions
This file was deleted.

docs/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# `svelte-css-vars`
2+
3+
> Ever wanted to have reactive css variables in svelte? What if I told you there's a way?
4+
5+
`svelte-css-vars` exposes a [svelte action](https://svelte.dev/docs#use_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.
6+
7+
## Usage
8+
9+
`import cssVars from 'svelte-css-vars';`
10+
11+
After importing the module, just define the reactive object of your custom properties:
12+
13+
```js
14+
let some_state_variable = true;
15+
16+
$: styleVars = {
17+
titleColor: some_state_variable ? 'red' : 'blue',
18+
};
19+
```
20+
21+
And then use the action, passing the object, on the desired element:
22+
23+
```html
24+
<style>
25+
div {
26+
color: var(--titleColor);
27+
}
28+
</style>
29+
30+
<div use:cssVars="{styleVars}">
31+
<!-- anything here will have access to var(--titleColor) -->
32+
</div>
33+
```
34+
35+
## Example
36+
37+
[![repl](./repl.png)](https://svelte.dev/repl/1522fe3bdf904843a01101d9f900241d)
38+
39+
[REPL](https://svelte.dev/repl/1522fe3bdf904843a01101d9f900241d)

docs/repl.png

144 KB
Loading

0 commit comments

Comments
 (0)