Skip to content

feature: add new hook useescClose for closing element in keypress #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/usehooks-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export * from './useTimeout'
export * from './useToggle'
export * from './useUnmount'
export * from './useWindowSize'

export * from './useEscclose'
1 change: 1 addition & 0 deletions packages/usehooks-ts/src/useEscclose/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useEscclose'
16 changes: 16 additions & 0 deletions packages/usehooks-ts/src/useEscclose/useEscclose.demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useState } from 'react'

import { useEscclose } from './useEscclose'

// Example Like Modal Component
export default function Component() {
const [isOpen, setIsOpen] = useState<boolean>(false)

const handleClose = () => {
setIsOpen(false)
}

useEscclose(isOpen, handleClose)

return <div>Put a Component to close on esc(ex. modal)</div>
}
5 changes: 5 additions & 0 deletions packages/usehooks-ts/src/useEscclose/useEscclose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A simple hook to close element on esc press

Related hooks:

- [`useOnClickOutside()`](/react-hook/use-on-click-outside)
71 changes: 71 additions & 0 deletions packages/usehooks-ts/src/useEscclose/useEscclose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { act, renderHook } from '@testing-library/react'

import { useEscclose } from './useEscclose'

describe('useEscclose()', () => {
let onCloseMock: jest.Mock

beforeEach(() => {
onCloseMock = jest.fn()
})

afterEach(() => {
jest.clearAllMocks()
})

it('should not call onClose if isOpen is false', () => {
renderHook(() => {
useEscclose(false, onCloseMock)
})

act(() => {
const event = new KeyboardEvent('keydown', { key: 'Escape' })
window.dispatchEvent(event)
})

expect(onCloseMock).not.toHaveBeenCalled()
})

it('should call onClose when ESC key is pressed and isOpen is true', () => {
renderHook(() => {
useEscclose(true, onCloseMock)
})

act(() => {
const event = new KeyboardEvent('keydown', { key: 'Escape' })
window.dispatchEvent(event)
})

expect(onCloseMock).toHaveBeenCalledTimes(1)
})

it('should not call onClose for other keys', () => {
renderHook(() => {
useEscclose(true, onCloseMock)
})

act(() => {
const event = new KeyboardEvent('keydown', { key: 'Enter' })
window.dispatchEvent(event)
})

expect(onCloseMock).not.toHaveBeenCalled()
})

it('should clean up the event listener on unmount', () => {
const { unmount } = renderHook(() => {
useEscclose(true, onCloseMock)
})

act(() => {
unmount()
})

act(() => {
const event = new KeyboardEvent('keydown', { key: 'Escape' })
window.dispatchEvent(event)
})

expect(onCloseMock).not.toHaveBeenCalled()
})
})
32 changes: 32 additions & 0 deletions packages/usehooks-ts/src/useEscclose/useEscclose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect } from 'react'

/**
* Custom hook that set handler to close element by ESC.
* @param {boolean} [isOpen] - Boolean value if element is open.
* @param {() => void} onClose - Function Logic to execute when closed.
* @example
* ```tsx
* const [isOpen, setIsOpen] = useState<boolean>(false);
*
* const closeHandler = () => {
* setIsOpen(false);
* // ...Things to do when close
* }
*
* const = useEscclose(isOpen, closeHandler)
* ```
*/
export function useEscclose(isOpen: boolean, onClose: () => void): void {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (isOpen && event.key === 'Escape') {
onClose()
}
}

window.addEventListener('keydown', handleKeyDown)
return () => {
window.removeEventListener('keydown', handleKeyDown)
}
}, [isOpen, onClose])
}
Loading