Private
Public Access
Add custom theme colors, full per-token control (#30)
Adds a 5th "Custom" option to the theme swatch grid alongside the existing 4 presets, opening a picker for all ~12 CSS custom properties (backgrounds, borders, text, three accent tiers, highlight, danger) plus a light/dark toggle for native control rendering. Persisted as a new users.custom_theme_colors JSONB column, validated server-side against exactly what a native <input type="color"> can ever produce. Colors survive switching to a preset and back, since there's no reason picking a preset for a moment should force redoing every color pick. Applied at runtime as inline custom properties on :root (presets stay static CSS) via a shared applyTheme() helper, which is also responsible for clearing those inline overrides when switching away -- otherwise they'd silently keep winning over whatever preset's own stylesheet values should apply next. Live preview on every color change; persists only on explicit "Save colors" (not per keystroke, since a color input fires continuously while dragging), and closing the modal without saving reverts the preview back to whatever's actually persisted. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,31 @@ class UserCreate(BaseModel):
|
||||
password: str = Field(min_length=8, max_length=200)
|
||||
|
||||
|
||||
# Matches exactly the CSS custom properties frontend/src/styles/themes.css
|
||||
# overrides per built-in preset -- a "custom" theme is applied the same way,
|
||||
# just as inline styles on :root instead of a static stylesheet block (see
|
||||
# frontend/src/lib/theme.ts). Hex-only (`#rrggbb`) since that's exactly what
|
||||
# a native <input type="color"> always produces -- no alpha, no shorthand --
|
||||
# so the pattern constraint can't reject anything the picker UI itself sends.
|
||||
_HEX_COLOR = Field(pattern=r"^#[0-9a-fA-F]{6}$")
|
||||
|
||||
|
||||
class CustomThemeColors(BaseModel):
|
||||
void: str = _HEX_COLOR
|
||||
void_2: str = _HEX_COLOR
|
||||
surface: str = _HEX_COLOR
|
||||
surface_2: str = _HEX_COLOR
|
||||
border: str = _HEX_COLOR
|
||||
text: str = _HEX_COLOR
|
||||
muted: str = _HEX_COLOR
|
||||
accent: str = _HEX_COLOR
|
||||
accent_2: str = _HEX_COLOR
|
||||
accent_3: str = _HEX_COLOR
|
||||
highlight: str = _HEX_COLOR
|
||||
danger: str = _HEX_COLOR
|
||||
color_scheme: Literal["light", "dark"]
|
||||
|
||||
|
||||
class UserRead(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@@ -21,6 +46,7 @@ class UserRead(BaseModel):
|
||||
is_site_admin: bool
|
||||
display_name: str | None
|
||||
theme: str | None
|
||||
custom_theme_colors: CustomThemeColors | None
|
||||
avatar_filename: str | None
|
||||
appear_offline: bool
|
||||
created_at: datetime
|
||||
@@ -34,7 +60,8 @@ class ProfileUpdate(BaseModel):
|
||||
# their defaults, and vice versa.
|
||||
display_name: str | None = Field(default=None, max_length=50)
|
||||
# Kept in sync with frontend/src/styles/themes.css's theme blocks.
|
||||
theme: Literal["dark", "light", "midnight", "sunset"] | None = Field(default=None)
|
||||
theme: Literal["dark", "light", "midnight", "sunset", "custom"] | None = Field(default=None)
|
||||
custom_theme_colors: CustomThemeColors | None = Field(default=None)
|
||||
appear_offline: bool | None = Field(default=None)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user