2025-01-19 05:24:56 +02:00
|
|
|
<script lang="ts" module>
|
2025-01-22 14:25:24 +02:00
|
|
|
import type { WithElementRef } from 'bits-ui';
|
|
|
|
import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';
|
|
|
|
import { type VariantProps, tv } from 'tailwind-variants';
|
2025-01-19 05:24:56 +02:00
|
|
|
|
|
|
|
export const buttonVariants = tv({
|
2025-01-31 19:37:17 +02:00
|
|
|
base: 'focus-visible:ring-ring inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-hidden focus-visible:ring-1 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
|
2025-01-19 05:24:56 +02:00
|
|
|
variants: {
|
|
|
|
variant: {
|
2025-01-31 19:37:17 +02:00
|
|
|
default: 'bg-primary text-primary-foreground hover:bg-primary/90 shadow-sm',
|
|
|
|
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90 shadow-xs',
|
2025-01-19 05:24:56 +02:00
|
|
|
outline:
|
2025-01-31 19:37:17 +02:00
|
|
|
'border-input bg-background hover:bg-accent hover:text-accent-foreground border shadow-xs',
|
|
|
|
secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80 shadow-xs',
|
2025-01-22 14:25:24 +02:00
|
|
|
ghost: 'hover:bg-accent hover:text-accent-foreground',
|
|
|
|
link: 'text-primary underline-offset-4 hover:underline'
|
2025-01-19 05:24:56 +02:00
|
|
|
},
|
|
|
|
size: {
|
2025-01-22 14:25:24 +02:00
|
|
|
default: 'h-9 px-4 py-2',
|
|
|
|
sm: 'h-8 rounded-md px-3 text-xs',
|
|
|
|
lg: 'h-10 rounded-md px-8',
|
|
|
|
icon: 'h-9 w-9'
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
},
|
|
|
|
defaultVariants: {
|
2025-01-22 14:25:24 +02:00
|
|
|
variant: 'default',
|
|
|
|
size: 'default'
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
});
|
|
|
|
|
2025-01-22 14:25:24 +02:00
|
|
|
export type ButtonVariant = VariantProps<typeof buttonVariants>['variant'];
|
|
|
|
export type ButtonSize = VariantProps<typeof buttonVariants>['size'];
|
2025-01-19 05:24:56 +02:00
|
|
|
|
|
|
|
export type ButtonProps = WithElementRef<HTMLButtonAttributes> &
|
|
|
|
WithElementRef<HTMLAnchorAttributes> & {
|
|
|
|
variant?: ButtonVariant;
|
|
|
|
size?: ButtonSize;
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2025-01-22 14:25:24 +02:00
|
|
|
import { cn } from '$lib/utils.js';
|
2025-01-19 05:24:56 +02:00
|
|
|
|
|
|
|
let {
|
|
|
|
class: className,
|
2025-01-22 14:25:24 +02:00
|
|
|
variant = 'default',
|
|
|
|
size = 'default',
|
2025-01-19 05:24:56 +02:00
|
|
|
ref = $bindable(null),
|
|
|
|
href = undefined,
|
2025-01-22 14:25:24 +02:00
|
|
|
type = 'button',
|
2025-01-19 05:24:56 +02:00
|
|
|
children,
|
|
|
|
...restProps
|
|
|
|
}: ButtonProps = $props();
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if href}
|
2025-01-22 14:25:24 +02:00
|
|
|
<a bind:this={ref} class={cn(buttonVariants({ variant, size }), className)} {href} {...restProps}>
|
2025-01-19 05:24:56 +02:00
|
|
|
{@render children?.()}
|
|
|
|
</a>
|
|
|
|
{:else}
|
|
|
|
<button
|
|
|
|
bind:this={ref}
|
|
|
|
class={cn(buttonVariants({ variant, size }), className)}
|
|
|
|
{type}
|
|
|
|
{...restProps}
|
|
|
|
>
|
|
|
|
{@render children?.()}
|
|
|
|
</button>
|
|
|
|
{/if}
|