Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-dismissable-layers

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-dismissable-layers

React dismissable context and hook with layers (nesting) support

  • 0.4.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source


react-dismissable-layers

npm package npm downloads demos

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.


npm i react-dismissable-layers
yarn add react-dismissable-layers

Demo

Check out the Storybook Demo to see in action.

Screen Shot 2021-07-07 at 8 35 04 AM

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

Keywords

FAQs

Package last updated on 03 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc