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-domnest

v0.0.10

Published

A lightweight utility for dynamically mounting React components using HTML data attributes. Provides synchronous and asynchronous component registration, flexible attribute naming, scoped updates, and support for React 18's createRoot API. Ideal for embed

Downloads

634

Readme

React DomNest

Overview

React DomNest is a lightweight utility designed for dynamically mounting React components using HTML data attributes. It provides a flexible and efficient way to register components, allowing for both synchronous and asynchronous loading. This is particularly useful for embedding React components in environments managed by Content Management Systems (CMS).

Features

  • Dynamic Component Registration: Easily register components using data attributes.
  • Synchronous and Asynchronous Loading: Supports both types of component loading.
  • Scoped Updates: Update components within specific scopes.
  • React 18 Support: Utilizes the createRoot API for optimal performance.

Installation

To get started with React DomNest, you can install it via npm:

npm install react-domnest

Usage

Prepare The Component

To prepare a component for use with React DomNest, you need to follow a few steps to ensure that your component is properly set up and can be dynamically registered and mounted. Here’s a guide on how to prepare your component:

  1. Create Your Component: Start by creating a React component that you want to use with DomNest. For example, you can create a simple functional component.
// src/components/component.js
import React from 'react';

const DemoComponent = ({ testProp }) => {
    return <div>{testProp}</div>;
};

export default DemoComponent;
  1. Export the Component: Ensure that your component is exported so that it can be imported and registered by DomNest.

  2. Create a Configuration File: Create a configuration file for your component. This file will be used to register the component with DomNest.

// src/components/config.domnest.js
import DemoComponent from './component';

export default {
    component: DemoComponent,
    name: "DemoComponent",
    async: true,
};

Registering the Components

To register the components, you can use the following code snippet:

import DomNest from 'react-domnest';

const getContexts = () => [
    require.context(
      './components/',
      true,
      /config\.domnest\.js$/
    ),
  ]
const contexts = getContexts()

const domNest = new DomNest();
domNest.context.registerComponents(contexts);
// Initialize and mount components
domNest.init(document);

HTML Setup

In your HTML file, you can specify the component and its props using data attributes:

<div data-component="DemoComponent" data-props='{"testProp": "DemoNest"}'></div>

Running the Demo

To run the demo component, you can use the http-server package. If you haven't installed it yet, you can do so globally:

npm install -g http-server

Once installed, navigate to the directory containing your index.html file and run:

http-server

This will start a local server, and you can access the demo in your browser at http://localhost:8080 (or the port specified by http-server).

Contributing

Contributions are welcome! If you have suggestions or improvements, feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.