Coder Social home page Coder Social logo

mcanthony / reactshadow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wildhoney/reactshadow

1.0 2.0 0.0 1.59 MB

Use Shadow DOM with React.js and CSS imports; write your component styles in CSS!

Home Page: http://react-shadow.herokuapp.com/

License: MIT License

JavaScript 85.85% CSS 8.03% HTML 6.12%

reactshadow's Introduction

ReactShadow

Travis   Experimental   License MIT

Screenshot


With ReactShadow you can apply a Shadow DOM root inside of your component. Under normal React.js conditions, your styles are written inline for style encapsulation – with ReactShadow your styles can now be moved into their rightful place – within CSS documents!

Note: Take a look at the Maple.js framework, which is a React.js framework with Shadow DOM, HTML Imports, and Custom Elements. It has a lot of functionality that I was unable to integrate into a simple React.js mixin.

Getting Started

ReactShadow is implemented as a mixin that you can import into your component:

var ReadmeApp = $react.createClass({
    mixins: [ReactShadow]
});

From there ReactShadow will take over – creating a shadow root inside of your component, and importing any CSS documents defined in your cssDocuments property – which can be either an array or a function:

var ReadmeApp = $react.createClass({
    mixins: [ReactShadow],
    cssDocuments: ['../css/Default.css']
});

If you're applying CSS documents at runtime then it may well be useful to have the cssDocuments property as a function:

var ReadmeApp = $react.createClass({
    mixins: [ReactShadow],
    cssDocuments: function cssDocuments() {
        return ['../css/Component.css', '../css/' + this.props.cssDocument];
    }
});

You can inline css with cssSource property.

var ReadmeApp = $react.createClass({
    mixins: [ReactShadow],
    cssSource: "body { color: black; }"
});

When cssDocuments and cssSource are both defined, style defined in cssSource is appended after cssDocuments.

Event Retargeting

As Shadow DOM has the concept of Event Retargeting for encapsulation purposes, event delegation will not function correctly because all events will appear to be coming from the Shadow DOM – therefore ReactShadow uses the React ID for each element to dispatch the event from the original element, therefore maintaining React's event delegation implementation.

Events are therefore written in exactly the same way:

var ReadmeApp = $react.createClass({
    render: function render() {
        return <a onClick={this.reset} title="Reset Counter">
                   Reset, Comrade!
               </a>
    }
});

reactshadow's People

Contributors

wildhoney avatar ziahamza avatar tjwudi avatar

Stargazers

Michael Anthony avatar

Watchers

Michael Anthony 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.