英文:
Allow tab navigation for a button within a div. If the div is focussed?
问题
我有一个类似这样的组件:(Code Sandbox: https://codesandbox.io/s/boring-platform-1ry6b2?file=/src/App.js)
绿色部分是 div。这是我的代码:
import { useState } from "react";
import "./styles.css";
export default function App() {
const [insideApp, setInsideApp] = useState(false);
return (
<div onFocus={() => setInsideApp(true)} onBlur={() => setInsideApp(false)}>
<div className="App">
<input className="input" />
{insideApp && <button>Show Button</button>}
</div>
<p>Out of Box content</p>
</div>
);
}
/* css */
.App {
display: flex;
border: 2px solid aqua;
align-items: center;
column-gap: 2vw;
padding: 2rem;
}
.App:focus-within {
border: 2px solid lime;
}
.input {
flex: auto;
}
理想情况下,我希望通过按Tab键导航到按钮时,它应该像下面显示的那样突出显示:
然而,由于我有条件地呈现按钮,我失去了焦点。有人能帮我想出一些创新的方法来实现相同的效果吗?我希望在焦点位于 div 内时,div 变为绿色,然后执行一个选项卡导航,以便它能检测输入和按钮。
另外,我希望只有在焦点位于 div 内时才显示按钮。
英文:
I am having a component like this: (Code Sandbox: https://codesandbox.io/s/boring-platform-1ry6b2?file=/src/App.js)
The green part is div. Here's my code:
import { useState } from "react";
import "./styles.css";
export default function App() {
const [insideApp, setInsideApp] = useState(false);
return (
<div onFocus={() => setInsideApp(true)} onBlur={() => setInsideApp(false)}>
<div className="App">
<input className="input" />
{insideApp && <button>Show Button</button>}
</div>
<p>Out of Box content</p>
</div>
);
}
/* css */
.App {
display: flex;
border: 2px solid aqua;
align-items: center;
column-gap: 2vw;
padding: 2rem;
}
.App:focus-within {
border: 2px solid lime;
}
.input {
flex: auto;
}
What I want ideally is when I navigate to the button through tab it should get highlighted as shown below:
However since I am rendering the button conditionally I am losing focus. Can someone help me come up with some innovative idea to achieve the same? I want the div to change to lime when its focussed and then do a tab navigation such that it detects input as well as button.
Also I want button to be only shown when focus is within the div.
答案1
得分: 1
查看 SyntheticEvents https://reactjs.org/docs/events.html
有两个事件 onMouseEnter
和 onMouseLeave
,可以使用这两个事件来高亮显示 div
并隐藏按钮。
<div
onMouseEnter={() => setInsideApp(true)}
onMouseLeave={() => setInsideApp(false)}
>
<div className="App">
{insideApp ? (
<div>
<input className="input" />
<button>Show Button</button>
</div>
) : null}
</div>
<p>Out of Box content</p>
</div>
英文:
Have look to SyntheticEvents https://reactjs.org/docs/events.html
There are two events onMouseEnter
and onMouseLeave
to you use these two events for highlighting the div and hiding the button.
<div
onMouseEnter={() => setInsideApp(true)}
onMouseLeave={() => setInsideApp(false)}
>
<div className="App">
{insideApp ? (
<div>
<input className="input" />
<button>Show Button</button>
</div>
) : null}
</div>
<p>Out of Box content</p>
</div>
答案2
得分: 1
你可以使用纯CSS来处理整个过程:
.App {
display: flex;
border: 2px solid aqua;
align-items: center;
column-gap: 2vw;
padding: 2rem;
}
.App:focus-within {
border: 2px solid lime;
}
.App button {
display: none;
pointer-events: none;
}
.App:focus-within button {
display: flex;
pointer-events: auto;
}
.input {
flex: auto;
}
解释:
- 使用
:focus-within
来有条件地显示按钮。 - 为
button
元素禁用指针事件并默认隐藏它,以防止交互。 - 当
:focus-within
处于活动状态时,启用指针事件并显示按钮。
英文:
You could use plain CSS to handle all the process:
import { useState } from "react";
import "./styles.css";
export default function App() {
// const [insideApp, setInsideApp] = useState(false);
return (
<div>
<div
className="App">
<input className="input" />
<button>Show Button</button>
</div>
<p>Out of Box content</p>
</div>
);
}
CSS:
.App {
display: flex;
border: 2px solid aqua;
align-items: center;
column-gap: 2vw;
padding: 2rem;
}
.App:focus-within {
border: 2px solid lime;
}
.App button {
display: none;
pointer-events: none;
}
.App:focus-within button {
display: flex;
pointer-events: auto;
}
.input {
flex: auto;
}
Explanation:
- use
:focus-within
to conditionnaly display the button - disable cursor events for the
button
element and hide it as default to prevent interactions - enable cursor events and display the
button
when:focus-within
is active
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论