impr: change Switch.svelte into radio based system

This commit is contained in:
Xylight committed 2025-09-24 16:51:25 -07:00
1 parent 6f2c7c8d2f
commit d85b889409
4 files changed
+39 -12

No files matched your search

+32 -7
View File
@@ -21,24 +21,49 @@
onselect, onselect,
}: Props = $props() }: Props = $props()
let selectedIndex = $state(0)
let id = $props.id()
/* you see id like this to be a super simple
*/
$effect(() => { $effect(() => {
onselect?.(selected) const newIndex = options.findIndex((i) => i === selected)
if (newIndex !== -1) {
selectedIndex = newIndex
}
})
$effect(() => {
const newSelected = options[selectedIndex]
if (newSelected !== undefined && newSelected !== selected) {
selected = newSelected
onselect?.(newSelected)
}
}) })
</script> </script>
<div <fieldset
class="flex items-center gap-1 w-max max-w-full z-0 relative overflow-auto" class="flex items-center gap-1 w-max max-w-full z-0 relative overflow-auto"
> >
{#each options as option, index} {#each options as option, index}
{@const slctd = selected == option} <label>
<input
type="radio"
bind:group={selectedIndex}
value={index}
name={id}
id="{id}-{option?.toString()}"
class="hidden"
/>
<TabButton <TabButton
selected={slctd} selected={selectedIndex == index}
onselect={() => (selected = option)}
disabled={disabled[index]} disabled={disabled[index]}
type="button" element="div"
> >
{optionNames[index] || option} {optionNames[index] || option}
</TabButton> </TabButton>
</label>
{/each} {/each}
</div> </fieldset>
{@render children?.({ selected })} {@render children?.({ selected })}
@@ -11,6 +11,7 @@
selected?: boolean selected?: boolean
children: Snippet children: Snippet
size?: keyof typeof sizes size?: keyof typeof sizes
element?: 'div' | 'a' | 'button'
[key: string]: any [key: string]: any
} }
@@ -21,12 +22,13 @@
children, children,
href, href,
size = 'sm', size = 'sm',
element = href ? 'a' : 'button',
...rest ...rest
}: Props = $props() }: Props = $props()
</script> </script>
<svelte:element <svelte:element
this={href ? 'a' : 'button'} this={element}
class={[ class={[
sizes[size], sizes[size],
'rounded-xl text-sm font-medium transition-colors duration-75 relative cursor-pointer border shadow-xs', 'rounded-xl text-sm font-medium transition-colors duration-75 relative cursor-pointer border shadow-xs',
+2 -2
View File
@@ -88,7 +88,7 @@
<Label <Label
text={label} text={label}
customText={customLabel} customText={customLabel}
class={['space-y-1 relative max-w-full w-max', baseClass]} class={['space-y-1 relative max-w-full w-max min-w-0', baseClass]}
> >
<div class="relative max-w-full" role="presentation"> <div class="relative max-w-full" role="presentation">
<select <select
@@ -97,7 +97,7 @@
bind:this={element} bind:this={element}
class={[ class={[
buttonSize[size], buttonSize[size],
'btn btn-secondary select rounded-xl appearance-none pr-6!', 'btn btn-secondary select rounded-xl appearance-none pr-6! w-full',
selectClass, selectClass,
clazz, clazz,
]} ]}
+2 -2
View File
@@ -41,8 +41,8 @@
<Header pageHeader> <Header pageHeader>
{$t('routes.frontpage.title')} {$t('routes.frontpage.title')}
{#snippet extended()} {#snippet extended()}
<form class="" method="get" action={page.url.pathname}> <form class="contents" method="get" action={page.url.pathname}>
<div class="flex flex-row gap-2"> <div class="flex flex-row gap-2 max-w-full">
<Location <Location
name="type" name="type"
navigate navigate