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

v0.0.3

Published

Makes React components overridable.

Downloads

3,697

Readme

react-overridable

With react-overridable you can mark your React components as overridable and allow other apps to customize them. This can be useful when creating libraries with a default implementation of components which requires to be overridden at runtime.

You can inject new props, override render elements or the component itself.

Usage

Create a React component and mark it as overridable:

import PropTypes from 'prop-types';
import React, {Component} from 'react';
import Overridable, {parametrize, OverridableContext} from 'react-overridable';

class TitleComponent extends Component {
  static propTypes = {
    title: PropTypes.string.isRequired,
    children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
  };

  static defaultProps = {
    children: null,
  };

  render() {
    const {title, children} = this.props;
    return (
      <Overridable id="TitleComponent.container" title={title}>
        <>
          <div>{title}</div>
          {children}
        </>
      </Overridable>
    );
  }
}

export const OverridableExampleComponent = Overridable.component('TitleComponent', TitleComponent);

In this example, the TitleComponent is marked as overridable inside the render function, via the React component <Overridable /> and then exported via the Higher-Order component Overridable.component. Each overridable component is identified by a unique id.

After marking components as overridable, there are 3 ways that you can use to override:

  1. Provide new props with parametrize: define new props to override the default component props.
const parametrized = parametrize(OverridableExampleComponent, {
  title: 'My new title',
});
// create a map {<component id>: <parametrized props>}
const overriddenComponents = {TitleComponent: parametrized};
  1. Provide new render elements: override the default rendered elements for the marked sections. Props are passed and can be used in the new template.
const overriddenRenderElement = ({title}) => (
  <h1>{title}</h1>
);
// create a map {<render element id>: <new render elements>}
const overriddenComponents = {TitleComponent.container: overriddenRenderElement};
  1. Provide a new component: you can replace the existing component with a new one.
const NewComponent = () => <strong>This is a new title</strong>;
// create a map {<component id>: <new component>}
const overriddenComponents = {TitleComponent: NewComponent};

In your app, inject the map of ids-components in the React Context OverridableContext so that the react-overridable library can use it and replace components when the default are rendered:

class App extends Component {
  render() {
    return (
      <OverridableContext.Provider value={overriddenComponents}>
        <....>
      </OverridableContext.Provider>
    )
  }
}

Install

To install the library, you will have to install the peer dependencies.

npm i react-overridable
npm i <peer dependencies>

Development

To run tests:

npm run test

To build the library:

npm run build

Note

In applying the MIT license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction.