Skip to content

React goes web component – Use custom tags to place react components directly in html.

License

Notifications You must be signed in to change notification settings

ph101pp/react-mount

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-mount

React goes web component – Use custom tags to place react components directly in html.

npm version Bower version

Install

Download

From Github, NPM:

$ npm install --save react-mount

or Bower:

$ bower install react-mount
Include

With AMD or Browserify:

var mount = require("react-mount");

Vanilla JS:

<script src="path/to/file/react-mount.js"></script>
<script>
	var mount = React.mount;
</script>

Basic Usage

<body>
	<react-component />
	
	<!-- Dependencies -->
	<script src="react.js"></script>
	<script src="JSXTransformer.js"></script>
	<script src="react-mount.js"></script>

	<script>
		// React component
		var ReactComponent = React.createClass({
			render: function() {
				return React.createElement("i", null, "Component mounted. React is running.")
			}
		});
		
		// Mount component
		React.mount({
			"react-component" : ReactComponent
		});
	</script>
</body>
Output

Component mounted. React is running.

Tag Content

Content of custom tags can be written in Html or JSX.

<translucent-component>
	<p style="text-transform: uppercase">
		Html is transformed to <span style="font-weight:bold">JSX</span> for you.
	</p>
</translucent-component>

The content of a custom tag is available in the mounted react component with this.props.children:

var translucentComponent = React.createClass({
	render: function() {
		return (
			{this.props.children}
		);
	}
});
Output

HTML IS TRANSFORMED TO JSX FOR YOU.

Nested Components

Custom component tags can be nested as long as all used components are properly mounted:

<translucent-component>
	<react-component />
</translucent-component>
mount({
	"react-component"		:	ReactComponent,
	"translucent-component"	:	TranslucentComponent
});
Output

Component mounted. React is running.

Expressions and Properties

{expressions} can be used within a tag and are executed properly.
Within {expressions} properties from the global namespace window can be accessed. However to keep the global namespace clear, properties can also be defined in the opts object passed to the mount function:

window.paragraph = "Component mounted. React is running.";

React.mount({
	"translucent-component" : ReactComponent
},
{
	props : {
		list : [
			  "Item 1",
			  "Item 2",
			  "Item 3"
		],
		attribute : "myAttribute"
	}
});

These properties are avaliable within expressions as props.key:

{window.paragraph} // "Component mounted. React is running."

{props.list.length <= 3 ? "Yes!" : "No!"} // Yes!

// shortcut for simple reference (props. is appended for you)
{attribute} 	// "myAttribute"
<translucent-component attribute={attribute}>
	<p>{window.paragraph}</p>
	<p>Less than four list items? </b>{props.list.length <= 3 ? "Yes!" : "No!"}</b></p>
	<ul>
		{props.list.map(function(value, i){
			return <li key={i} >{value}</li>;
		})}
	</ul>
</translucent-component>

Within react component:
this.props.attribute === "myAttribute"
this.props.children contains <p>...</p>, <p>...</p> and <ul>...</ul>.

Output

Component mounted. React is running.

Less than four list items? Yes!

  • Item 1
  • Item 2
  • Item 3

Case-Sensitive Attributes

Html is case insensitiv and transforms everything outside of strings to lowercase. React props however are case sensitive and therefore some components require correctly capitalized attributes.

There are two ways to preserve the capitalization of attributes:

A) Per component

mount({
	"react-component" : ["camelCaseAttribute", "anotherAttribute", ReactComponent]
});

B) For all components

mount({
	"react-component" : ReactComponent
}, {
	preserveAttributes : ["camelCaseAttribute", "anotherAttribute"]
});

In both cases you can now safely use the preserved attributes:

<react-component camelCaseAttribute="preserved attribute" notPreservedAttribute="not preserved" />

Within react component:
this.props.camelCaseAttribute === "preserved attribute"
but
this.props.notPreservedAttribute === undefined
this.props.notpreservedattribute === "not preserved"

HTML Comments

Html comments do not affect the output of the rendering in any way.
They can be used to mask unrendered content before react kicks in.

<translucent-component>
	<!--
		<i>{paragraph}</i>
	-->
</translucent-component>

this.props.children still contains <i>value</i>.

Output

value

Tipp: Use JSX style {/* comments */} for actual comments.

API

mount( tags [, opts] );

tags required

Type Object

Object with tags and their corresponding components to be mounted.

{
	"tag-name"	: 	ReactComponent,
	"tag-Name"	: 	ReactComponent,
  ...
	"TAG-NAME"	: 	["camelCaseAttribute", ReactComponent],
	...
	[tag]		: 	[component | Array]
}

Tag names are case insensitive. All the above definitions would do the same / the first component would be mounted to all of the tags.

Optional: Instead of a React component, an array with case-sensitive attributes can be defined. The last item of the array is the react component to be mounted.
See: Case-Sensitive Attributes

Tipp: Lower case tag names containing at least one dash are/will be valid html.
See: http://w3c.github.io/webcomponents/spec/custom/#concepts

opts optional

Type Object

Available options for react-mount:

{
	"context"				: 	[HTMLElement],
	"props"					: 	{...},
	"preserveAttributes"	: 	[...],
	"wrapper"				: 	[HTMLElement]
}
context

Type HTMLElement
Default document.body

Only tags within this element will be mounted.

props

Type Object

key:value pairs of Properties.
Properties can be used in {expressions} within the mounted tags.

See: Expressions and Properties

preserveAttributes

Type Array

Array of case-sensitive attributes to preserve capitatization.

See: Case-Sensitive Attributes

wrapper

Type HTMLElement
Default document.createElement("div")

Define a custom wrapper to mount react components into.

License

The MIT License (MIT)

Copyright (c) 2015 Philipp Adrian, philippadrian.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

React goes web component – Use custom tags to place react components directly in html.

Resources

License

Stars

Watchers

Forks

Packages

No packages published