GithubHelp home page GithubHelp logo

custom-element-boilerplate's Introduction

Custom Elements

Simple boilerplate for custom elements, using ES6 but without the overhead of polymer.

For details, see the custom elements W3C spec, or some other good posts

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="calcite-web.css">
    <script src="item-rating.js"></script>
  </head>
  <body>
    <h1>My Item</h1>

    <item-rating id="item-rating" itemid="30e5fe3149c34df1ba922e6f5bbf808f" numratings="5" rating="4.25">

    <script>
      var rating = document.getElementById('item-rating');
      rating.addEventListener('rateitem', function(e) {
        console.log('Rate Item:', e.detail.rating);
      });
    </script>

  </body>
</html>

Get going

You may also want to look at the source code for the component or the PDF presentation.

Notes

Strictly Adhere to DOM Standards

As a rule for maximum compatibility with the largest number of tools you should keep the following standards in mind.

  1. Always use DOM style event names, all lowercase, with no special characters. This helps keep any kind of event mapping magic (Ember) consistent.
  2. Each attribute should always have a corresponding getter and getter that matches the attribute name.
  3. Attributes should always adhere to DOM standards, all lowercase with no special characters. This combined with the above point is essential for Aurelia.
  4. You should always bubble your custom events. This is necessary for Ember (since it uses event delegation to listen to events) and for components to nest inside each other and listen to events

Required Polyfills

All of these polyfills come out to about 25kb minified and gzipped. Most of that is the Babel polyfill which is 22kb minified and gzipped. You can save 5kb by using the shim from Core JS directly directly and could likely save even more with a custom build.

You can also reduce the needs for polyfills by restricting features (for example using only class, arrow functions and multiline srings) and using loose mode in Babel.

Browser support

With the required polyfills in place this should work in all modern browsers back to IE 9. However this needs testing.

React

Currently React 0.13 uses a whitelist of valid HTML attributes so you cannot use non-standard attributes in React. This will be fixed for custom elements with React 0.14. With this change custom elements should work totally out of the box.

Ember

Currently in addition to wrapping the custom element as a component you must register any custom events with Ember. Eventually the component restriction will go away with the move to angle bracket components in Ember 2.2, see the RFC and the PR.

The restriction to add custom event to the global registry will still remain. However the author of a proposed event refactoring RFC is interested in removing the requirement.

Angular 2.0

Works totally out of the box.

Angular 1.0

Attribute binding works fine out of the box but in order to bind custom events you must write a small custom directive.

Backbone

Works totally out of the box.

Aurelia

Works totally out of the box, as long as you obey the DOM standards, particularly with regards to properties and attributes. If you want to break standards for more readable attribute names or some other reason you can extend Aurelia's parser to support your use case.

License

MIT

custom-element-boilerplate's People

Contributors

patrickarlt avatar mjuniper avatar ajturner avatar

Stargazers

 avatar Nate Goldman avatar Tom Wayson avatar  avatar

Watchers

 avatar Tom Wayson avatar James Cloos avatar Brendan Heberton 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.