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

redux-plugin

v0.0.10

Published

Redux architecture for 'plugin' react components

Downloads

33

Readme

Redux Plugin

Redux sub-architecture for 'plugin' React components.

Installation

npm install redux-plugin --save

Motivation

Several times you will be writing reducers that manages multiple homogeneous state trees. For example, I wrote a PopOver component whose state is managed by redux. This component and it's reducer handles the basic tasks:

  • Open / Close specific popover
  • Open / Close all popovers
  • Open / Close specific group of popovers
  • Render another component as the popover content

Each instance of the PopOver component has it's own redux state sub-tree, coordinated by the popover reducer. The popover reducer use reducer composition to handle each popover individual state.

redux-plugin helps you defining plugin components and composed plugin reducers.

Key concepts

redux-plugin supposes a special mindset about components: the plugin components. I encourage reading this section before jumping to the library, in order to see if this specific mindset fits your project.

Plugin reducer

The plugin reducer manages multiple instances of each plugin element state. For example, the popover plugin reducer manages the following part of the state:

{
  ...,
  popover: {
    navNotifications: {
      isOpen: false,
      groups: ['popovers', 'tour-1'],
      configuration: {
        vertical: "below",
        horizontal: "center",
        withTip: true
      }
    },
    submitTooltip: {
      isOpen: true,
      groups: ['tooltips', 'tour-2'],
      configuration: {
        vertical: "above",
        horizontal: "center",
        withTip: true
      }
    },
    formError: {
      isOpen: false,
      groups: ['errors'],
      configuration: {
        vertical: "above",
        horizontal: "right",
        offset: 25,
        withTip: true
      }
    }
  }
}

The creation of a plugin reducer happens with a higher order reducer provided by redux-plugin:


import { pluginReducer as plugin } from 'redux-plugin';

const popover = plugin({
  name: 'popover',
  prefix: '@@redux-popover',
  reducer: popoverElement
});

If the plugin reducer receives a global action (ie. paylod has not the id key), it iterates and passes down the action to all the plugin element reducers.

Plugin element reducer

It's a plain old reducer that manages the state for each plugin element (you just have to think of the state transitions of one popover!).

import { combineReducers } from 'redux';

const popoverElement = combineReducers({
  isOpen,
  groups,
  configuration
});

Plugin element

Each instance of a plugin. It's state is managed by the element reducer, but proxied by the plugin reducer.

Plugin element component

Dummy (presentational) component that takes simple props and renders the a plugin element instance (you just have to think the rendering of one popover!).

Plugin component

Smart (connected) component that takes an id as prop, fetches the specific plugin element state and passes it down to the plugin element component. This component is generated by a higher order component.

import { pluginComponent as plugin } from 'redux-plugin';
import { register } from './actions';
import Popover from './components/popover';

export const ReduxPopover = plugin({
  defaultStateKey: 'popover',
  registerPluginElement: register
})(Popover);

Registration action creator

Simple action creator that handles the registration of a new instance of a plugin (plugin element). The signature of this action creator should be (id, initialState), where id is the plugin element id, and initialState is the first state of this plugin element.

export const register = (id, { isOpen = false, groups = [], configuration }) => ({
  type: types.POPOVER_REGISTERED,
  payload: { id, isOpen, groups, configuration }
});

Plugin

A plugin is the combination of a plugin reducer and a plugin component.

Use

Read the descriptions above to understand how to create the Popover component, then the following code should make sense:

import { Popover } from './components/popover';

const PopoverContent = () => (
  <div>
    <h1>Hi!</h1>
    <p>This is the popover content!</p>
  </div>
);

const View = () => (
  <Popover
    id='buttonPopover'
    initialState={ { isOpen: true } }
    content={ PopoverContent }>
    <button>Open!</button>
  </Popover>
);

Contributors

@samuelchvez

License

Copyright (c) 2017 Samuel Chávez

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.