Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

dimfeld/svelte-style-action

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

svelte-style-action

This is a simple Svelte action that makes it easier to manage style attributes without needing to manually concatenate strings together.

This action uses node.style.setProperty to set the property values, which means that the object keys should dash-separated as when typing CSS, not camel-cased. (i.e. font-weight instead of fontWeight). This also means that CSS custom properties work properly.

<script>
  import { style } from 'svelte-style-action';

  const styles = {
    color: 'green',
    '--aVariable': '40px',
    'font-weight': 700
  };

  setTimeout(() => {
    styles = {
      ...styles,
      color: 'red'
    };
  }, 1000);
</script>

<div use:style={styles}>
  Some content
</div>

For convenience, you can also pass a string instead of an object, but you may be better off just setting the style attribute directly in that case.

Check it out in the Svelte REPL!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published