write a svelte 5 button component
- User
write a svelte 5 button component
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write a svelte 5 button component
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Svelte 5 button component with variants, sizes, loading states, and icon support using Tailwind CSS:
<!-- Button.svelte -->
<script>
let {
variant = 'primary',
size = 'md',
loading = false,
disabled = false,
icon,
iconPosition = 'left',
fullWidth = false,
type = 'button',
...rest
} = $props();
// Variant classes
const variants = {
primary: 'bg-indigo-600 hover:bg-indigo-700 text-white shadow-sm focus:ring-indigo-600',
secondary: 'bg-gray-200 hover:bg-gray-300 text-gray-900 shadow-sm focus:ring-gray-500',
outline: 'border border-gray-300 hover:bg-gray-50 text-gray-700 focus:ring-indigo-600',
ghost: 'hover:bg-gray-100 text-gray-700 focus:ring-indigo-600',
destructive: 'bg-red-600 hover:bg-red-700 text-white shadow-sm focus:ring-red-600'
};
// Size classes
const sizes = {
sm: 'px-2.5 py-1.5 text-xs',
md: 'px-4 py-2 text-sm',
lg: 'px-5 py-3 text-base'
};
// Combined classes
$derived(
baseClasses = [
'inline-flex items-center justify-center font-medium rounded-md',
'focus:outline-none focus:ring-2 focus:ring-offset-2',
'transition-colors duration-150',
variants[variant],
sizes[size],
fullWidth ? 'w-full' : '',
disabled || loading ? 'opacity-70 cursor-not-allowed' : 'cursor-pointer'
].join(' ')
);
</script>
<button
{type}
{disabled: disabled || loading}
class={baseClasses}
{...rest}
>
{#if loading}
<!-- Loading spinner -->
<svg
class="animate-spin -ml-1 mr-2 h-4 w-4"
class:mr-2={!icon || iconPosition === 'left'}
class:ml-2={iconPosition === 'right'}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
class="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
stroke-width="4"
/>
<path
class="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
{/if}
{#if icon && !loading && iconPosition === 'left'}
<span class="mr-2">{@html icon}</span>
{/if}
<slot />
{#if icon && !loading && iconPosition === 'right'}
<span class="ml-2">{@html icon}</span>
{/if}
</button>
primary, secondary, outline, ghost, destructivesm, md, lg<script>
import Button from './Button.svelte';
// Example icon (using Heroicons)
const plusIcon = `<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd"/></svg>`;
</script>
<Button>Primary</Button>
<Button variant="secondary" size="lg">Large Secondary</Button>
<Button variant="outline" icon={plusIcon}>With Icon</Button>
<Button variant="destructive" loading={true}>Deleting...</Button>
<Button fullWidth icon={plusIcon} iconPosition="right">Full Width</Button>
| Prop | Type | Default | Description |
|---|---|---|---|
variant | string | 'primary' | Button style variant |
size | string | 'md' | Button size |
loading | boolean | false | Show loading spinner |
disabled | boolean | false | Disable button |
icon | string | undefined | SVG icon string |
iconPosition | 'left'/'right' | 'left' | Icon position |
fullWidth | boolean | false | Full-width button |
type | string | 'button' | Button type attribute |
...restFor TypeScript users, add this interface:
interface $$Props {
variant?: 'primary' | 'secondary' | 'outline' | 'ghost' | 'destructive';
size?: 'sm' | 'md' | 'lg';
loading?: boolean;
disabled?: boolean;
icon?: string;
iconPosition?: 'left' | 'right';
fullWidth?: boolean;
type?: 'button' | 'submit' | 'reset';
}
This component provides a flexible, accessible button with modern features while maintaining a clean API.