diff --git a/docs/src/routes/docs/examples/+page.server.ts b/docs/src/routes/docs/examples/+page.server.ts index d9fd162aa2..2e7a51783f 100644 --- a/docs/src/routes/docs/examples/+page.server.ts +++ b/docs/src/routes/docs/examples/+page.server.ts @@ -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; @@ -14,33 +17,43 @@ interface ExampleInfo { } export async function load() { - const catalogModules = import.meta.glob('/src/examples/catalog/*.json', { + // `@layerchart/webgl`'s catalogs sit in `webgl/`, as their names repeat LayerChart's (ex. `Area`) + const catalogModules = import.meta.glob('/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(); 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 diff --git a/docs/src/routes/docs/examples/+page.svelte b/docs/src/routes/docs/examples/+page.svelte index 7011efda40..1fa088be27 100644 --- a/docs/src/routes/docs/examples/+page.svelte +++ b/docs/src/routes/docs/examples/+page.svelte @@ -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 */ @@ -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 @@ -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; @@ -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 })) ) @@ -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(); - 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', @@ -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' })) diff --git a/packages/webgl/CHANGELOG.md b/packages/webgl/CHANGELOG.md index e17c4e6047..716e1c76f7 100644 --- a/packages/webgl/CHANGELOG.md +++ b/packages/webgl/CHANGELOG.md @@ -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)) -