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

mount-component

v1.3.0

Published

Mounts React and plain Javascript components to HTML-DOM elements

Downloads

1

Readme

Mount Component

Binds Javascript- and React-Components to HTML-DOM elements, making them usable similar to WebComponents. A "component" can be any Javascript-Constructor or React-Components.

mount-component also takes care about initial props, passed to each component. Props can be defined directly on the mounting-tags in a form of data-attributes. Any available data-attribute will be passed to the component's constructor. It's also possible to pass larger chunks of JSON data, by placing them in dedicated script-tags below or on the mounting-node (see below).

Features

Usage

<Slider data-show="3" data-content="{...}"></Slider>
<div class="grid" data-limit="12" data-offset="0" data-content="{...}"></div>

import {mountComponent} from 'mount-component';

// Mounts components to custom tags.
mountComponent('Slider', SliderComponent);
mountComponent('.grid', GridComponent);

Passing chunks of JSON-data

While it's possible to encode JSON into data-attributes and mount-component will actually handle decoding them for you. There are good reasons not to use attributes for larger data-structures. Instead mount-component recognizes dedicated JSON-blocks nested inside or on a mounting-tag.

Those Tags require to be script-tags with a type of "application/json" and an optional data-name-attribute set for naming the prop for the data. If you don't specify a data-name-attribute, the default prop-name will be data.

Example

<MyComponent data-title="Hello World!">
    <script type="application/json" data-name="my-prop">
        {
            "users": [
                {"name": "Jane", "age": 24},
                {"name": "John", "age": 21},
                ...
            ]
        }
    </script>
</MyComponent>

mountComponent('MyComponent', MyComponent);

// -> props: {
//      title: "Hello World",
//      myProp: {users: [ ... ]}
// }

There is also support for data-only Components, that don't have a visual aspect and therefor don't require to be mounted to a special DOM-element, but may need to be initialized with data from the DOM.

In that case, you can directly specify a script/Json tag as a mounting-element.

<script id="tracking" type="application/json">
    {
        "tracking": {
            ...
        }
    }
</script>

mountComponent('#tracking', TrackingComponent);

// -> props: {
//      data: {tracking: { ... }}
// }

API

mountComponent(selector, component)

Mounts a single component to any elements matching selector.

selector: string Selector to convert into react-components. Can be everything that querySelector accepts. component: React.Component|string|function The (un-instantiated) React-Component, it's name or any non-react function(node: Element, props: Object).

registerComponent(selector, component)

Registers a component and it's selector globally.

selector: string Selector to convert into react-components. Can be everything that querySelector accepts. component: React.Component|string|function The (un-instantiated) React-Component, it's name or any non-react function(node: Element, props: Object).

function mountAll()

Mounts all registered components.

Changelog

1.3.0

  • Constructor arguments on pure-JS components have been swapped, so that props come first.
  • The default Json-key has been changed from "json" to "data"
  • Script/Json-Tags can be mounted directly, without the need of an additional wrapper.
  • A JSON-tag's prop-name can now only be specified through the attribute data-name.

Authors

License

This project is licensed under the ISC License.