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 button component that is compatible with Svelte 3 or 4 (since Svelte 5 has not yet been released). This component includes:
primary, secondary)Button.svelte<script>
import { createEventDispatcher } from 'svelte';
// Define props
export let variant = 'primary'; // Default variant
export let disabled = false; // Default disabled state
export let text = 'Click me'; // Button text
// Event dispatcher for click events
const dispatch = createEventDispatcher();
// Handle click event
function handleClick() {
if (!disabled) {
dispatch('click', { message: 'Button clicked' });
}
}
</script>
<!-- Button element with dynamic classes and disabled state -->
<button
class={`btn ${variant}`}
disabled={disabled}
on:click={handleClick}
>
{text}
</button>
<!-- Scoped styles for the button -->
<style>
.btn {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
font-size: 16px;
}
.btn.primary {
background-color: #007bff;
color: white;
}
.btn.secondary {
background-color: #6c757d;
color: white;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn:hover {
opacity: 0.9;
}
.btn:active {
transform: scale(0.98);
}
</style>
You can use the component like this in another Svelte file:
<script>
function handleButtonClick(event) {
console.log('Button clicked:', event.detail.message);
}
</script>
<Button
variant="primary"
text="Submit"
on:click={handleButtonClick}
/>
loading, icon, or href for more flexibility.Let me know if you'd like to add more advanced features like loading states or custom icons!