Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import React, {useRef, useState} from 'react';
import clsx from 'clsx';
import {useLocation} from '@docusaurus/router';
import {translate} from '@docusaurus/Translate';
import useScrollPosition from '@theme/hooks/useScrollPosition';

import styles from './styles.module.css';
import {ThemeClassNames} from '@docusaurus/theme-common';
import {ThemeClassNames, useScrollPosition} from '@docusaurus/theme-common';

const threshold = 300;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
MobileSecondaryMenuFiller,
MobileSecondaryMenuComponent,
ThemeClassNames,
useScrollPosition,
} from '@docusaurus/theme-common';
import useWindowSize from '@theme/hooks/useWindowSize';
import useScrollPosition from '@theme/hooks/useScrollPosition';
import Logo from '@theme/Logo';
import IconArrow from '@theme/IconArrow';
import {translate} from '@docusaurus/Translate';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AnnouncementBarProvider,
DocsPreferredVersionContextProvider,
MobileSecondaryMenuProvider,
ScrollControllerProvider,
} from '@docusaurus/theme-common';
import type {Props} from '@theme/LayoutProviders';

Expand All @@ -20,11 +21,13 @@ export default function LayoutProviders({children}: Props): JSX.Element {
<ThemeProvider>
<AnnouncementBarProvider>
<UserPreferencesProvider>
<DocsPreferredVersionContextProvider>
<MobileSecondaryMenuProvider>
{children}
</MobileSecondaryMenuProvider>
</DocsPreferredVersionContextProvider>
<ScrollControllerProvider>
<DocsPreferredVersionContextProvider>
<MobileSecondaryMenuProvider>
{children}
</MobileSecondaryMenuProvider>
</DocsPreferredVersionContextProvider>
</ScrollControllerProvider>
</UserPreferencesProvider>
</AnnouncementBarProvider>
</ThemeProvider>
Expand Down
42 changes: 12 additions & 30 deletions packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@
import React, {useState, cloneElement, Children, ReactElement} from 'react';
import useIsBrowser from '@docusaurus/useIsBrowser';
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
import {useScrollPositionBlocker} from '@docusaurus/theme-common';
import type {Props} from '@theme/Tabs';
import type {Props as TabItemProps} from '@theme/TabItem';

import clsx from 'clsx';

import styles from './styles.module.css';

function isInViewport(element: HTMLElement): boolean {
const {top, left, bottom, right} = element.getBoundingClientRect();
const {innerHeight, innerWidth} = window;

return top >= 0 && right <= innerWidth && bottom <= innerHeight && left >= 0;
}

function TabsComponent(props: Props): JSX.Element {
const {
lazy,
Expand Down Expand Up @@ -50,6 +44,8 @@ function TabsComponent(props: Props): JSX.Element {
const {tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext();
const [selectedValue, setSelectedValue] = useState(defaultValue);
const tabRefs: (HTMLLIElement | null)[] = [];
const {blockElementScrollPositionUntilNextRender} =
useScrollPositionBlocker();

if (groupId != null) {
const relevantTabGroupChoice = tabGroupChoices[groupId];
Expand All @@ -65,31 +61,17 @@ function TabsComponent(props: Props): JSX.Element {
const handleTabChange = (
event: React.FocusEvent<HTMLLIElement> | React.MouseEvent<HTMLLIElement>,
) => {
const selectedTab = event.currentTarget;
const selectedTabIndex = tabRefs.indexOf(selectedTab);
const selectedTabValue = values[selectedTabIndex].value;

setSelectedValue(selectedTabValue);
const newTab = event.currentTarget;
const newTabIndex = tabRefs.indexOf(newTab);
const newTabValue = values[newTabIndex].value;

if (groupId != null) {
setTabGroupChoices(groupId, selectedTabValue);
if (newTabValue !== selectedValue) {
blockElementScrollPositionUntilNextRender(newTab);
setSelectedValue(newTabValue);

setTimeout(() => {
if (isInViewport(selectedTab)) {
return;
}

selectedTab.scrollIntoView({
block: 'center',
behavior: 'smooth',
});

selectedTab.classList.add(styles.tabItemActive);
setTimeout(
() => selectedTab.classList.remove(styles.tabItemActive),
2000,
);
}, 150);
if (groupId != null) {
setTabGroupChoices(groupId, newTabValue);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,3 @@
.tabItem {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import {useState, useCallback, useEffect, useRef} from 'react';
import {useLocation} from '@docusaurus/router';
import useScrollPosition from '@theme/hooks/useScrollPosition';
import {useLocationChange} from '@docusaurus/theme-common';
import {useLocationChange, useScrollPosition} from '@docusaurus/theme-common';
import type {useHideableNavbarReturns} from '@theme/hooks/useHideableNavbar';

const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
Expand Down

This file was deleted.

13 changes: 0 additions & 13 deletions packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,6 @@ declare module '@theme/hooks/usePrismTheme' {
export default usePrismTheme;
}

declare module '@theme/hooks/useScrollPosition' {
export type ScrollPosition = {scrollX: number; scrollY: number};

const useScrollPosition: (
effect: (
position: ScrollPosition,
lastPosition: ScrollPosition | null,
) => void,
deps?: unknown[],
) => void;
export default useScrollPosition;
}

declare module '@theme/hooks/useTabGroupChoice' {
export type useTabGroupChoiceReturns = {
readonly tabGroupChoices: {readonly [groupId: string]: string};
Expand Down
7 changes: 7 additions & 0 deletions packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ export {default as useTOCHighlight} from './utils/useTOCHighlight';
export type {TOCHighlightConfig} from './utils/useTOCHighlight';

export {useTOCFilter} from './utils/tocUtils';

export {
ScrollControllerProvider,
useScrollController,
useScrollPosition,
useScrollPositionBlocker,
} from './utils/scrollUtils';
Loading