Giter Club home page Giter Club logo

shadycss's Introduction

ShadyCSS

ShadyCSS provides a shim for, CSS Custom Properties, CSS Mixins with @apply support, and ShadowDOM V1 style encapsulation with the ShadyDOM library.

Requirements

ShadyCSS requires support for CustomElements, ShadowDOM, MutationObserver, Promise, Object.assign

This library is distributed as ES2016 only, due to the included <custom-style> element definition.

Usage

The shim will transparently no-op if some or all native support is available.

If native ShadowDOM is not available, stylesheet selectors will be modified to simulate scoping.

if CSS Custom Properties are not available, stylesheets will be generated with realized values for custom properties.

@apply is not native in any browser, so they will be shimmed with CSS Custom Properties in browsers that support them, or via generated stylesheets with the CSS Custom Properties shim.

To use ShadyCSS:

  1. First, call ShadyCSS.prepareTemplate(template, name) on a <template> element that will be imported into a shadowRoot.

  2. When the element instance is connected, call ShadyCSS.applyStyle(element)

  3. Create and stamp the element's shadowRoot

  4. Whenever dynamic updates are required, call ShadyCSS.applyStyle(element).

  5. If a styling change is made that may affect the whole document, call ShadyCSS.updateStyles().

Example

The following example uses ShadyCSS and ShadyDOM to define a custom element.

<template id="myElementTemplate">
  <style>
    :host {
      display: block;
      padding: 8px;
    }

    #content {
      background-color: var(--content-color);
    }

    .slot-container ::slotted(*) {
      border: 1px solid steelblue;
      margin: 4px;
    }
  </style>
  <div id="content">Content</div>
  <div class="slot-container">
    <slot></slot>
  </div>
</template>
<script>
  ShadyCSS.prepareTemplate(myElementTemplate, 'my-element');
  class MyElement extends HTMLElement {
    connectedCallback() {
      ShadyCSS.applyStyle(this);
      if (!this.shadowRoot) {
        this.attachShadow({mode: 'open'});
        this.shadowRoot.appendChild(
          document.importNode(myElementTemplate.content, true));
      }
    }
  }

  customElements.define('my-element', MyElement);
</script>

Type Extension elements

ShadyCSS can also be used with type extension elements by supplying the base element name to prepareTemplate as a third argument.

Example

<template id="myElementTemplate">
  <style>
    :host {
      display: block;
      padding: 8px;
    }

    #content {
      background-color: var(--content-color);
    }

    .slot-container ::slotted(*) {
      border: 1px solid steelblue;
      margin: 4px;
    }
  </style>
  <div id="content">Content</div>
  <div class="slot-container">
    <slot></slot>
  </div>
</template>
<script>
  ShadyCSS.prepareTemplate(myElementTemplate, 'my-element', 'div');
  class MyElement extends HTMLDivElement {
    connectedCallback() {
      ShadyCSS.applyStyle(this);
      if (!this.shadowRoot) {
        this.attachShadow({mode: 'open'});
        this.shadowRoot.appendChild(
          document.importNode(myElementTemplate.content, true));
      }
    }
  }

  customElements.define('my-element', MyElement, {extends: 'div'});
</script>

<custom-style>

The <custom-style> element allows <style> elements that are not inside of Custom Elements to be processed by the ShadyCSS library.

Example

<custom-style>
  <style>
  html {
    --content-color: brown;
  }
  </style>
</custom-style>
<my-element>This text will be brown!</my-element>

Imperative values for Custom properties

To set the value of a CSS Custom Property imperatively, ShadyCSS.applyStyle and ShadyCSS.updateStyles support an additional argument of an object mapping variable name to value.

Defining new mixins or new values for current mixins imperatively is not supported.

Example

<my-element id="a">Text</my-element>
<my-element>Text</my-element>
<script>
let el = document.querySelector('my-element#a');
// Set the color of all my-element instances to 'green'
ShadyCSS.updateStyles({'--content-color' : 'green'});
// Set the color my-element#a's text to 'red'
ShadyCSS.applyStyle(el, {'--content-color' : 'red'});
</script>

Limitations

Selector scoping

You must have a selector to the left of the ::slotted pseudo-element.

Custom properties and @apply

Dynamic changes are not automatically applied. If elements change such that they conditionally match selectors they did not previously, ShadyCSS.updateStyles() must be called.

For a given element's shadowRoot, only 1 value is allowed per custom properties. Properties cannot change from parent to child as they can under native custom properties; they can only change when a shadowRoot boundary is crossed.

To receive a custom property, an element must directly matcha selector that defines the property in its host's stylesheet.

<custom-style> Flash of unstyled content

If ShadyCss.applyStyle is never called, <custom-style> elements will process after HTML Imports have loaded, after the document loads, or after the next paint. This means that there may be a flash of unstyled content on the first load.

If there are only <custom-style> elements in the page, you may call ShadyCSS.updateStyles() to remove the flash of unstyled content.

Mixins do not cascade throught <slot>

Crawling the DOM and updating styles is very expensive, and we found that trying to update mixins through <slot> insertion points to be too expensive to justify for both polyfilled CSS Mixins and polyfilled CSS Custom Properties.

shadycss's People

Contributors

dfreedm avatar bicknellr avatar kevinpschaaf avatar meai1 avatar

Watchers

James Cloos avatar Diógenes Polanco avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.