Official
themes
The stock themes: presets of every colour, type size, corner, density token and background, chosen per light and dark mode and edited in Settings, beside the themes other plugins ship.
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/themes@0.3.3
Permissions in 0.3.3
Files
settings.tsx9.9 KB
// The Themes section in Settings (`contributes.settings.view`): the preset of
// each mode, the presets, and the editor of the one picked. A name or a
// choice writes the plugin's settings at once; a typed value waits for
// Enter or Save. `main.ts` applies what is written.
// HTM whitespace is significant; keep these templates out of Prettier's HTML formatter.
import type { Api, Element, ThemeTokens, ViewContext } from "divergence";
import { DEFAULT_SIZES } from "./presets.ts";
import { allPresets, chosen, duplicate, readSettings } from "./settings-model.ts";
import type { Preset, ThemeSettings } from "./settings-model.ts";
const column = { display: "flex", flexDirection: "column" } as const;
// The preset open in the editor, kept while the section is shown.
let editing: string | null = null;
// What is typed in the editor's fields, by field id, until Enter or Save
// applies it, so typing 14 never applies 1 on the way.
const drafts = new Map<string, string>();
// Another preset opened in the editor starts without the last one's typing.
const open = (id: string | null) => {
editing = id;
drafts.clear();
};
const HEX = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
type Group = "type" | "shape" | "density";
const GROUPS: readonly { readonly key: Group; readonly title: string }[] = [
{ key: "type", title: "Type" },
{ key: "shape", title: "Shape" },
{ key: "density", title: "Density" },
];
// "fieldHeight" → "Field height".
const words = (key: string) => key.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase());
export function render(ctx: ViewContext<object>, api: Api): Element {
const values = readSettings(api.settings.get());
const presets = allPresets(values);
const shown = presets.find((preset) => preset.id === editing) ?? null;
const save = (next: ThemeSettings["presets"]) => api.settings.set("presets", next);
const choose = (mode: "light" | "dark", id: string) => api.settings.set("active", { ...values.active, [mode]: id });
const chooser = (mode: "light" | "dark") => (
<setting title={mode === "light" ? "Light theme" : "Dark theme"} description={`Drawn when the appearance is ${mode === "light" ? "Light" : "Dark"} (or the system's is).`}>
<select id={`theme-${mode}`} value={chosen(values, mode).id} options={presets.filter((preset) => preset.mode === mode).map((preset) => ({ value: preset.id, label: preset.name }))} onChange={(e: { value: string }) => choose(mode, e.value)} />
</setting>
);
const copyOf = (source: Preset) => {
const copy = duplicate(source, presets);
open(copy.id);
void save({ ...values.presets, [copy.id]: { name: copy.name, mode: copy.mode, tokens: copy.tokens } }).then(() =>
choose(copy.mode, copy.id),
);
};
const row = (preset: Preset) => {
const used = chosen(values, preset.mode).id === preset.id;
const mode = preset.mode === "light" ? "Light" : "Dark";
return (
<item key={preset.id} id={`preset-${preset.id}`} label={preset.name} borderRadius="inner" sublabel={`${mode}${preset.stock ? " · stock" : ""}${used ? " · in use" : ""}`} icon={preset.mode === "light" ? "sun" : "moon"} iconTone={used ? "primary" : undefined} selected={preset.id === editing} actions={[
...(used ? [] : [{ icon: "check", tooltip: `Use for ${mode}`, onClick: () => choose(preset.mode, preset.id) }]),
{ icon: "copy", tooltip: "Duplicate", onClick: () => copyOf(preset) },
...(preset.stock
? []
: [
{
icon: "trash-2",
tooltip: "Delete",
onClick: () => {
const { [preset.id]: _gone, ...rest } = values.presets;
if (editing === preset.id) open(null);
void save(rest);
},
},
]),
]} onClick={() => {
open(preset.id);
ctx.update();
}} />
);
};
return (
<div {...column} gap="large">
<div {...column}>
<setting heading title="In use" description="The preset drawn in each appearance; System, Light and Dark in General choose between them." />
<card divided>{chooser("light")}{chooser("dark")}</card>
</div>
<div {...column}>
<setting heading title="Presets" description="Open one to see its tokens. A stock preset is read only: duplicate it to edit a copy." />
<card divided>{presets.map(row)}</card>
</div>
{shown ? editor(ctx, shown, values, save, copyOf) : null}
</div>
);
}
type Save = (next: ThemeSettings["presets"]) => unknown;
// A field whose typing waits for Enter or Save; Save shows once the text
// differs and is enabled only while `apply` accepts it.
function field(ctx: ViewContext<object>, id: string, value: string, apply: (text: string) => boolean): Element {
const text = drafts.get(id) ?? value;
const commit = () => {
if (apply(text.trim())) drafts.delete(id);
ctx.update();
};
return (
<div display="flex" flexDirection="row" alignItems="center" gap="small">
<input id={id} value={text} onChange={(e: { value: string }) => {
drafts.set(id, e.value);
ctx.update();
}} onSubmit={commit} />
{text !== value ? <button id={`${id}-save`} label="Save" small variant="primary" onClick={commit} /> : null}
</div>
);
}
// The open preset: a stock one offers a copy, one of the user's edits its tokens.
function editor(ctx: ViewContext<object>, preset: Preset, values: ThemeSettings, save: Save, copyOf: (source: Preset) => void): Element {
if (preset.stock) {
return <callout title={preset.name} text="A stock preset does not change. Duplicate it to make one of your own to edit."><button id="duplicate-open" label="Duplicate" onClick={() => copyOf(preset)} /></callout>;
}
const own = values.presets[preset.id]!;
const write = (tokens: ThemeTokens) => void save({ ...values.presets, [preset.id]: { ...own, tokens } });
const colors = preset.tokens.colors ?? {};
const sizes = (group: Group): Readonly<Record<string, number>> => ({
...DEFAULT_SIZES[group],
...((preset.tokens[group] ?? {}) as Readonly<Record<string, number>>),
});
return (
<div {...column} gap="large">
<div {...column}>
<setting heading title={preset.name} description="A typed value applies on Enter or Save." />
<card divided>
<setting title="Name">
<input id="preset-name" value={preset.name} onChange={(e: { value: string }) => e.value.trim() && void save({ ...values.presets, [preset.id]: { ...own, name: e.value.trim(), tokens: { ...own.tokens, name: e.value.trim() } } })} />
</setting>
<setting title="Ground opacity" description="How much of the ground colour lies over the blurred desktop, 0 to 1 (macOS).">
{field(ctx, "preset-ground", String(preset.tokens.groundAlpha ?? 0.95), (text) => {
const n = Number(text);
if (!text || !(n >= 0 && n <= 1)) return false;
write({ ...own.tokens, groundAlpha: n });
return true;
})}
</setting>
</card>
</div>
{GROUPS.map(
({ key, title }) => (
<div {...column}>
<setting heading title={title} />
<card divided>
{Object.entries(sizes(key)).map(
([name, value]) => (
<setting key={name} title={words(name)}>
{field(ctx, `preset-${key}-${name}`, String(value), (text) => {
const n = Number(text);
if (!text || !Number.isFinite(n) || n < 0) return false;
write({ ...own.tokens, [key]: { ...sizes(key), [name]: n } });
return true;
})}
</setting>
),
)}
</card>
</div>
),
)}
<div {...column}>
<setting heading title="Colors" description="Hex values, #rrggbb or #rrggbbaa." />
<card divided>
{Object.entries(colors)
.filter(([, value]) => value.startsWith("#"))
.map(
([name, value]) => (
<setting key={name} title={name}>
<div display="flex" flexDirection="row" alignItems="center" gap="small">
<div width={16} height={16} borderRadius="sm" background={value} />
{field(ctx, `preset-color-${name}`, value, (text) => {
if (!HEX.test(text)) return false;
write({ ...own.tokens, colors: { ...colors, [name]: text.toLowerCase() } });
return true;
})}
</div>
</setting>
),
)}
</card>
</div>
</div>
);
}
No comments yet.