@delangle/use-modal
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "@delangle/use-modal", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"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,3 +61,3 @@ "@types/react": "^16.8.20", | ||
"react-test-renderer": "^16.9.0", | ||
"rimraf": "^2.6.3", | ||
"rimraf": "^3.0.0", | ||
"rollup-plugin-typescript2": "^0.22.0", | ||
@@ -65,0 +64,0 @@ "sinon": "^7.3.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 | ||
) { | ||
@@ -108,0 +115,0 @@ handleClose() |
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
1
17982
10
412