mirror of
https://github.com/vercel/commerce.git
synced 2025-06-18 13:11:23 +00:00
A nice focus trap :D
This commit is contained in:
parent
51a25b640f
commit
4f3ce796d1
@ -7,7 +7,7 @@ import {
|
|||||||
enableBodyScroll,
|
enableBodyScroll,
|
||||||
clearAllBodyScrollLocks,
|
clearAllBodyScrollLocks,
|
||||||
} from 'body-scroll-lock'
|
} from 'body-scroll-lock'
|
||||||
import FocusTrap from 'focus-trap-react'
|
import FocusTrap from '@lib/focus-trap'
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string
|
className?: string
|
||||||
children?: any
|
children?: any
|
||||||
@ -45,21 +45,19 @@ const Modal: FC<Props> = ({ children, open, onClose, onEnter = null }) => {
|
|||||||
return (
|
return (
|
||||||
<Portal>
|
<Portal>
|
||||||
{open ? (
|
{open ? (
|
||||||
<FocusTrap>
|
<div role="dialog" className={s.root} ref={ref}>
|
||||||
<div className={s.root} ref={ref}>
|
<div className={s.modal}>
|
||||||
<div className={s.modal}>
|
<button
|
||||||
<button
|
onClick={() => onClose()}
|
||||||
onClick={() => onClose()}
|
aria-label="Close panel"
|
||||||
aria-label="Close panel"
|
className="hover:text-gray-500 transition ease-in-out duration-150 focus:outline-none absolute right-0 top-0 m-6"
|
||||||
className="hover:text-gray-500 transition ease-in-out duration-150 focus:outline-none absolute right-0 top-0 m-6"
|
>
|
||||||
>
|
<Cross className="h-6 w-6" />
|
||||||
<Cross className="h-6 w-6" />
|
</button>
|
||||||
</button>
|
|
||||||
|
|
||||||
<div>{children}</div>
|
<FocusTrap>{children}</FocusTrap>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</FocusTrap>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</Portal>
|
</Portal>
|
||||||
)
|
)
|
||||||
|
37
lib/focus-trap.tsx
Normal file
37
lib/focus-trap.tsx
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import React, { useEffect, RefObject } from 'react'
|
||||||
|
import { tabbable } from 'tabbable'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
children: React.ReactNode | any
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function FocusTrap({ children }: Props) {
|
||||||
|
const root: RefObject<any> = React.useRef()
|
||||||
|
const anchor: RefObject<any> = React.useRef(document.activeElement)
|
||||||
|
|
||||||
|
const returnFocus = () => {
|
||||||
|
if (anchor) {
|
||||||
|
anchor.current.focus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const trapFocus = () => {
|
||||||
|
const focusableElements = tabbable(root.current)
|
||||||
|
|
||||||
|
if (focusableElements[0]) {
|
||||||
|
focusableElements[0].focus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setTimeout(trapFocus, 20)
|
||||||
|
return returnFocus
|
||||||
|
}, [root, children])
|
||||||
|
|
||||||
|
return React.createElement('div', {
|
||||||
|
ref: root,
|
||||||
|
children,
|
||||||
|
className: 'outline-none focus-trap',
|
||||||
|
tabIndex: -1,
|
||||||
|
})
|
||||||
|
}
|
7965
package-lock.json
generated
Normal file
7965
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -52,7 +52,6 @@
|
|||||||
"bowser": "^2.11.0",
|
"bowser": "^2.11.0",
|
||||||
"classnames": "^2.2.6",
|
"classnames": "^2.2.6",
|
||||||
"email-validator": "^2.0.4",
|
"email-validator": "^2.0.4",
|
||||||
"focus-trap-react": "^8.3.2",
|
|
||||||
"js-cookie": "^2.2.1",
|
"js-cookie": "^2.2.1",
|
||||||
"keen-slider": "^5.2.4",
|
"keen-slider": "^5.2.4",
|
||||||
"lodash.random": "^3.2.0",
|
"lodash.random": "^3.2.0",
|
||||||
@ -66,6 +65,7 @@
|
|||||||
"react-intersection-observer": "^8.30.1",
|
"react-intersection-observer": "^8.30.1",
|
||||||
"react-merge-refs": "^1.1.0",
|
"react-merge-refs": "^1.1.0",
|
||||||
"react-ticker": "^1.2.2",
|
"react-ticker": "^1.2.2",
|
||||||
|
"tabbable": "^5.1.5",
|
||||||
"tailwindcss": "^1.9"
|
"tailwindcss": "^1.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -8,6 +8,11 @@ module.exports = {
|
|||||||
'./pages/**/*.{js,ts,jsx,tsx}',
|
'./pages/**/*.{js,ts,jsx,tsx}',
|
||||||
'./components/**/*.{js,ts,jsx,tsx}',
|
'./components/**/*.{js,ts,jsx,tsx}',
|
||||||
],
|
],
|
||||||
|
options: {
|
||||||
|
safelist: {
|
||||||
|
standard: ['outline-none'],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
theme: {
|
theme: {
|
||||||
extend: {
|
extend: {
|
||||||
|
16
yarn.lock
16
yarn.lock
@ -2047,20 +2047,6 @@ flatten@^1.0.2:
|
|||||||
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.3.tgz#c1283ac9f27b368abc1e36d1ff7b04501a30356b"
|
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.3.tgz#c1283ac9f27b368abc1e36d1ff7b04501a30356b"
|
||||||
integrity sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==
|
integrity sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==
|
||||||
|
|
||||||
focus-trap-react@^8.3.2:
|
|
||||||
version "8.3.2"
|
|
||||||
resolved "https://registry.yarnpkg.com/focus-trap-react/-/focus-trap-react-8.3.2.tgz#b319888b57c7d7e63ce9f5c9a3b687df64c03369"
|
|
||||||
integrity sha512-sy4zUjlyC2szBejQ9RVf6WCeA+9fWbxrK6fo+jnrMvfiP5rbuzuK6461V6Q7YsEsHwZolVL3AthlfFfIvZrPwg==
|
|
||||||
dependencies:
|
|
||||||
focus-trap "^6.2.2"
|
|
||||||
|
|
||||||
focus-trap@^6.2.2:
|
|
||||||
version "6.2.3"
|
|
||||||
resolved "https://registry.yarnpkg.com/focus-trap/-/focus-trap-6.2.3.tgz#1f1443063f08241020d4ffb269bab9847ada97b7"
|
|
||||||
integrity sha512-87orNbj6UqKEDxmqDfYBDLBbqgxNIA5cXUBvHCt7dZ8/L0KTuzkjKoI0xXHU+5TyI9fImqfOJyvEkXYmZeClZA==
|
|
||||||
dependencies:
|
|
||||||
tabbable "^5.1.4"
|
|
||||||
|
|
||||||
forwarded@~0.1.2:
|
forwarded@~0.1.2:
|
||||||
version "0.1.2"
|
version "0.1.2"
|
||||||
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
|
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
|
||||||
@ -4477,7 +4463,7 @@ swr@0.3.6:
|
|||||||
dependencies:
|
dependencies:
|
||||||
dequal "2.0.2"
|
dequal "2.0.2"
|
||||||
|
|
||||||
tabbable@^5.1.4:
|
tabbable@^5.1.5:
|
||||||
version "5.1.5"
|
version "5.1.5"
|
||||||
resolved "https://registry.yarnpkg.com/tabbable/-/tabbable-5.1.5.tgz#efec48ede268d511c261e3b81facbb4782a35147"
|
resolved "https://registry.yarnpkg.com/tabbable/-/tabbable-5.1.5.tgz#efec48ede268d511c261e3b81facbb4782a35147"
|
||||||
integrity sha512-oVAPrWgLLqrbvQE8XqcU7CVBq6SQbaIbHkhOca3u7/jzuQvyZycrUKPCGr04qpEIUslmUlULbSeN+m3QrKEykA==
|
integrity sha512-oVAPrWgLLqrbvQE8XqcU7CVBq6SQbaIbHkhOca3u7/jzuQvyZycrUKPCGr04qpEIUslmUlULbSeN+m3QrKEykA==
|
||||||
|
Loading…
x
Reference in New Issue
Block a user