mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
Add dark/light mode toggle and coolors.co palettes
Admin UI: - system / light / dark switch in the sidebar (cycle button on mobile), stored in localStorage and applied before first paint - dark mode now driven by data-theme instead of only the media query Link page themes: - keep the Midnight and Paper presets, replace the others with the 4 trending palettes from coolors.co - paste a coolors.co link (or a bare hex list) to use any palette; the colors are read from the URL, nothing is fetched - palettes are mapped to background, text and button colors by contrast, with a dark/light background choice Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,11 +3,11 @@
|
|||||||
A small self-hosted URL shortener and link-page tool for a handful of people, with login through Authentik (OIDC).
|
A small self-hosted URL shortener and link-page tool for a handful of people, with login through Authentik (OIDC).
|
||||||
|
|
||||||
- **Short links**: `/<code>` for admins, `/<username>/<code>` for everyone else. Codes can be custom or random, and both codes and destinations can be edited later.
|
- **Short links**: `/<code>` for admins, `/<username>/<code>` for everyone else. Codes can be custom or random, and both codes and destinations can be edited later.
|
||||||
- **Link pages**: a "link in bio" page with avatar, link buttons, Markdown text blocks and custom colours. Blocks are reordered by drag and drop.
|
- **Link pages**: a "link in bio" page with avatar, link buttons, Markdown text blocks and colour themes, including trending or pasted [coolors.co](https://coolors.co) palettes. Blocks are reordered by drag and drop.
|
||||||
- **QR codes**: choose the error correction level and colours, then download as PNG or SVG.
|
- **QR codes**: choose the error correction level and colours, then download as PNG or SVG.
|
||||||
- **Privacy-friendly stats**: clicks per day, daily unique visitors, last visit. No raw IPs are stored, the visitor hash uses a salt that rotates daily, and individual clicks are deleted after 90 days.
|
- **Privacy-friendly stats**: clicks per day, daily unique visitors, last visit. No raw IPs are stored, the visitor hash uses a salt that rotates daily, and individual clicks are deleted after 90 days.
|
||||||
- **API with personal access tokens**: for scripts and iOS Shortcuts.
|
- **API with personal access tokens**: for scripts and iOS Shortcuts.
|
||||||
- **Mobile-friendly admin UI**, installable as a PWA. Everything runs as a single container with SQLite.
|
- **Mobile-friendly admin UI** with light and dark mode, installable as a PWA. Everything runs as a single container with SQLite.
|
||||||
|
|
||||||
See [REQUIREMENTS.md](REQUIREMENTS.md) for the full feature list.
|
See [REQUIREMENTS.md](REQUIREMENTS.md) for the full feature list.
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
import { KeyRound, LayoutDashboard, Link2, LogOut, PanelsTopLeft, ShieldCheck } from "lucide-react";
|
import { KeyRound, LayoutDashboard, Link2, LogOut, PanelsTopLeft, ShieldCheck } from "lucide-react";
|
||||||
import type { Me } from "../../shared/api-types.js";
|
import type { Me } from "../../shared/api-types.js";
|
||||||
|
import { ThemeCycleButton, ThemeSwitch } from "../components/ThemeToggle.js";
|
||||||
import { Button } from "../components/ui/button.js";
|
import { Button } from "../components/ui/button.js";
|
||||||
import { Link } from "../components/ui/misc.js";
|
import { Link } from "../components/ui/misc.js";
|
||||||
import { cx } from "../lib/cx.js";
|
import { cx } from "../lib/cx.js";
|
||||||
@@ -74,6 +75,9 @@ export function Shell({ me, route, onLogout, children }: { me: Me; route: Route;
|
|||||||
</Link>
|
</Link>
|
||||||
))}
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
|
<div className="px-4 pb-3">
|
||||||
|
<ThemeSwitch />
|
||||||
|
</div>
|
||||||
<div className="flex items-center gap-3 border-t border-border p-4">
|
<div className="flex items-center gap-3 border-t border-border p-4">
|
||||||
<UserAvatar username={me.username} />
|
<UserAvatar username={me.username} />
|
||||||
<div className="min-w-0 flex-1">
|
<div className="min-w-0 flex-1">
|
||||||
@@ -90,6 +94,7 @@ export function Shell({ me, route, onLogout, children }: { me: Me; route: Route;
|
|||||||
<header className="sticky top-0 z-30 flex h-14 items-center justify-between border-b border-border bg-surface/85 px-4 backdrop-blur lg:hidden">
|
<header className="sticky top-0 z-30 flex h-14 items-center justify-between border-b border-border bg-surface/85 px-4 backdrop-blur lg:hidden">
|
||||||
<Logo />
|
<Logo />
|
||||||
<div className="flex items-center gap-1">
|
<div className="flex items-center gap-1">
|
||||||
|
<ThemeCycleButton />
|
||||||
<UserAvatar username={me.username} />
|
<UserAvatar username={me.username} />
|
||||||
<Button variant="ghost" size="icon-sm" onClick={onLogout} aria-label="Sign out">
|
<Button variant="ghost" size="icon-sm" onClick={onLogout} aria-label="Sign out">
|
||||||
<LogOut />
|
<LogOut />
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import { Monitor, Moon, Sun } from "lucide-react";
|
||||||
|
import { cx } from "../lib/cx.js";
|
||||||
|
import { setThemePreference, useThemePreference, type ThemePreference } from "../lib/theme.js";
|
||||||
|
import { Button } from "./ui/button.js";
|
||||||
|
|
||||||
|
const OPTIONS: { value: ThemePreference; label: string; icon: typeof Sun }[] = [
|
||||||
|
{ value: "system", label: "System", icon: Monitor },
|
||||||
|
{ value: "light", label: "Light", icon: Sun },
|
||||||
|
{ value: "dark", label: "Dark", icon: Moon },
|
||||||
|
];
|
||||||
|
|
||||||
|
// Three-way switch for the sidebar.
|
||||||
|
export function ThemeSwitch() {
|
||||||
|
const current = useThemePreference();
|
||||||
|
return (
|
||||||
|
<div role="radiogroup" aria-label="Color mode" className="flex rounded-lg border border-border bg-surface-2 p-0.5">
|
||||||
|
{OPTIONS.map(({ value, label, icon: Icon }) => (
|
||||||
|
<button
|
||||||
|
key={value}
|
||||||
|
type="button"
|
||||||
|
role="radio"
|
||||||
|
aria-checked={current === value}
|
||||||
|
title={label}
|
||||||
|
onClick={() => setThemePreference(value)}
|
||||||
|
className={cx(
|
||||||
|
"flex h-7 flex-1 cursor-pointer items-center justify-center gap-1.5 rounded-md text-xs font-medium transition",
|
||||||
|
current === value ? "bg-surface text-fg shadow-sm" : "text-muted hover:text-fg",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<Icon className="size-3.5" />
|
||||||
|
{label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compact button that cycles system → light → dark, for the mobile top bar.
|
||||||
|
export function ThemeCycleButton() {
|
||||||
|
const current = useThemePreference();
|
||||||
|
const index = OPTIONS.findIndex((o) => o.value === current);
|
||||||
|
const { icon: Icon, label } = OPTIONS[index]!;
|
||||||
|
const next = OPTIONS[(index + 1) % OPTIONS.length]!;
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={() => setThemePreference(next.value)}
|
||||||
|
aria-label={`Color mode: ${label}. Switch to ${next.label}`}
|
||||||
|
title={`Color mode: ${label}`}
|
||||||
|
>
|
||||||
|
<Icon />
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<link rel="manifest" href="/~/manifest.webmanifest" />
|
<link rel="manifest" href="/~/manifest.webmanifest" />
|
||||||
<link rel="apple-touch-icon" href="/~/icon-192x192.png" />
|
<link rel="apple-touch-icon" href="/~/icon-192x192.png" />
|
||||||
<title>short</title>
|
<title>short</title>
|
||||||
|
<script src="/~/theme-init.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
export function stripMarkdown(markdown: string): string {
|
export function stripMarkdown(markdown: string): string {
|
||||||
return markdown
|
return markdown
|
||||||
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
||||||
.replace(/[*_`#>~]/g, "")
|
.replace(/^\s*(#{1,6}|>|[-*+]|\d+\.)\s+/gm, "")
|
||||||
|
.replace(/[*_`~]/g, "")
|
||||||
.replace(/\s+/g, " ")
|
.replace(/\s+/g, " ")
|
||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
import type { Theme } from "../../shared/validation.js";
|
||||||
|
|
||||||
|
// Turning coolors.co palettes (5–10 colors) into a link page theme (4 roles).
|
||||||
|
|
||||||
|
export interface Palette {
|
||||||
|
name: string;
|
||||||
|
colors: string[]; // "#rrggbb"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snapshot of the top 4 on https://coolors.co/palettes/trending (October 2026).
|
||||||
|
export const TRENDING_PALETTES: Palette[] = [
|
||||||
|
{ name: "Fiery Ocean", colors: hexList("780000-c1121f-fdf0d5-003049-669bbc") },
|
||||||
|
{ name: "Olive Garden Feast", colors: hexList("606c38-283618-fefae0-dda15e-bc6c25") },
|
||||||
|
{ name: "Pastel Dreamland", colors: hexList("cdb4db-ffc8dd-ffafcc-bde0fe-a2d2ff") },
|
||||||
|
{ name: "Ocean Sunset", colors: hexList("001219-005f73-0a9396-94d2bd-e9d8a6-ee9b00-ca6702-bb3e03-ae2012-9b2226") },
|
||||||
|
];
|
||||||
|
|
||||||
|
function hexList(dashed: string): string[] {
|
||||||
|
return dashed.split("-").map((h) => `#${h.toLowerCase()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accepts coolors.co links (generator "coolors.co/264653-2a9d8f-…" or "coolors.co/palette/264653-…")
|
||||||
|
// as well as a bare "264653-2a9d8f-…" list. The colors are part of the URL, so nothing needs to be fetched.
|
||||||
|
export function parseCoolorsInput(input: string): string[] | null {
|
||||||
|
const value = input.trim();
|
||||||
|
if (!value) return null;
|
||||||
|
let candidate = value;
|
||||||
|
if (/^(https?:\/\/)?([a-z0-9-]+\.)*coolors\.co\//i.test(value)) {
|
||||||
|
try {
|
||||||
|
const url = new URL(/^https?:\/\//i.test(value) ? value : `https://${value}`);
|
||||||
|
candidate = url.pathname.split("/").filter(Boolean).pop() ?? "";
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!/^([0-9a-f]{6}-)+[0-9a-f]{6}$/i.test(candidate)) return null;
|
||||||
|
const colors = hexList(candidate);
|
||||||
|
return colors.length >= 2 && colors.length <= 10 ? colors : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function channels(hex: string): [number, number, number] {
|
||||||
|
const n = parseInt(hex.slice(1), 16);
|
||||||
|
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
||||||
|
}
|
||||||
|
|
||||||
|
// WCAG relative luminance and contrast ratio.
|
||||||
|
function luminance(hex: string): number {
|
||||||
|
const [r, g, b] = channels(hex).map((c) => {
|
||||||
|
const s = c / 255;
|
||||||
|
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
||||||
|
}) as [number, number, number];
|
||||||
|
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function contrast(a: string, b: string): number {
|
||||||
|
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [number, number];
|
||||||
|
return (hi + 0.05) / (lo + 0.05);
|
||||||
|
}
|
||||||
|
|
||||||
|
function saturation(hex: string): number {
|
||||||
|
const [r, g, b] = channels(hex).map((c) => c / 255) as [number, number, number];
|
||||||
|
const max = Math.max(r, g, b);
|
||||||
|
const min = Math.min(r, g, b);
|
||||||
|
const l = (max + min) / 2;
|
||||||
|
return max === min ? 0 : (max - min) / (1 - Math.abs(2 * l - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxBy = <T>(items: T[], score: (item: T) => number): T =>
|
||||||
|
items.reduce((best, item) => (score(item) > score(best) ? item : best));
|
||||||
|
|
||||||
|
export type Variant = "dark" | "light";
|
||||||
|
|
||||||
|
// Background: darkest (or lightest) color. Text: the color that reads best on it.
|
||||||
|
// Buttons: the most colorful remaining color that stands out from the background.
|
||||||
|
// White or black is used whenever no palette color is readable enough.
|
||||||
|
export function themeFromPalette(colors: string[], variant: Variant): Theme {
|
||||||
|
const byLuminance = [...colors].sort((a, b) => luminance(a) - luminance(b));
|
||||||
|
const background = variant === "dark" ? byLuminance[0]! : byLuminance[byLuminance.length - 1]!;
|
||||||
|
const rest = colors.filter((c) => c !== background);
|
||||||
|
|
||||||
|
const readable = (on: string, options: string[]) => {
|
||||||
|
const best = maxBy([...options, "#ffffff", "#000000"], (c) => contrast(c, on));
|
||||||
|
const fromPalette = options.filter((c) => contrast(c, on) >= 4.5);
|
||||||
|
return fromPalette.length ? maxBy(fromPalette, (c) => contrast(c, on)) : best;
|
||||||
|
};
|
||||||
|
|
||||||
|
const text = readable(background, rest);
|
||||||
|
const buttonCandidates = rest.filter((c) => c !== text);
|
||||||
|
const visible = buttonCandidates.filter((c) => contrast(c, background) >= 1.4);
|
||||||
|
const button = (visible.length ? maxBy(visible, saturation) : buttonCandidates[0]) ?? text;
|
||||||
|
const buttonText = readable(button, colors.filter((c) => c !== button));
|
||||||
|
|
||||||
|
return { background, text, button, buttonText };
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { useSyncExternalStore } from "react";
|
||||||
|
|
||||||
|
// Light/dark preference of the admin UI. "system" follows the OS setting.
|
||||||
|
// theme-init.js applies the same logic before the app loads.
|
||||||
|
|
||||||
|
export type ThemePreference = "system" | "light" | "dark";
|
||||||
|
|
||||||
|
const STORAGE_KEY = "short-theme";
|
||||||
|
const media = matchMedia("(prefers-color-scheme: dark)");
|
||||||
|
const listeners = new Set<() => void>();
|
||||||
|
|
||||||
|
function readPreference(): ThemePreference {
|
||||||
|
const stored = localStorage.getItem(STORAGE_KEY);
|
||||||
|
return stored === "light" || stored === "dark" ? stored : "system";
|
||||||
|
}
|
||||||
|
|
||||||
|
let preference = readPreference();
|
||||||
|
|
||||||
|
function resolved(): "light" | "dark" {
|
||||||
|
return preference === "system" ? (media.matches ? "dark" : "light") : preference;
|
||||||
|
}
|
||||||
|
|
||||||
|
function apply() {
|
||||||
|
document.documentElement.dataset.theme = resolved();
|
||||||
|
listeners.forEach((l) => l());
|
||||||
|
}
|
||||||
|
|
||||||
|
media.addEventListener("change", () => {
|
||||||
|
if (preference === "system") apply();
|
||||||
|
});
|
||||||
|
|
||||||
|
export function setThemePreference(next: ThemePreference) {
|
||||||
|
preference = next;
|
||||||
|
if (next === "system") localStorage.removeItem(STORAGE_KEY);
|
||||||
|
else localStorage.setItem(STORAGE_KEY, next);
|
||||||
|
apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
const subscribe = (listener: () => void) => {
|
||||||
|
listeners.add(listener);
|
||||||
|
return () => listeners.delete(listener);
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useThemePreference(): ThemePreference {
|
||||||
|
return useSyncExternalStore(subscribe, () => preference);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useResolvedTheme(): "light" | "dark" {
|
||||||
|
return useSyncExternalStore(subscribe, resolved);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// Applies the saved light/dark preference before the first paint to avoid a flash.
|
||||||
|
(function () {
|
||||||
|
try {
|
||||||
|
var pref = localStorage.getItem("short-theme") || "system";
|
||||||
|
var dark = pref === "dark" || (pref === "system" && matchMedia("(prefers-color-scheme: dark)").matches);
|
||||||
|
document.documentElement.dataset.theme = dark ? "dark" : "light";
|
||||||
|
} catch (e) {}
|
||||||
|
})();
|
||||||
@@ -51,7 +51,7 @@ export function OverviewScreen({ me }: { me: Me }) {
|
|||||||
const links = all.filter((p) => p.kind === "redirect");
|
const links = all.filter((p) => p.kind === "redirect");
|
||||||
const linkPages = all.filter((p) => p.kind === "linkpage");
|
const linkPages = all.filter((p) => p.kind === "linkpage");
|
||||||
const clicks = all.reduce((sum, p) => sum + p.totalClicks, 0);
|
const clicks = all.reduce((sum, p) => sum + p.totalClicks, 0);
|
||||||
const scope = me.isAdmin ? "across all namespaces" : `in /${me.username}/`;
|
const scope = me.isAdmin ? "all users" : `in /${me.username}/`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -10,26 +10,16 @@ import { Segmented } from "../../components/ui/form.js";
|
|||||||
import { Stat } from "../../components/ui/misc.js";
|
import { Stat } from "../../components/ui/misc.js";
|
||||||
import { Card, CardHeader, Skeleton } from "../../components/ui/surface.js";
|
import { Card, CardHeader, Skeleton } from "../../components/ui/surface.js";
|
||||||
import { useAction } from "../../components/ui/toast.js";
|
import { useAction } from "../../components/ui/toast.js";
|
||||||
|
import { useResolvedTheme } from "../../lib/theme.js";
|
||||||
|
|
||||||
type Range = 7 | 30 | 90;
|
type Range = 7 | 30 | 90;
|
||||||
|
|
||||||
const cssVar = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
const cssVar = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
||||||
|
|
||||||
// Re-renders when the system switches between light and dark, so the chart picks up the new colors.
|
|
||||||
function useColorScheme() {
|
|
||||||
const [dark, setDark] = useState(() => matchMedia("(prefers-color-scheme: dark)").matches);
|
|
||||||
useEffect(() => {
|
|
||||||
const mq = matchMedia("(prefers-color-scheme: dark)");
|
|
||||||
const update = () => setDark(mq.matches);
|
|
||||||
mq.addEventListener("change", update);
|
|
||||||
return () => mq.removeEventListener("change", update);
|
|
||||||
}, []);
|
|
||||||
return dark;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Chart({ series }: { series: StatsDay[] }) {
|
function Chart({ series }: { series: StatsDay[] }) {
|
||||||
const container = useRef<HTMLDivElement>(null);
|
const container = useRef<HTMLDivElement>(null);
|
||||||
const dark = useColorScheme();
|
// Redraw when light/dark mode changes so the chart picks up the new colors.
|
||||||
|
const mode = useResolvedTheme();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const el = container.current;
|
const el = container.current;
|
||||||
@@ -70,7 +60,7 @@ function Chart({ series }: { series: StatsDay[] }) {
|
|||||||
resize.disconnect();
|
resize.disconnect();
|
||||||
plot.destroy();
|
plot.destroy();
|
||||||
};
|
};
|
||||||
}, [series, dark]);
|
}, [series, mode]);
|
||||||
|
|
||||||
return <div ref={container} className="w-full" />;
|
return <div ref={container} className="w-full" />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,20 @@
|
|||||||
import { useRef, useState } from "react";
|
import { useRef, useState, type ReactNode } from "react";
|
||||||
import { Check, ImageUp, Trash2 } from "lucide-react";
|
import { Check, ExternalLink, ImageUp, Trash2 } from "lucide-react";
|
||||||
import type { PageDetail } from "../../../shared/api-types.js";
|
import type { PageDetail } from "../../../shared/api-types.js";
|
||||||
import type { Theme } from "../../../shared/validation.js";
|
import type { Theme } from "../../../shared/validation.js";
|
||||||
import { api } from "../../api.js";
|
import { api } from "../../api.js";
|
||||||
import { Button } from "../../components/ui/button.js";
|
import { Button } from "../../components/ui/button.js";
|
||||||
import { ColorInput, Field, Input } from "../../components/ui/form.js";
|
import { ColorInput, Field, Input, Segmented } from "../../components/ui/form.js";
|
||||||
import { Card, CardBody, CardFooter, CardHeader } from "../../components/ui/surface.js";
|
import { Card, CardBody, CardFooter, CardHeader } from "../../components/ui/surface.js";
|
||||||
import { useAction } from "../../components/ui/toast.js";
|
import { useAction } from "../../components/ui/toast.js";
|
||||||
import { cx } from "../../lib/cx.js";
|
import { cx } from "../../lib/cx.js";
|
||||||
import { stripMarkdown } from "../../lib/markdown.js";
|
import { stripMarkdown } from "../../lib/markdown.js";
|
||||||
|
import { parseCoolorsInput, themeFromPalette, TRENDING_PALETTES, type Variant } from "../../lib/palette.js";
|
||||||
import { PhoneFrame } from "./PhonePreview.js";
|
import { PhoneFrame } from "./PhonePreview.js";
|
||||||
|
|
||||||
const PRESETS: { name: string; theme: Theme }[] = [
|
const PRESETS: { name: string; theme: Theme }[] = [
|
||||||
{ name: "Midnight", theme: { background: "#111827", text: "#e5e7eb", button: "#475569", buttonText: "#ffffff" } },
|
{ name: "Midnight", theme: { background: "#111827", text: "#e5e7eb", button: "#475569", buttonText: "#ffffff" } },
|
||||||
{ name: "Paper", theme: { background: "#fafaf9", text: "#1c1917", button: "#1c1917", buttonText: "#fafaf9" } },
|
{ name: "Paper", theme: { background: "#fafaf9", text: "#1c1917", button: "#1c1917", buttonText: "#fafaf9" } },
|
||||||
{ name: "Violet", theme: { background: "#2e1065", text: "#ede9fe", button: "#7c3aed", buttonText: "#ffffff" } },
|
|
||||||
{ name: "Forest", theme: { background: "#052e16", text: "#dcfce7", button: "#15803d", buttonText: "#ffffff" } },
|
|
||||||
{ name: "Sunset", theme: { background: "#431407", text: "#ffedd5", button: "#ea580c", buttonText: "#ffffff" } },
|
|
||||||
{ name: "Ocean", theme: { background: "#082f49", text: "#e0f2fe", button: "#0284c7", buttonText: "#ffffff" } },
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const COLOR_FIELDS: [keyof Theme, string][] = [
|
const COLOR_FIELDS: [keyof Theme, string][] = [
|
||||||
@@ -29,6 +26,194 @@ const COLOR_FIELDS: [keyof Theme, string][] = [
|
|||||||
|
|
||||||
const sameTheme = (a: Theme, b: Theme) => COLOR_FIELDS.every(([k]) => a[k].toLowerCase() === b[k].toLowerCase());
|
const sameTheme = (a: Theme, b: Theme) => COLOR_FIELDS.every(([k]) => a[k].toLowerCase() === b[k].toLowerCase());
|
||||||
|
|
||||||
|
function OptionCard({ selected, onClick, swatch, label }: { selected: boolean; onClick: () => void; swatch: ReactNode; label: string }) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onClick}
|
||||||
|
aria-pressed={selected}
|
||||||
|
className={cx(
|
||||||
|
"relative cursor-pointer overflow-hidden rounded-xl border text-left transition",
|
||||||
|
selected ? "border-accent ring-3 ring-accent/20" : "border-border hover:border-border-strong",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{swatch}
|
||||||
|
<div className="flex items-center justify-between gap-2 px-3 py-2 text-sm font-medium">
|
||||||
|
<span className="truncate">{label}</span>
|
||||||
|
{selected && <Check className="size-4 shrink-0 text-accent" />}
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PaletteStrip({ colors }: { colors: string[] }) {
|
||||||
|
return (
|
||||||
|
<div className="flex h-14">
|
||||||
|
{colors.map((c, i) => (
|
||||||
|
<div key={i} className="flex-1" style={{ background: c }} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function SectionLabel({ children, actions }: { children: ReactNode; actions?: ReactNode }) {
|
||||||
|
return (
|
||||||
|
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||||||
|
<h3 className="text-sm font-medium">{children}</h3>
|
||||||
|
{actions}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Theme picker: basic presets, coolors.co palettes (trending or pasted link) and custom colors.
|
||||||
|
function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (theme: Theme) => void }) {
|
||||||
|
const [variant, setVariant] = useState<Variant>("dark");
|
||||||
|
// The palette the current theme was generated from, so switching dark/light can re-map it.
|
||||||
|
const [source, setSource] = useState<string[] | null>(null);
|
||||||
|
const [link, setLink] = useState("");
|
||||||
|
const [imported, setImported] = useState<string[] | null>(null);
|
||||||
|
const [linkError, setLinkError] = useState<string>();
|
||||||
|
|
||||||
|
const applyPalette = (colors: string[], v = variant) => {
|
||||||
|
setSource(colors);
|
||||||
|
onChange(themeFromPalette(colors, v));
|
||||||
|
};
|
||||||
|
const changeVariant = (v: Variant) => {
|
||||||
|
setVariant(v);
|
||||||
|
if (source) onChange(themeFromPalette(source, v));
|
||||||
|
};
|
||||||
|
const importLink = () => {
|
||||||
|
const colors = parseCoolorsInput(link);
|
||||||
|
if (!colors) {
|
||||||
|
setLinkError("That doesn't look like a coolors.co palette link.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setLinkError(undefined);
|
||||||
|
setImported(colors);
|
||||||
|
applyPalette(colors);
|
||||||
|
};
|
||||||
|
|
||||||
|
const isSelected = (colors: string[]) => source === colors && sameTheme(themeFromPalette(colors, variant), theme);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-7">
|
||||||
|
<div>
|
||||||
|
<SectionLabel>Basic</SectionLabel>
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
{PRESETS.map((preset) => (
|
||||||
|
<OptionCard
|
||||||
|
key={preset.name}
|
||||||
|
label={preset.name}
|
||||||
|
selected={sameTheme(preset.theme, theme)}
|
||||||
|
onClick={() => {
|
||||||
|
setSource(null);
|
||||||
|
onChange(preset.theme);
|
||||||
|
}}
|
||||||
|
swatch={
|
||||||
|
<div className="flex h-14 flex-col items-center justify-center gap-1.5 px-6" style={{ background: preset.theme.background }}>
|
||||||
|
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
|
||||||
|
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<SectionLabel
|
||||||
|
actions={
|
||||||
|
<Segmented
|
||||||
|
ariaLabel="Background for palettes"
|
||||||
|
value={variant}
|
||||||
|
onChange={changeVariant}
|
||||||
|
options={[
|
||||||
|
{ value: "dark", label: "Dark background" },
|
||||||
|
{ value: "light", label: "Light background" },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Trending on{" "}
|
||||||
|
<a href="https://coolors.co/palettes/trending" target="_blank" rel="noreferrer" className="text-accent hover:underline">
|
||||||
|
coolors.co
|
||||||
|
</a>
|
||||||
|
</SectionLabel>
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
{TRENDING_PALETTES.map((p) => (
|
||||||
|
<OptionCard
|
||||||
|
key={p.name}
|
||||||
|
label={p.name}
|
||||||
|
selected={isSelected(p.colors)}
|
||||||
|
onClick={() => applyPalette(p.colors)}
|
||||||
|
swatch={<PaletteStrip colors={p.colors} />}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<SectionLabel>Import from coolors.co</SectionLabel>
|
||||||
|
<form
|
||||||
|
className="flex gap-2"
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
importLink();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
aria-label="coolors.co link"
|
||||||
|
placeholder="https://coolors.co/palette/264653-2a9d8f-e9c46a-f4a261-e76f51"
|
||||||
|
value={link}
|
||||||
|
onChange={(e) => {
|
||||||
|
setLink(e.target.value);
|
||||||
|
setLinkError(undefined);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Button type="submit" variant="secondary" disabled={!link.trim()}>
|
||||||
|
Apply
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
{linkError ? (
|
||||||
|
<p className="mt-1.5 text-xs text-danger">{linkError}</p>
|
||||||
|
) : (
|
||||||
|
<p className="mt-1.5 text-xs text-muted">
|
||||||
|
Create or pick a palette on{" "}
|
||||||
|
<a href="https://coolors.co/generate" target="_blank" rel="noreferrer" className="inline-flex items-center gap-0.5 underline">
|
||||||
|
coolors.co <ExternalLink className="size-3" />
|
||||||
|
</a>{" "}
|
||||||
|
and paste its URL here.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{imported && (
|
||||||
|
<div className="mt-3 grid grid-cols-2 gap-3">
|
||||||
|
<OptionCard label="Imported palette" selected={isSelected(imported)} onClick={() => applyPalette(imported)} swatch={<PaletteStrip colors={imported} />} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<SectionLabel>Custom colors</SectionLabel>
|
||||||
|
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||||
|
{COLOR_FIELDS.map(([key, label]) => (
|
||||||
|
<div key={key} className="flex flex-col gap-1.5">
|
||||||
|
<span className="text-xs text-muted">{label}</span>
|
||||||
|
<ColorInput
|
||||||
|
label={label}
|
||||||
|
value={theme[key]}
|
||||||
|
onChange={(v) => {
|
||||||
|
setSource(null);
|
||||||
|
onChange({ ...theme, [key]: v });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Renders the page with unsaved settings so changes are visible while editing.
|
// Renders the page with unsaved settings so changes are visible while editing.
|
||||||
function MockPreview({ title, avatarUrl, theme, page }: { title: string; avatarUrl: string | null; theme: Theme; page: PageDetail }) {
|
function MockPreview({ title, avatarUrl, theme, page }: { title: string; avatarUrl: string | null; theme: Theme; page: PageDetail }) {
|
||||||
return (
|
return (
|
||||||
@@ -125,41 +310,9 @@ export function AppearanceTab({ page, onChange }: { page: PageDetail; onChange:
|
|||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader title="Theme" description="Pick a preset or set your own colors." />
|
<CardHeader title="Theme" description="Pick a preset, use a palette from coolors.co or set your own colors." />
|
||||||
<CardBody className="flex flex-col gap-6">
|
<CardBody>
|
||||||
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3">
|
<ThemePicker theme={theme} onChange={setTheme} />
|
||||||
{PRESETS.map((preset) => {
|
|
||||||
const selected = sameTheme(preset.theme, theme);
|
|
||||||
return (
|
|
||||||
<button
|
|
||||||
key={preset.name}
|
|
||||||
type="button"
|
|
||||||
onClick={() => setTheme(preset.theme)}
|
|
||||||
className={cx(
|
|
||||||
"relative cursor-pointer overflow-hidden rounded-xl border text-left transition",
|
|
||||||
selected ? "border-accent ring-3 ring-accent/20" : "border-border hover:border-border-strong",
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<div className="flex h-16 flex-col items-center justify-center gap-1.5 px-6" style={{ background: preset.theme.background }}>
|
|
||||||
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
|
|
||||||
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
|
|
||||||
</div>
|
|
||||||
<div className="flex items-center justify-between px-3 py-2 text-sm font-medium">
|
|
||||||
{preset.name}
|
|
||||||
{selected && <Check className="size-4 text-accent" />}
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
||||||
{COLOR_FIELDS.map(([key, label]) => (
|
|
||||||
<div key={key} className="flex flex-col gap-1.5">
|
|
||||||
<span className="text-sm font-medium">{label}</span>
|
|
||||||
<ColorInput label={label} value={theme[key]} onChange={(v) => setTheme({ ...theme, [key]: v })} />
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</CardBody>
|
</CardBody>
|
||||||
<CardFooter>
|
<CardFooter>
|
||||||
{dirty && <span className="mr-auto text-sm text-muted">Unsaved changes</span>}
|
{dirty && <span className="mr-auto text-sm text-muted">Unsaved changes</span>}
|
||||||
|
|||||||
+23
-22
@@ -1,5 +1,8 @@
|
|||||||
@import "tailwindcss";
|
@import "tailwindcss";
|
||||||
|
|
||||||
|
/* Dark mode is controlled by data-theme on <html> (see theme-init.js), not only the system setting. */
|
||||||
|
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
|
||||||
|
|
||||||
/* Semantic color tokens. Components only use these, so light and dark mode stay in sync. */
|
/* Semantic color tokens. Components only use these, so light and dark mode stay in sync. */
|
||||||
@theme {
|
@theme {
|
||||||
--color-canvas: #f6f6f8;
|
--color-canvas: #f6f6f8;
|
||||||
@@ -40,28 +43,26 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
:root[data-theme="dark"] {
|
||||||
:root {
|
--color-canvas: #0c0c10;
|
||||||
--color-canvas: #0c0c10;
|
--color-surface: #141419;
|
||||||
--color-surface: #141419;
|
--color-surface-2: #1d1d24;
|
||||||
--color-surface-2: #1d1d24;
|
--color-border: #26262e;
|
||||||
--color-border: #26262e;
|
--color-border-strong: #34343e;
|
||||||
--color-border-strong: #34343e;
|
--color-fg: #ececf1;
|
||||||
--color-fg: #ececf1;
|
--color-muted: #9898a6;
|
||||||
--color-muted: #9898a6;
|
--color-accent: #8b5cf6;
|
||||||
--color-accent: #8b5cf6;
|
--color-accent-hover: #7c3aed;
|
||||||
--color-accent-hover: #7c3aed;
|
--color-accent-soft: #241a3d;
|
||||||
--color-accent-soft: #241a3d;
|
--color-accent-soft-fg: #c4b5fd;
|
||||||
--color-accent-soft-fg: #c4b5fd;
|
--color-danger: #ef4444;
|
||||||
--color-danger: #ef4444;
|
--color-danger-soft: #2c1517;
|
||||||
--color-danger-soft: #2c1517;
|
--color-danger-soft-fg: #fca5a5;
|
||||||
--color-danger-soft-fg: #fca5a5;
|
--color-warning-soft: #2b2112;
|
||||||
--color-warning-soft: #2b2112;
|
--color-warning-soft-fg: #fcd34d;
|
||||||
--color-warning-soft-fg: #fcd34d;
|
--color-chart-1: #a78bfa;
|
||||||
--color-chart-1: #a78bfa;
|
--color-chart-2: #38bdf8;
|
||||||
--color-chart-2: #38bdf8;
|
color-scheme: dark;
|
||||||
color-scheme: dark;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@layer base {
|
@layer base {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { contrast, parseCoolorsInput, themeFromPalette, TRENDING_PALETTES } from "../src/web/lib/palette.js";
|
||||||
|
|
||||||
|
describe("coolors.co palettes", () => {
|
||||||
|
it("parses palette and generator links as well as bare color lists", () => {
|
||||||
|
const expected = ["#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"];
|
||||||
|
expect(parseCoolorsInput("https://coolors.co/palette/264653-2a9d8f-e9c46a-f4a261-e76f51")).toEqual(expected);
|
||||||
|
expect(parseCoolorsInput("https://coolors.co/264653-2a9d8f-e9c46a-f4a261-e76f51")).toEqual(expected);
|
||||||
|
expect(parseCoolorsInput("coolors.co/palette/264653-2A9D8F-e9c46a-f4a261-e76f51?ref=x")).toEqual(expected);
|
||||||
|
expect(parseCoolorsInput(" 264653-2a9d8f-e9c46a-f4a261-e76f51 ")).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects anything else", () => {
|
||||||
|
expect(parseCoolorsInput("")).toBeNull();
|
||||||
|
expect(parseCoolorsInput("https://example.com/264653-2a9d8f")).toBeNull();
|
||||||
|
expect(parseCoolorsInput("https://coolors.co/palettes/trending")).toBeNull();
|
||||||
|
expect(parseCoolorsInput("264653")).toBeNull();
|
||||||
|
expect(parseCoolorsInput("zzzzzz-2a9d8f")).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("maps every trending palette to a readable theme in both variants", () => {
|
||||||
|
for (const { colors } of TRENDING_PALETTES) {
|
||||||
|
for (const variant of ["dark", "light"] as const) {
|
||||||
|
const theme = themeFromPalette(colors, variant);
|
||||||
|
expect(contrast(theme.text, theme.background)).toBeGreaterThanOrEqual(4.5);
|
||||||
|
expect(contrast(theme.buttonText, theme.button)).toBeGreaterThanOrEqual(4.5);
|
||||||
|
expect(theme.button).not.toBe(theme.background);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses the darkest color as dark background and the lightest as light background", () => {
|
||||||
|
const colors = ["#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"];
|
||||||
|
expect(themeFromPalette(colors, "dark").background).toBe("#264653");
|
||||||
|
expect(themeFromPalette(colors, "light").background).toBe("#e9c46a");
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user