@transferwise/components 0.0.0-experimental-9a61b0a → 0.0.0-experimental-73858ea

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- import { ReactNode } from 'react';
1
+ /// <reference types="react" />
2
2
  type FocusBoundaryProps = {
3
- children: ReactNode;
3
+ children: React.ReactNode;
4
4
  };
5
5
  declare const FocusBoundary: ({ children }: FocusBoundaryProps) => import("react").JSX.Element;
6
6
  export default FocusBoundary;
@@ -1 +1 @@
1
- {"version":3,"file":"FocusBoundary.d.ts","sourceRoot":"","sources":["../../../../src/common/focusBoundary/FocusBoundary.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAA+B,MAAM,OAAO,CAAC;AAS/D,KAAK,kBAAkB,GAAG;IACxB,QAAQ,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF,QAAA,MAAM,aAAa,iBAAkB,kBAAkB,gCA6BtD,CAAC;AAEF,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"FocusBoundary.d.ts","sourceRoot":"","sources":["../../../../src/common/focusBoundary/FocusBoundary.tsx"],"names":[],"mappings":";AAGA,KAAK,kBAAkB,GAAG;IACxB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,CAAC;AAEF,QAAA,MAAM,aAAa,iBAAkB,kBAAkB,gCAatD,CAAC;AAEF,eAAe,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transferwise/components",
3
- "version": "0.0.0-experimental-9a61b0a",
3
+ "version": "0.0.0-experimental-73858ea",
4
4
  "description": "Neptune React components",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -103,6 +103,7 @@
103
103
  "@headlessui/react": "^1.7.17",
104
104
  "@popperjs/core": "^2.6.0",
105
105
  "@radix-ui/react-id": "^1.0.0",
106
+ "@react-aria/focus": "^3.14.2",
106
107
  "@react-aria/overlays": "^3.17.0",
107
108
  "@react-spring/web": "~9.6.1",
108
109
  "@transferwise/formatting": "^2.1.0",
@@ -7,9 +7,12 @@ exports[`BottomSheet renders content when open 1`] = `
7
7
  class="np-theme-light"
8
8
  >
9
9
  <div
10
- class="np-focus-boundary outline-none"
11
10
  tabindex="-1"
12
11
  >
12
+ <span
13
+ data-focus-scope-start="true"
14
+ hidden=""
15
+ />
13
16
  <div
14
17
  class="dimmer"
15
18
  role="presentation"
@@ -63,6 +66,10 @@ exports[`BottomSheet renders content when open 1`] = `
63
66
  </div>
64
67
  </div>
65
68
  </div>
69
+ <span
70
+ data-focus-scope-end="true"
71
+ hidden=""
72
+ />
66
73
  </div>
67
74
  </div>
68
75
  </body>
@@ -1,44 +1,21 @@
1
- import { isKey, isUndefined } from '@transferwise/neptune-validation';
2
- import { ReactNode, useEffect, useRef, useState } from 'react';
3
-
4
- import { useConditionalListener } from '../hooks';
5
- import { Key } from '../key';
6
-
7
- import { getFocusableElements, resetFocus } from './utils';
8
-
9
- const { TAB } = Key;
1
+ import { FocusScope } from '@react-aria/focus';
2
+ import { useEffect, useRef } from 'react';
10
3
 
11
4
  type FocusBoundaryProps = {
12
- children: ReactNode;
5
+ children: React.ReactNode;
13
6
  };
14
7
 
15
8
  const FocusBoundary = ({ children }: FocusBoundaryProps) => {
16
- const boundaryReference = useRef<HTMLDivElement | null>(null);
17
- const parent = isUndefined(document) ? undefined : document;
18
- const [focusableEls, setFocusableEls] = useState({});
19
-
9
+ const wrapperReference = useRef<HTMLDivElement>(null);
20
10
  useEffect(() => {
21
- if (boundaryReference?.current) {
22
- boundaryReference.current.focus({ preventScroll: true });
23
- setFocusableEls(getFocusableElements(boundaryReference.current));
24
- }
11
+ wrapperReference.current?.focus({ preventScroll: true });
25
12
  }, []);
26
13
 
27
- // If event type is Tab the resetFocus will force the focus to either the first focusable or last in boundaryRef .
28
- useConditionalListener({
29
- eventType: 'keydown',
30
- callback: (event) => {
31
- if (isKey({ keyType: TAB, event })) {
32
- resetFocus({ event, focusableEls });
33
- }
34
- },
35
- attachListener: true,
36
- parent,
37
- });
38
-
39
14
  return (
40
- <div ref={boundaryReference} tabIndex={-1} className="np-focus-boundary outline-none">
41
- {children}
15
+ <div ref={wrapperReference} tabIndex={-1}>
16
+ <FocusScope contain restoreFocus>
17
+ {children}
18
+ </FocusScope>
42
19
  </div>
43
20
  );
44
21
  };
@@ -1,2 +0,0 @@
1
- export function getFocusableElements(focusBoundaryContainer: Node): object;
2
- //# sourceMappingURL=getFocusableElements.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getFocusableElements.d.ts","sourceRoot":"","sources":["../../../../../src/common/focusBoundary/utils/getFocusableElements.js"],"names":[],"mappings":"AAOO,6DAJI,IAAI,GACF,MAAM,CAoBlB"}
@@ -1,3 +0,0 @@
1
- export { getFocusableElements } from "./getFocusableElements";
2
- export { resetFocus } from "./resetFocus";
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/common/focusBoundary/utils/index.js"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export function resetFocus({ focusableEls: { first, last }, event }: object): void;
2
- //# sourceMappingURL=resetFocus.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"resetFocus.d.ts","sourceRoot":"","sources":["../../../../../src/common/focusBoundary/utils/resetFocus.js"],"names":[],"mappings":"AAOO,qEAJI,MAAM,QAmBhB"}
@@ -1,66 +0,0 @@
1
- import { render, screen, userEvent } from '../../test-utils';
2
-
3
- import FocusBoundary from './FocusBoundary';
4
-
5
- const props = {
6
- onClose: jest.fn(),
7
- };
8
-
9
- describe('FocusBoundary', () => {
10
- beforeEach(() => {
11
- jest.clearAllMocks();
12
- });
13
- it('renders component', () => {
14
- const { container } = render(
15
- <FocusBoundary {...props}>
16
- <a href="test">Test</a>
17
- </FocusBoundary>,
18
- );
19
-
20
- expect(container).toMatchSnapshot();
21
- });
22
-
23
- it('focus on container by default', () => {
24
- render(
25
- <>
26
- <a href="test">not focusable</a>
27
- <FocusBoundary {...props}>
28
- <a href="test">Test</a>
29
- </FocusBoundary>
30
- </>,
31
- );
32
-
33
- expect(focusBoundary()).toHaveFocus();
34
- });
35
-
36
- it('traps the focus when user presses tab', () => {
37
- render(
38
- <>
39
- <a href="test">not focusable</a>
40
- <FocusBoundary {...props}>
41
- <a href="test">firstFocusable</a>
42
- <a href="test">lastFocusable</a>
43
- </FocusBoundary>
44
- </>,
45
- );
46
-
47
- userEvent.tab();
48
- expect(firstFocusableElement()).toHaveFocus();
49
-
50
- userEvent.tab();
51
- expect(lastFocusableElement()).toHaveFocus();
52
-
53
- userEvent.tab();
54
- expect(firstFocusableElement()).toHaveFocus();
55
-
56
- userEvent.tab({ shift: true });
57
- expect(lastFocusableElement()).toHaveFocus();
58
-
59
- userEvent.tab({ shift: true });
60
- expect(firstFocusableElement()).toHaveFocus();
61
- });
62
-
63
- const firstFocusableElement = () => screen.getByText('firstFocusable');
64
- const lastFocusableElement = () => screen.getByText('lastFocusable');
65
- const focusBoundary = () => document.querySelector('.np-focus-boundary');
66
- });
@@ -1,16 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`FocusBoundary renders component 1`] = `
4
- <div>
5
- <div
6
- class="np-focus-boundary outline-none"
7
- tabindex="-1"
8
- >
9
- <a
10
- href="test"
11
- >
12
- Test
13
- </a>
14
- </div>
15
- </div>
16
- `;
@@ -1,25 +0,0 @@
1
- /**
2
- * This function returns the first and the last focusable elements within a node.
3
- *
4
- * @param {Node} focusBoundaryContainer - the area that contains focused elements.
5
- * @returns {object} focusableEls - which contains the first focusable element and the last focusable element. First and last can be the same element if area contains one or none focusable element.
6
- */
7
-
8
- export const getFocusableElements = (focusBoundaryContainer) => {
9
- const focusableEls = { first: focusBoundaryContainer, last: focusBoundaryContainer };
10
-
11
- if (focusBoundaryContainer?.querySelectorAll) {
12
- const allEls = [
13
- ...focusBoundaryContainer.querySelectorAll(
14
- 'a, button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])',
15
- ),
16
- ].filter((element) => !element.hasAttribute('disabled'));
17
-
18
- if (allEls.length > 0) {
19
- [focusableEls.first] = allEls;
20
- focusableEls.last = allEls[allEls.length - 1];
21
- }
22
- }
23
-
24
- return focusableEls;
25
- };
@@ -1,51 +0,0 @@
1
- import { cleanup, render, screen } from '../../../test-utils';
2
-
3
- import { getFocusableElements } from './getFocusableElements';
4
-
5
- describe('getFocusableElements', () => {
6
- afterEach(() => {
7
- cleanup();
8
- });
9
- it('returns first and last focusable element', () => {
10
- const { container } = render(
11
- <>
12
- <div>another element</div>
13
- <a href="test">first</a>
14
- <div>another element</div>
15
- <a href="test">middle</a>
16
- <div>another element</div>
17
- <a href="test">last</a>
18
- <div>another element</div>
19
- </>,
20
- );
21
- getFocusableElements(container);
22
- expect(getFocusableElements(container)).toStrictEqual({
23
- first: first(),
24
- last: last(),
25
- });
26
- });
27
-
28
- it('returns container if there are no focusable elements', () => {
29
- const { container } = render(
30
- <>
31
- <div>another element</div>
32
- </>,
33
- );
34
- getFocusableElements(container);
35
- expect(getFocusableElements(container)).toStrictEqual({
36
- first: container,
37
- last: container,
38
- });
39
- });
40
-
41
- it('returns undefined if container is not provided', () => {
42
- getFocusableElements();
43
- expect(getFocusableElements()).toStrictEqual({
44
- first: undefined,
45
- last: undefined,
46
- });
47
- });
48
-
49
- const first = () => screen.getByText('first');
50
- const last = () => screen.getByText('last');
51
- });
@@ -1,2 +0,0 @@
1
- export { getFocusableElements } from './getFocusableElements';
2
- export { resetFocus } from './resetFocus';
@@ -1,23 +0,0 @@
1
- /**
2
- * This function resets the focus to either last of first focusable elements within a node.
3
- *
4
- * @param {object} focusableEls - contains the first last of first focusable elements within a node.
5
- * @param {object} event - the triggered event
6
- */
7
-
8
- export const resetFocus = ({ focusableEls: { first, last }, event }) => {
9
- const { activeElement } = document;
10
- if (event.shiftKey && activeElement === first) {
11
- if (last) {
12
- last.focus();
13
- }
14
- event.preventDefault();
15
- }
16
-
17
- if (!event.shiftKey && activeElement === last) {
18
- if (first) {
19
- first.focus();
20
- }
21
- event.preventDefault();
22
- }
23
- };
@@ -1,103 +0,0 @@
1
- import { cleanup, render, screen, userEvent } from '../../../test-utils';
2
- import { fakeKeyDownEvent } from '../../fakeEvents';
3
-
4
- import { resetFocus } from './resetFocus';
5
-
6
- describe('resetFocus', () => {
7
- beforeEach(() => {
8
- render(
9
- <>
10
- <a href="test">first</a>
11
- <a href="test">middle</a>
12
- <a href="test">last</a>
13
- </>,
14
- );
15
- });
16
- afterEach(() => {
17
- cleanup();
18
- });
19
- it('set focus to first element when last element is focused and event is tab', () => {
20
- focusOnLast();
21
-
22
- resetFocus({
23
- focusableEls: { first: first(), last: last() },
24
- event: fakeKeyDownEvent({ shiftKey: false }),
25
- });
26
- expect(first()).toHaveFocus();
27
- });
28
-
29
- it('set focus to last element when first element is focused and event is shift + tab', () => {
30
- focusOnFirst();
31
-
32
- resetFocus({
33
- focusableEls: { first: first(), last: last() },
34
- event: fakeKeyDownEvent({ shiftKey: true }),
35
- });
36
- expect(last()).toHaveFocus();
37
- });
38
-
39
- it(`doesn't change focus when first element is focused and event is tab`, () => {
40
- focusOnFirst();
41
-
42
- resetFocus({
43
- focusableEls: { first: first(), last: last() },
44
- event: fakeKeyDownEvent(),
45
- });
46
- expect(first()).toHaveFocus();
47
- });
48
-
49
- it(`doesn't change focus when last element is focused and event is shift+tab`, () => {
50
- focusOnLast();
51
-
52
- resetFocus({
53
- focusableEls: { first: first(), last: last() },
54
- event: fakeKeyDownEvent({ shiftKey: true }),
55
- });
56
- expect(last()).toHaveFocus();
57
- });
58
-
59
- it(`doesn't change focus when middle element is focused and event is shift+tab or tab`, () => {
60
- focusOnMiddle();
61
-
62
- resetFocus({
63
- focusableEls: { first: first(), last: last() },
64
- event: fakeKeyDownEvent({ shiftKey: true }),
65
- });
66
- expect(middle()).toHaveFocus();
67
-
68
- resetFocus({
69
- focusableEls: { first: first(), last: last() },
70
- event: fakeKeyDownEvent(),
71
- });
72
- expect(middle()).toHaveFocus();
73
- });
74
-
75
- it(`doesn't change focus when first is the only element provided`, () => {
76
- focusOnFirst();
77
- resetFocus({
78
- focusableEls: { first: first(), last: undefined },
79
- event: fakeKeyDownEvent({ shiftKey: true }),
80
- });
81
- expect(first()).toHaveFocus();
82
- });
83
-
84
- it(`doesn't change focus when last is the only element provided`, () => {
85
- focusOnLast();
86
- resetFocus({
87
- focusableEls: { first: undefined, last: last() },
88
- event: fakeKeyDownEvent(),
89
- });
90
- expect(last()).toHaveFocus();
91
- });
92
-
93
- const first = () => screen.getByText('first');
94
- const last = () => screen.getByText('last');
95
- const middle = () => screen.getByText('middle');
96
-
97
- const focusOnFirst = () => userEvent.tab();
98
- const focusOnLast = () => userEvent.tab({ shift: true });
99
- const focusOnMiddle = () => {
100
- userEvent.tab();
101
- userEvent.tab();
102
- };
103
- });