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
10 changes: 10 additions & 0 deletions apps/www/app/_components/sidebar/sidebar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
margin-bottom: var(--ds-size-5);
}

.search {
margin-bottom: var(--ds-size-5);
padding: 0 var(--ds-size-4);
}

.noResults {
padding: 0 var(--ds-size-4);
color: var(--ds-color-neutral-text-subtle);
}

.list {
margin: 0;
padding: 0;
Expand Down
93 changes: 90 additions & 3 deletions apps/www/app/_components/sidebar/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Button, Link, Paragraph } from '@digdir/designsystemet-react';
import { Button, Link, Paragraph, Search } from '@digdir/designsystemet-react';
import { useDebounceCallback } from '@internal/components';
import { ChevronRightLastIcon, XMarkIcon } from '@navikt/aksel-icons';
import cl from 'clsx/lite';
import { type HTMLAttributes, useRef } from 'react';
import {
type HTMLAttributes,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { NavLink } from 'react-router';
import classes from './sidebar.module.css';
Expand All @@ -11,10 +18,16 @@ export type SidebarProps = {
[key: string]: {
title: string;
url: string;
keywords?: string;
}[];
};
title: string;
hideCatTitle?: boolean;
/**
* Show a search input above the sidebar navigation that filters items by
* title and keywords.
*/
searchable?: boolean;
/**
* mapped list of suffixes to it's category key
*/
Expand All @@ -27,12 +40,61 @@ export const Sidebar = ({
cats,
title,
hideCatTitle = false,
searchable = false,
suffix = {},
className,
...props
}: SidebarProps) => {
const { t } = useTranslation();
const closeMenuRef = useRef<HTMLButtonElement>(null);
const [query, setQuery] = useState('');

const filteredCats = useMemo(() => {
const normalizedQuery = query.trim().toLowerCase();
if (!normalizedQuery) {
return cats;
}

const result: SidebarProps['cats'] = {};
for (const [key, value] of Object.entries(cats)) {
const items = value.filter((item) => {
const itemTitle = t(`sidebar.items.${item.title}`, item.title);
const haystack =
`${itemTitle} ${item.title} ${item.keywords ?? ''}`.toLowerCase();
return haystack.includes(normalizedQuery);
});
if (items.length) {
result[key] = items;
}
}
return result;
}, [cats, query, t]);

const hasResults = Object.values(filteredCats).some(
(value) => value.length > 0,
);

const resultCount = useMemo(
() =>
Object.values(filteredCats).reduce((sum, value) => sum + value.length, 0),
[filteredCats],
);

const [announce, setAnnounce] = useState('');
/* delay announce so it is not interrupted while the user is typing */
const debouncedAnnounce = useDebounceCallback((value: string) => {
setAnnounce(value);
}, 1000);

useEffect(() => {
if (!searchable || !query.trim()) {
debouncedAnnounce('');
return;
}
debouncedAnnounce(
`${t('search.srA')} ${resultCount} ${t('search.srB')} ${query}`,
);
}, [searchable, query, resultCount, t, debouncedAnnounce]);

return (
<div
Expand Down Expand Up @@ -70,8 +132,33 @@ export const Sidebar = ({
{t(`sidebar.${title}`, title)}
</Paragraph>
)}
{searchable ? (
<Search className={classes.search} data-size='sm'>
<Search.Input
aria-label={t('sidebar.search.label', 'Search')}
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<Search.Clear onClick={() => setQuery('')} />
</Search>
) : null}
{searchable ? (
<div
className='ds-sr-only'
aria-live='assertive'
aria-atomic='true'
aria-relevant='text'
>
{announce}
</div>
) : null}
{searchable && !hasResults ? (
<Paragraph data-size='sm' className={classes.noResults}>
{t('sidebar.search.noResults', 'No results')}
</Paragraph>
) : null}
<ul className={classes.list}>
{Object.entries(cats).map(([key, value]) => {
{Object.entries(filteredCats).map(([key, value]) => {
if (!value.length) {
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions apps/www/app/layouts/components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const loader = async ({
title: string;
url: string;
order?: number;
keywords?: string;
}[];
} = {
' ': [],
Expand Down Expand Up @@ -84,6 +85,7 @@ export const loader = async ({
file.relativePath.replace('.mdx', ''),
url: `/${lang}/components/${folder}/${slug}`,
order: parseInt(result.frontmatter.order, 10) || 9999,
keywords: result.frontmatter.search_terms || '',
});
}

Expand Down Expand Up @@ -114,10 +116,18 @@ export const loader = async ({
const parsedMetadata = JSON.parse(metadataJson);
const category = parsedMetadata.category || 'components';

const overviewMdx = getFileFromContentDir(
join('components', folder, lang, 'overview.mdx'),
);
const keywords = overviewMdx
? (await generateFromMdx(overviewMdx)).frontmatter.search_terms || ''
: '';

return {
category,
title: parsedMetadata[lang].title || folder,
url: `/${lang}/components/docs/${folder}`,
keywords,
};
}),
);
Expand Down Expand Up @@ -187,6 +197,7 @@ export default function Layout({
title={'Components'}
suffix={sidebarSuffix}
hideCatTitle
searchable
/>
<div className={classes.content} id='main'>
<Outlet />
Expand Down
4 changes: 4 additions & 0 deletions apps/www/app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export default {
show: 'Show',
hide: 'Hide',
sidebar: 'sidebar',
search: {
label: 'Search',
noResults: 'No results',
},
},
navigation: {
intro: 'Intro',
Expand Down
4 changes: 4 additions & 0 deletions apps/www/app/locales/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export default {
show: 'Vis',
hide: 'Skjul',
sidebar: 'sidemeny',
search: {
label: 'Søk',
noResults: 'Ingen treff',
},
},
navigation: {
intro: 'Intro',
Expand Down
Loading