Socket
Socket
Sign inDemoInstall

@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 0.2.1 to 1.0.0

dist/index.js.map

124

dist/index.js

@@ -1,116 +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);
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(() => {
const handleKeyDown = (e) => {
if (!configRef.current.persistent &&
configRef.current.open &&
e.key === ESCAPE_KEY) {
handleClose();
}
};
const handleClick = (e) => {
if (!configRef.current.persistent &&
configRef.current.open &&
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]);
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]);
return {
state,
close: handleClose,
ref: domRef,
hasAlreadyBeenOpened,
};
};
exports.default = useModal;
Object.defineProperty(exports, "__esModule", { value: true });
const 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 { ModalFullConfig, Modal } from './useModal.interface';
import { Modal, ModalFullConfig } 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": "0.2.1",
"version": "1.0.0",
"description": "React hook for modal management",

@@ -61,3 +61,3 @@ "private": false,

"rimraf": "^3.0.0",
"rollup-plugin-typescript2": "^0.22.0",
"rollup-plugin-typescript2": "^0.24.0",
"sinon": "^7.3.2",

@@ -64,0 +64,0 @@ "ts-jest": "^24.0.2",

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

@@ -77,2 +77,18 @@ } 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(() => {

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

React.useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (
!configRef.current.persistent &&
configRef.current.open &&
e.key === ESCAPE_KEY
) {
handleClose()
if (state === ModalState.opened) {
const handleKeyDown = (e: KeyboardEvent) => {
if (
!configRef.current.persistent &&
configRef.current.open &&
e.key === ESCAPE_KEY
) {
handleClose()
}
}
}
const handleClick = (e: MouseEvent) => {
if (
!configRef.current.persistent &&
configRef.current.open &&
domRef.current &&
!domRef.current.contains(e.target as Node)
) {
handleClose()
const handleClick = (e: MouseEvent) => {
if (
!configRef.current.persistent &&
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])
}, [domRef, handleClose, state])
const state = React.useMemo<ModalState>(() => {
if (!open && !isLocalOpened) {
return ModalState.closed
}
console.log('STATE', state)
if (!open && isLocalOpened) {
return ModalState.closing
}
if (open && !isLocalOpened) {
return ModalState.opening
}
return ModalState.opened
}, [open, isLocalOpened])
return {

@@ -152,0 +155,0 @@ state,

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