Files
homepage/src/lib/components/Toggle.svelte
Alexander Bocken 705a10bb3a recipes: overhaul nutrition editor UI and defer saves to form submission
- Nutrition mappings and global overwrites are now local-only until
  the recipe is saved, preventing premature DB writes on generate/edit
- Generate endpoint supports ?preview=true for non-persisting previews
- Show existing nutrition data immediately instead of requiring generate
- Replace raw checkboxes with Toggle component for global overwrites,
  initialized from existing NutritionOverwrite records
- Fix search dropdown readability (solid backgrounds, proper theming)
- Use fuzzy search (fzf-style) for manual nutrition ingredient lookup
- Swap ingredient display: German primary, English in brackets
- Allow editing g/u on manually mapped ingredients
- Make translation optional: separate save (FAB) and translate buttons
- "Vollständig neu übersetzen" now triggers actual full retranslation
- Show existing translation inline instead of behind a button
- Replace nord0 dark backgrounds with semantic theme variables
2026-04-02 19:46:03 +02:00

104 lines
2.6 KiB
Svelte

<script lang="ts">
let { checked = $bindable(false), label = "", accentColor = "var(--color-primary)", href = undefined as string | undefined, onchange = undefined as (() => void) | undefined } = $props<{ checked?: boolean, label?: string, accentColor?: string, href?: string, onchange?: () => void }>();
</script>
<style>
.toggle-wrapper {
display: inline-flex;
}
.toggle-wrapper label,
.toggle-wrapper a {
display: flex;
align-items: center;
gap: 0.75rem;
cursor: pointer;
font-size: 0.95rem;
color: var(--nord4);
text-decoration: none;
}
@media(prefers-color-scheme: light) {
:global(:root:not([data-theme="dark"])) .toggle-wrapper label,
:global(:root:not([data-theme="dark"])) .toggle-wrapper a {
color: var(--nord2);
}
}
:global(:root[data-theme="light"]) .toggle-wrapper label,
:global(:root[data-theme="light"]) .toggle-wrapper a {
color: var(--nord2);
}
.toggle-wrapper span {
user-select: none;
}
/* iOS-style toggle switch — shared by checkbox and link variants */
.toggle-track,
.toggle-wrapper input[type="checkbox"] {
appearance: none;
-webkit-appearance: none;
margin: 0;
width: 44px;
height: 24px;
background: var(--nord2);
border-radius: 24px;
position: relative;
cursor: pointer;
transition: background 0.3s ease;
outline: none;
border: none;
flex-shrink: 0;
display: inline-block;
}
@media(prefers-color-scheme: light) {
:global(:root:not([data-theme="dark"])) .toggle-track:not(.checked),
:global(:root:not([data-theme="dark"])) .toggle-wrapper input[type="checkbox"]:not(:checked) {
background: var(--nord4);
}
}
:global(:root[data-theme="light"]) .toggle-track:not(.checked),
:global(:root[data-theme="light"]) .toggle-wrapper input[type="checkbox"]:not(:checked) {
background: var(--nord4);
}
.toggle-track.checked,
.toggle-wrapper input[type="checkbox"]:checked {
background: var(--accent-color);
}
.toggle-track::before,
.toggle-wrapper input[type="checkbox"]::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
top: 2px;
left: 2px;
background: white;
transition: transform 0.3s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.toggle-track.checked::before,
.toggle-wrapper input[type="checkbox"]:checked::before {
transform: translateX(20px);
}
</style>
<div class="toggle-wrapper" style="--accent-color: {accentColor}">
{#if href}
<a {href} onclick={(e) => { e.preventDefault(); checked = !checked; }}>
<span class="toggle-track" class:checked></span>
<span>{label}</span>
</a>
{:else}
<label>
<input type="checkbox" bind:checked onchange={onchange} />
<span>{label}</span>
</label>
{/if}
</div>