Skip to content

Commit

Permalink
(focus-trap) Remove possibility to provide escape as a function.
Browse files Browse the repository at this point in the history
It is unnecessary, as the user of the library can easily listen for `Esc` key presses by themselves.

As a side note, the implementation of the previous behaviour was buggy.
(The e2e tests will be refactored and estended soon)
  • Loading branch information
DaviDevMod committed May 9, 2023
1 parent a5aa0c1 commit 2c17880
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .changeset/clean-buttons-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@davidevmod/focus-trap': patch
---

Remove possibility to provide `escape` as a function.

It is unnecessary, as the user of the library can easily listen for `Esc` key presses by themselves.

As a side note, the implementation of the previous behaviour was buggy.
10 changes: 5 additions & 5 deletions packages/focus-trap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Trap the focus within your DOM elements.

- Trap the focus within a group of DOM elements
- Choose an element receiving the initial focus
- Decide whether to prevent clicks on elements outside of the trap
- Possibility to customise the behaviour of <kbd>Esc</kbd> key presses
- Customise the behaviour of clicks on elements outside of the trap
- Decide whether to demolish a trap after an <kbd>Esc</kbd> key press
- Choose an element receiving the focus after a trap is demolished
- Build, demolish, pause and resume your focus trap at any time

Expand Down Expand Up @@ -37,7 +37,7 @@ interface TrapConfig {
initialFocus?: boolean | Focusable | string;
returnFocus?: boolean | Focusable | string;
lock?: boolean | Function;
escape?: boolean | Function;
escape?: boolean;
}

type TrapAction = 'PAUSE' | 'RESUME' | 'DEMOLISH';
Expand Down Expand Up @@ -98,7 +98,7 @@ You can tweak the behaviour of your trap by providing a `TrapConfig`:
| initialFocus | No | `boolean \| Focusable \| string` | `true` |
| returnFocus | No | `boolean \| Focusable \| string` | `true` |
| lock | No | `boolean \| Function` | `true` |
| escape | No | `boolean \| Function` | `true` |
| escape | No | `boolean` | `true` |

- **roots**
The array of elements (and/or IDs) within which the focus has to be trapped.
Expand All @@ -125,7 +125,7 @@ You can tweak the behaviour of your trap by providing a `TrapConfig`:
- **escape**
The behaviour for <kbd>Esc</kbd> key presses.
By default the trap is demolished Whenever the <kbd>Esc</kbd> key is pressed.
You can provide the boolean `false` to switch off the default behaviour or alternatively your own handler for the keyboard events in question.
You can provide the boolean `false` to switch off the default behaviour.

## TrapAction

Expand Down
13 changes: 3 additions & 10 deletions packages/focus-trap/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ const handleKeyPress = (event: KeyboardEvent): void => {
event.preventDefault();
destination.value.focus();
}
} else if (
(event.key === 'Escape' || event.key === 'Esc' || event.keyCode === 27) &&
state.normalisedConfig?.escape !== false
) {
demolish(true);
} else if (event.key === 'Escape' || event.key === 'Esc' || event.keyCode === 27) {
if (state.normalisedConfig?.escape) demolish(true);
}
};

Expand All @@ -43,11 +40,7 @@ export const eventListeners = (action: 'ADD' | 'REMOVE'): void => {
REMOVE: 'removeEventListener' as keyof Document,
};

(document[listenerActions[action]] as Function)(
'keydown',
state.normalisedConfig?.escape instanceof Function ? state.normalisedConfig.escape : handleKeyPress,
true
);
(document[listenerActions[action]] as Function)('keydown', handleKeyPress, true);

if (state.normalisedConfig?.lock) {
for (const event of ['mousedown', 'touchstart', 'click']) {
Expand Down
4 changes: 2 additions & 2 deletions packages/focus-trap/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface TrapConfig {
initialFocus?: boolean | Focusable | string;
returnFocus?: boolean | Focusable | string;
lock?: boolean | Function;
escape?: boolean | Function;
escape?: boolean;
}

// The shape of config used internally by the focus trap.
Expand All @@ -23,7 +23,7 @@ export interface NormalisedTrapConfig {
initialFocus: boolean | Focusable;
returnFocus: Focusable | null;
lock: boolean | Function;
escape: boolean | Function;
escape: boolean;
}

interface State {
Expand Down

0 comments on commit 2c17880

Please sign in to comment.