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.1.1 to 0.2.0

5

package.json
{
"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",

8

src/useModal.interface.ts
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

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