The plumbing layer the v0.16 UI features (and dark mode) build on.
Shared design tokens (Tailwind v4):
- tailwind-theme.css (new): @custom-variant dark (class-driven, beats OS
default) + @theme color/font/radius tokens + baseline html/html.dark
rules so any page that hasn't been re-themed still renders the right
body bg + color-scheme.
- src/app.css + export-viewer/src/app.css now import the shared theme.
- src/app.html: 6-line FOUC guard sets <html class="dark"> before paint
(mirrored from theme-store.ts) so dark reloads no longer flash white.
Adds <meta name="theme-color"> kept in sync by initTheme().
Cross-cutting stores (one per concern, per docs/FEATURES §2.9):
- data-mode-store.ts: 'saver' | 'original' per-device, plus pickMediaUrl
helper so feed cards / lightbox / diashow all resolve URLs the same way.
- privacy-note-store.ts: hydrated from /me/context, refreshed on SSE
event-updated.
- quota-store.ts: { enabled, used, limit, active_uploaders, free_disk },
refreshed after each upload completes.
- theme-store.ts: 'system' | 'light' | 'dark' preference + derived
appliedTheme + initTheme() that syncs <html class>, localStorage,
and the theme-color meta. Listens to prefers-color-scheme.
- auth.ts: currentPin writable mirror + clearPin() helper called from
the global pin-reset SSE handler — fixes the stale-PIN bug where the
localStorage copy survived a reset.
DTO mirror:
- types.ts: QuotaDto, MeContextDto, PinResetResponse, DeltaResponse each
carry a `// mirrors backend/...` comment per the lib README convention.
SSE client:
- sse.ts: KNOWN_EVENTS registry (one entry per server-emitted type),
synthetic feed-delta dispatched after foreground reconnect via the
/feed/delta?since= endpoint, exponential backoff (1 → 60 s + jitter)
on errors, attempt counter reset on user-initiated visibility resume.
Upload queue:
- upload-queue.ts: IDB schema bumped to v2 — entries tagged with userId;
loadQueue filters by current user (no cross-user leak on shared
devices); uploadItem refuses to upload an entry whose userId differs
from getUserId() (defense-in-depth); new clearQueue() called on
explicit logout. v2 upgrade wipes pre-v2 entries (no userId, can't
attribute safely).
Mobile primitives:
- actions/longpress.ts: 500 ms hold with 10 px move tolerance, swallows
the next click + the right-click contextmenu so the gesture doesn't
double-fire the inner button's onclick.
- actions/doubletap.ts: tap-pair detector that preventDefaults the
second tap so iOS Safari doesn't also zoom on double-tap.
- components/ContextSheet.svelte: generic bottom sheet driven by a
ContextAction[] prop. Reused by feed posts, comments, host user rows.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
72 lines
2.7 KiB
TypeScript
72 lines
2.7 KiB
TypeScript
// Per-device theme preference. Mirrors the data-mode store pattern.
|
|
//
|
|
// Three options:
|
|
// - 'system' follows `prefers-color-scheme` (default for new visitors)
|
|
// - 'light' force light
|
|
// - 'dark' force dark
|
|
//
|
|
// `appliedTheme` is the *resolved* light/dark for the current moment — derived from
|
|
// `themePreference` + the OS preference when 'system' is selected. Consumers that
|
|
// just want "is it dark right now?" should read `$appliedTheme`.
|
|
//
|
|
// The `applyTheme` side-effect toggles a `dark` class on the <html> element so the
|
|
// Tailwind v4 dark variant (configured in `tailwind-theme.css`) kicks in for every
|
|
// `dark:` utility across the app.
|
|
|
|
import { writable, derived, get } from 'svelte/store';
|
|
import { browser } from '$app/environment';
|
|
|
|
export type ThemePreference = 'system' | 'light' | 'dark';
|
|
export type AppliedTheme = 'light' | 'dark';
|
|
|
|
const KEY = 'eventsnap_theme';
|
|
const DEFAULT: ThemePreference = 'system';
|
|
|
|
function readInitial(): ThemePreference {
|
|
if (!browser) return DEFAULT;
|
|
const raw = localStorage.getItem(KEY);
|
|
return raw === 'light' || raw === 'dark' || raw === 'system' ? raw : DEFAULT;
|
|
}
|
|
|
|
function systemPrefersDark(): boolean {
|
|
if (!browser) return false;
|
|
return window.matchMedia?.('(prefers-color-scheme: dark)').matches ?? false;
|
|
}
|
|
|
|
export const themePreference = writable<ThemePreference>(readInitial());
|
|
|
|
/** Resolved light/dark — recomputed when the preference or OS theme changes. */
|
|
export const appliedTheme = derived(themePreference, ($pref, set) => {
|
|
const compute = () => set($pref === 'system' ? (systemPrefersDark() ? 'dark' : 'light') : $pref);
|
|
compute();
|
|
if (!browser || $pref !== 'system') return; // no OS listener needed when forced
|
|
const mq = window.matchMedia('(prefers-color-scheme: dark)');
|
|
const listener = () => compute();
|
|
mq.addEventListener('change', listener);
|
|
return () => mq.removeEventListener('change', listener);
|
|
});
|
|
|
|
/** Side-effect: keep the <html> class + localStorage + meta-color in sync. */
|
|
export function initTheme(): void {
|
|
if (!browser) return;
|
|
themePreference.subscribe((pref) => {
|
|
try {
|
|
localStorage.setItem(KEY, pref);
|
|
} catch {
|
|
// localStorage may be unavailable (Safari private mode); ignore.
|
|
}
|
|
});
|
|
appliedTheme.subscribe((mode) => {
|
|
document.documentElement.classList.toggle('dark', mode === 'dark');
|
|
// Update the browser chrome / status bar color so iOS Safari + Android stop
|
|
// painting it white on a dark page.
|
|
const meta = document.querySelector('meta[name="theme-color"]');
|
|
if (meta) meta.setAttribute('content', mode === 'dark' ? '#111827' : '#ffffff');
|
|
});
|
|
}
|
|
|
|
/** Convenience for one-off reads outside reactive contexts. */
|
|
export function isDark(): boolean {
|
|
return get(appliedTheme) === 'dark';
|
|
}
|