Skip to content

Commit

Permalink
fix: sidebar active
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Oct 16, 2022
1 parent fb041b4 commit 27a2fc6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/theme-default/components/Siderbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@ import styles from './index.module.scss';
import React from 'react';
import { Link } from '../Link/index';
import { DefaultTheme } from '../../../shared/types';
import { normalizeHref, useSidebarData } from '../../logic/index';
import {
normalizeHref,
useLocaleSiteData,
useSidebarData,
normalizeSlash
} from '../../logic/index';
import { useLocation } from 'react-router-dom';
import { isActive } from '../../logic/index';

export function SideBar() {
const { pathname } = useLocation();
const localesData = useLocaleSiteData();
const { items: sidebarData } = useSidebarData(pathname);
const langRoutePrefix = normalizeSlash(localesData.routePrefix || '');

const renderGroupItem = (item: DefaultTheme.SidebarItem, depth = 0) => {
const marginLeft = `${depth * 20}px`;
let children: React.ReactElement[] = [];
if ('items' in item) {
children = item.items.map((child) => renderGroupItem(child, depth + 1));
}
const active = isActive(pathname, item.link);
// Extract lang route prefix
// TODO: base url
const active = isActive(
pathname.replace(langRoutePrefix, ''),
item.link?.replace(langRoutePrefix, '')
);
return (
<div style={{ marginLeft }}>
<div
Expand Down
4 changes: 4 additions & 0 deletions src/theme-default/logic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export function removeTrailingSlash(url: string) {
return url.charAt(url.length - 1) === '/' ? url.slice(0, -1) : url;
}

export function normalizeSlash(url: string) {
return removeTrailingSlash(addLeadingSlash(url));
}

export function normalizeHref(url?: string) {
if (!url) {
return '/';
Expand Down

1 comment on commit 27a2fc6

@vercel
Copy link

@vercel vercel bot commented on 27a2fc6 Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.