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
45 changes: 29 additions & 16 deletions docs/src/routes/docs/examples/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { allComponents } from 'content-collections';
import type { ComponentCatalog } from '@layerstack/docs/catalog';

interface ExampleInfo {
/** The component's slug, as its route and screenshots use it (ex. `Area`, `webgl/Area`) */
component: string;
/** What to call the component (ex. `Area`, `Area (WebGL)`) */
label: string;
category?: string;
examples: Array<{
name: string;
Expand All @@ -14,33 +17,43 @@ interface ExampleInfo {
}

export async function load() {
const catalogModules = import.meta.glob<ComponentCatalog>('/src/examples/catalog/*.json', {
// `@layerchart/webgl`'s catalogs sit in `webgl/`, as their names repeat LayerChart's (ex. `Area`)
const catalogModules = import.meta.glob<ComponentCatalog>('/src/examples/catalog/**/*.json', {
eager: true,
import: 'default'
});

// Create a map of component names to their categories
// Create a map of component slugs to their categories
const componentCategoryMap = new Map<string, string>();
for (const component of allComponents) {
if (component.category) {
componentCategoryMap.set(component.name, component.category);
componentCategoryMap.set(component.slug, component.category);
}
}

// Build components list from catalog data
const components: ExampleInfo[] = Object.values(catalogModules)
.filter((catalog) => catalog.examples.length > 0)
.map((catalog) => ({
component: catalog.component,
category: componentCategoryMap.get(catalog.component),
examples: catalog.examples.map((example) => ({
name: example.name,
title: example.title,
path: `/example/${catalog.component}/${example.name}`,
createdAt: example.createdAt
}))
}))
.sort((a, b) => a.component.localeCompare(b.component));
const components: ExampleInfo[] = Object.entries(catalogModules)
.filter(([, catalog]) => catalog.examples.length > 0)
.map(([path, catalog]) => {
const slug = path.replace('/src/examples/catalog/', '').replace(/\.json$/, '');
const isWebGL = slug.startsWith('webgl/');
return {
component: slug,
label:
isWebGL && catalog.component !== 'WebGL'
? `${catalog.component} (WebGL)`
: catalog.component,
category: componentCategoryMap.get(slug),
examples: catalog.examples.map((example) => ({
name: example.name,
title: example.title,
path: `/docs/components/${slug}/${example.name}`,
createdAt: example.createdAt
}))
};
})
// Each WebGL component right after its LayerChart namesake
.sort((a, b) => a.label.localeCompare(b.label));

return {
components
Expand Down
51 changes: 26 additions & 25 deletions docs/src/routes/docs/examples/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import LucideZoomOut from '~icons/lucide/zoom-out';
import { cls } from '@layerstack/tailwind';

import { webglCategories } from '$lib/webglCategories.js';

let { data } = $props();

const stringParam = z.preprocess(
(val) => (typeof val === 'boolean' ? String(val) : val),
z.string().nullable().default(null)
);
export const schema = z.object({
const schema = z.object({
filter: stringParam,
category: stringParam,
/** `null` groups by component; `newest` groups by the month each example was added */
Expand Down Expand Up @@ -63,10 +65,10 @@
};

return filtered
.map(({ component, category, examples }) => {
.map(({ component, label, category, examples }) => {
// If component name matches, return all examples for this component
if (matchesQuery(component)) {
return { component, category, examples };
if (matchesQuery(component) || matchesQuery(label)) {
return { component, label, category, examples };
}

// Otherwise, filter examples by name
Expand All @@ -76,7 +78,7 @@

// Only return component if it has matching examples
if (filteredExamples.length > 0) {
return { component, category, examples: filteredExamples };
return { component, label, category, examples: filteredExamples };
}

return null;
Expand All @@ -102,19 +104,19 @@
*/
let groups = $derived.by(() => {
if (params.sort !== 'newest') {
return visibleExamples.map(({ component, examples }) => ({
return visibleExamples.map(({ component, label, examples }) => ({
key: component,
heading: component,
heading: label,
examples: examples.map((example) => ({ ...example, component, label: example.title }))
}));
}

const examples = visibleExamples
.flatMap(({ component, examples }) =>
.flatMap(({ component, label, examples }) =>
examples.map((example) => ({
...example,
component,
label: `${component} · ${example.title}`,
label: `${label} · ${example.title}`,
date: example.createdAt
}))
)
Expand Down Expand Up @@ -144,16 +146,14 @@
() => sum(visibleExamples.map(({ examples }) => examples.length)) ?? 0
);

// Get unique categories from components
// Get unique categories from components (lowercased, with their frontmatter casing to show)
let componentCategories = $derived.by(() => {
const categories = new Set<string>();
data.components.forEach(({ category }) => {
if (category) {
categories.add(category);
}
});
return Array.from(categories).sort(
sortFunc((category) =>
const categories = group(
data.components.flatMap(({ category }) => category ?? []),
(category) => category.toLowerCase()
);
return Array.from(categories, ([value, [label]]) => ({ value, label })).sort(
sortFunc(({ value }) =>
[
'charts',
'common',
Expand All @@ -166,21 +166,22 @@
'fill',
'clipping',
'layers',
'other'
].indexOf(category)
'other',
...Object.keys(webglCategories)
].indexOf(value)
)
);
});

let categoryOptions = $derived.by(() => [
{ label: 'All', value: null },
...componentCategories.map((category) => ({
label: category.charAt(0).toUpperCase() + category.slice(1),
value: category,
...componentCategories.map(({ value, label }) => ({
label: label.charAt(0).toUpperCase() + label.slice(1),
value,
group: 'Categories'
})),
...data.components.map(({ component }) => ({
label: component,
...data.components.map(({ component, label }) => ({
label,
value: component,
group: 'Components'
}))
Expand Down
1 change: 0 additions & 1 deletion packages/webgl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
### Minor Changes

- feat: New `@layerchart/webgl` package, drawing data-dense 2D charts and 3D views (perspective, lights and shadows, surfaces, globes, map terrain) with WebGL ([#985](https://github.com/techniq/layerchart/pull/985))

Loading