Giter VIP home page Giter VIP logo

reactshadow's Introduction

ReactShadow

Utilise Shadow DOM in React with all the benefits of style encapsulation.

Travis   Coveralls   npm   License MIT

Screenshot


Getting Started

By using ReactShadow you have all the benefits of Shadow DOM in React.

import ShadowDOM from 'react-shadow';

export default props => {

    return (
        <ShadowDOM include={['css/core/calendar.css', props.theme]}>
            <h1>Calendar for {props.date}</h1>
        </ShadowDOM>
    );

}

In the above example the h1 element will become the host element with a shadow boundary — and the two defined CSS documents will be fetched and appended.

Preventing FOIC

As the CSS documents are being fetched over the network, the host element will have a className of resolving for you to avoid the dreaded FOIC. Once all of the documents have been attached the className will change to resolved.

Using the resolved class name you could then allow the component to appear once all styles have been applied.

.component {
    opacity: 0;
    transform: scale(.75);
    transition: all .35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.component.resolved {
    opacity: 1;
    transform: scale(1);
}

Cached Documents

Oftentimes components share the same documents, however only one instance will be fetched due to memoize of the fetchInclude function.

Inline Styles

Instead of defining external CSS documents to fetch, you could choose to add all of the component's styles to the component itself by simply embedding a style node in your component. Naturally all styles added this way will be encapsulated within the shadow boundary.

export default props => {

    const styles = `:host { background-color: ${props.theme} }`;

    return (
        <ShadowDOM>
          <div>
            <h1>Calendar for {props.date}</h1>
            <style type="text/css">{styles}</style>
          </div>
        </ShadowDOM>
    );
}

Note: Using inline styles will not combine styles into one style node.

reactshadow's People

Contributors

frontendphil avatar javadoug avatar tjwudi avatar wildhoney avatar ziahamza avatar

Watchers

 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.