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

v0.0.3

Published

A library for handling the render state for components

Downloads

211

Readme

react-visualize is a library designed to manage the render state of multiple UI elements, such as modals, drawers, or toast notifications. It provides an easy way to register, show, hide, and manage visual elements in your React applications with various customizable options, like animations, backdrops, and positioning.

Features

  • Multi-element Rendering: Register and manage multiple visual elements simultaneously.
  • Customizable Options: Easily set animations, backdrop color, positioning, and more.
  • Singleton Pattern: Expose the register, show, and hide functions globally for easy use.
  • Global State Management: Handle the state of visual elements through a centralized reducer.

Installation

npm install react-visualize

Usage

Step 1: Wrap Your App with VisualizeProvider

To use react-visualize, wrap your application with the VisualizeProvider component. This ensures the context is available globally.

import React from "react";
import ReactDOM from "react-dom";
import { VisualizeProvider } from "react-visualize";
import App from "./App";

ReactDOM.render(
  <VisualizeProvider>
    <App />
  </VisualizeProvider>,
  document.getElementById("root")
);

Step 2: Register Elements

You can register visual elements using the registerElement or registerElements function. Each element must have a unique id.

import { registerElement, showElement } from "react-visualize";

function ExampleComponent() {
  const handleRegister = () => {
    registerElement("exampleModal", <MyModalComponent />, {
      showBackdrop: true,
      backdropColor: "rgba(0, 0, 0, 0.5)",
      animation: {
        shown: { opacity: 1 },
        hidden: { opacity: 0 },
        always: { transition: "all 0.5s ease-in-out" },
      },
    });
  };

  const handleShow = () => {
    showElement("exampleModal");
  };

  return (
    <div>
      <button onClick={handleRegister}>Register Modal</button>
      <button onClick={handleShow}>Show Modal</button>
    </div>
  );
}

Step 3: Show or Hide Elements

Use showElement or hideElement functions to control the visibility of a registered element.

import { showElement, hideElement } from "react-visualize";

function ExampleComponent() {
  const handleShow = () => {
    showElement("exampleModal");
  };

  const handleHide = () => {
    hideElement("exampleModal");
  };

  return (
    <div>
      <button onClick={handleShow}>Show Modal</button>
      <button onClick={handleHide}>Hide Modal</button>
    </div>
  );
}

Example Element Component

Your visual elements, such as modals or popovers, are rendered automatically by the Container component in the library, which is part of the VisualizeProvider. Each element is styled and positioned based on the options provided during registration.

API

registerElement(id: string, body: ReactNode, options?: ElementOptions) Registers a new element.

  • id: A unique identifier for the element.
  • body: The content to be rendered.
  • options: Optional configuration options for the element.

registerElements(elements: { id: string; body: ReactNode; options?: ElementOptions }[]) Registers multiple elements at once.

showElement(id: string) Shows a registered element by its unique id.

hideElement(id: string) Hides a registered element by its unique id.

useVisualize() A custom hook to access the state of the visual elements. Must be used within the VisualizeProvider.

Options

When registering an element, you can pass the following ElementOptions: | Option | Type | Description | Default Value | | ------ | ---- | ----------- | ------------- | | position | Position | Sets the position of the element on the screen. | { top: "50%", left: "50%", transform: "translate(-50%, -50%)" } | | showBackdrop | boolean | Whether to show a backdrop behind the element. | true | | backdropColor | string | Sets the color of the backdrop. | "rgba(0, 0, 0, 0.5)" | | clickBackdropToClose | boolean | Whether clicking on the backdrop hides the element. | true | | animation | Animation | CSS properties to animate showing and hiding. | {} | | backdropAnimation | Animation | CSS properties for the backdrop animation. | {} | | timeout | number | Duration in milliseconds to auto-hide the element. | undefined |

License

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

Github

https://github.com/mbb10324/react-visualize