|
| 1 | +<script lang="ts"> |
| 2 | + import { RadioGroup, type RadioGroupValueChangeDetails } from '@ark-ui/svelte/radio-group' |
| 3 | + import { getSetting, setSetting, getSettingDefinition, type SettingId, type SettingsValues } from '$lib/settings' |
| 4 | + import type { Snippet } from 'svelte' |
| 5 | +
|
| 6 | + interface Props { |
| 7 | + id: SettingId |
| 8 | + disabled?: boolean |
| 9 | + customContent?: Snippet<[string]> |
| 10 | + } |
| 11 | +
|
| 12 | + const { id, disabled = false, customContent }: Props = $props() |
| 13 | +
|
| 14 | + const definition = getSettingDefinition(id) |
| 15 | + const options = definition?.constraints?.options ?? [] |
| 16 | +
|
| 17 | + let value = $state(String(getSetting(id))) |
| 18 | +
|
| 19 | + async function handleValueChange(details: RadioGroupValueChangeDetails) { |
| 20 | + if (details.value) { |
| 21 | + value = details.value |
| 22 | + await setSetting(id, details.value as SettingsValues[typeof id]) |
| 23 | + } |
| 24 | + } |
| 25 | +</script> |
| 26 | + |
| 27 | +<RadioGroup.Root {value} onValueChange={handleValueChange} {disabled}> |
| 28 | + <div class="radio-group"> |
| 29 | + {#each options as option} |
| 30 | + <RadioGroup.Item value={String(option.value)} class="radio-item" {disabled}> |
| 31 | + <RadioGroup.ItemControl class="radio-control" /> |
| 32 | + <RadioGroup.ItemText class="radio-text"> |
| 33 | + <span class="radio-label">{option.label}</span> |
| 34 | + {#if option.description} |
| 35 | + <span class="radio-description">{option.description}</span> |
| 36 | + {/if} |
| 37 | + </RadioGroup.ItemText> |
| 38 | + <RadioGroup.ItemHiddenInput /> |
| 39 | + </RadioGroup.Item> |
| 40 | + |
| 41 | + <!-- Show custom content after the matching option --> |
| 42 | + {#if customContent && option.value === value} |
| 43 | + <div class="custom-content"> |
| 44 | + {@render customContent(String(option.value))} |
| 45 | + </div> |
| 46 | + {/if} |
| 47 | + {/each} |
| 48 | + </div> |
| 49 | +</RadioGroup.Root> |
| 50 | + |
| 51 | +<style> |
| 52 | + .radio-group { |
| 53 | + display: flex; |
| 54 | + flex-direction: column; |
| 55 | + gap: var(--spacing-xs); |
| 56 | + } |
| 57 | +
|
| 58 | + :global(.radio-item) { |
| 59 | + display: flex; |
| 60 | + align-items: flex-start; |
| 61 | + gap: var(--spacing-sm); |
| 62 | + padding: var(--spacing-xs) 0; |
| 63 | + cursor: pointer; |
| 64 | + } |
| 65 | +
|
| 66 | + :global(.radio-item[data-disabled]) { |
| 67 | + cursor: not-allowed; |
| 68 | + opacity: 0.5; |
| 69 | + } |
| 70 | +
|
| 71 | + :global(.radio-control) { |
| 72 | + width: 16px; |
| 73 | + height: 16px; |
| 74 | + border: 2px solid var(--color-border-primary); |
| 75 | + border-radius: 50%; |
| 76 | + background: var(--color-bg-primary); |
| 77 | + flex-shrink: 0; |
| 78 | + margin-top: 2px; |
| 79 | + transition: all 0.15s; |
| 80 | + } |
| 81 | +
|
| 82 | + :global(.radio-control[data-state='checked']) { |
| 83 | + border-color: var(--color-accent); |
| 84 | + background: var(--color-accent); |
| 85 | + box-shadow: inset 0 0 0 3px var(--color-bg-primary); |
| 86 | + } |
| 87 | +
|
| 88 | + :global(.radio-item:hover:not([data-disabled]) .radio-control) { |
| 89 | + border-color: var(--color-accent); |
| 90 | + } |
| 91 | +
|
| 92 | + :global(.radio-text) { |
| 93 | + display: flex; |
| 94 | + flex-direction: column; |
| 95 | + gap: 2px; |
| 96 | + } |
| 97 | +
|
| 98 | + .radio-label { |
| 99 | + color: var(--color-text-primary); |
| 100 | + font-size: var(--font-size-sm); |
| 101 | + } |
| 102 | +
|
| 103 | + .radio-description { |
| 104 | + color: var(--color-text-muted); |
| 105 | + font-size: var(--font-size-xs); |
| 106 | + } |
| 107 | +
|
| 108 | + .custom-content { |
| 109 | + margin-left: 24px; |
| 110 | + margin-top: var(--spacing-xs); |
| 111 | + margin-bottom: var(--spacing-sm); |
| 112 | + } |
| 113 | +</style> |
0 commit comments