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';
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';
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 {
rootNode?: HTMLElement | Document;
}
const DismissableLayersGlobalProvider = React.FC<DismissableLayersGlobalProviderProps>;
-
useDismissable
- a hook to toggle and dismiss poppers.
interface Options {
ref?: RefObject<Element>;
onClose?: null | VoidFunction;
dismissEvent?: DismissEventType;
disableLayers?: boolean;
skipDefaultPrevented?: boolean;
}
type Api = readonly [
isOpened: boolean,
toggle: VoidFunction,
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> {
readonly _subscriber: Subscriber;
readonly rootNode: T;
readonly dismiss: VoidFunction;
readonly hasHandler: () => boolean;
readonly addHandler: (eventType: DismissEventType, handler: DismissEventHandler) => void;
readonly removeHandler: (eventType: DismissEventType) => void;
readonly dismissAllGlobally: VoidFunction;
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 {
rootNode?: HTMLElement | Document;
}
const DismissableLayerProvider = React.FC<DismissableLayerProviderProps>;