mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 02:08:55 +02:00
fix(v2): tab label showing outline & background when clicked (#3171)
* fix: tab label showing outline & background when clicked * show outline when tab pressed otherwise do not * listen to all keyboard keys* for outline * quick fix on typo * edits on handleMouseEvent func
This commit is contained in:
parent
68f4bd00f4
commit
d560a52ef2
2 changed files with 30 additions and 6 deletions
|
@ -5,7 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, {useState, Children, ReactElement} from 'react';
|
import React, {useState, Children, ReactElement, useEffect} from 'react';
|
||||||
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
|
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
|
||||||
|
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
@ -15,6 +15,7 @@ import styles from './styles.module.css';
|
||||||
const keys = {
|
const keys = {
|
||||||
left: 37,
|
left: 37,
|
||||||
right: 39,
|
right: 39,
|
||||||
|
tab: 9,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
@ -29,6 +30,7 @@ function Tabs(props: Props): JSX.Element {
|
||||||
const {block, children, defaultValue, values, groupId} = props;
|
const {block, children, defaultValue, values, groupId} = props;
|
||||||
const {tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext();
|
const {tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext();
|
||||||
const [selectedValue, setSelectedValue] = useState(defaultValue);
|
const [selectedValue, setSelectedValue] = useState(defaultValue);
|
||||||
|
const [keyboardPress, setKeyboardPress] = useState(false);
|
||||||
|
|
||||||
if (groupId != null) {
|
if (groupId != null) {
|
||||||
const relevantTabGroupChoice = tabGroupChoices[groupId];
|
const relevantTabGroupChoice = tabGroupChoices[groupId];
|
||||||
|
@ -83,6 +85,23 @@ function Tabs(props: Props): JSX.Element {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleKeyboardEvent = (event) => {
|
||||||
|
if (event.metaKey || event.altKey || event.ctrlKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setKeyboardPress(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMouseEvent = () => {
|
||||||
|
setKeyboardPress(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('keydown', handleKeyboardEvent);
|
||||||
|
window.addEventListener('mousedown', handleMouseEvent);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<ul
|
<ul
|
||||||
|
@ -99,11 +118,19 @@ function Tabs(props: Props): JSX.Element {
|
||||||
className={clsx('tabs__item', styles.tabItem, {
|
className={clsx('tabs__item', styles.tabItem, {
|
||||||
'tabs__item--active': selectedValue === value,
|
'tabs__item--active': selectedValue === value,
|
||||||
})}
|
})}
|
||||||
|
style={keyboardPress ? {} : {outline: 'none'}}
|
||||||
key={value}
|
key={value}
|
||||||
ref={(tabControl) => tabRefs.push(tabControl)}
|
ref={(tabControl) => tabRefs.push(tabControl)}
|
||||||
onKeyDown={(event) => handleKeydown(tabRefs, event.target, event)}
|
onKeyDown={(event) => {
|
||||||
|
handleKeydown(tabRefs, event.target, event);
|
||||||
|
handleKeyboardEvent(event);
|
||||||
|
}}
|
||||||
onFocus={() => changeSelectedValue(value)}
|
onFocus={() => changeSelectedValue(value)}
|
||||||
onClick={() => changeSelectedValue(value)}>
|
onClick={() => {
|
||||||
|
changeSelectedValue(value);
|
||||||
|
setKeyboardPress(false);
|
||||||
|
}}
|
||||||
|
onPointerDown={() => setKeyboardPress(false)}>
|
||||||
{label}
|
{label}
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
|
|
|
@ -9,6 +9,3 @@
|
||||||
margin-top: 0 !important;
|
margin-top: 0 !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tabItem:focus {
|
|
||||||
background-color: var(--ifm-hover-overlay);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue