如何使用JS中的React中创建TAB陷阱?
我有一个模态窗口,当用户按在该过程中的某个按钮上时将打开。
为了访问,我需要该选项卡在模式窗口内。
现在,不幸的是,该标签在所有页面上都可以使用。
如何在模态内捕获选项卡?
演示: https://codesandbox.io/s/jolly/s/jolly/s/jolly/s/jolly/s/jolly -wood-pequk1?file =/src/app.js
孩子道具将是我为每个页面动态传递的两个按钮组件。
import React from 'react'
import E from './Modal.style'
const Modal = (props) => {
const {
children,
modalHeader,
modalText,
left,
} = props
window.onload = () => {
const firstAnchor = document.getElementById('dialog-start');
const lastAnchor = document.getElementById('dialog-end');
function keydownHandler(e) {
const evt = e || window.event;
const keyCode = evt.which || evt.keyCode;
if (keyCode === 9) { // TAB pressed
if (evt.preventDefault) evt.preventDefault();
else evt.returnValue = false;
firstAnchor.focus();
}
}
if (lastAnchor.addEventListener) lastAnchor.addEventListener('keydown', keydownHandler, false);
else if (lastAnchor.attachEvent) lastAnchor.attachEvent('onkeydown', keydownHandler);
}
return (
<E.Root left={left}>
<div className="modal" tabIndex="-1" id="dialog-start">
<button type="button" className="xButton" onFocus>X</button>
{modalHeader && <h3>{modalHeader}</h3>}
{modalText && <span className="modalText">{modalText}</span>}
<E.ButtonWrapper id="dialog-end">
{children}
</E.ButtonWrapper>
</div>
</E.Root>
)
};
export default Modal;
I have an modal window that will open when the user press on some button in the process.
I need, for accessibility, that the tab will be trap inside the modal window.
Now , unfortunately, the tab work on all the page.
How can I trap the tab inside the modal?
Demo : https://codesandbox.io/s/jolly-wood-pequk1?file=/src/App.js
The children prop will be two buttons components that I pass dynamically for each page.
import React from 'react'
import E from './Modal.style'
const Modal = (props) => {
const {
children,
modalHeader,
modalText,
left,
} = props
window.onload = () => {
const firstAnchor = document.getElementById('dialog-start');
const lastAnchor = document.getElementById('dialog-end');
function keydownHandler(e) {
const evt = e || window.event;
const keyCode = evt.which || evt.keyCode;
if (keyCode === 9) { // TAB pressed
if (evt.preventDefault) evt.preventDefault();
else evt.returnValue = false;
firstAnchor.focus();
}
}
if (lastAnchor.addEventListener) lastAnchor.addEventListener('keydown', keydownHandler, false);
else if (lastAnchor.attachEvent) lastAnchor.attachEvent('onkeydown', keydownHandler);
}
return (
<E.Root left={left}>
<div className="modal" tabIndex="-1" id="dialog-start">
<button type="button" className="xButton" onFocus>X</button>
{modalHeader && <h3>{modalHeader}</h3>}
{modalText && <span className="modalText">{modalText}</span>}
<E.ButtonWrapper id="dialog-end">
{children}
</E.ButtonWrapper>
</div>
</E.Root>
)
};
export default Modal;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以使用软件包,那么此软件包真的很容易使用以实现此 https://www.npmjs.com/package/focus-trap-react
他们给出的示例也使用了一种模态:),
所以您可以做:
If you're open to using a package, then this package is really simple to use to achieve this https://www.npmjs.com/package/focus-trap-react
The example they give uses a modal too :)
So you could do: