portfolio_site/src/lib/components/ui/button/button.svelte

70 lines
2.1 KiB
Svelte
Raw Normal View History

<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';
export const buttonVariants = tv({
2025-01-22 14:25:24 +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-none focus-visible:ring-1 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
variants: {
variant: {
2025-01-22 14:25:24 +02:00
default: 'bg-primary text-primary-foreground hover:bg-primary/90 shadow',
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90 shadow-sm',
outline:
2025-01-22 14:25:24 +02:00
'border-input bg-background hover:bg-accent hover:text-accent-foreground border shadow-sm',
secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80 shadow-sm',
ghost: 'hover:bg-accent hover:text-accent-foreground',
link: 'text-primary underline-offset-4 hover:underline'
},
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'
}
},
defaultVariants: {
2025-01-22 14:25:24 +02:00
variant: 'default',
size: 'default'
}
});
2025-01-22 14:25:24 +02:00
export type ButtonVariant = VariantProps<typeof buttonVariants>['variant'];
export type ButtonSize = VariantProps<typeof buttonVariants>['size'];
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';
let {
class: className,
2025-01-22 14:25:24 +02:00
variant = 'default',
size = 'default',
ref = $bindable(null),
href = undefined,
2025-01-22 14:25:24 +02:00
type = 'button',
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}>
{@render children?.()}
</a>
{:else}
<button
bind:this={ref}
class={cn(buttonVariants({ variant, size }), className)}
{type}
{...restProps}
>
{@render children?.()}
</button>
{/if}