Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 941f717

Browse files
committedMay 15, 2023
fix(theme): fix collapsible sidebar behavior when prefers-reduced-motion (#8971)
1 parent 2a16f63 commit 941f717

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed
 

‎packages/docusaurus-theme-classic/src/theme/DocPage/Layout/Sidebar/index.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import React, {type ReactNode, useState, useCallback} from 'react';
99
import clsx from 'clsx';
10-
import {ThemeClassNames} from '@docusaurus/theme-common';
10+
import {prefersReducedMotion, ThemeClassNames} from '@docusaurus/theme-common';
1111
import {useDocsSidebar} from '@docusaurus/theme-common/internal';
1212
import {useLocation} from '@docusaurus/router';
1313
import DocSidebar from '@theme/DocSidebar';
@@ -40,6 +40,11 @@ export default function DocPageLayoutSidebar({
4040
if (hiddenSidebar) {
4141
setHiddenSidebar(false);
4242
}
43+
// onTransitionEnd won't fire when sidebar animation is disabled
44+
// fixes https://github.com/facebook/docusaurus/issues/8918
45+
if (!hiddenSidebar && prefersReducedMotion()) {
46+
setHiddenSidebar(true);
47+
}
4348
setHiddenSidebarContainer((value) => !value);
4449
}, [setHiddenSidebarContainer, hiddenSidebar]);
4550

‎packages/docusaurus-theme-common/src/components/Collapsible/index.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import React, {
1717
type ReactNode,
1818
} from 'react';
1919
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
20+
import {prefersReducedMotion} from '../../utils/accessibilityUtils';
2021

2122
const DefaultAnimationEasing = 'ease-in-out';
2223

@@ -65,18 +66,14 @@ function applyCollapsedStyle(el: HTMLElement, collapsed: boolean) {
6566
el.style.height = collapsedStyles.height;
6667
}
6768

68-
function userPrefersReducedMotion(): boolean {
69-
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
70-
}
71-
7269
/*
7370
Lex111: Dynamic transition duration is used in Material design, this technique
7471
is good for a large number of items.
7572
https://material.io/archive/guidelines/motion/duration-easing.html#duration-easing-dynamic-durations
7673
https://github.com/mui-org/material-ui/blob/e724d98eba018e55e1a684236a2037e24bcf050c/packages/material-ui/src/styles/createTransitions.js#L40-L43
7774
*/
7875
function getAutoHeightDuration(height: number) {
79-
if (userPrefersReducedMotion()) {
76+
if (prefersReducedMotion()) {
8077
// Not using 0 because it prevents onTransitionEnd to fire and bubble up :/
8178
// See https://github.com/facebook/docusaurus/pull/8906
8279
return 1;

‎packages/docusaurus-theme-common/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export {useCollapsible, Collapsible} from './components/Collapsible';
4343

4444
export {ThemeClassNames} from './utils/ThemeClassNames';
4545

46+
export {prefersReducedMotion} from './utils/accessibilityUtils';
47+
4648
export {
4749
useIsomorphicLayoutEffect,
4850
useEvent,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export function prefersReducedMotion(): boolean {
9+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.