|
| 1 | +<script lang="ts"> |
| 2 | + import { RadioGroup, Slider, Switch } from "bits-ui"; |
| 3 | + import Input from "../ui/Input.svelte"; |
| 4 | + import Label from "../ui/Label.svelte"; |
| 5 | + import Field from "./Field.svelte"; |
| 6 | + import type { SettingsField } from "./types"; |
| 7 | +
|
| 8 | + interface Props { |
| 9 | + field: SettingsField; |
| 10 | + depth?: number; |
| 11 | + } |
| 12 | +
|
| 13 | + const { field, depth = 2 }: Props = $props(); |
| 14 | +
|
| 15 | + const heading = `h${depth}`; |
| 16 | +</script> |
| 17 | + |
| 18 | +{#if field.type === "group"} |
| 19 | + <div> |
| 20 | + <svelte:element |
| 21 | + this={heading} |
| 22 | + class="mb-4 inline-block font-semibold [h2]:text-xl [h3]:text-lg" |
| 23 | + > |
| 24 | + {field.label} |
| 25 | + </svelte:element> |
| 26 | + |
| 27 | + <div class="space-y-6"> |
| 28 | + {#each field.fields as subField} |
| 29 | + <Field field={subField} depth={depth + 1} /> |
| 30 | + {/each} |
| 31 | + </div> |
| 32 | + </div> |
| 33 | +{:else if field.type === "custom"} |
| 34 | + <div class="flex flex-col gap-2"> |
| 35 | + <span class="text-lg font-semibold">{field.label}</span> |
| 36 | + |
| 37 | + {@render description(field.description)} |
| 38 | + |
| 39 | + <field.component /> |
| 40 | + </div> |
| 41 | +{:else if field.type === "input"} |
| 42 | + <div class="flex flex-col gap-2"> |
| 43 | + <Label for={field.id}>{field.label}</Label> |
| 44 | + |
| 45 | + <Input |
| 46 | + id={field.id} |
| 47 | + class="max-w-1/2" |
| 48 | + type="text" |
| 49 | + autocapitalize="off" |
| 50 | + autocomplete="off" |
| 51 | + disabled={field.disabled?.()} |
| 52 | + bind:value={field.binding.get, field.binding.set} |
| 53 | + /> |
| 54 | + |
| 55 | + {@render description(field.description)} |
| 56 | + </div> |
| 57 | +{:else if field.type === "radio"} |
| 58 | + <div class="flex flex-col gap-2"> |
| 59 | + <Label for={field.id}>{field.label}</Label> |
| 60 | + |
| 61 | + {@render description(field.description)} |
| 62 | + |
| 63 | + <RadioGroup.Root |
| 64 | + id={field.id} |
| 65 | + class="group space-y-1 data-disabled:cursor-not-allowed data-disabled:opacity-50" |
| 66 | + disabled={field.disabled?.()} |
| 67 | + bind:value={field.binding.get, field.binding.set} |
| 68 | + > |
| 69 | + {#each field.options as option (option.value)} |
| 70 | + <Label |
| 71 | + class="hover:bg-muted has-data-[state=checked]:bg-muted flex items-center gap-3 rounded-md px-3 py-2 text-sm transition-colors duration-100 hover:cursor-pointer aria-disabled:cursor-not-allowed" |
| 72 | + aria-disabled={field.disabled?.()} |
| 73 | + > |
| 74 | + <RadioGroup.Item |
| 75 | + class="data-[state=checked]:border-twitch data-[state=checked]:bg-foreground size-4 rounded-full border data-[state=checked]:border-5" |
| 76 | + value={option.value} |
| 77 | + /> |
| 78 | + |
| 79 | + {option.label} |
| 80 | + </Label> |
| 81 | + {/each} |
| 82 | + </RadioGroup.Root> |
| 83 | + </div> |
| 84 | +{:else if field.type === "slider"} |
| 85 | + <div> |
| 86 | + <div class="mb-4"> |
| 87 | + <Label class="mb-2" for={field.id}>{field.label}</Label> |
| 88 | + |
| 89 | + {@render description(field.description)} |
| 90 | + </div> |
| 91 | + |
| 92 | + <Slider.Root |
| 93 | + id={field.id} |
| 94 | + class="relative flex items-center" |
| 95 | + type="single" |
| 96 | + min={field.min} |
| 97 | + max={field.max} |
| 98 | + step={field.step} |
| 99 | + disabled={field.disabled?.()} |
| 100 | + bind:value={field.binding.get, field.binding.set} |
| 101 | + > |
| 102 | + <div class="bg-input relative h-1.5 w-full rounded-full hover:cursor-pointer"> |
| 103 | + <Slider.Range class="bg-twitch absolute h-full rounded-full" /> |
| 104 | + </div> |
| 105 | + |
| 106 | + <Slider.Thumb |
| 107 | + class="flex size-5 justify-center rounded-full bg-white hover:cursor-grab active:scale-110 active:cursor-grabbing" |
| 108 | + index={0} |
| 109 | + > |
| 110 | + <div class="mt-7 text-center text-xs font-medium"> |
| 111 | + {field.binding.get()} |
| 112 | + </div> |
| 113 | + </Slider.Thumb> |
| 114 | + </Slider.Root> |
| 115 | + </div> |
| 116 | +{:else if field.type === "toggle"} |
| 117 | + <div class="space-y-2"> |
| 118 | + <Label class="flex items-center justify-between hover:cursor-pointer" for={field.id}> |
| 119 | + <span class="font-medium">{field.label}</span> |
| 120 | + |
| 121 | + <Switch.Root |
| 122 | + id={field.id} |
| 123 | + class="data-[state=checked]:bg-twitch data-[state=unchecked]:bg-input h-6 w-11 items-center rounded-full border-2 border-transparent transition-colors" |
| 124 | + bind:checked={field.binding.get, field.binding.set} |
| 125 | + > |
| 126 | + <Switch.Thumb |
| 127 | + class="pointer-events-none block size-4.5 rounded-full bg-white shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-5 data-[state=unchecked]:translate-x-0.5" |
| 128 | + /> |
| 129 | + </Switch.Root> |
| 130 | + </Label> |
| 131 | + |
| 132 | + {@render description(field.description)} |
| 133 | + </div> |
| 134 | +{/if} |
| 135 | + |
| 136 | +{#snippet description(description?: string)} |
| 137 | + {#if description} |
| 138 | + <p class="text-muted-foreground text-sm"> |
| 139 | + {@html description} |
| 140 | + </p> |
| 141 | + {/if} |
| 142 | +{/snippet} |
0 commit comments