Refactored Select component

This commit is contained in:
Justin Edmund 2025-09-17 22:10:38 -07:00
parent 5466a70916
commit bca4843885
2 changed files with 355 additions and 383 deletions

View file

@ -0,0 +1,355 @@
<svelte:options runes={true} />
<script lang="ts" generics="T extends string | number">
import { Select as SelectPrimitive } from 'bits-ui'
import { Label } from 'bits-ui'
import Icon from '../Icon.svelte'
interface Option {
value: T
label: string
disabled?: boolean
image?: string
}
interface Props {
options: Option[]
value?: T
onValueChange?: (value: T | undefined) => void
placeholder?: string
disabled?: boolean
size?: 'small' | 'medium' | 'large'
contained?: boolean
fullWidth?: boolean
label?: string
error?: string
required?: boolean
class?: string
}
let {
options = [],
value = $bindable(),
onValueChange,
placeholder = 'Select an option',
disabled = false,
size = 'medium',
contained = false,
fullWidth = false,
label,
error,
required = false,
class: className = ''
}: Props = $props()
// Convert options to string values for Bits UI (which expects strings internally)
const stringOptions = $derived(
options.map((opt) => ({
...opt,
value: String(opt.value)
}))
)
const selected = $derived(options.find((opt) => opt.value === value))
const hasWrapper = $derived(label || error)
const fieldsetClasses = $derived(
['fieldset', fullWidth && 'full', className].filter(Boolean).join(' ')
)
const selectClasses = $derived(
[
'select',
size,
contained && 'contained',
fullWidth && 'full',
disabled && 'disabled'
]
.filter(Boolean)
.join(' ')
)
function handleValueChange(newValue: string | undefined) {
if (newValue !== undefined) {
// Convert string back to original type
const typedValue = (typeof options[0]?.value === 'number' ? Number(newValue) : newValue) as T
value = typedValue
if (onValueChange) {
onValueChange(typedValue)
}
}
}
</script>
{#if hasWrapper}
<fieldset class={fieldsetClasses}>
{#if label}
<Label.Root class="label" for={crypto.randomUUID()}>
{label}
{#if required}
<span class="required">*</span>
{/if}
</Label.Root>
{/if}
<SelectPrimitive.Root type="single" value={value !== undefined && value !== null ? String(value) : undefined} onValueChange={handleValueChange} {disabled} items={stringOptions}>
<SelectPrimitive.Trigger class={selectClasses} data-placeholder={!selected}>
{#if selected?.image}
<img src={selected.image} alt={selected.label} class="image" />
{/if}
<span class="text">{selected?.label || placeholder}</span>
<Icon name="chevron-down-small" size={14} class="chevron" />
</SelectPrimitive.Trigger>
<SelectPrimitive.Content class="content">
<SelectPrimitive.Viewport>
{#each options as option}
<SelectPrimitive.Item value={String(option.value)} disabled={option.disabled} class="item">
{#if option.image}
<img src={option.image} alt={option.label} class="image" />
{/if}
<span class="text">{option.label}</span>
<SelectPrimitive.ItemIndicator class="indicator">
<Icon name="check" size={14} />
</SelectPrimitive.ItemIndicator>
</SelectPrimitive.Item>
{/each}
</SelectPrimitive.Viewport>
</SelectPrimitive.Content>
</SelectPrimitive.Root>
{#if error}
<span class="error">{error}</span>
{/if}
</fieldset>
{:else}
<SelectPrimitive.Root type="single" value={value !== undefined && value !== null ? String(value) : undefined} onValueChange={handleValueChange} {disabled} items={stringOptions} class={className}>
<SelectPrimitive.Trigger class={selectClasses} data-placeholder={!selected}>
{#if selected?.image}
<img src={selected.image} alt={selected.label} class="image" />
{/if}
<span class="text">{selected?.label || placeholder}</span>
<Icon name="chevron-down-small" size={14} class="chevron" />
</SelectPrimitive.Trigger>
<SelectPrimitive.Content class="content">
<SelectPrimitive.Viewport>
{#each options as option}
<SelectPrimitive.Item value={String(option.value)} disabled={option.disabled} class="item">
{#if option.image}
<img src={option.image} alt={option.label} class="image" />
{/if}
<span class="text">{option.label}</span>
<SelectPrimitive.ItemIndicator class="indicator">
<Icon name="check" size={14} />
</SelectPrimitive.ItemIndicator>
</SelectPrimitive.Item>
{/each}
</SelectPrimitive.Viewport>
</SelectPrimitive.Content>
</SelectPrimitive.Root>
{/if}
<style lang="scss">
@use '$src/themes/spacing' as *;
@use '$src/themes/colors' as *;
@use '$src/themes/typography' as *;
@use '$src/themes/layout' as *;
@use '$src/themes/mixins' as *;
@use '$src/themes/effects' as *;
// Fieldset wrapper (matching Input component)
.fieldset {
display: flex;
flex-direction: column;
gap: $unit-half;
&.full {
width: 100%;
}
:global(.label) {
color: var(--text-primary);
font-size: $font-small;
font-weight: $medium;
margin-bottom: $unit-half;
}
:global(.label .required) {
color: $error;
margin-left: $unit-fourth;
}
.error {
color: $error;
font-size: $font-small;
padding: $unit-half $unit-2x;
}
}
// Select trigger styling (matching Input component)
:global([data-select-trigger]) {
all: unset;
box-sizing: border-box;
display: inline-flex;
cursor: pointer;
}
:global([data-select-trigger].select) {
-webkit-font-smoothing: antialiased;
align-items: center;
background-color: var(--input-bg);
border-radius: $input-corner;
border: 2px solid transparent;
color: var(--text-primary);
display: flex;
font-family: var(--font-family);
gap: $unit;
width: 100%;
@include smooth-transition($duration-quick, background-color, border-color);
&:hover:not(.disabled) {
background-color: var(--input-bg-hover);
}
&:focus-visible {
@include focus-ring($blue);
}
}
// Size modifiers (matching Button component)
:global([data-select-trigger].select.small) {
padding: $unit $unit-2x;
font-size: $font-small;
min-height: calc($unit * 3.5);
}
:global([data-select-trigger].select.medium) {
padding: $unit ($unit * 2.5);
font-size: $font-regular;
height: calc($unit * 5.5);
}
:global([data-select-trigger].select.large) {
padding: $unit-2x $unit-3x;
font-size: $font-large;
min-height: calc($unit * 6.5);
}
// Variant modifiers
:global([data-select-trigger].select.contained) {
background-color: var(--select-contained-bg);
&:hover:not(.disabled) {
background-color: var(--select-contained-bg-hover);
}
}
// Other modifiers
:global([data-select-trigger].select.full) {
width: 100%;
}
:global([data-select-trigger].select.disabled) {
opacity: 0.5;
cursor: not-allowed;
}
:global([data-select-trigger].select[data-placeholder='true']) .text {
color: var(--text-tertiary);
}
// Inner elements of select trigger
:global([data-select-trigger].select) {
.text {
flex: 1;
text-align: left;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.image {
width: $unit-3x;
height: auto;
flex-shrink: 0;
}
:global(.chevron) {
flex-shrink: 0;
color: var(--text-secondary);
margin-left: auto;
}
}
// Dropdown content styling
:global([data-select-content].content) {
background: var(--dialog-bg);
border-radius: $card-corner;
border: 1px solid rgba(0, 0, 0, 0.1);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
padding: $unit-half;
min-width: var(--bits-select-anchor-width);
max-height: 40vh;
overflow: auto;
z-index: 50;
animation: fadeIn $duration-opacity-fade ease-out;
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-4px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
}
// Dropdown items
:global([data-select-item].item) {
align-items: center;
border-radius: $item-corner-small;
color: var(--text-primary);
cursor: pointer;
display: flex;
gap: $unit;
padding: $unit $unit-2x;
user-select: none;
@include smooth-transition($duration-quick, background-color);
&:hover {
background-color: var(--option-bg-hover);
}
&[data-disabled] {
color: var(--text-tertiary);
cursor: not-allowed;
opacity: 0.5;
}
&[data-highlighted] {
background-color: var(--option-bg-hover);
}
&[data-selected] {
font-weight: $medium;
}
.text {
flex: 1;
}
.image {
width: $unit-3x;
height: auto;
flex-shrink: 0;
}
:global(.indicator) {
margin-left: auto;
color: var(--accent-blue);
}
}
</style>

View file

@ -1,383 +0,0 @@
<script lang="ts" generics="T extends string | number">
import { Select } from 'bits-ui';
interface Option {
value: T;
label: string;
disabled?: boolean;
image?: string;
}
interface Props {
options: Option[];
value?: T;
onValueChange?: (value: T | undefined) => void;
placeholder?: string;
disabled?: boolean;
variant?: 'default' | 'bound' | 'small' | 'table';
position?: 'left' | 'right';
fullWidth?: boolean;
hidden?: boolean;
name?: string;
required?: boolean;
class?: string;
}
const {
options = [],
value = $bindable(),
onValueChange,
placeholder = 'Select an option',
disabled = false,
variant = 'default',
position = 'left',
fullWidth = false,
hidden = false,
name,
required = false,
class: className = ''
}: Props = $props();
const selected = $derived(options.find(opt => opt.value === value));
const triggerClasses = [
'trigger',
variant === 'bound' && 'bound',
variant === 'small' && 'small',
variant === 'table' && 'table',
position === 'right' && 'right',
position === 'left' && 'left',
fullWidth && 'full',
hidden && 'hidden',
disabled && 'disabled',
className
].filter(Boolean).join(' ');
const contentClasses = [
'select',
variant === 'bound' && 'bound'
].filter(Boolean).join(' ');
function handleValueChange(newValue: string | undefined) {
if (newValue !== undefined && onValueChange) {
onValueChange(newValue as T);
}
}
</script>
<Select.Root
bind:value
onValueChange={handleValueChange}
{disabled}
{name}
{required}
>
<Select.Trigger
class={triggerClasses}
data-placeholder={!selected}
>
{#if selected?.image}
<img src={selected.image} alt={selected.label} />
{/if}
<span>{selected?.label || placeholder}</span>
<span class="icon">
<span class="chevron"></span>
</span>
</Select.Trigger>
<Select.Content class={contentClasses}>
<div class="scroll up">
<span class="chevron"></span>
</div>
<Select.Viewport>
{#each options as option}
<Select.Item
value={option.value}
disabled={option.disabled}
class="item"
>
{#if option.image}
<img src={option.image} alt={option.label} />
{/if}
<span>{option.label}</span>
<Select.ItemIndicator class="indicator">
<span></span>
</Select.ItemIndicator>
</Select.Item>
{/each}
</Select.Viewport>
<div class="scroll down">
<span class="chevron"></span>
</div>
</Select.Content>
</Select.Root>
<style lang="scss">
@use 'themes/spacing' as *;
@use 'themes/colors' as *;
@use 'themes/typography' as *;
@use 'themes/layout' as *;
@use 'themes/effects' as *;
@use 'themes/mixins' as *;
.trigger {
align-items: center;
background-color: var(--input-bg);
border-radius: $input-corner;
border: 2px solid transparent;
display: flex;
gap: $unit;
padding: calc($unit * 1.5) $unit-2x;
white-space: nowrap;
cursor: pointer;
@include smooth-transition($duration-zoom, background-color);
&.small {
& > span:not(.icon) {
font-size: $font-small;
margin: 0;
max-width: 200px;
}
@include breakpoint(tablet) {
&::before {
content: '';
display: block;
width: calc($unit-2x - 1px);
}
& > span:not(.icon) {
width: 100%;
max-width: inherit;
text-align: center;
}
}
}
&.grow {
flex-grow: 1;
}
&.left {
flex-grow: 1;
width: 100%;
}
&.right {
flex-grow: 0;
text-align: right;
min-width: 12rem;
}
&.bound {
background-color: var(--select-contained-bg);
&:hover {
background-color: var(--select-contained-bg-hover);
&.disabled {
background-color: var(--select-contained-bg);
}
}
}
&.full {
width: 100%;
}
&.table {
min-width: calc($unit * 30);
@include breakpoint(phone) {
width: 100%;
}
}
&.hidden {
display: none;
}
&:hover {
background-color: var(--input-bg-hover);
span:not(.icon),
&[data-placeholder] > span:not(.icon) {
color: var(--text-primary);
}
.icon svg {
fill: var(--text-primary);
}
}
&.disabled:hover {
background-color: var(--input-bg);
cursor: not-allowed;
}
&[data-placeholder='true'] > span:not(.icon) {
color: var(--text-secondary);
}
& > span:not(.icon) {
color: var(--text-primary);
flex-grow: 1;
font-size: $font-regular;
text-align: left;
}
img {
width: $unit-4x;
height: auto;
}
.icon {
display: flex;
align-items: center;
svg {
fill: var(--icon-secondary);
}
.chevron {
font-size: 10px;
color: var(--icon-secondary);
}
}
}
.select {
animation: scaleIn $duration-zoom ease-out;
background: var(--dialog-bg);
border-radius: $card-corner;
border: 1px solid rgba(0, 0, 0, 0.24);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.16);
padding: 0 $unit;
min-width: var(--radix-select-trigger-width);
transform-origin: var(--radix-select-content-transform-origin);
max-height: 40vh;
z-index: 40;
&.bound {
background-color: var(--select-content-contained-bg);
}
.scroll.up,
.scroll.down {
padding: $unit 0;
text-align: center;
&:hover svg, &:hover .chevron {
fill: var(--icon-secondary-hover);
color: var(--icon-secondary-hover);
}
svg {
fill: var(--icon-secondary);
}
.chevron {
font-size: 10px;
color: var(--icon-secondary);
}
}
.scroll.up {
transform: scale(1, -1);
}
@keyframes scaleIn {
0% {
opacity: 0;
transform: scale(0);
}
20% {
opacity: 0.2;
transform: scale(0.4);
}
40% {
opacity: 0.4;
transform: scale(0.8);
}
60% {
opacity: 0.6;
transform: scale(1);
}
65% {
opacity: 0.65;
transform: scale(1.1);
}
70% {
opacity: 0.7;
transform: scale(1);
}
75% {
opacity: 0.75;
transform: scale(0.98);
}
80% {
opacity: 0.8;
transform: scale(1.02);
}
90% {
opacity: 0.9;
transform: scale(0.96);
}
100% {
opacity: 1;
transform: scale(1);
}
}
}
.item {
align-items: center;
border-radius: $item-corner-small;
color: var(--text-primary);
cursor: pointer;
display: flex;
gap: $unit;
padding: $unit $unit-2x;
position: relative;
user-select: none;
@include smooth-transition($duration-opacity-fade, background-color);
&:hover {
background-color: var(--option-bg-hover);
}
&[data-disabled] {
color: var(--text-tertiary);
cursor: not-allowed;
opacity: 0.5;
}
&[data-selected] {
background-color: var(--option-bg-hover);
font-weight: $medium;
}
img {
width: $unit-3x;
height: auto;
}
span {
flex-grow: 1;
}
.indicator {
display: flex;
align-items: center;
justify-content: center;
margin-left: auto;
svg, span {
fill: var(--accent-blue);
color: var(--accent-blue);
}
}
}
</style>