@delangle/use-modal
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -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 = 27; | ||
const DEFAULT_CONFIG = { | ||
animationDuration: 300, | ||
animated: false, | ||
persistent: false, | ||
open: false, | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
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; | ||
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; | ||
}); | ||
React.useLayoutEffect(() => { | ||
if (!configRef.current.animated && open !== isLocalOpened) { | ||
setLocalOpened(open); | ||
} | ||
}, [isLocalOpened, open]); | ||
const handleClose = React.useCallback(() => { | ||
if (configRef.current.onClose) { | ||
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.keyCode === 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 { ModalConfig, Modal } from './useModal.interface'; | ||
declare const useModal: <ContainerElement extends HTMLElement = HTMLDivElement>(baseConfig: Partial<ModalConfig<ContainerElement>>) => Modal<ContainerElement>; | ||
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 |
import * as React from 'react'; | ||
export declare type ModalConfig<ContainerElement extends HTMLElement = HTMLDivElement> = { | ||
export declare type ModalFullConfig<ContainerElement extends HTMLElement = HTMLDivElement> = { | ||
open: boolean; | ||
@@ -7,5 +7,6 @@ persistent: boolean; | ||
animationDuration: number; | ||
onClose?: () => void; | ||
onClose: () => void; | ||
ref?: React.Ref<ContainerElement>; | ||
}; | ||
export declare type ModalConfig<ContainerElement extends HTMLElement = HTMLDivElement> = Partial<ModalFullConfig<ContainerElement>>; | ||
export declare enum ModalState { | ||
@@ -12,0 +13,0 @@ opening = "opening", |
{ | ||
"name": "@delangle/use-modal", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "React hook for modal management", | ||
@@ -49,3 +49,2 @@ "private": false, | ||
"@testing-library/dom": "^6.0.1", | ||
"@testing-library/react": "^8.0.1", | ||
"@types/jest": "^24.0.15", | ||
@@ -62,4 +61,4 @@ "@types/react": "^16.8.20", | ||
"react-test-renderer": "^16.9.0", | ||
"rimraf": "^2.6.3", | ||
"rollup-plugin-typescript2": "^0.22.0", | ||
"rimraf": "^3.0.0", | ||
"rollup-plugin-typescript2": "^0.23.0", | ||
"sinon": "^7.3.2", | ||
@@ -66,0 +65,0 @@ "ts-jest": "^24.0.2", |
import * as React from 'react' | ||
export type ModalConfig< | ||
export type ModalFullConfig< | ||
ContainerElement extends HTMLElement = HTMLDivElement | ||
@@ -10,6 +10,10 @@ > = { | ||
animationDuration: number | ||
onClose?: () => void | ||
onClose: () => void | ||
ref?: React.Ref<ContainerElement> | ||
} | ||
export type ModalConfig< | ||
ContainerElement extends HTMLElement = HTMLDivElement | ||
> = Partial<ModalFullConfig<ContainerElement>> | ||
export enum ModalState { | ||
@@ -16,0 +20,0 @@ opening = 'opening', |
import * as React from 'react' | ||
import { ModalConfig, Modal, ModalState } from './useModal.interface' | ||
import { | ||
ModalConfig, | ||
ModalFullConfig, | ||
Modal, | ||
ModalState, | ||
} from './useModal.interface' | ||
const ESCAPE_KEY = 27 | ||
const ESCAPE_KEY = 'Escape' | ||
const DEFAULT_CONFIG: Omit<ModalConfig<HTMLElement>, 'ref'> = { | ||
const DEFAULT_CONFIG: Omit<ModalFullConfig<HTMLElement>, 'ref'> = { | ||
animationDuration: 300, | ||
@@ -12,4 +17,8 @@ animated: false, | ||
open: false, | ||
onClose: () => {}, | ||
} | ||
const useSSRLayoutEffect = | ||
typeof window === 'object' ? React.useLayoutEffect : React.useEffect | ||
const useMergedRef = <RefElement>( | ||
@@ -36,3 +45,3 @@ ref: React.Ref<RefElement> | null | undefined | ||
const shouldDelayOpening = !hasAlreadyRendered.current && animated | ||
const shouldDelayOpening = !hasAlreadyRendered.current && animated && open | ||
@@ -55,5 +64,5 @@ const [canBeOpened, setCanBeOpened] = React.useState<boolean>( | ||
const useModal = <ContainerElement extends HTMLElement = HTMLDivElement>( | ||
baseConfig: Partial<ModalConfig<ContainerElement>> | ||
baseConfig: ModalConfig<ContainerElement> | ||
): Modal<ContainerElement> => { | ||
const config: ModalConfig<ContainerElement> = { | ||
const config: ModalFullConfig<ContainerElement> = { | ||
...DEFAULT_CONFIG, | ||
@@ -75,3 +84,3 @@ ...baseConfig, | ||
React.useLayoutEffect(() => { | ||
useSSRLayoutEffect(() => { | ||
if (!configRef.current.animated && open !== isLocalOpened) { | ||
@@ -83,5 +92,3 @@ setLocalOpened(open) | ||
const handleClose = React.useCallback(() => { | ||
if (configRef.current.onClose) { | ||
configRef.current.onClose() | ||
} | ||
configRef.current.onClose() | ||
}, []) | ||
@@ -105,3 +112,3 @@ | ||
configRef.current.open && | ||
e.keyCode === ESCAPE_KEY | ||
e.key === ESCAPE_KEY | ||
) { | ||
@@ -115,3 +122,3 @@ handleClose() | ||
!configRef.current.persistent && | ||
configRef.current.open && | ||
isLocalOpened && | ||
domRef.current && | ||
@@ -131,3 +138,3 @@ !domRef.current.contains(e.target as Node) | ||
} | ||
}, [domRef, handleClose]) | ||
}, [domRef, handleClose, isLocalOpened]) | ||
@@ -134,0 +141,0 @@ const state = React.useMemo<ModalState>(() => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
19
22
1
29561
571