Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 77872db

Browse files
authored
fix(modals): replace focus-trap by focus-lock (#69)
1 parent 5e82690 commit 77872db

File tree

5 files changed

+57
-51
lines changed

5 files changed

+57
-51
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"bundlesize": [
1717
{
1818
"path": "./packages/core-em/dist/smooth-ui-core-em.min.js",
19-
"maxSize": "18 kB"
19+
"maxSize": "20 kB"
2020
},
2121
{
2222
"path": "./packages/core-sc/dist/smooth-ui-core-sc.min.js",
23-
"maxSize": "18 kB"
23+
"maxSize": "20 kB"
2424
}
2525
],
2626
"devDependencies": {

packages/core-em/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"access": "public"
2727
},
2828
"dependencies": {
29-
"focus-trap": "^3.0.0",
3029
"polished": "^2.3.0",
3130
"prop-types": "^15.6.2",
31+
"react-focus-lock": "^1.16.0",
3232
"react-transition-group": "^2.5.0"
3333
},
3434
"peerDependencies": {

packages/core-sc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"access": "public"
2626
},
2727
"dependencies": {
28-
"focus-trap": "^3.0.0",
2928
"polished": "^2.3.0",
3029
"prop-types": "^15.6.2",
30+
"react-focus-lock": "^1.16.0",
3131
"react-transition-group": "^2.5.0"
3232
},
3333
"peerDependencies": {

packages/shared/core/Modal.js

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
22
import React from 'react'
33
import PropTypes from 'prop-types'
4-
import createFocusTrap from 'focus-trap'
4+
import FocusLock from 'react-focus-lock'
55
import { css, withTheme } from './styled-engine'
66
import { th, mixin } from './utils/system'
77
import Transition from './Transition'
@@ -51,23 +51,19 @@ class ModalComponent extends React.Component {
5151

5252
handleDialogRef = dialogNode => {
5353
if (dialogNode) {
54-
const { initialFocusRef } = this.props
5554
this.disposeAriaHider = createAriaHider(dialogNode)
56-
this.trap = createFocusTrap(dialogNode, {
57-
initialFocus: initialFocusRef
58-
? () => initialFocusRef.current
59-
: undefined,
60-
fallbackFocus: this.contentRef.current,
61-
escapeDeactivates: false,
62-
clickOutsideDeactivates: false,
63-
})
64-
this.trap.activate()
6555
} else {
66-
this.trap.deactivate()
6756
this.disposeAriaHider()
6857
}
6958
}
7059

60+
onFocusLockActivation = () => {
61+
const { initialFocusRef } = this.props
62+
if (initialFocusRef && initialFocusRef.current) {
63+
initialFocusRef.current.focus()
64+
}
65+
}
66+
7167
render() {
7268
const {
7369
className,
@@ -92,31 +88,33 @@ class ModalComponent extends React.Component {
9288
if (status === 'exited') return null
9389
return (
9490
<Portal>
95-
<div
96-
className={cx(
97-
'sui-modal',
98-
`sui-modal-transition-${status}`,
99-
className,
100-
)}
101-
ref={this.handleDialogRef}
102-
onClick={wrapEvent(onClick, event => {
103-
event.stopPropagation()
104-
onClose()
105-
})}
106-
onKeyDown={wrapEvent(onKeyDown, event => {
107-
if (event.key === 'Escape') {
91+
<FocusLock returnFocus onActivation={this.onFocusLockActivation}>
92+
<div
93+
className={cx(
94+
'sui-modal',
95+
`sui-modal-transition-${status}`,
96+
className,
97+
)}
98+
ref={this.handleDialogRef}
99+
onClick={wrapEvent(onClick, event => {
108100
event.stopPropagation()
109101
onClose()
110-
}
111-
})}
112-
{...props}
113-
>
114-
<ModalContext.Provider
115-
value={{ contentRef: this.contentRef, onClose }}
102+
})}
103+
onKeyDown={wrapEvent(onKeyDown, event => {
104+
if (event.key === 'Escape') {
105+
event.stopPropagation()
106+
onClose()
107+
}
108+
})}
109+
{...props}
116110
>
117-
{children}
118-
</ModalContext.Provider>
119-
</div>
111+
<ModalContext.Provider
112+
value={{ contentRef: this.contentRef, onClose }}
113+
>
114+
{children}
115+
</ModalContext.Provider>
116+
</div>
117+
</FocusLock>
120118
</Portal>
121119
)
122120
}}

yarn.lock

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5820,13 +5820,10 @@ flush-write-stream@^1.0.0:
58205820
inherits "^2.0.1"
58215821
readable-stream "^2.0.4"
58225822

5823-
focus-trap@^3.0.0:
5824-
version "3.0.0"
5825-
resolved "https://registry.yarnpkg.com/focus-trap/-/focus-trap-3.0.0.tgz#4d2ee044ae66bf7eb6ebc6c93bd7a1039481d7dc"
5826-
integrity sha512-jTFblf0tLWbleGjj2JZsAKbgtZTdL1uC48L8FcmSDl4c2vDoU4NycN1kgV5vJhuq1mxNFkw7uWZ1JAGlINWvyw==
5827-
dependencies:
5828-
tabbable "^3.1.0"
5829-
xtend "^4.0.1"
5823+
focus-lock@^0.5.0:
5824+
version "0.5.0"
5825+
resolved "https://registry.yarnpkg.com/focus-lock/-/focus-lock-0.5.0.tgz#6cf24e1091bf7e2aa6704f61f7b0ee0a5f4cf012"
5826+
integrity sha512-EtQvprh8R8ulU6x0qPLVMDHLHRcD8Dx+flVT8fllK70ls4q8Uagt5FKmikLBraYZZxzQoQSGl4JBMQK0miqMbg==
58305827

58315828
follow-redirects@1.0.0:
58325829
version "1.0.0"
@@ -10660,6 +10657,13 @@ re-resizable@^4.9.0:
1066010657
resolved "https://registry.yarnpkg.com/re-resizable/-/re-resizable-4.9.0.tgz#0160728b9d7e32371fb26562221678ae643e6557"
1066110658
integrity sha512-AkTHHC/I1+MUnabFu3/9ADwR5A+HyjiL3xgqlcgNKdyJZVb851I7sGre/4JIU7XfhaN5t+xZBvJPOuvEdvSMcw==
1066210659

10660+
react-clientside-effect@^1.0.0:
10661+
version "1.0.0"
10662+
resolved "https://registry.yarnpkg.com/react-clientside-effect/-/react-clientside-effect-1.0.0.tgz#81c4f91bd4ec0f8792ba76fe29c1d09e37fff062"
10663+
integrity sha512-XYOjooNlAEos60o4ha9zDb1R9h3LMJUdXJOjFH6fNUwFxXu2k+Wq4Cd0JWmurgeY0DWTzGvpNJ0cY6eHyfhL1Q==
10664+
dependencies:
10665+
shallowequal "^1.1.0"
10666+
1066310667
react-codemirror2@^5.1.0:
1066410668
version "5.1.0"
1066510669
resolved "https://registry.yarnpkg.com/react-codemirror2/-/react-codemirror2-5.1.0.tgz#62de4460178adea40eb52eabf7491669bf3794b8"
@@ -10754,6 +10758,15 @@ react-feather@^1.1.3:
1075410758
resolved "https://registry.yarnpkg.com/react-feather/-/react-feather-1.1.4.tgz#d0143da95f9d52843cf13a553091573a7c617897"
1075510759
integrity sha512-Odke3WUl00+a2scGQKlep4m+qDESl/x+PsTRB3v6fAV/2dteF4EaRjqhX8h7HM3mU3V3v0xGTv8Z5S26bpQ5PQ==
1075610760

10761+
react-focus-lock@^1.16.0:
10762+
version "1.16.0"
10763+
resolved "https://registry.yarnpkg.com/react-focus-lock/-/react-focus-lock-1.16.0.tgz#62fe9971e562016a5b51e0f3ae78280b7012c53c"
10764+
integrity sha512-lWO4trLJKRGiI9Zt1tHLDr3Fboi0xsgQVVBaUddD7hgpVdF7r5G8xMnEE9kedN+gigpTIgC636if4QmuVjUtfw==
10765+
dependencies:
10766+
focus-lock "^0.5.0"
10767+
prop-types "^15.6.2"
10768+
react-clientside-effect "^1.0.0"
10769+
1075710770
react-hot-loader@4.3.8:
1075810771
version "4.3.8"
1075910772
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.3.8.tgz#81f174a9b70fb5a4d4f2e9134d73659ddcd779cf"
@@ -11801,7 +11814,7 @@ shallow-clone@^0.1.2:
1180111814
lazy-cache "^0.2.3"
1180211815
mixin-object "^2.0.1"
1180311816

11804-
shallowequal@^1.0.2:
11817+
shallowequal@^1.0.2, shallowequal@^1.1.0:
1180511818
version "1.1.0"
1180611819
resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"
1180711820
integrity sha512-y0m1JoUZSlPAjXVtPPW70aZWfIL/dSP7AFkRnniLCrK/8MDKog3TySTBmckD+RObVxH0v4Tox67+F14PdED2oQ==
@@ -12451,11 +12464,6 @@ symbol-tree@^3.2.2:
1245112464
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6"
1245212465
integrity sha1-rifbOPZgp64uHDt9G8KQgZuFGeY=
1245312466

12454-
tabbable@^3.1.0:
12455-
version "3.1.1"
12456-
resolved "https://registry.yarnpkg.com/tabbable/-/tabbable-3.1.1.tgz#db7512f28a9a1ed16e4275bd190131be9d5ad8e9"
12457-
integrity sha512-583MHIOwictf7+zbxqO/L5fBqMN6Li4SJ1XTKQA9WzHRA7c2BB+D+Ny7Y6kGqU2u+rHK59+oRzrBvMU53aZz+A==
12458-
1245912467
table@^4.0.3:
1246012468
version "4.0.3"
1246112469
resolved "https://registry.yarnpkg.com/table/-/table-4.0.3.tgz#00b5e2b602f1794b9acaf9ca908a76386a7813bc"

0 commit comments

Comments
 (0)