Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions src/components/Layout/LeftSidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,25 @@ describe('LeftSidebar', () => {
// Should not be inside an accordion trigger button
expect(sectionHeading.closest('button')).toBeNull();
});

it('renders a top-level entry with a link but no child pages as a clickable link', () => {
mockUseLayoutContext.mockReturnValue({
activePage: {
page: { name: 'Postgres database connector', link: '/docs/livesync/postgres' },
tree: [{ index: 6, page: { name: 'Ably LiveSync', link: '/docs/livesync' } }],
languages: [],
language: 'javascript',
product: 'liveSync',
template: null,
hasProductBar: false,
},
});

render(<LeftSidebar />);

// "LiveSync pricing" is a top-level link, so it should render as an anchor, not a heading
const pricingLink = screen.getByText('LiveSync pricing').closest('a');
expect(pricingLink).not.toBeNull();
expect(pricingLink).toHaveAttribute('href', '/docs/livesync/pricing');
});
});
39 changes: 36 additions & 3 deletions src/components/Layout/LeftSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const accordionTriggerClassName = cn(

const accordionLinkClassName = 'pl-3 py-1';

const sectionHeadingClassName = 'ui-text-label2 font-bold text-neutral-1300 dark:text-neutral-000 pb-2 pt-5 pl-3 pr-2';

const iconClassName = 'text-neutral-1300 dark:text-neutral-000 transition-transform';

const ChildAccordion = ({ content, tree }: { content: (NavProductPage | NavProductContent)[]; tree: number[] }) => {
Expand Down Expand Up @@ -193,16 +195,47 @@ const ChildAccordion = ({ content, tree }: { content: (NavProductPage | NavProdu

/** Render top-level nav sections as static headings with their content always expanded. */
const SectionNav = ({ content, tree }: { content: (NavProductPage | NavProductContent)[]; tree: number[] }) => {
const { activePage } = useLayoutContext();
const location = useLocation();
const searchParams = useMemo(() => new URLSearchParams(location.search), [location.search]);

return (
<div className="p-3">
{content.map((page, index) => {
const hasDeeperLayer = 'pages' in page && page.pages;

// A top-level entry with a link but no child pages (e.g. a product's pricing page)
// is a destination, not a section, so render it as a clickable link rather than a
// static heading.
if (!hasDeeperLayer && 'link' in page && page.link) {
const isSelected = page.link === activePage.page.link;

return (
<Link
key={page.name}
to={buildLinkWithParams(page.link, searchParams)}
className={cn(
'ui-text-label2 font-bold text-neutral-1300 dark:text-neutral-000',
'mt-4 flex items-center justify-between gap-2 rounded-lg py-1.5 pl-3 pr-2',
'hover:bg-neutral-100 dark:hover:bg-neutral-1200',
isSelected && 'bg-orange-100 hover:bg-orange-100',
)}
{...(page.external && {
target: '_blank',
rel: 'noopener noreferrer',
})}
>
<span>{page.name}</span>
{page.external && (
<Icon name="icon-gui-arrow-top-right-on-square-outline" additionalCSS={iconClassName} size="16px" />
)}
</Link>
);
}

return (
<div key={page.name}>
<div className="ui-text-label2 font-bold text-neutral-1300 dark:text-neutral-000 pb-2 pt-5 pl-3 pr-2">
{page.name}
</div>
<div className={sectionHeadingClassName}>{page.name}</div>
{hasDeeperLayer && <ChildAccordion content={page.pages} tree={[...tree, index]} />}
</div>
);
Expand Down
Loading