Skip to content

Commit

Permalink
fix(v2): tab label showing outline & background when clicked (#3171)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdfaizan7 authored Jul 31, 2020
1 parent 68f4bd0 commit d560a52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
33 changes: 30 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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 clsx from 'clsx';
Expand All @@ -15,6 +15,7 @@ import styles from './styles.module.css';
const keys = {
left: 37,
right: 39,
tab: 9,
};

type Props = {
Expand All @@ -29,6 +30,7 @@ function Tabs(props: Props): JSX.Element {
const {block, children, defaultValue, values, groupId} = props;
const {tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext();
const [selectedValue, setSelectedValue] = useState(defaultValue);
const [keyboardPress, setKeyboardPress] = useState(false);

if (groupId != null) {
const relevantTabGroupChoice = tabGroupChoices[groupId];
Expand Down Expand Up @@ -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 (
<div>
<ul
Expand All @@ -99,11 +118,19 @@ function Tabs(props: Props): JSX.Element {
className={clsx('tabs__item', styles.tabItem, {
'tabs__item--active': selectedValue === value,
})}
style={keyboardPress ? {} : {outline: 'none'}}
key={value}
ref={(tabControl) => tabRefs.push(tabControl)}
onKeyDown={(event) => handleKeydown(tabRefs, event.target, event)}
onKeyDown={(event) => {
handleKeydown(tabRefs, event.target, event);
handleKeyboardEvent(event);
}}
onFocus={() => changeSelectedValue(value)}
onClick={() => changeSelectedValue(value)}>
onClick={() => {
changeSelectedValue(value);
setKeyboardPress(false);
}}
onPointerDown={() => setKeyboardPress(false)}>
{label}
</li>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@
margin-top: 0 !important;
}

.tabItem:focus {
background-color: var(--ifm-hover-overlay);
}

0 comments on commit d560a52

Please sign in to comment.