I'm trying to replicate Chrome's default outline styling that occurs when one focuses on a button element with an anchor element that I'm using as a link button. Since link buttons do not have that default outline styling that regular buttons have, I'm using onFocus and onBlur to set an outline state -- which will be used to trigger the outline styling. I'm using a ternary operator in the style attribute to display the outline, depending on whether the user has focused on the anchor element.
At the moment, the styling does not appear when the user focuses on the anchor element. However, if I were to set '5px auto -webkit-focus-ring-color'
as the outline value in the false condition of the ternary operator, it appears when the user doesn't focus on the anchor element. If I were to leave the general ternary operator structure as it is in the code shown below, but change auto
to solid
or dotted
, the outline is visible during a focused state. I'd appreciate it if anyone could explain why the outline doesn't appear when I use auto
in the outline value in the true condition of the ternary operator. Here's a sandbox with the code. The following is my code:
const [outlined, setOutlined] = useState(false);
const onFocus = () => {
setOutlined(true);
};
const onBlur = () => {
setOutlined(false);
};
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<a
href="https://www.stackoverflow.com"
onFocus={onFocus}
onBlur={onBlur}
style={{ backgroundColor: '#f44336', color: 'white', padding: '14px 25px', outline: outlined ? '5px auto -webkit-focus-ring-color' : 'none' }}
>
Swag
</a>
</div>
);