-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ onClickOut or onEscKey to handle modal #137
- Loading branch information
Showing
6 changed files
with
133 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@fessional/razor-common": patch | ||
"@fessional/razor-mobile": patch | ||
--- | ||
|
||
✨ onClickOut or onEscKey to handle modal #137 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { KeyFilter, MaybeElementRef } from '@vueuse/core'; | ||
import { onKeyDown, onClickOutside } from '@vueuse/core'; | ||
|
||
/** | ||
* listen PointerEvent or KeyboardEvent | ||
* | ||
* - click outside of the element | ||
* - keydown, default `Escape` | ||
*/ | ||
export function useClickoutKeydown(eleRef: MaybeElementRef, keys: KeyFilter = 'Escape', handler: (evt: PointerEvent | KeyboardEvent) => void) { | ||
let clickoutStub: (() => void) | null = null; | ||
let keydownStub: (() => void) | null = null; | ||
|
||
function active(force: boolean = false) { | ||
if (clickoutStub == null) { | ||
clickoutStub = onClickOutside(eleRef, handler); | ||
} | ||
else if (force) { | ||
clickoutStub(); | ||
clickoutStub = onClickOutside(eleRef, handler); | ||
} | ||
|
||
if (keydownStub == null) { | ||
keydownStub = onKeyDown(keys, handler); | ||
} | ||
else if (force) { | ||
keydownStub(); | ||
keydownStub = onKeyDown(keys, handler); | ||
} | ||
} | ||
|
||
function inactive() { | ||
if (clickoutStub != null) { | ||
clickoutStub(); | ||
clickoutStub = null; | ||
} | ||
if (keydownStub != null) { | ||
keydownStub(); | ||
keydownStub = null; | ||
} | ||
} | ||
|
||
return { active, inactive }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { describe, it, expect, vi, afterEach } from 'vitest'; | ||
import { ref } from 'vue'; | ||
import { onClickOutside, onKeyDown } from '@vueuse/core'; | ||
import { useClickoutKeydown } from '../composables/UseClickoutKeydown'; | ||
|
||
vi.mock('@vueuse/core', () => ({ | ||
onClickOutside: vi.fn((eleRef, handler) => { | ||
const callback = (evt: MouseEvent) => handler(evt); | ||
document.addEventListener('click', callback); | ||
return () => document.removeEventListener('click', callback); | ||
}), | ||
onKeyDown: vi.fn((keys, handler) => { | ||
const callback = (evt: KeyboardEvent) => handler(evt); | ||
document.addEventListener('keydown', callback); | ||
return () => document.removeEventListener('keydown', callback); | ||
}), | ||
})); | ||
|
||
describe('useClickoutKeydown', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should activate and listen for click outside events', () => { | ||
const handler = vi.fn(); | ||
const eleRef = ref(document.createElement('div')); | ||
const { active, inactive } = useClickoutKeydown(eleRef, 'Escape', handler); | ||
|
||
active(); | ||
|
||
// Simulate click outside | ||
const event = new MouseEvent('click', { bubbles: true }); | ||
document.dispatchEvent(event); | ||
|
||
expect(onClickOutside).toHaveBeenCalledWith(eleRef, expect.any(Function)); | ||
expect(handler).toHaveBeenCalledWith(event); | ||
|
||
inactive(); | ||
|
||
// Simulate click after inactive | ||
document.dispatchEvent(new MouseEvent('click', { bubbles: true })); | ||
expect(handler).toHaveBeenCalledTimes(1); // No additional calls | ||
}); | ||
|
||
it('should activate and listen for keydown events', () => { | ||
const handler = vi.fn(); | ||
const eleRef = ref(document.createElement('div')); | ||
const { active, inactive } = useClickoutKeydown(eleRef, 'Escape', handler); | ||
|
||
active(); | ||
|
||
// Simulate keydown | ||
const event = new KeyboardEvent('keydown', { key: 'Escape' }); | ||
document.dispatchEvent(event); | ||
|
||
expect(onKeyDown).toHaveBeenCalledWith('Escape', expect.any(Function)); | ||
expect(handler).toHaveBeenCalledWith(event); | ||
|
||
inactive(); | ||
|
||
// Simulate keydown after inactive | ||
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' })); | ||
expect(handler).toHaveBeenCalledTimes(1); // No additional calls | ||
}); | ||
|
||
it('should force rebind listeners when active is called with force=true', () => { | ||
const handler = vi.fn(); | ||
const eleRef = ref(document.createElement('div')); | ||
const { active } = useClickoutKeydown(eleRef, 'Escape', handler); | ||
|
||
active(); | ||
active(true); // Force rebinding | ||
|
||
expect(onClickOutside).toHaveBeenCalledTimes(2); | ||
expect(onKeyDown).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters