Skip to content

Commit d44e808

Browse files
authored
[@mantine/hooks] use-hotkeys: Fix Escape key not being handled correctly in some browsers (#7928)
* [@matine/hooks] parse-hotkey.ts escapeHotkey fix add test code * [@mantine/hooks] fix: update Escape key mapping in parse-hotkey.ts and add tests - Change 'Escape' and 'Esc' mapping from 'esc' to 'escape' for consistency - Add 'esc' key to keyNameMap for backward compatibility - Add comprehensive tests for Escape key handling
1 parent c45813c commit d44e808

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

packages/@mantine/hooks/src/use-hotkeys/parse-hotkey.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,78 @@ describe('@mantine/hooks/use-hot-key/parse-hotkey', () => {
125125
key: '+',
126126
});
127127
});
128+
129+
it('handles Escape key correctly', () => {
130+
const escapeResult = parseHotkey('Escape');
131+
expect(escapeResult).toMatchObject({
132+
alt: false,
133+
ctrl: false,
134+
meta: false,
135+
mod: false,
136+
shift: false,
137+
key: 'escape',
138+
});
139+
140+
const escResult = parseHotkey('Esc');
141+
expect(escResult).toMatchObject({
142+
alt: false,
143+
ctrl: false,
144+
meta: false,
145+
mod: false,
146+
shift: false,
147+
key: 'esc',
148+
});
149+
150+
expect(
151+
getHotkeyMatcher('Escape')(
152+
new KeyboardEvent('keydown', {
153+
key: 'Escape',
154+
})
155+
)
156+
).toBe(true);
157+
158+
expect(
159+
getHotkeyMatcher('mod+Escape')(
160+
new KeyboardEvent('keydown', {
161+
ctrlKey: true,
162+
key: 'Escape',
163+
})
164+
)
165+
).toBe(true);
166+
167+
expect(
168+
getHotkeyMatcher('escape')(
169+
new KeyboardEvent('keydown', {
170+
key: 'Escape',
171+
})
172+
)
173+
).toBe(true);
174+
});
175+
176+
it('ensures compatibility with existing escape key usage', () => {
177+
expect(
178+
getHotkeyMatcher('esc')(
179+
new KeyboardEvent('keydown', {
180+
key: 'Escape',
181+
})
182+
)
183+
).toBe(true);
184+
185+
expect(
186+
getHotkeyMatcher('ESC')(
187+
new KeyboardEvent('keydown', {
188+
key: 'Escape',
189+
})
190+
)
191+
).toBe(true);
192+
193+
expect(
194+
getHotkeyMatcher('mod+esc')(
195+
new KeyboardEvent('keydown', {
196+
ctrlKey: true,
197+
key: 'Escape',
198+
})
199+
)
200+
).toBe(true);
201+
});
128202
});

packages/@mantine/hooks/src/use-hotkeys/parse-hotkey.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const keyNameMap: Record<string, string> = {
1919
ArrowRight: 'arrowright',
2020
ArrowUp: 'arrowup',
2121
ArrowDown: 'arrowdown',
22-
Escape: 'esc',
23-
Esc: 'esc',
22+
Escape: 'escape',
23+
Esc: 'escape',
24+
esc: 'escape',
2425
Enter: 'enter',
2526
Tab: 'tab',
2627
Backspace: 'backspace',

0 commit comments

Comments
 (0)