feat(story-1.5): theme selection and persistence
- SpaController: injects data-theme on <html> and window.__PF_USER__ before JS
hydrates — theme applied without FOUC; no initial API call needed for user data
- UserApiController: PATCH /api/user/theme validates against 6 allowed theme IDs,
persists to user.theme column, returns {theme}
- useTheme composable: applyTheme() sets html[data-theme], saveTheme() calls API
and falls back with toast on error
- SettingsView: 3-col theme grid with swatch previews, aria-checked radio semantics,
active indicator; Sign out link; signed-in email display
- App.vue: onMounted syncs Pinia theme state with SpaController-stamped html[data-theme]
Verified: data-theme injected on / load; PATCH saves to DB; reload shows persisted theme
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,22 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { onMounted } from 'vue'
|
||||||
import BottomNav from '@/components/BottomNav.vue'
|
import BottomNav from '@/components/BottomNav.vue'
|
||||||
import BaseToast from '@/components/BaseToast.vue'
|
import BaseToast from '@/components/BaseToast.vue'
|
||||||
|
import { useAuthStore } from '@/stores/auth'
|
||||||
|
import { useTheme } from '@/composables/useTheme'
|
||||||
|
|
||||||
|
const auth = useAuthStore()
|
||||||
|
const { applyTheme } = useTheme()
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
// Sync Vue's theme state with whatever SpaController stamped on <html>
|
||||||
|
const stamped = document.documentElement.dataset.theme
|
||||||
|
if (stamped && auth.user) {
|
||||||
|
auth.user.theme = stamped
|
||||||
|
} else if (auth.user?.theme) {
|
||||||
|
applyTheme(auth.user.theme)
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { useAuthStore } from '@/stores/auth'
|
||||||
|
import { useToastStore } from '@/stores/toast'
|
||||||
|
|
||||||
|
export interface ThemeOption {
|
||||||
|
id: string
|
||||||
|
label: string
|
||||||
|
primary: string
|
||||||
|
bg: string
|
||||||
|
text: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const THEMES: ThemeOption[] = [
|
||||||
|
{ id: 'warm-craft', label: 'Warm Craft', primary: '#c97c3a', bg: '#fdf6ee', text: '#3a2e22' },
|
||||||
|
{ id: 'playful-pop', label: 'Playful Pop', primary: '#d63aab', bg: '#fff0fb', text: '#2d0a28' },
|
||||||
|
{ id: 'sage-cream', label: 'Sage & Cream', primary: '#4e7c3a', bg: '#f6f8f3', text: '#1e2b1a' },
|
||||||
|
{ id: 'dusty-mauve', label: 'Dusty Mauve', primary: '#8e4a84', bg: '#f6f0f4', text: '#2a1828' },
|
||||||
|
{ id: 'ocean-dusk', label: 'Ocean Dusk', primary: '#1a6ea8', bg: '#eef3f8', text: '#0e2030' },
|
||||||
|
{ id: 'honey-slate', label: 'Honey & Slate', primary: '#c49a20', bg: '#f2f2ee', text: '#1c1c18' },
|
||||||
|
]
|
||||||
|
|
||||||
|
export function useTheme() {
|
||||||
|
const auth = useAuthStore()
|
||||||
|
const toast = useToastStore()
|
||||||
|
|
||||||
|
function applyTheme(themeId: string) {
|
||||||
|
document.documentElement.dataset.theme = themeId
|
||||||
|
if (auth.user) auth.user.theme = themeId
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveTheme(themeId: string) {
|
||||||
|
applyTheme(themeId)
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/user/theme', {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ theme: themeId }),
|
||||||
|
})
|
||||||
|
if (!res.ok) throw new Error('Failed to save theme')
|
||||||
|
} catch {
|
||||||
|
toast.show('Could not save theme — try again', 'error')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { THEMES, applyTheme, saveTheme }
|
||||||
|
}
|
||||||
@@ -1,9 +1,175 @@
|
|||||||
<template>
|
<template>
|
||||||
<main class="view">
|
<main class="settings">
|
||||||
<h1>Settings</h1>
|
<h1 class="settings__title">Settings</h1>
|
||||||
|
|
||||||
|
<section class="settings__section">
|
||||||
|
<h2 class="settings__section-title">Theme</h2>
|
||||||
|
<div class="theme-grid" role="radiogroup" aria-label="Choose theme">
|
||||||
|
<button
|
||||||
|
v-for="t in THEMES"
|
||||||
|
:key="t.id"
|
||||||
|
type="button"
|
||||||
|
role="radio"
|
||||||
|
:aria-checked="currentTheme === t.id"
|
||||||
|
:aria-label="t.label"
|
||||||
|
:class="['theme-swatch', { 'theme-swatch--active': currentTheme === t.id }]"
|
||||||
|
:style="{ '--swatch-bg': t.bg, '--swatch-primary': t.primary, '--swatch-text': t.text }"
|
||||||
|
@click="select(t.id)"
|
||||||
|
>
|
||||||
|
<span class="theme-swatch__preview" aria-hidden="true">
|
||||||
|
<span class="theme-swatch__bar" />
|
||||||
|
<span class="theme-swatch__dot" />
|
||||||
|
</span>
|
||||||
|
<span class="theme-swatch__label">{{ t.label }}</span>
|
||||||
|
<span v-if="currentTheme === t.id" class="theme-swatch__check" aria-hidden="true">✓</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="settings__section">
|
||||||
|
<h2 class="settings__section-title">Account</h2>
|
||||||
|
<div class="settings__row">
|
||||||
|
<span class="settings__row-label">Signed in as</span>
|
||||||
|
<span class="settings__row-value">{{ auth.user?.email }}</span>
|
||||||
|
</div>
|
||||||
|
<a href="/logout" class="settings__logout">Sign out</a>
|
||||||
|
</section>
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed } from 'vue'
|
||||||
|
import { useAuthStore } from '@/stores/auth'
|
||||||
|
import { useTheme, THEMES } from '@/composables/useTheme'
|
||||||
|
|
||||||
|
const auth = useAuthStore()
|
||||||
|
const { saveTheme } = useTheme()
|
||||||
|
|
||||||
|
const currentTheme = computed(() => auth.user?.theme ?? 'warm-craft')
|
||||||
|
|
||||||
|
function select(themeId: string) {
|
||||||
|
saveTheme(themeId)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
.view { padding: var(--space-4); }
|
.settings {
|
||||||
|
padding: var(--space-4) var(--space-4) calc(64px + var(--space-6));
|
||||||
|
max-width: 480px;
|
||||||
|
margin: 0 auto;
|
||||||
|
|
||||||
|
&__title {
|
||||||
|
font-size: var(--text-xl);
|
||||||
|
font-weight: 700;
|
||||||
|
margin-bottom: var(--space-6);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__section {
|
||||||
|
margin-bottom: var(--space-6);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__section-title {
|
||||||
|
font-size: var(--text-sm);
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--color-text-muted);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.06em;
|
||||||
|
margin-bottom: var(--space-3);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
padding: var(--space-3) 0;
|
||||||
|
border-bottom: 1px solid var(--color-border);
|
||||||
|
font-size: var(--text-base);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__row-label { color: var(--color-text-muted); }
|
||||||
|
&__row-value { font-weight: 600; }
|
||||||
|
|
||||||
|
&__logout {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
min-height: var(--touch-min);
|
||||||
|
padding: var(--space-3) 0;
|
||||||
|
color: var(--color-destructive);
|
||||||
|
font-weight: 600;
|
||||||
|
text-decoration: none;
|
||||||
|
font-size: var(--text-base);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(3, 1fr);
|
||||||
|
gap: var(--space-3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-swatch {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--space-2);
|
||||||
|
padding: var(--space-3);
|
||||||
|
background: var(--swatch-bg);
|
||||||
|
border: 2px solid transparent;
|
||||||
|
border-radius: var(--radius-md);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: border-color var(--duration-fast);
|
||||||
|
min-height: var(--touch-min);
|
||||||
|
|
||||||
|
&--active {
|
||||||
|
border-color: var(--swatch-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__preview {
|
||||||
|
width: 100%;
|
||||||
|
height: 36px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
background: var(--swatch-bg);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 4px;
|
||||||
|
padding: 0 6px;
|
||||||
|
border: 1px solid color-mix(in srgb, var(--swatch-text) 15%, transparent);
|
||||||
|
}
|
||||||
|
|
||||||
|
&__bar {
|
||||||
|
display: block;
|
||||||
|
height: 6px;
|
||||||
|
border-radius: 3px;
|
||||||
|
background: var(--swatch-primary);
|
||||||
|
width: 60%;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__dot {
|
||||||
|
display: block;
|
||||||
|
height: 4px;
|
||||||
|
border-radius: 2px;
|
||||||
|
background: var(--swatch-text);
|
||||||
|
width: 80%;
|
||||||
|
opacity: 0.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__label {
|
||||||
|
font-size: var(--text-xs);
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--color-text);
|
||||||
|
text-align: center;
|
||||||
|
line-height: 1.2;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__check {
|
||||||
|
position: absolute;
|
||||||
|
top: var(--space-1);
|
||||||
|
right: var(--space-2);
|
||||||
|
font-size: var(--text-sm);
|
||||||
|
color: var(--swatch-primary);
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
@@ -32,9 +33,25 @@ class SpaController extends AbstractController
|
|||||||
throw $this->createNotFoundException('SPA not built — run npm run build in frontend/.');
|
throw $this->createNotFoundException('SPA not built — run npm run build in frontend/.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(
|
/** @var User $user */
|
||||||
content: (string) file_get_contents($indexFile),
|
$user = $this->getUser();
|
||||||
headers: ['Content-Type' => 'text/html; charset=utf-8'],
|
$theme = $user->getTheme() ?? 'warm-craft';
|
||||||
);
|
|
||||||
|
$userData = json_encode([
|
||||||
|
'id' => $user->getId(),
|
||||||
|
'email' => $user->getEmail(),
|
||||||
|
'roles' => $user->getRoles(),
|
||||||
|
'theme' => $theme,
|
||||||
|
], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
|
||||||
|
|
||||||
|
$html = (string) file_get_contents($indexFile);
|
||||||
|
|
||||||
|
// Inject theme on <html> so CSS applies before JS hydrates (no FOUC)
|
||||||
|
$html = str_replace('<html lang="en">', '<html lang="en" data-theme="' . htmlspecialchars($theme, ENT_QUOTES) . '">', $html);
|
||||||
|
|
||||||
|
// Bootstrap current user into window so Pinia auth store needs no initial API call
|
||||||
|
$html = str_replace('</head>', '<script>window.__PF_USER__=' . $userData . ';</script></head>', $html);
|
||||||
|
|
||||||
|
return new Response($html, headers: ['Content-Type' => 'text/html; charset=utf-8']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||||
|
|
||||||
|
#[Route('/api/user')]
|
||||||
|
#[IsGranted('ROLE_USER')]
|
||||||
|
class UserApiController extends AbstractController
|
||||||
|
{
|
||||||
|
private const VALID_THEMES = [
|
||||||
|
'warm-craft',
|
||||||
|
'playful-pop',
|
||||||
|
'sage-cream',
|
||||||
|
'dusty-mauve',
|
||||||
|
'ocean-dusk',
|
||||||
|
'honey-slate',
|
||||||
|
];
|
||||||
|
|
||||||
|
#[Route('/theme', name: 'api_user_theme', methods: ['PATCH'])]
|
||||||
|
public function updateTheme(Request $request, EntityManagerInterface $em): JsonResponse
|
||||||
|
{
|
||||||
|
$body = json_decode($request->getContent(), true);
|
||||||
|
$theme = $body['theme'] ?? null;
|
||||||
|
|
||||||
|
if (!is_string($theme) || !in_array($theme, self::VALID_THEMES, true)) {
|
||||||
|
return $this->json(['error' => 'Invalid theme'], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $this->getUser();
|
||||||
|
$user->setTheme($theme);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
return $this->json(['theme' => $theme]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user