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
25 changes: 17 additions & 8 deletions docs/src/lib/componentPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,29 @@ export async function loadComponentPage(
}

/**
* The same-named component on the other side, to link to: `@layerchart/webgl`'s for one of
* LayerChart's (ex. `Points` ↔ `webgl/Points`), and LayerChart's for one of its, labeled with its layers
* Counterparts not named alike: `Chart` and `Layer` draw in WebGL through its `WebGL` layer, whose
* page leads back to `Layer`
*/
function counterpartOf(componentName: string): { href: string; label: string } | null {
const counterpartAliases: Record<string, string> = {
Chart: 'webgl/WebGL',
Layer: 'webgl/WebGL',
'webgl/WebGL': 'Layer'
};

/**
* The same-named component (or an alias's) on the other side, to switch to: `@layerchart/webgl`'s for one of
* LayerChart's (ex. `Points` ↔ `webgl/Points`), and LayerChart's for one of its, with its layers
*/
function counterpartOf(componentName: string): { href: string; layers: string[] } | null {
const webgl = componentName.startsWith('webgl/');
const name = webgl ? componentName.slice(6) : `webgl/${componentName}`;
const name =
counterpartAliases[componentName] ??
(webgl ? componentName.slice(6) : `webgl/${componentName}`);
const other = allComponents.find((c) => c.slug === name);
if (!other) return null;
const layers = ((other as { layers?: string[] }).layers ?? []).map(
(layer) => layer[0].toUpperCase() + layer.slice(1)
);
return {
href: `/docs/components/${name}`,
label: webgl ? layers.join(' / ') || 'Core' : 'WebGL'
layers: webgl ? ((other as { layers?: string[] }).layers ?? []) : ['webgl']
};
}

Expand Down
113 changes: 86 additions & 27 deletions docs/src/lib/components/PageHeader.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { dev } from '$app/environment';
import { page } from '$app/state';
import { goto } from '$app/navigation';
import type { Component } from 'svelte';
import { fly } from 'svelte/transition';
import { getSettings, isometric } from 'layerchart';
Expand All @@ -9,11 +10,11 @@
import { toTitleCase } from '@layerstack/utils';
import OpenWithButton from '$lib/components/OpenWithButton.svelte';

import LucideCheck from '~icons/lucide/check';
import LucideChevronDown from '~icons/lucide/chevron-down';
import LucideChevronRight from '~icons/lucide/chevron-right';
import LucideSettings from '~icons/lucide/settings';
import LucideArrowUpToLine from '~icons/lucide/arrow-up-to-line';
import LucideArrowRight from '~icons/lucide/arrow-right';

type Crumb = { label: string; href?: string };
type TocItem = { id: string; text: string; level: number };
Expand All @@ -31,19 +32,39 @@
title: string;
breadcrumbs?: Crumb[];
description?: string;
/** Layers offered by the page-wide Svg/Canvas/Html toggle (hidden when empty) */
/** Layers offered by the page-wide Svg/Canvas/Html toggle (hidden when empty; a single layer is shown selected) */
layers?: string[];
/** Passed to `OpenWithButton` */
metadata?: Record<string, any>;
showOpenWith?: boolean;
/** The same component drawn the other way (LayerChart's, or `@layerchart/webgl`'s), to link to */
counterpart?: { href: string; label: string } | null;
/** The same component drawn the other way (LayerChart's, or `@layerchart/webgl`'s), whose layers join the toggle and switch to it */
counterpart?: { href: string; layers: string[] } | null;
/** Docs section icon (matching the sidebar), shown on links to parent pages */
icon?: Component<{ class?: string }>;
} = $props();

// TODO: `setSettings({...})` or just use default?
const settings = getSettings();
const layerOrder = ['svg', 'canvas', 'html', 'webgl'];
const layerLabels: Record<string, string> = { webgl: 'WebGL' };
const layerLabel = (layer: string) => layerLabels[layer] ?? toTitleCase(layer);
// This page's layers, then the counterpart's (switching to its page), in a fixed order
const toggleLayers = $derived(
[...layers, ...(counterpart?.layers.filter((layer) => !layers.includes(layer)) ?? [])].sort(
(a, b) => layerOrder.indexOf(a) - layerOrder.indexOf(b)
)
);
// A single layer of this page's has nothing to toggle: show it selected
const selectedLayer = {
get current() {
return layers.length === 1 ? layers[0] : settings.layer;
},
set current(value) {
// `webgl` is a page, not a setting
if (value !== 'webgl') settings.layer = value;
if (counterpart && !layers.includes(value)) goto(counterpart.href);
}
};
const origin = $derived(dev ? 'https://layerchart.com' : page.url.origin);
const url = $derived(`${origin}${page.url.pathname}`);
const toc = $derived<TocItem[]>(page.data.metadata?.toc ?? []);
Expand Down Expand Up @@ -196,41 +217,79 @@
<OpenWithButton {metadata} />
</div>
{/if}
{#if layers.length}
{#if toggleLayers.length}
<!-- A dropdown on phones, where the options don't fit -->
<ToggleGroup
bind:value={settings.layer}
bind:value={selectedLayer.current}
variant="outline"
color="primary"
inset
rounded="full"
size="sm"
class="bg-surface-100 rounded-full"
class="bg-surface-100 rounded-full max-sm:hidden"
classes={{
options: 'bg-surface-100'
}}
>
{#each layers as layer}
<ToggleOption value={layer}>{toTitleCase(layer)}</ToggleOption>
{#each toggleLayers as layer, i}
<!-- WebGL is a separate package (and page), set apart from the core layers -->
{#if layer === 'webgl' && i > 0}
<span
aria-hidden="true"
class="self-center h-3.5 mx-1 border-l border-surface-content/20"
></span>
{/if}
<ToggleOption value={layer}>{layerLabel(layer)}</ToggleOption>
{/each}
</ToggleGroup>
{/if}
{#if counterpart}
<Tooltip
title={counterpart.label === 'WebGL'
? 'Also drawn with WebGL, by @layerchart/webgl'
: 'Also drawn without WebGL, in Svg, Canvas, or Html layers'}
>
<Button
href={counterpart.href}
variant="outline"
rounded="full"
size="sm"
class="bg-surface-100 text-surface-content/60 font-normal tracking-normal h-[30px] px-3 gap-1 [--border-color:color-mix(in_oklab,var(--color-surface-content)_20%,transparent)]"
>
{counterpart.label}
<LucideArrowRight class="size-3.5" />
</Button>
</Tooltip>

<Toggle let:on={open} let:toggle let:toggleOff>
<div class="sm:hidden">
<Button
variant="outline"
rounded="full"
size="sm"
class="bg-surface-100 text-surface-content h-[30px] px-3 gap-1 [--border-color:color-mix(in_oklab,var(--color-surface-content)_20%,transparent)]"
aria-haspopup="menu"
aria-expanded={open}
on:click={toggle}
>
{toggleLayers.includes(selectedLayer.current)
? layerLabel(selectedLayer.current)
: 'Layer'}
<LucideChevronDown class="size-3.5 text-surface-content/70" />
</Button>
<Menu
{open}
on:close={toggleOff}
placement="bottom-end"
classes={{ menu: 'py-1 min-w-32' }}
>
{#each toggleLayers as layer, i}
{#if layer === 'webgl' && i > 0}
<hr class="my-1 border-surface-content/10" />
{/if}
<button
onclick={() => {
toggleOff();
if (layer !== selectedLayer.current) selectedLayer.current = layer;
}}
class={cls(
'flex items-center justify-between gap-2 w-full px-3 py-1 text-sm text-left cursor-pointer',
layer === selectedLayer.current
? 'text-primary font-medium'
: 'text-surface-content/70 hover:text-surface-content'
)}
>
{layerLabel(layer)}
{#if layer === selectedLayer.current}
<LucideCheck class="size-3.5" />
{/if}
</button>
{/each}
</Menu>
</div>
</Toggle>
{/if}
<Toggle let:on={open} let:toggle let:toggleOff>
<Tooltip title="Settings">
Expand Down
5 changes: 4 additions & 1 deletion docs/src/routes/docs/components/[name]/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@
? intersectExampleLayers(exampleInfo.components, metadata.layers ?? [])
: null;
});
// `@layerchart/webgl`'s pages draw in a `WebGL` layer only, shown (selected) on its own
let layers = $derived(
pageExample?.module?.layers ?? computedExampleLayers ?? metadata.layers ?? []
data.componentName.startsWith('webgl/')
? ['webgl']
: (pageExample?.module?.layers ?? computedExampleLayers ?? metadata.layers ?? [])
);

// Component names as-is (ex. `ArcChart`), example names in sentence case (ex. `Axis labels inside`)
Expand Down
Loading