0.1.7 • Published 7 months ago

svelte-preprocess-css-hash v0.1.7

Weekly downloads
13
License
MIT
Repository
github
Last release
7 months ago

svelte-preprocess-css-hash

Passing hashed css class name to child component. It is used to avoid class name conflicts.

Example

input:

<style>
  :global(.--child-cls) {
    color: red;
  }

  :global(.--child-cls:hover .--nested-cls) {
    color: green;
  }

  :global(html.night .--child-cls .--nested-cls) {
    color: blue;
  }
</style>

<ChildComponent class="--child-cls" />

output:

<style>
  :global(.--child-cls-pXX_fA) {
    color: red;
  }

  /* only the first class name that is prefixed with '--' will be hashed */
  :global(.--child-cls-pXX_fA:hover .--nested-cls) {
    color: green;
  }

  :global(html.night .--child-cls-pXX_fA .--nested-cls) {
    color: blue;
  }
</style>

<ChildComponent class="--child-cls-pXX_fA" />

The first class name that is prefixed with -- within :global() will be hashed. It's simply a find-replace operation. So you could pass the class names to other attributes. For example:

<ChildComponent wrapperCls="--wrapper" contentCls="--content" />

And in child component, you can receive the class name as a normal prop:

ChildComponent.svelte:

<script>
  let className = '';
  export { className as class };
</script>

<div class={className}>
...
</div>

How to use

Create a svelte.config.js file in your project root folder with following contents:

const cssHash = require('svelte-preprocess-css-hash');

module.exports = {
  preprocess: [
    cssHash()
  ]
};

License

MIT

0.1.7

7 months ago

0.1.6

2 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago