Skip to content

Latest commit

 

History

History
73 lines (60 loc) · 1.31 KB

20.09-reactive-style.md

File metadata and controls

73 lines (60 loc) · 1.31 KB
layout title label permalink nav_order parent
page
Using Reactive CSS to Update Styles in Web-Components
Reactive CSS
/usage-web-components/reactive-css/
9
Usage

Reactive CSS <style>

CSS is much more fun when it's scoped. Here it come with the web-components.

Here again, no trick, just the full power of web-components and scoping styles. Well, you should know that the css is reactive too! 😲

Writing CSS is as easy as

<template>
  <h1>Bonjour!</h1>
</template>

<script>
  export default class extends Lego {
    init() {
      this.state = { fontScale: 1 }
    }
  }
</script>

<style>
  :host {
    font-size: ${state.fontScale}rem;
  }
  h1 {
    padding: 1rem;
    text-align: center;
  }
</style>

Host

:host is a native selector for web-components. It allows to select the current component itself.

Variables

You can use variables in your CSS just like in your templates.

Example:

<template>
  <h1>Bonjour<h1>
</template>
<script>
  export default class extends Lego {
    init() {
      this.state = { color: '#357' }
    }
  }
</script>
<style>
  h1 {
    color: ${ state.color };
  }
</style>

will apply the #357 color onto h1.