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-dismissable-layers

v0.3.2

Published

React dismissable context and hook with layers (nesting) support

Downloads

976

Readme

maintained by @voiceflow

Context and hook to add support for nested, auto-dismissable layers. State can be globally controlled through context. Best used with react-popper.

Demo

Check out the Storybook Demo to see in action.

Quick Start

Add <DismissableLayersGlobalProvider> on a parent component. Use the useDismissable() hook to associate different toggleable components.

import { useDismissable } from 'react-dismissable-layers';

// open and close
const Component = () => {
  const [open, toggleOpen] = useDismissable(false);

  return (
    <div>
      <button onClick={toggleOpen}>Open Tooltip</button>
      {open && <Popper>Tooltip Content</Popper>}
    </div>
  );
};
import { DismissableLayerContext } from 'react-dismissable-layers';

// close all dismissibles in context
const OtherComponent = () => {
  const dismissOverlay = React.useContext(DismissableLayerContext);

  const close = React.useCallback(() => {
    dismissOverlay.dismissAllGlobally();
  }, []);

  return <button onClick={close}>Close All</button>;
};

API

  • DismissableLayersGlobalProvider - global provider for Dismissable Layers, wrap the whole app to make sure the useDismissable hook works with layers.

    interface DismissableLayersGlobalProviderProps {
      /**
       * optional prop, the HTML-node to listen close events, default is `document`
       */
      rootNode?: HTMLElement | Document;
    }
    
    const DismissableLayersGlobalProvider = React.FC<DismissableLayersGlobalProviderProps>;
  • useDismissable - a hook to toggle and dismiss poppers.

    interface Options {
      /**
       * ref for the popper content, to not close on the content's [dismissEvent] action
       */
      ref?: RefObject<Element>;
    
      /**
       * callback which will be invoked when the popper is closed
       */
      onClose?: null | VoidFunction;
    
      /**
       * event on which popper will be closed, default is `'click'`
       */
      dismissEvent?: DismissEventType;
    
      /**
       * the popper will be closed just by the [dismissEvent] action, without any layers logic, default is `false`
       */
      disableLayers?: boolean;
    
      /**
       * do not close on default prevented events, default is `true`
       */
      skipDefaultPrevented?: boolean;
    }
    
    type Api = readonly [
      isOpened: boolean,
    
      /**
       * function to toggle popper
       */
      toggle: VoidFunction,
    
      /**
       * function to force close popper
       */
      close: VoidFunction
    ];
    
    const useDismissable = (defaultValue = false, options: Options = {}) => Api;
  • DismissableLayerContext - a context to read a dissmissable layer, in most cases shouldn't be used in app layer.

    interface DismissableLayerValue<T extends HTMLElement | Document = Document> {
      /**
       * for internal usage only
       */
      readonly _subscriber: Subscriber;
    
      /**
       * root node of the dismiss layer
       */
      readonly rootNode: T;
    
      /**
       * dismiss currently opened in the current layer
       */
      readonly dismiss: VoidFunction;
    
      /**
       * has handler on the current layer
       */
      readonly hasHandler: () => boolean;
    
      /**
       * add close handler to the current layer
       */
      readonly addHandler: (eventType: DismissEventType, handler: DismissEventHandler) => void;
    
      /**
       * remove close handler from the current layer
       */
      readonly removeHandler: (eventType: DismissEventType) => void;
    
      /**
       * dismiss all on all layers
       */
      readonly dismissAllGlobally: VoidFunction;
    
      /**
       * has subscriber on any layer
       */
      readonly hasHandlersGlobally: () => boolean;
    }
    
    const DismissableLayersGlobalProvider = React.FC<DismissableLayersGlobalProviderProps>;
  • DismissableLayerProvider - provider for Dismissable Layer, wrap the popper content to make the nested poppers works as a nested ones.

    interface DismissableLayerProviderProps {
      /**
       * optional prop, the HTML-node to listen close events, default is `document`
       */
      rootNode?: HTMLElement | Document;
    }
    
    const DismissableLayerProvider = React.FC<DismissableLayerProviderProps>;