Skip to content
Open
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
56 changes: 45 additions & 11 deletions components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useLayoutEffect } from 'react';
import SideBar from '@node-core/ui-components/Containers/Sidebar';
import withIsland from '@doc-kit/generator-react/html/ui/islands/withIsland.jsx';
import { sidebar } from '../../site.json' with { type: 'json' };
Expand All @@ -16,14 +17,47 @@ const PrefetchLink = props => <a {...props} rel="prefetch" />;
*
* @param {{ pathname: string }} props
*/
const Sidebar = ({ pathname }) => (
<SideBar
pathname={pathname}
groups={sidebar}
onSelect={redirect}
as={PrefetchLink}
title="Navigation"
/>
);

export default withIsland(Sidebar, { name: 'Sidebar', on: { idle: true } });
const Sidebar = ({ pathname }) => {
// Each navigation is a full page load, so the sidebar always starts at the
// top. Scroll the active link into view (stateless, no storage needed).
useLayoutEffect(() => {
const active = document.querySelector(`aside a[href="${pathname}"]`);
const aside = active?.closest('aside');
const scroller =
aside && aside.scrollHeight > aside.clientHeight
? aside
: aside?.parentElement;

if (
!scroller ||
!active ||
scroller.scrollHeight <= scroller.clientHeight
) {
return;
}

const scrollerRect = scroller.getBoundingClientRect();
const activeRect = active.getBoundingClientRect();

// Only scroll if the active link is outside the visible area
if (
activeRect.top < scrollerRect.top ||
activeRect.bottom > scrollerRect.bottom
) {
scroller.scrollTop +=
activeRect.top - scrollerRect.top - scroller.clientHeight / 2;
}
}, [pathname]);

return (
<SideBar
pathname={pathname}
groups={sidebar}
onSelect={redirect}
as={PrefetchLink}
title="Navigation"
/>
);
};

export default withIsland(Sidebar, { name: 'SideBar', on: { idle: true } });
2 changes: 1 addition & 1 deletion doc-kit.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {
// Registers the components as islands, so they hydrate client-side
components: {
Authors: join(import.meta.dirname, 'components/Authors/index.jsx'),
Sidebar: join(import.meta.dirname, 'components/Sidebar/index.jsx'),
SideBar: join(import.meta.dirname, 'components/Sidebar/index.jsx'),
},

// Imports
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/interactions.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ test.describe('Small screens', () => {
await page.goto('/learn/getting-started/fetch');

// The sidebar collapses into a dropdown that only works once hydrated.
await waitForIsland(page, 'Sidebar');
await waitForIsland(page, 'SideBar');
await page.getByRole('combobox', { name: 'Navigation' }).click();

// Radix ignores a selection made in the first moments after the list
Expand Down