Skip to content

Commit 5331de2

Browse files
authored
fix(theme): collapse doc sidebar category on label click if active (#10886)
1 parent c5a793d commit 5331de2

File tree

3 files changed

+8
-31
lines changed
  • packages
    • docusaurus-theme-classic/src/theme/DocSidebarItem/Category
    • docusaurus-theme-common/src/components

3 files changed

+8
-31
lines changed

packages/docusaurus-theme-classic/src/theme/DocSidebarItem/Category/index.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ export default function DocSidebarItemCategory({
188188
? (e) => {
189189
onItemClick?.(item);
190190
if (href) {
191-
updateCollapsed(false);
191+
if (isActive) {
192+
e.preventDefault();
193+
updateCollapsed();
194+
} else {
195+
// When navigating to a new category, we always expand
196+
// see https://github.com/facebook/docusaurus/issues/10854#issuecomment-2609616182
197+
updateCollapsed(false);
198+
}
192199
} else {
193200
e.preventDefault();
194201
updateCollapsed();

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

-29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import React, {
1515
type SetStateAction,
1616
type ReactNode,
1717
} from 'react';
18-
import useIsBrowser from '@docusaurus/useIsBrowser';
1918
import useIsomorphicLayoutEffect from '@docusaurus/useIsomorphicLayoutEffect';
2019
import {prefersReducedMotion} from '../../utils/accessibilityUtils';
2120

@@ -157,24 +156,6 @@ type CollapsibleElementType = React.ElementType<
157156
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd' | 'style'>
158157
>;
159158

160-
/**
161-
* Prevent hydration layout shift before animations are handled imperatively
162-
* with JS
163-
*/
164-
function getSSRStyle({
165-
collapsed,
166-
isBrowser,
167-
}: {
168-
collapsed: boolean;
169-
isBrowser: boolean;
170-
}) {
171-
// After hydration, styles are applied
172-
if (isBrowser) {
173-
return undefined;
174-
}
175-
return collapsed ? CollapsedStyles : ExpandedStyles;
176-
}
177-
178159
type CollapsibleBaseProps = {
179160
/** The actual DOM element to be used in the markup. */
180161
as?: CollapsibleElementType;
@@ -192,12 +173,6 @@ type CollapsibleBaseProps = {
192173
onCollapseTransitionEnd?: (collapsed: boolean) => void;
193174
/** Class name for the underlying DOM element. */
194175
className?: string;
195-
/**
196-
* This is mostly useful for details/summary component where ssrStyle is not
197-
* needed (as details are hidden natively) and can mess up with the browser's
198-
* native behavior when JS fails to load or is disabled
199-
*/
200-
disableSSRStyle?: boolean;
201176
};
202177

203178
function CollapsibleBase({
@@ -207,9 +182,7 @@ function CollapsibleBase({
207182
animation,
208183
onCollapseTransitionEnd,
209184
className,
210-
disableSSRStyle,
211185
}: CollapsibleBaseProps) {
212-
const isBrowser = useIsBrowser();
213186
const collapsibleRef = useRef<HTMLElement>(null);
214187

215188
useCollapseAnimation({collapsibleRef, collapsed, animation});
@@ -219,8 +192,6 @@ function CollapsibleBase({
219192
// @ts-expect-error: the "too complicated type" is produced from
220193
// "CollapsibleElementType" being a huge union
221194
ref={collapsibleRef as RefObject<never>} // Refs are contravariant, which is not expressible in TS
222-
// Not even sure we need this SSRStyle anymore, try to remove it?
223-
style={disableSSRStyle ? undefined : getSSRStyle({collapsed, isBrowser})}
224195
onTransitionEnd={(e: React.TransitionEvent) => {
225196
if (e.propertyName !== 'height') {
226197
return;

packages/docusaurus-theme-common/src/components/Details/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export function Details({
109109
<Collapsible
110110
lazy={false} // Content might matter for SEO in this case
111111
collapsed={collapsed}
112-
disableSSRStyle // Allows component to work fine even with JS disabled!
113112
onCollapseTransitionEnd={(newCollapsed) => {
114113
setCollapsed(newCollapsed);
115114
setOpen(!newCollapsed);

0 commit comments

Comments
 (0)