简体   繁体   English

一旦模态打开,模态的按钮首先是可选项卡的

[英]modal's buttons to be tabbable first once modal is open

I have a list of offers, each of them with a delete button, that once clicked opens a modal with another 2 buttons: delete and cancel.我有一个报价列表,每个报价都有一个删除按钮,一旦点击就会打开一个带有另外 2 个按钮的模式:删除和取消。

When the modal has opened, I want to be able to tab to these 2 buttons first, but at the moment tab continues with the other tabbable elements that are after the delete button in the row, eventually hitting the buttons after completing the tab round.当模态打开时,我希望能够首先对这两个按钮进行制表符,但此时制表符继续与行中删除按钮之后的其他可制表元素,最终在完成制表符回合后点击按钮。

Is there a way of achieving that?有没有办法做到这一点?

<section tabIndex="-1" role="dialog" onClick={onCancel}>
  <section role="document" onClick={(e) => e.stopPropagation()}>
    <Button tabIndex="0" secondary className={Styles.cancel} onClick={onCancel}>
      {t("abort")}
    </Button>
    <Button tabIndex="0" secondary className={Styles.delete} onClick={onDelete}>
      {t("deleteModal.CTA")}
    </Button>
  </section>
</section>

Tab continues wherever the focus is set. Tab 在焦点所在的地方继续。 Your code already includes tabindex="-1" which allows to focus the dialog element after opening it, with .focus() in JavaScript.您的代码已经包含tabindex="-1" ,它允许在打开对话框元素后将其聚焦,在 JavaScript 中使用.focus()

If the dialog content includes semantic structures […] then it is advisable to add tabindex="-1" to a static element at the start of the content and initially focus that element.如果对话内容包含语义结构 [...],则建议在内容开头将 tabindex="-1" 添加到静态元素并最初关注该元素。

In your case, the dialog is really simple, and it would be more advisable to focus the primary button instead of the dialog itself.在您的情况下,对话框非常简单,最好关注主按钮而不是对话框本身。

See Dialog (Modal) from the ARIA Authoring Practices Guide (APG)请参阅ARIA 创作实践指南 (APG) 中的对话框(模态)

Your dialog needs an accessible name.您的对话框需要一个可访问的名称。 Usually a aria-labelledby attribute is used to refer to the dialog's title element, if you don't have a visual title you can use aria-label .通常使用aria-labelledby属性来引用对话框的标题元素,如果您没有可视标题,则可以使用aria-label

It is also important that you establish a focus trap in the dialog, meaning that one cannot leave the dialog by means of tab, but only by means of close.在对话框中建立焦点陷阱也很重要,这意味着不能通过选项卡离开对话框,而只能通过关闭。 Usually the focus wraps around back to the dialog-element.通常焦点会回到对话框元素。

Browser support for the native <dialog> element recently got better and you might consider using that one.浏览器对原生<dialog>元素的支持最近变得更好,您可以考虑使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM