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.
2
Answers
Have look to SyntheticEvents https://reactjs.org/docs/events.html
There are two events
onMouseEnter
andonMouseLeave
to you use these two events for highlighting the div and hiding the button.You could use plain CSS to handle all the process:
CSS:
Explanation:
:focus-within
to conditionnaly display the buttonbutton
element and hide it as default to prevent interactionsbutton
when:focus-within
is active