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

@delangle/use-modal

Package Overview
Dependencies
Maintainers
1
Versions
383
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@delangle/use-modal - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

dist/index.js.map

125

dist/index.js

@@ -1,117 +0,10 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
(function (ModalState) {
ModalState["opening"] = "opening";
ModalState["opened"] = "opened";
ModalState["closing"] = "closing";
ModalState["closed"] = "closed";
})(exports.ModalState || (exports.ModalState = {}));
const ESCAPE_KEY = 'Escape';
const DEFAULT_CONFIG = {
animationDuration: 300,
animated: false,
persistent: false,
open: false,
onClose: () => { },
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const useSSRLayoutEffect = typeof window === 'object' ? React.useLayoutEffect : React.useEffect;
const useMergedRef = (ref) => {
const innerRef = React.useRef(null);
return (ref ? ref : innerRef);
};
const useHasAlreadyBeenOpened = (open) => {
const hasAlreadyBeenOpened = React.useRef(false);
if (!hasAlreadyBeenOpened.current && open) {
hasAlreadyBeenOpened.current = true;
}
return hasAlreadyBeenOpened.current;
};
const useDelayedOpen = (open, animated) => {
const hasAlreadyRendered = React.useRef(false);
const shouldDelayOpening = !hasAlreadyRendered.current && animated && open;
const [canBeOpened, setCanBeOpened] = React.useState(!shouldDelayOpening);
React.useEffect(() => {
hasAlreadyRendered.current = true;
if (!canBeOpened) {
setCanBeOpened(true);
}
}, [canBeOpened]);
return !!(canBeOpened && open);
};
const useModal = (baseConfig) => {
const config = {
...DEFAULT_CONFIG,
...baseConfig,
};
const open = useDelayedOpen(config.open, config.animated);
const hasAlreadyBeenOpened = useHasAlreadyBeenOpened(open);
const domRef = useMergedRef(config.ref);
const configRef = React.useRef(config);
const [isLocalOpened, setLocalOpened] = React.useState(false);
const state = React.useMemo(() => {
if (!open && !isLocalOpened) {
return exports.ModalState.closed;
}
if (!open && isLocalOpened) {
return exports.ModalState.closing;
}
if (open && !isLocalOpened) {
return exports.ModalState.opening;
}
return exports.ModalState.opened;
}, [open, isLocalOpened]);
React.useEffect(() => {
configRef.current = config;
});
useSSRLayoutEffect(() => {
if (!configRef.current.animated && open !== isLocalOpened) {
setLocalOpened(open);
}
}, [isLocalOpened, open]);
const handleClose = React.useCallback(() => {
configRef.current.onClose();
}, []);
React.useEffect(() => {
if (configRef.current.animated) {
const timeout = setTimeout(() => setLocalOpened(open), configRef.current.animationDuration);
return () => clearTimeout(timeout);
}
}, [open]);
React.useEffect(() => {
if (state === exports.ModalState.opened) {
const handleKeyDown = (e) => {
if (!configRef.current.persistent &&
configRef.current.open &&
e.key === ESCAPE_KEY) {
handleClose();
}
};
const handleClick = (e) => {
if (!configRef.current.persistent &&
domRef.current &&
!domRef.current.contains(e.target)) {
handleClose();
}
};
window.addEventListener('keydown', handleKeyDown);
window.addEventListener('click', handleClick);
return () => {
window.removeEventListener('keydown', handleKeyDown);
window.removeEventListener('click', handleClick);
};
}
}, [domRef, handleClose, state]);
return {
state,
close: handleClose,
ref: domRef,
hasAlreadyBeenOpened,
};
};
exports.default = useModal;
exports.__esModule = true;
var useModal_1 = __importDefault(require("./useModal"));
var useModal_interface_1 = require("./useModal.interface");
exports.ModalState = useModal_interface_1.ModalState;
exports["default"] = useModal_1["default"];
//# sourceMappingURL=index.js.map

@@ -1,4 +0,4 @@

import { Modal, ModalFullConfig } from './useModal.interface';
import { ModalFullConfig, Modal } from './useModal.interface';
declare const useModal: <ContainerElement extends HTMLElement = HTMLDivElement>(baseConfig: Partial<ModalFullConfig<ContainerElement>>) => Modal<ContainerElement>;
export default useModal;
//# sourceMappingURL=useModal.d.ts.map
{
"name": "@delangle/use-modal",
"version": "1.0.2",
"version": "1.1.0",
"description": "React hook for modal management",

@@ -5,0 +5,0 @@ "private": false,

import * as React from 'react'
import {
Modal,
ModalConfig,
ModalFullConfig,
Modal,
ModalState,

@@ -77,18 +77,2 @@ } from './useModal.interface'

const state = React.useMemo<ModalState>(() => {
if (!open && !isLocalOpened) {
return ModalState.closed
}
if (!open && isLocalOpened) {
return ModalState.closing
}
if (open && !isLocalOpened) {
return ModalState.opening
}
return ModalState.opened
}, [open, isLocalOpened])
React.useEffect(() => {

@@ -120,33 +104,48 @@ configRef.current = config

React.useEffect(() => {
if (state === ModalState.opened) {
const handleKeyDown = (e: KeyboardEvent) => {
if (
!configRef.current.persistent &&
configRef.current.open &&
e.key === ESCAPE_KEY
) {
handleClose()
}
const handleKeyDown = (e: KeyboardEvent) => {
if (
!configRef.current.persistent &&
configRef.current.open &&
e.key === ESCAPE_KEY
) {
handleClose()
}
}
const handleClick = (e: MouseEvent) => {
if (
!configRef.current.persistent &&
domRef.current &&
!domRef.current.contains(e.target as Node)
) {
handleClose()
}
const handleClick = (e: MouseEvent) => {
if (
!configRef.current.persistent &&
isLocalOpened &&
domRef.current &&
!domRef.current.contains(e.target as Node)
) {
handleClose()
}
}
window.addEventListener('keydown', handleKeyDown)
window.addEventListener('click', handleClick)
window.addEventListener('keydown', handleKeyDown)
window.addEventListener('click', handleClick)
return () => {
window.removeEventListener('keydown', handleKeyDown)
window.removeEventListener('click', handleClick)
}
return () => {
window.removeEventListener('keydown', handleKeyDown)
window.removeEventListener('click', handleClick)
}
}, [domRef, handleClose, state])
}, [domRef, handleClose, isLocalOpened])
const state = React.useMemo<ModalState>(() => {
if (!open && !isLocalOpened) {
return ModalState.closed
}
if (!open && isLocalOpened) {
return ModalState.closing
}
if (open && !isLocalOpened) {
return ModalState.opening
}
return ModalState.opened
}, [open, isLocalOpened])
return {

@@ -153,0 +152,0 @@ state,

@@ -12,3 +12,2 @@ {

"strict": true,
"target": "esnext",
"module": "commonjs",

@@ -15,0 +14,0 @@ "esModuleInterop": true,

Sorry, the diff of this file is not supported yet

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