• Stars
    star
    431
  • Rank 96,976 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Maple.js is a React webcomponents based framework mixing ES6 with Custom Elements, HTML Imports and Shadow DOM. It has in-built support for SASS and JSX, including a Gulp task for vulcanizing your project.

Maple.js

Travis ย  Bower ย  npm ย  License MIT


Maple is a seamless module that allows you to organise your React project in terms of webcomponents โ€” with HTML Imports, Shadow DOM, and Custom Elements โ€” allowing you to implement any Flux architecture you choose, and then compile with Mapleify for production.

Screenshot

Getting Started

๐Ÿ Watch "Getting Started with Maple": https://vimeo.com/128387987 (Previous)

๐Ÿ’Ž Install all dependencies and start server using npm start.

Given the typical Flux architecture where components reside in their respective components directory, we continue that trend in Maple, where one component can register one or many custom elements โ€“ but each HTML document can only have one template element.

Within the directory my-app/components we create our component's index that will be imported โ€” date-time.html โ€” which will import its associated JavaScript and CSS documents:

<template>
    <script type="text/jsx" src="date-time.js"></script>
    <script type="text/javascript" src="../../../vendor/moment/moment.js"></script>
    <link rel="stylesheet" type="text/css" href="date-time.css" />
</template>

Note: When we import the date-time.js file we use the local path, which Maple.js understands as being a part of the module โ€“ whereas our third-party module โ€” moment.js โ€” resides outside of the component's directory and is therefore imported into the window scope.

Within our CSS file, we can be as loose as we like, because the date-time.js component will be imported under its own shadow boundary, preventing the styles from bleeding over into other components โ€” even components that are children of our component.

We next need to add some standard ES6 React code to our date-time.js to make it return a date and time when rendered:

export default class MyDateTime extends React.Component {

    render() {
        let dateTime = moment().format(this.props.format || 'YYYY-MM-DD');
        return <time>{dateTime}</time>
    }

}

Note: You could use the React.createElement('datetime', null, dateTime) approach as well โ€“ the System.import we use recognises when it's a JSX file and will transpile it automatically for you.

By looking at the above React component, we can immediately deduce that the eventual custom element will be called my-date-time. For those eagle-eyed individuals amongst us, you'll have noticed we use this.props.format to specify the date/time format โ€“ and this is something we'll pass into our component when adding the custom element to the DOM.

Next all we need to do is add a little CSS to our date-time.css document:

time {
    color: rebeccapurple;
    font-family: Arial, Tahoma, Helvetica, sans-serif;
}

And finally import the component into our main index.html document that includes the maple.js and react.js imports:

<link rel="import" type="text/html" href="my-app/components/time-date/index.html" />

Note: You may have noticed that the component's directory name is largely irrelevant โ€“ and it is, in most cases. However, there are certain circumstances where the component's directory matters โ€“ such as when registering a Worker โ€” In this case Maple provides the component directory as this.props.path.

Once the HTML document has been imported, Maple will register our custom element and it will be then usable in our application โ€“ although don't forget that we should pass in the optional format attribute to override YYYY-MM-DD:

<my-date-time data-format="YYYY-MM-DD HH:mm"></my-date-time>

Note: In the above example we use data-format, whereas our React component expects format โ€” you'll be glad to know that in these cases, Maple strips the data- segment from the attribute, which allows you to write perfectly valid HTML5 syntax.

Component Path

From within your React component, use the this.props.path.getRelativePath() to get the path of the current component โ€“ with this information, you can easily register Workers and other relatively stored documents:

let name   = 'MyWebWorker.js',
    path   = `${this.props.path.getRelativePath()}/${name}`,
    worker = new Worker(path);

Ignore Import

Importing a HTML file may not require Maple at all, and therefore if the imports were left to be processed by Maple this would be a waste of resources โ€“ as no components would be contained within the import. For these cases you can add the data-ignore attribute to the HTML import, and Maple will leave them unprocessed:

<link rel="import" type="text/html" href="example.html" data-ignore />

Multiple Elements

Each HTML document can have exactly one template element registering components. In cases where you want to register multiple components, you must split them into their individual HTML documents for developers to import separately. For instance, a DateTime component could yield date-time-gmt, date-time-bst, etc... Each element can have its own associated CSS documents as well. There are two approaches for this:

  1. Create two HTML documents: index-gmt.html and index-bst.html and require them to be imported separately;
  2. Create one HTML import with one template node and import both JS documents with a shared CSS document:
<template>
    <script type="text/javascript" src="datetime-gmt.js"></script>
    <script type="text/javascript" src="datetime-bst.js"></script>
    <link rel="stylesheet" type="text/css" href="shared.css" />
</template>

Choosing between the two approaches should be evident โ€“ if you want to apply custom CSS documents to each component individually โ€” datetime-gmt.css to one, and datetime-bst.css to the other โ€” then you should have two HTML documents. Otherwise if the two are directly related, and share the same CSS and JS documents, then they can be kept together in one HTML document.

JSX Compilation

In development environments it is often useful to compile JSX documents โ€” Maple supports JSX compilation. All you have to do is import JSX the usual JSX way using the text/jsx type:

<template>
    <script type="text/jsx" src="my-jsx-document.js"></script>
</template>

Note: When using Mapleify to render your app โ€“ Mapleify merely changes the type of your script elements from text/jsx to text/javascript and changes the extensions from .jsx to .js (pre v1.2.0 when JSX files were included with JSX extensions) โ€“ it's left entirely up to the developer to write their Gulp/Grunt scripts to convert their JSX โ€” and SASS โ€” documents prior to Mapleify compilation.

Also Note: Since the release of v1.2.0 JSX files must have a JS extension โ€“ also JSX files will import just fine using a text/javascript type, too.

Nested Shadow Boundaries

As Maple uses Custom Elements to create the components, it's straightforward to have components within components โ€“ you only need to place your Custom Element node into your React component:

render() {
    return <li><date-time data-unix={model.date}></date-time></li>
}

SASS Transpiling

๐Ÿ Watch "SASS to CSS": https://vimeo.com/128343626

In a development environment Maple supports transpiling SASS documents to CSS documents โ€“ for production you should use your build tool to transpile SASS to CSS documents.

Maple uses Sass.js and can be installed separately:

bower install sass.js -D

Once you have included sass.js all documents that are included with the type text/scss will be automatically transpiled for you to CSS before being appended to the shadow boundary:

<link type="text/scss" href="default.scss" />

Resolved Components (FOUC)

๐Ÿ Watch "Preventing FOUC": https://vimeo.com/128343604

Maple uses the same mechanism as Polymer when it comes to preventing FOUC. Place the unresolved attribute on each element, and then once they're upgraded by Maple, the unresolved attribute will be replaced with the resolved attribute:

<date-time unresolved></date-time>

With the following styles the date-time element will fade in gradually once upgraded:

date-time {
    opacity: 0;
    display: block;
    transition: opacity 3s;
}

date-time[resolved] {
    opacity: 1;
}

Mutation Observer

๐Ÿ Watch "Mutation Observer": https://vimeo.com/128588608

Maple uses the MutationObserver to listen for changes to the document.head element โ€“ if new elements are added to the node, then Maple will eagerly attempt to resolve the HTML imports and load them dynamically.

For components to be processed by the mutation observer, link elements must:

  • Be a child of the document.head element;
  • Pass the utility.isHTMLImport method;

The utility.isHTMLImport method checks for the following to determine whether the newly added element is a valid link import:

  • Is an instance of HTMLLinkElement;
  • rel attribute resolves to string import;
  • Has the href attribute defined;
  • type attribute resolves to string text/html;

Once the element has passed the aforementioned check, Maple will load in the component and it will be ready to use. As an example, let's dynamically load our DateTime component from the first tutorial:

var linkElement = document.createElement('link');
linkElement.setAttribute('href', 'app/components/todo-form/index.html');
linkElement.setAttribute('type', 'text/html');
linkElement.setAttribute('rel', 'import');
document.head.appendChild(linkElement);

It's worth noting that the above code contains a fair amount of boilerplate code, which is why you'll likely want to have a wrapper function for this. After the linkElement has been appended to the document.head element, Maple will resolve the HTML import via the MutationObserver.

Extending Native Elements

โš ๏ธ Not yet supported โ€“ merged and pending release.

๐Ÿ Watch "Extending Elements": https://vimeo.com/128589729

By default all of your Maple components will be simple elements. For example, the class DateTime object will create an element called date-time โ€“ in cases where you'd like the element to be specialised โ€” such as extending the HTMLButtonElement then you need to modify the object's name:

export default class DateTime_Button {}

In the above case the element will still be registered as date-time โ€“ but now the date-time element will extend HTMLButtonElement.prototype:

<button is="date-time">
    DateTime Button!
</button>

Mapleify (Vulcanization)

For development purposes the HTML Imports are an acceptable design implementation โ€“ however when pushing to production โ€” as you do with Polymer โ€” you'll want to minify and concatenate your resources. In Polymer you would use vulcanize โ€“ Maple utilises vulcanize to create Mapleify which compiles your HTML document.

You can install Mapleify globally with npm: npm install mapleify -g โ€“ it can then be used from your terminal:

mapleify -i index.html (default renders to mapleify.html โ€“ change with -o rendered.html)

Browser Support

Chrome Firefox Opera Safari Safari

Note: Example has also been tested in IE11 where it seems to be functioning well.

Maple also comes distributed with a dist/maple-polyfill.js file that includes all necessary polyfills for the widest possible support in modern browsers.

Example Todo

We have a typical todo example on Heroku which uses Maple along with the Alt.js Flux library. Everything should look familiar to a seasoned React.js developer โ€“ with the customary stores and actions โ€“ where the codebase differs is in the components directory, where each of the three components are written in ES6 and exported using export default.

Selectors

It's crucial to know how Maple traverses the DOM to find your CSS/SASS and JS documents. Maple attempts to adhere to the HTML5 standard โ€“ and therefore if you notice something amiss, please open an issue!

  • External CSS: Must have rel="stylesheet" โ€“ all other attributes optional;
  • Inline CSS: Optional type="text/css" attribute;
  • HTML Imports: Must have rel="import" โ€“ all other attributes optional;
  • Inline Templates: Must have a ref attribute โ€“ automated by Mapleify;
  • External JS: Optional type="text/css" attribute โ€“ matches JSX with type="text/jsx";

Namespaces

In some cases it may be desirable to prepend a namespace to all custom elements โ€“ especially in the case where you're loaded a third-party import and are unable to touch their custom elements directly. In these instances Maple allows you to specify a namespace when importing the document:

<link rel="import" href="app/components/date-time/index.html" data-namespace="x" />

By specifying the data-namespace attribute, you effectively prepend x to all custom elements imported by that document. Therefore if date-time defined a date-time element, with the data-namespace attribute as x the element would now be x-date-time which helps to prevent naming conflicts.

Testing

Maple uses Polymer's wct testing tool โ€“ which relies on the Chai assertion library.

  • npm install
  • bower install
  • gulp test

Optionally you may also invoke the wct testing yourself by issuing the wct command in your terminal.

More Repositories

1

ReactShadow

๐Ÿ”ฐ Utilise Shadow DOM in React with all the benefits of style encapsulation.
JavaScript
1,262
star
2

Leaflet.FreeDraw

๐ŸŒ FreeDraw allows the free-hand drawing of shapes on your Leaflet.js map layer โ€“ providing an intuitive and familiar UX for creating geospatial boundaries similar to Zoopla and others. Included out-of-the-box is the concaving of polygons, polygon merging and simplifying, as well as the ability to add edges and modify existing shapes.
JavaScript
526
star
3

Magento-on-Angular

Angular.js application using Magento as the backend API
PHP
361
star
4

Legofy

Legofy your images with retina support using SVG.
JavaScript
299
star
5

ngDroplet

Angular.js HTML5 file uploading with drag & drop and image/file preview.
JavaScript
286
star
6

Switzerland

๐Ÿ‡จ๐Ÿ‡ญSwitzerland takes a functional approach to Web Components by applying middleware to your components. Supports Redux, attribute mutations, CSS variables, React-esque setState/state, etcโ€ฆ out-of-the-box, along with Shadow DOM for style encapsulation and Custom Elements for interoperability.
TypeScript
270
star
7

ngVideo

Modularised ~13KB HTML5 audio/video implementation using Angular.js
JavaScript
227
star
8

Keo

Plain functions for a more functional Deku approach to creating stateless React components, with functional goodies such as compose, memoize, etc... for free.
JavaScript
227
star
9

Standalone

Create framework agnostic components that are truly reusable and interoperable with all the benefits of the React ecosystem โ€“ using the HTML5 custom elements API to extend HTML's vocabulary.
JavaScript
206
star
10

Snapshot.js

Node.js app for slicing and dicing ~100,000 models in <5ms with easy sorting, paginating, and filtering.
JavaScript
202
star
11

EmberDroplet

Ember.js HTML5 file uploading with drag & drop and image/file preview.
JavaScript
198
star
12

EmberSockets

Socket.io (WebSockets) integrated with Ember.js' observer pattern.
JavaScript
136
star
13

Dory

Dory is a responsive, universal, GitHub collaborated blogging platform built on React and powered by Express. By combining awesome features such as automatic RSS generation, HTML5 offline support, push notifications, with a powerful development environment using hot reloading, SASS and Markdown, Dory allows developers to quickly dive into the depths of blogging.
JavaScript
128
star
14

Interpose

Apply stylesheet variables to your React components for use in your stylesheets. Interpose reduces the clutter of React components by bridging the gap between JS and CSS without resorting to complicating your components with CSS logic.
JavaScript
125
star
15

Mocktail

๐Ÿน Mock all of your ES6 module components with Mocktail using dependency injection.
JavaScript
103
star
16

ngCrossfilter

Usual Angular.js style filtering and sorting with a twist of Crossfilter for improved performance.
JavaScript
87
star
17

ngContextMenu

Handcraft your very own context menus for a richer UX!
JavaScript
80
star
18

gulp-processhtml

Process html files at build time to modify them depending on the release environment
JavaScript
77
star
19

L.Pather

Branching from Leaflet.FreeDraw, L.Pather is a freehand polyline creator that simplifies the polyline for mutability.
JavaScript
69
star
20

Freelancer

๐Ÿ‘” An implementation of on-the-fly defined WebWorkers that are created inline using data URIs, rather than separate physical files โ€” for the benefit of all humanity.
JavaScript
66
star
21

Amelie

HTML5 audio visualiser experiment using D3 with a curious Amelie theme.
JavaScript
57
star
22

ngRangeSlider

Multi-handle range slider utilising the native HTML5 input range elements.
JavaScript
54
star
23

EmberCrossfilter

Instead of using Ember DataStore, EmberCrossfilter provides a basic architecture for creating Ember models with Crossfilter; which allows for much quicker sorting and filtering.
JavaScript
48
star
24

DetectFont

Detect which font your system has cherry-picked from font-family.
JavaScript
40
star
25

CloudConvert

Easy-to-use Node.js implementation of the CloudConvert API.
JavaScript
38
star
26

Redecorate

Simple module for reducing immutable nested properties in Redux applications.
JavaScript
27
star
27

ngPourOver

PourOver wrapper for Angular.js with super-quick filtering and sorting.
JavaScript
27
star
28

ReactDelayed

Small component for delaying the mounting and unmounting of a child component for CSS animation purposes.
JavaScript
21
star
29

ngImgur

Take your favourite cat picture, and upload it to Imgur.com w/ Angular!
JavaScript
17
star
30

RedisCache

Simple Node.js based Redis cache for storing large collections of data.
JavaScript
16
star
31

ngRoundabout

Three-dimensional HTML5 carousel implemented in Angular.js.
JavaScript
14
star
32

ReactCrossfilter

Crossfilter.js implemented as a mixin for ultra-fast filtering and sorting techniques baked into React.js components.
JavaScript
13
star
33

ngDonut

Lightweight and extensible Angular implementation of the D3 donut graph.
JavaScript
11
star
34

Readlint

๐Ÿ“™Lint all of the code examples in your README documentation using shared configs.
JavaScript
10
star
35

ngObelisk

Easily use Obelisk to create animations using Angular.js.
JavaScript
10
star
36

ngTeleport

Move a section of the DOM and inherit the scope of the target node.
JavaScript
9
star
37

Webmonkey

๐Ÿ™Š Robust and versatile headless monkey testing for the modern web with reproducible steps, error alerts, strategy sharing and many other good things.
JavaScript
9
star
38

Formv

๐Ÿ—ณReact form validation using the validation native to all recent browsers. Also includes support for handling API validation messages, success messages, memoized and nested form state, and super easy styling.
JavaScript
9
star
39

Taskfile

๐Ÿ“ฆ Yet another attempt at a simple task runner for npm with parallelisation support using bash commands via YAML.
JavaScript
8
star
40

EmberRickshaw

Rickshaw faรงade for Ember which allows automatic redrawing of graphs using Ember's observer pattern.
JavaScript
7
star
41

Memoria

Extensible form storage for memorising user inputs with local storage. Never lose your form data ever again!
JavaScript
7
star
42

EarthApp

Three.js w/ Angular implementation of planet earth using SketchUp for the models.
JavaScript
7
star
43

Koi

Koi is a time aware interactive bird written in THREE.js with voice recognition, and an artificial IQ.
JavaScript
6
star
44

Pellucid

Experimental module using HTML5's Custom Elements that creates a crystalline blurred background.
JavaScript
6
star
45

Catwalk

Intuitive and fast relational CRUD interface for modelling relationships using vanilla objects written in ES6.
JavaScript
6
star
46

Mapleify

Mapleify is a build tool for Maple.js; it uses Polymer's vulcanize to process HTML imports with a little twist of Maple specific logic for HTML Import paths, and the processing of SASS/JSX documents.
JavaScript
6
star
47

Bi-cycle

Bi-cycle assists in making infinite carousels and sliders by handling the index logic for you.
JavaScript
5
star
48

Openroulette

Openroulette is a Chatroulette implementation built using the WebRTC component.
JavaScript
4
star
49

Vik

CLI Semver autoincrement with major, minor, and patch.
JavaScript
4
star
50

Draught

Drawing tool in ES6 for creating diagrams using D3 with an extensible event driven architecture.
JavaScript
4
star
51

Maducer

An experimental map-reduce concurrency over web workers using shared array buffer for handling large datasets.
JavaScript
3
star
52

OrderlyQueue

Implementation of a promise-based FIFO queuing system using ES2017 async generators.
JavaScript
3
star
53

AnsibleAlexa

Development deployment using Vagrant w/ Ansible provisioner.
PHP
3
star
54

Canvas-Background

A super useful function for applying a background colour to a canvas element before invoking toDataURL.
JavaScript
3
star
55

MayBee

Safe chaining of object properties and functions using ES2015 Proxy.
JavaScript
3
star
56

Angularise

Deferred compilation of Angular.js templates for applications that render HTML from asynchronous processes.
JavaScript
3
star
57

PointerEvents

Allow the emulation of pointer events for browsers without native support, such as Internet Explorer.
JavaScript
3
star
58

Instamap

โœˆ๏ธ Towards the end of 2016, Instagram removed the Photo Maps feature. Instagram says it was unused, but we remember! Instamap brings it back for good, open-source and ad-free.
JavaScript
3
star
59

mundus-meus

Leaflet.js & Angular.js mapping tool for finding entities based on your location.
JavaScript
2
star
60

Typified

๐Ÿ—ผAn experimental implementation of first class functional types using pure ES at runtime, inspired by Haskell, PureScript and Idris.
JavaScript
2
star
61

UMLApp

JavaScript
2
star
62

UploadButton

UploadButton is a tiny module for a custom stylable upload button. Using HTML5's Custom Element API and Shadow DOM for encapsulation.
JavaScript
2
star
63

Lenin

Diagram tool using D3 providing a set of common functions for easy integration and extensibility.
JavaScript
2
star
64

Needle.js

Angular.js style dependency injection using reflection.
JavaScript
2
star
65

Biutiful

๐ŸŒฟ Biutiful transform ES imports into browser usable ECMAScript imports.
JavaScript
2
star
66

Shift.js

Use the shift key to select a range of elements, such as checkboxes.
JavaScript
2
star
67

Tail.cat

Modern e-mail client and SMTP/POP server using MongoDB, Ruby, Angular
JavaScript
2
star
68

Banter.js

Angular based real-time app for communicating with customers via IRC. Individual customers connect to a common IRC channel, giving staff members the ability to see all customers' messages, with the ability to respond directly and individually.
JavaScript
2
star
69

Tessellate

Small vanilla JavaScript module for gracefully removing floating elements from the page.
JavaScript
2
star
70

PolymerDroplet

Polymer.js adaptation of my popular EmberDroplet module for Ember.js.
JavaScript
2
star
71

DjangoExceptions

Handle and parse Django REST Framework validation messages with aplomb.
JavaScript
2
star
72

Moggy

Miniature ~2kb library that brings immutability to existing prototype functions employing the principle of least astonishment.
JavaScript
2
star
73

NodeURLImports

Transform browser URL imports into Node compatible import/require statements using local dependencies.
JavaScript
2
star
74

Viewport

Determine how much of an element is visible in the viewport.
JavaScript
1
star
75

Tdo

Terminal based todo app for managing today's tasks with gentle reminders
Rust
1
star
76

Memor

Use memoization for Rust functions to increase performance
Rust
1
star
77

ReduxLocal

Redux helper for maintaining pseudo-local state in a single tree.
JavaScript
1
star
78

Tidal.js

Socket.io benchmarking with realtime web-based statistics.
JavaScript
1
star
79

StickyRice

๐Ÿš React implementation to allow the natural position sticky behaviour in table headers and other elements
JavaScript
1
star
80

Kiwi.js

DRY interface for Angular.js tests in Jasmine/Karma.
JavaScript
1
star
81

Workex

1
star
82

Funkel

Simple and lightweight functional toolset inspired by Clojure using import.
JavaScript
1
star
83

Hylian

Quick and easy doubly and singly linked immutable list implementation that allows for inserting, removing and shifting.
JavaScript
1
star
84

DeveloperInk

Facilitates the use of ZURB's Ink for developers using the CLI with SASS, Email Testing, Compilation, etc... ๐Ÿ™
CSS
1
star
85

Mareos

MapReduce over WebSockets using Goroutines.
Go
1
star
86

WeakTree

๐ŸŒฒWeakMap implementation that allows for composite keys in a tree formation.
JavaScript
1
star
87

ava-webcomponents

Utility middleware for testing web components in AVA via Puppeteer.
JavaScript
1
star
88

SetOrder

Tiny module for sorting by a set order, using a custom sort function for omitting explicits.
JavaScript
1
star
89

Doogle

Node.js app for taking HTML snapshots of JavaScript pages to make your dynamic apps Google crawlable.
JavaScript
1
star
90

Paramo

๐ŸŒตSwiss-army knife of stringifying, parsing and manipulating URL parameters by applying types to the parameters.
JavaScript
1
star
91

Regrowth

๐Ÿ”ฌRegrowth is a monstrous laboratory experiment in container queries brought to life.
JavaScript
1
star
92

Termtodo

Todo app for the terminal for keeping reminders for later
Rust
1
star
93

Honey.js

Simple JavaScript library with auto-updating templates.
JavaScript
1
star
94

ReactAutolist

Browser native implementation of autocomplete using the datalist element.
JavaScript
1
star
95

Relayed

Convenient Node.js app for circumventing CORS issues when developing on localhost.
JavaScript
1
star
96

Cinematic.js

Experimental module for video sequence scrolling with zero dependencies.
JavaScript
1
star
97

gulp-envy

Gulp plugin for transferring your chosen environment variables to objects, with module loader, globals, angular, and other strategies.
1
star
98

redux-nest

Redux middleware for wrapping store in a Proxy to help with complex nested states.
JavaScript
1
star
99

Async

Yet another simple Promises/A+ compliant async flow control using ES6 generators.
JavaScript
1
star
100

TravelMap

๐ŸŒ Pin visited locations on a simple-to-use map, as well as future places you'd love to see. Then share with the world!
Python
1
star