Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(theme-classic): autocollapse sidebar categories when navigating with paginator #7411

Merged
merged 7 commits into from
May 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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