Skip to content

Commit

Permalink
fix(theme-common): fix issue in tab scroll position restoration on ta…
Browse files Browse the repository at this point in the history
…b click (#8628)
  • Loading branch information
slorber committed Feb 3, 2023
1 parent ed13d5c commit cc767ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/docusaurus-theme-common/src/utils/scrollUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ export function useScrollPositionBlocker(): {
);

useLayoutEffect(() => {
nextLayoutEffectCallbackRef.current?.();
// Queuing permits to restore scroll position after all useLayoutEffect
// have run, and yet preserve the sync nature of the scroll restoration
// See https://github.com/facebook/docusaurus/issues/8625
queueMicrotask(() => nextLayoutEffectCallbackRef.current?.());
});

return {
Expand Down
6 changes: 4 additions & 2 deletions packages/docusaurus-theme-common/src/utils/tabsUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import React, {
isValidElement,
useCallback,
useEffect,
useState,
useMemo,
type ReactNode,
type ReactElement,
useLayoutEffect,
} from 'react';
import {useHistory} from '@docusaurus/router';
import {useQueryStringValue} from '@docusaurus/theme-common/internal';
Expand Down Expand Up @@ -246,7 +246,9 @@ export function useTabs(props: TabsProps): {
}
return value;
})();
useEffect(() => {
// Sync in a layout/sync effect is important, for useScrollPositionBlocker
// See https://github.com/facebook/docusaurus/issues/8625
useLayoutEffect(() => {
if (valueToSync) {
setSelectedValue(valueToSync);
}
Expand Down
26 changes: 26 additions & 0 deletions website/_dogfooding/_pages tests/tabs-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,29 @@ export const isMacOS = typeof window !== 'undefined' && navigator.platform.start
</Tabs>
</BrowserWindow>
```

## Tabs sync with different heights

```mdx-code-block
<Tabs groupId="operating-systems">
<TabItem value="win" label="Windows">
Use Ctrl + C to copy.
</TabItem>
<TabItem value="mac" label="macOS">
very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text very long text
</TabItem>
</Tabs>
```

```mdx-code-block
<Tabs groupId="operating-systems">
<TabItem value="win" label="Windows">
Use Ctrl + V to paste.
</TabItem>
<TabItem value="mac" label="macOS">
Use Command + V to paste.
</TabItem>
</Tabs>
```

When clicking tabs above, they should stay under cursor and we should adjust the scroll position.

0 comments on commit cc767ed

Please sign in to comment.