如何使用JS中的React中创建TAB陷阱?

发布于 2025-02-07 02:09:16 字数 1736 浏览 1 评论 0原文

我有一个模态窗口,当用户按在该过程中的某个按钮上时将打开。

为了访问,我需要该选项卡在模式窗口内。

现在,不幸的是,该标签在所有页面上都可以使用。

如何在模态内捕获选项卡?

演示: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2025-02-14 02:09:17

如果您可以使用软件包,那么此软件包真的很容易使用以实现此 https://www.npmjs.com/package/focus-trap-react

他们给出的示例也使用了一种模态:),

<FocusTrap>
  <div id="modal-dialog" className="modal" >
    <button>Ok</button>
    <button>Cancel</button>
  </div>
</FocusTrap>

所以您可以做:

<FocusTrap>
 <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>
</FocusTrap>

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 :)

<FocusTrap>
  <div id="modal-dialog" className="modal" >
    <button>Ok</button>
    <button>Cancel</button>
  </div>
</FocusTrap>

So you could do:

<FocusTrap>
 <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>
</FocusTrap>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文