histoire
Version:
A fast story book powered by Vite
101 lines (100 loc) • 5.44 kB
JavaScript
import { markGeneratedStoryKnownEmpty } from '../../../generated-story-metadata.js';
import { HstCopyIcon, HstNumber, HstText, HstTextarea, HstTokenGrid, HstTokenList, useControlsTheme } from '../../../vendors/controls.js';
import { createApp, h } from '../../../vendors/vue.js';
import { renderTokenSample, tailwindSampleCss } from './render.js';
/** Initialize primitive values once; host-provided iframe state remains authoritative. */
export function initializeTailwindState(state) {
state.search ??= '';
state.sampleText ??= 'The quick brown fox jumps over the lazy dog.';
state.fontSize ??= 16;
}
/** Mount over variant-owned state; onUpdate also supports nonreactive vanilla callers. */
function mount(api, render, canvas = false) {
initializeTailwindState(api.state);
const app = createApp({
setup() {
const dark = useControlsTheme();
// Canvas owns inspection backing; an opaque wrapper hides colors and checkerboards.
return () => canvas
? h('section', { 'class': 'histoire-wrapper histoire-tailwind-book', 'data-histoire-control-appearance': dark.value ? 'dark' : 'light', 'style': { minHeight: '100vh', padding: '1px' } }, render())
: render();
},
});
const instance = app.mount(api.el);
api.onUpdate(() => instance.$forceUpdate());
api.onUnmount(() => app.unmount());
}
/** Shared filter and meaningful typography controls, synchronized by existing state protocol. */
function controls(category, state) {
const fields = [h(HstText, { 'title': 'Filter tokens', 'modelValue': state.search, 'onUpdate:modelValue': (value) => {
state.search = value;
} })];
if (category.kind === 'text' || category.kind === 'text-color') {
fields.push(h(HstTextarea, { 'title': 'Sample text', 'modelValue': state.sampleText, 'onUpdate:modelValue': (value) => {
state.sampleText = value;
}, 'rows': 3 }));
if (category.id !== 'font-size') {
fields.push(h(HstNumber, { 'title': 'Font size', 'modelValue': state.fontSize, 'onUpdate:modelValue': (value) => {
state.fontSize = value;
}, 'min': 1 }));
}
}
return fields;
}
/** Generate compatible vanilla story while keeping immutable theme data separate from state. */
export function createTailwindStory(snapshot, EmptyState) {
const story = {
id: snapshot.source.id,
title: snapshot.source.title,
group: 'design-system',
icon: 'mdi:tailwind',
responsiveDisabled: true,
layout: { type: 'single' },
variants: [
...snapshot.categories.map((category) => {
// These immutable projections belong to category metadata, not render/update work.
const byKey = new Map(category.tokens.map(token => [token.key, token]));
const tokens = Object.fromEntries(category.tokens.map(token => [token.key, token.value]));
const component = ['text', 'padding', 'margin', 'width', 'height', 'value'].includes(category.kind) ? HstTokenList : HstTokenGrid;
return {
id: category.id,
title: category.title,
icon: category.kind.startsWith('text') ? 'carbon:text-font' : 'carbon:color-palette',
onMount(api) {
mount(api, () => h('div', { 'data-tailwind-source': snapshot.source.id }, [
h('style', tailwindSampleCss),
h(component, {
tokens,
getName: key => byKey.get(key).name,
search: api.state.search,
virtualize: true,
}, {
default: ({ token }) => {
const entry = byKey.get(token.key);
return [
renderTokenSample(category, entry, api.state),
entry.cssVariable
? h('div', { class: 'htw-flex htw-items-center htw-gap-1 htw-min-w-0' }, [
h('code', { class: 'htw-truncate' }, entry.cssVariable),
h(HstCopyIcon, { content: entry.cssVariable, label: 'Copy CSS variable' }),
])
: null,
];
},
empty: () => h(EmptyState, { icon: 'search', title: 'No matching tokens' }),
}),
]), true);
},
onMountControls(api) { mount(api, () => controls(category, api.state)); },
};
}),
{
id: 'full-config',
title: 'Full Config',
icon: 'carbon:code',
onMount(api) { mount(api, () => h('pre', { style: { margin: '16px', whiteSpace: 'pre-wrap', overflowWrap: 'anywhere' } }, JSON.stringify(snapshot.config, null, 2)), true); },
},
],
};
return markGeneratedStoryKnownEmpty(story);
}