Skip to content

Commit

Permalink
fix(theme-classic): autocollapse sidebar categories when navigating w…
Browse files Browse the repository at this point in the history
…ith paginator (#7411)

* fix: autocollapse sidebar categories

Auto collapse sidebar categories when navigating via paginator.

* Add autoCollapseCategories in dependency array

* restore collapse button behavior

* simplify

Co-authored-by: Joshua Chen <[email protected]>

* hopefully a better fix

* refactor

* try this

Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
pranabdas and Josh-Cena authored May 14, 2022
1 parent d2ca4a1 commit 3f110a3
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ import useIsBrowser from '@docusaurus/useIsBrowser';
function useAutoExpandActiveCategory({
isActive,
collapsed,
setCollapsed,
updateCollapsed,
}: {
isActive: boolean;
collapsed: boolean;
setCollapsed: (b: boolean) => void;
updateCollapsed: (b: boolean) => void;
}) {
const wasActive = usePrevious(isActive);
useEffect(() => {
const justBecameActive = isActive && !wasActive;
if (justBecameActive && collapsed) {
setCollapsed(false);
updateCollapsed(false);
}
}, [isActive, wasActive, collapsed, setCollapsed]);
}, [isActive, wasActive, collapsed, updateCollapsed]);
}

/**
Expand Down Expand Up @@ -105,6 +105,11 @@ export default function DocSidebarItemCategory({
...props
}: Props): JSX.Element {
const {items, label, collapsible, className, href} = item;
const {
docs: {
sidebar: {autoCollapseCategories},
},
} = useThemeConfig();
const hrefWithSSRFallback = useCategoryHrefWithSSRFallback(item);

const isActive = isActiveSidebarItem(item, activePath);
Expand All @@ -121,17 +126,13 @@ export default function DocSidebarItemCategory({
},
});

useAutoExpandActiveCategory({isActive, collapsed, setCollapsed});
const {expandedItem, setExpandedItem} = useDocSidebarItemsExpandedState();
function updateCollapsed(toCollapsed: boolean = !collapsed) {
// Use this instead of `setCollapsed`, because it is also reactive
const updateCollapsed = (toCollapsed: boolean = !collapsed) => {
setExpandedItem(toCollapsed ? null : index);
setCollapsed(toCollapsed);
}
const {
docs: {
sidebar: {autoCollapseCategories},
},
} = useThemeConfig();
};
useAutoExpandActiveCategory({isActive, collapsed, updateCollapsed});
useEffect(() => {
if (
collapsible &&
Expand Down

0 comments on commit 3f110a3

Please sign in to comment.