Svelte Material Ripple is a library for adding ripple effects to interactive elements.
npm install -D svelte-material-ripple
Import the Ripple
component and place it in a position: relative
container.
<script>
import { Ripple } from "svelte-material-ripple";
</script>
<button class="relative">
<Ripple />
</button>
<style>
.relative {
position: relative;
}
</style>
By default, the ripple is attached to the parent element, but some elements like <input>
don't accept children. You can attach the ripple to a reference element by passing the id of that element to the for
prop.
<div class="parent">
<input id="checkbox" type="checkbox" />
<Ripple for="checkbox" class="ripple" />
</div>
You can also just pass the element directly to the for
prop.
<script lang="ts">
let input = $state<HTMLInputElement>();
</script>
<div class="parent">
<input bind:this={input} type="checkbox" />
<Ripple for={input} />
</div>
<div class="parent">
<input id="checkbox" type="checkbox" />
<Ripple for="checkbox" class="ripple" />
</div>
<style>
.parent {
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
.ripple {
inset: unset;
height: 32px;
width: 32px;
border-radius: 9999px;
}
</style>
Ripples can be customized globally using CSS variables.
Token | Description | Default |
---|---|---|
--ripple-hover-color | The color of the ripple when hovered | currentColor |
--ripple-hover-opacity | The opacity of the ripple when hovered | 0.08 |
--ripple-pressed-color | The color of the ripple when pressed | currentColor |
--ripple-pressed-opacity | The opacity of the ripple when pressed | 0.12 |
You can also pass a theme
object to the component, which will override the global styles.
<Ripple
theme={{
hover: {
color: "orange",
opacity: 0.15,
},
pressed: {
color: "red",
opacity: 0.3,
},
}}
/>
<Ripple />