admin管理员组文章数量:1323524
When the Material UI Backdrop
component is in use, the elements in the website are still accessible, for example, through Tab
, which can be fairly easy to see in the official demo, or by using this CodeSandbox
:
I went through certain questions in plain JS
, such as tabindex -1 not working for child elements and How to make a bunch of elements not accessible from keyboard, and many suggested using visibility: hidden
or display: none
. However, I would like to know if there are ways to prevent other components from accessing, but still being visible.
When the Material UI Backdrop
component is in use, the elements in the website are still accessible, for example, through Tab
, which can be fairly easy to see in the official demo, or by using this CodeSandbox
:
I went through certain questions in plain JS
, such as tabindex -1 not working for child elements and How to make a bunch of elements not accessible from keyboard, and many suggested using visibility: hidden
or display: none
. However, I would like to know if there are ways to prevent other components from accessing, but still being visible.
- are the buttons clickable tho when backdrop open stackblitz/edit/react-n5jldtnj?file=Demo.tsx (check console logs). is the problem the background is visible. or the backdrop disappears when clicked out – cmgchess Commented Jan 9 at 6:01
- hmm for me the backdrop disappears and button onclick will not trigger when background open – cmgchess Commented Jan 9 at 6:19
2 Answers
Reset to default 0You can try using pointer-events: none;
style.
To start with, I use the open
attribute with useState
to indicate whether the elements should be inaccessible.
Two methods can achieve the desired effect: making elements visible but inaccessible through a mouse
or keyboard
.
The simplest way is to use tabIndex="-1"
, and for the
// ...other JS codes
return (
{/* ...other HTML codes */}
<div>
<div>
<button tabIndex={open ? -1 : 0}>Button A</button>
</div>
<div>
<button tabIndex={open ? -1 : 0}>Button B</button>
</div>
<div>
<button tabIndex={open ? -1 : 0}>Button C</button>
</div>
</div>
);
But as mentioned in the question, it is not feasible for many child elements that are separated into different files and components, so I came up with using inert
.
// ...other JS codes
return (
{/* ...other HTML codes */}
<div inert={open}>
<div>
<button>Button A</button>
</div>
<div>
<button>Button B</button>
</div>
<div>
<button>Button C</button>
</div>
</div>
);
And for those who are using React
with TypeScript
, check this question: Error when using `inert` attribute with Typescript.
本文标签:
版权声明:本文标题:javascript - How to make background components visible but inaccessible when React Material UI Backdrop is opened? - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742131824a2422195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论