npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-mount

v0.1.3

Published

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

Downloads

251

Readme

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 savely 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.