mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(theme): collapse doc sidebar category on label click if active (#10886)
This commit is contained in:
parent
c5a793de9d
commit
5331de2962
3 changed files with 8 additions and 31 deletions
|
@ -188,7 +188,14 @@ export default function DocSidebarItemCategory({
|
||||||
? (e) => {
|
? (e) => {
|
||||||
onItemClick?.(item);
|
onItemClick?.(item);
|
||||||
if (href) {
|
if (href) {
|
||||||
updateCollapsed(false);
|
if (isActive) {
|
||||||
|
e.preventDefault();
|
||||||
|
updateCollapsed();
|
||||||
|
} else {
|
||||||
|
// When navigating to a new category, we always expand
|
||||||
|
// see https://github.com/facebook/docusaurus/issues/10854#issuecomment-2609616182
|
||||||
|
updateCollapsed(false);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
updateCollapsed();
|
updateCollapsed();
|
||||||
|
|
|
@ -15,7 +15,6 @@ import React, {
|
||||||
type SetStateAction,
|
type SetStateAction,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
||||||
import useIsomorphicLayoutEffect from '@docusaurus/useIsomorphicLayoutEffect';
|
import useIsomorphicLayoutEffect from '@docusaurus/useIsomorphicLayoutEffect';
|
||||||
import {prefersReducedMotion} from '../../utils/accessibilityUtils';
|
import {prefersReducedMotion} from '../../utils/accessibilityUtils';
|
||||||
|
|
||||||
|
@ -157,24 +156,6 @@ type CollapsibleElementType = React.ElementType<
|
||||||
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd' | 'style'>
|
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd' | 'style'>
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/**
|
|
||||||
* Prevent hydration layout shift before animations are handled imperatively
|
|
||||||
* with JS
|
|
||||||
*/
|
|
||||||
function getSSRStyle({
|
|
||||||
collapsed,
|
|
||||||
isBrowser,
|
|
||||||
}: {
|
|
||||||
collapsed: boolean;
|
|
||||||
isBrowser: boolean;
|
|
||||||
}) {
|
|
||||||
// After hydration, styles are applied
|
|
||||||
if (isBrowser) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return collapsed ? CollapsedStyles : ExpandedStyles;
|
|
||||||
}
|
|
||||||
|
|
||||||
type CollapsibleBaseProps = {
|
type CollapsibleBaseProps = {
|
||||||
/** The actual DOM element to be used in the markup. */
|
/** The actual DOM element to be used in the markup. */
|
||||||
as?: CollapsibleElementType;
|
as?: CollapsibleElementType;
|
||||||
|
@ -192,12 +173,6 @@ type CollapsibleBaseProps = {
|
||||||
onCollapseTransitionEnd?: (collapsed: boolean) => void;
|
onCollapseTransitionEnd?: (collapsed: boolean) => void;
|
||||||
/** Class name for the underlying DOM element. */
|
/** Class name for the underlying DOM element. */
|
||||||
className?: string;
|
className?: string;
|
||||||
/**
|
|
||||||
* This is mostly useful for details/summary component where ssrStyle is not
|
|
||||||
* needed (as details are hidden natively) and can mess up with the browser's
|
|
||||||
* native behavior when JS fails to load or is disabled
|
|
||||||
*/
|
|
||||||
disableSSRStyle?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function CollapsibleBase({
|
function CollapsibleBase({
|
||||||
|
@ -207,9 +182,7 @@ function CollapsibleBase({
|
||||||
animation,
|
animation,
|
||||||
onCollapseTransitionEnd,
|
onCollapseTransitionEnd,
|
||||||
className,
|
className,
|
||||||
disableSSRStyle,
|
|
||||||
}: CollapsibleBaseProps) {
|
}: CollapsibleBaseProps) {
|
||||||
const isBrowser = useIsBrowser();
|
|
||||||
const collapsibleRef = useRef<HTMLElement>(null);
|
const collapsibleRef = useRef<HTMLElement>(null);
|
||||||
|
|
||||||
useCollapseAnimation({collapsibleRef, collapsed, animation});
|
useCollapseAnimation({collapsibleRef, collapsed, animation});
|
||||||
|
@ -219,8 +192,6 @@ function CollapsibleBase({
|
||||||
// @ts-expect-error: the "too complicated type" is produced from
|
// @ts-expect-error: the "too complicated type" is produced from
|
||||||
// "CollapsibleElementType" being a huge union
|
// "CollapsibleElementType" being a huge union
|
||||||
ref={collapsibleRef as RefObject<never>} // Refs are contravariant, which is not expressible in TS
|
ref={collapsibleRef as RefObject<never>} // Refs are contravariant, which is not expressible in TS
|
||||||
// Not even sure we need this SSRStyle anymore, try to remove it?
|
|
||||||
style={disableSSRStyle ? undefined : getSSRStyle({collapsed, isBrowser})}
|
|
||||||
onTransitionEnd={(e: React.TransitionEvent) => {
|
onTransitionEnd={(e: React.TransitionEvent) => {
|
||||||
if (e.propertyName !== 'height') {
|
if (e.propertyName !== 'height') {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -109,7 +109,6 @@ export function Details({
|
||||||
<Collapsible
|
<Collapsible
|
||||||
lazy={false} // Content might matter for SEO in this case
|
lazy={false} // Content might matter for SEO in this case
|
||||||
collapsed={collapsed}
|
collapsed={collapsed}
|
||||||
disableSSRStyle // Allows component to work fine even with JS disabled!
|
|
||||||
onCollapseTransitionEnd={(newCollapsed) => {
|
onCollapseTransitionEnd={(newCollapsed) => {
|
||||||
setCollapsed(newCollapsed);
|
setCollapsed(newCollapsed);
|
||||||
setOpen(!newCollapsed);
|
setOpen(!newCollapsed);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue