Skip to content

Commit

Permalink
(focus-trap) Remove exceptions.ts and throw errors even in production.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviDevMod committed May 29, 2023
1 parent 1fdb94d commit bcb45df
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-seals-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@davidevmod/focus-trap': major
---

Remove exceptions.ts and throw errors even in production.
7 changes: 0 additions & 7 deletions packages/focus-trap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,6 @@ interface NormalisedTrapConfig {

</details>

## Error handling

A focus trap is a huge gain in matter of accessibility, but it's not strictly necessary for an application to be functional.
That's why **this library chooses not to throw errors in production**.

Also note that the error handling available in environments other than _production_ does not cover type checking, as the library takes advantage of being written in **TypeScript**.

## Dependencies & Browser Support

The are no dependencies and the library can run in any major browser.
Expand Down
5 changes: 2 additions & 3 deletions packages/focus-trap/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { throwInEnv } from './exceptions.js';
import { state, reducers } from './state.js';
import { isFocusable } from './tabbability.js';
import { getDestination } from './destination.js';
Expand All @@ -19,11 +18,11 @@ const handleKeyPress = (event: KeyboardEvent): void => {

const rootsUpdate = reducers.updateRoots();

if (rootsUpdate.isErr) return throwInEnv(rootsUpdate.error);
if (rootsUpdate.isErr) throw new Error(rootsUpdate.error);

const destination = getDestination(state.normalisedConfig!.roots, target, shiftKey ? 'BACKWARD' : 'FORWARD');

if (destination.isErr) return throwInEnv(destination.error);
if (destination.isErr) throw new Error(destination.error);

if ('focus' in destination.value) {
event.preventDefault();
Expand Down
18 changes: 0 additions & 18 deletions packages/focus-trap/src/exceptions.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/focus-trap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Roots, TrapConfig, NormalisedTrapConfig } from './state.js';
import { throwInEnv } from './exceptions.js';
import { build, resume, demolish, pause } from './trap-actions.js';
import { state } from './state.js';

Expand All @@ -17,7 +16,7 @@ export const focusTrap = (arg: TrapArg) => {
: pause()
: build(Array.isArray(arg) ? { roots: arg } : arg);

if (result.isErr) throwInEnv(result.error);
if (result.isErr) throw new Error(result.error);

return { ...state.normalisedConfig } as NormalisedTrapConfig;
};
Expand Down
7 changes: 3 additions & 4 deletions packages/focus-trap/src/normalise.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Result, err, ok } from 'true-myth/result';

import type { Focusable, Roots, TrapConfig, NormalisedTrapConfig } from './state.js';
import { warnInEnv } from './exceptions.js';
import { isFocusable } from './tabbability.js';

const resolveId = <T>(arg: T) =>
Expand All @@ -10,15 +9,15 @@ const resolveId = <T>(arg: T) =>
const isValidRoot = (root: unknown): root is Focusable => {
const isValid = isFocusable(root);

if (!isValid) warnInEnv(`${root} is not a valid root.`);
if (!isValid) console.warn(`${root} is not a valid root.`);

return isValid;
};

const isNotNestedRoot = (root: Focusable, _index: number, roots: Focusable[]) => {
const isNotNested = roots.every((anotherRoot) => !anotherRoot.contains(root) || anotherRoot === root);

if (!isNotNested) warnInEnv(`${root} is contained by another root.`);
if (!isNotNested) console.warn(`${root} is contained by another root.`);

return isNotNested;
};
Expand All @@ -27,7 +26,7 @@ const dedupeRoots = (roots: Focusable[]) => {
const dedupedRoots = Array.from(new Set(roots));

if (dedupedRoots.length !== roots.length) {
warnInEnv('Duplicate elements were found in the "roots" array. They have been deduplicated.');
console.warn('Duplicate elements were found in the "roots" array. They have been deduplicated.');
}

return dedupedRoots;
Expand Down

0 comments on commit bcb45df

Please sign in to comment.