mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-09 22:32:53 +02:00
feat(v2): auto focus to tab if it is outside viewport (#4209)
* Simplify solution * fix typo Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
0fc7276cc7
commit
788b4a76d8
2 changed files with 36 additions and 0 deletions
|
@ -14,6 +14,13 @@ import clsx from 'clsx';
|
||||||
|
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
|
function isInViewport(element: HTMLElement) {
|
||||||
|
const {top, left, bottom, right} = element.getBoundingClientRect();
|
||||||
|
const {innerHeight, innerWidth} = window;
|
||||||
|
|
||||||
|
return top >= 0 && right <= innerWidth && bottom <= innerHeight && left >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
const keys = {
|
const keys = {
|
||||||
left: 37,
|
left: 37,
|
||||||
right: 39,
|
right: 39,
|
||||||
|
@ -48,6 +55,23 @@ function Tabs(props: Props): JSX.Element {
|
||||||
|
|
||||||
if (groupId != null) {
|
if (groupId != null) {
|
||||||
setTabGroupChoices(groupId, selectedTabValue);
|
setTabGroupChoices(groupId, selectedTabValue);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
if (isInViewport(selectedTab)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedTab.scrollIntoView({
|
||||||
|
block: 'center',
|
||||||
|
behavior: 'smooth',
|
||||||
|
});
|
||||||
|
|
||||||
|
selectedTab.classList.add(styles.tabItemActive);
|
||||||
|
setTimeout(
|
||||||
|
() => selectedTab.classList.remove(styles.tabItemActive),
|
||||||
|
2000,
|
||||||
|
);
|
||||||
|
}, 150);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -9,3 +9,15 @@
|
||||||
margin-top: 0 !important;
|
margin-top: 0 !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.tabItemActive {
|
||||||
|
animation: blink 0.5s ease-in-out 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes blink {
|
||||||
|
0% {
|
||||||
|
background-color: var(--ifm-hover-overlay);
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
background-color: rgba(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue