feat: 为Select组件实现DaisyUI样式

This commit is contained in:
2025-12-23 14:39:03 +08:00
parent be210a3e36
commit 00bd397986
3 changed files with 52 additions and 11 deletions

View File

@ -0,0 +1,21 @@
export const COLOR_MAP = {
neutral: 'select-neutral',
primary: 'select-primary',
secondary: 'select-secondary',
accent: 'select-accent',
info: 'select-info',
success: 'select-success',
warning: 'select-warning',
error: 'select-error',
} as const;
export const SIZE_MAP = {
xs: 'select-xs',
sm: 'select-sm',
md: 'select-md',
lg: 'select-lg',
xl: 'select-xl',
} as const;
export type Color = keyof typeof COLOR_MAP;
export type Size = keyof typeof SIZE_MAP;

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { Select as BitsSelect, type WithoutChildren } from 'bits-ui'; import { Select as BitsSelect, type WithoutChildren } from 'bits-ui';
import { elasticOut, cubicOut } from 'svelte/easing'; import { cubicOut } from 'svelte/easing';
type Props = WithoutChildren<BitsSelect.ContentProps> & { type Props = WithoutChildren<BitsSelect.ContentProps> & {
items: { value: string; label: string; disabled?: boolean }[]; items: { value: string; label: string; disabled?: boolean }[];
@ -21,7 +21,7 @@
) { ) {
const { const {
delay = 0, delay = 0,
duration = 1000, duration = 200,
easing = cubicOut, easing = cubicOut,
y = -6, y = -6,
start = 0.95, start = 0.95,
@ -59,11 +59,7 @@ transform: ${existingTransform} ${translate} ${scale};
{#snippet child({ wrapperProps, props, open })} {#snippet child({ wrapperProps, props, open })}
{#if open} {#if open}
<div {...wrapperProps}> <div {...wrapperProps}>
<div <div {...props} transition:selectTransition>
{...props}
in:selectTransition={{ easing: elasticOut }}
out:selectTransition={{ duration: 200 }}
>
<BitsSelect.ScrollUpButton>up</BitsSelect.ScrollUpButton> <BitsSelect.ScrollUpButton>up</BitsSelect.ScrollUpButton>
<BitsSelect.Viewport class="p-1"> <BitsSelect.Viewport class="p-1">
{#each items as { value, label, disabled } (value)} {#each items as { value, label, disabled } (value)}

View File

@ -2,16 +2,40 @@
import { Select as BitsSelect, type WithoutChildren } from 'bits-ui'; import { Select as BitsSelect, type WithoutChildren } from 'bits-ui';
import type { Snippet } from 'svelte'; import type { Snippet } from 'svelte';
import { COLOR_MAP, SIZE_MAP } from './Select.variants';
import type { Color, Size } from './Select.variants';
type Props = WithoutChildren<BitsSelect.TriggerProps> & { type Props = WithoutChildren<BitsSelect.TriggerProps> & {
color?: Color;
size?: Size;
ghost?: boolean;
class?: string; class?: string;
children?: Snippet; children?: Snippet;
}; };
let { children, class: className = '' }: Props = $props(); let {
color,
size,
ghost,
children,
class: className = '',
...restProps
}: Props = $props();
const classes = $derived(
[
'select',
color && COLOR_MAP[color],
size && SIZE_MAP[size],
ghost && 'select-ghost',
className,
]
.filter(Boolean)
.join(' ')
);
</script> </script>
<BitsSelect.Trigger <BitsSelect.Trigger class={classes.trim()} {...restProps}>
class={`select outline-none focus:border-base-content/20 focus-visible:border-primary focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 ${className}`}
>
{@render children?.()} {@render children?.()}
</BitsSelect.Trigger> </BitsSelect.Trigger>