I like to use CSS custom properties for this.
<!-- Button.svelte -->
<script>
export let backgroundColor;
</script>
<style>
button {
background-color: var(--backgroundColor);
}
button:hover {
filter: brightness(95%);
}
</style>
<button style="--backgroundColor: {backgroundColor}">
<slot></slot>
</button>
This component can be used like so.
<script>
import Button from './Button.svelte';
</script>
<Button backgroundColor="lightgreen">
Button
</Button>
<Button backgroundColor="#ffaaff">
Button
</Button>
<Button backgroundColor="hsla(170, 45%, 45%, 1)">
Button
</Button>
A proposed Svelte feature will make this easier.
I apply a CSS filter on hover so the hover color is slightly darker than the background color. See this Stack Overflow answer for some other pure CSS options.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…