Button Library · Hover
Ripple
A ring spreads from where you press.
States
Default
Hover
Active
Focus
Code
hover-ripple.html
<button class="btn-hover-ripple" type="button"> <span>Get started</span> </button>
hover-ripple.css
.btn-hover-ripple {
--btn-bg: #7c5cff;
--btn-fg: #ffffff;
--btn-font: inherit;
--btn-size: 15px;
--btn-weight: 600;
--btn-px: 22px;
--btn-py: 11px;
--btn-w: auto;
--btn-h: auto;
--btn-bw: 0px;
--btn-line: #8b6cff;
--btn-radius: 10px;
--btn-shadow: none;
--btn-speed: 180ms;
}
.btn-hover-ripple {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5em;
box-sizing: border-box;
width: var(--btn-w);
height: var(--btn-h);
padding: var(--btn-py) var(--btn-px);
font-family: var(--btn-font);
font-size: var(--btn-size);
font-weight: var(--btn-weight);
line-height: 1.2;
color: var(--btn-fg);
background: var(--btn-bg);
border: var(--btn-bw) solid var(--btn-line);
border-radius: var(--btn-radius);
box-shadow: var(--btn-shadow);
cursor: pointer;
text-decoration: none;
white-space: nowrap;
transition: all var(--btn-speed) ease;
overflow: hidden;
}
.btn-hover-ripple:focus-visible {
outline: 2px solid var(--btn-bg);
outline-offset: 2px;
}
.btn-hover-ripple:disabled {
opacity: 0.5;
pointer-events: none;
}
.btn-hover-ripple__icon {
width: 1.15em;
height: 1.15em;
flex-shrink: 0;
}
@media (prefers-reduced-motion: reduce) {
.btn-hover-ripple {
animation: none !important;
transition: none !important;
}
}
hover-ripple.js
document.querySelectorAll(".btn-hover-ripple").forEach((el) => {
const down = (e) => {
if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const box = el.getBoundingClientRect();
const size = Math.max(box.width, box.height) * 2;
const dot = document.createElement("span");
dot.style.cssText = `position:absolute;left:${e.clientX - box.left - size / 2}px;top:${e.clientY - box.top - size / 2}px;width:${size}px;height:${size}px;border-radius:50%;background:currentColor;pointer-events:none`;
el.append(dot);
dot.animate([{ transform: "scale(0)", opacity: 0.35 }, { transform: "scale(1)", opacity: 0 }], { duration: 600, easing: "ease-out" }).onfinish = () => dot.remove();
};
el.addEventListener("pointerdown", down);
return () => el.removeEventListener("pointerdown", down);
});
hover-ripple.tsx
import { useEffect, useRef } from "react";
import type React from "react";
import "./hover-ripple.css"; // the CSS tab
function attach(el: HTMLElement) {
const down = (e: PointerEvent) => {
if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const box = el.getBoundingClientRect();
const size = Math.max(box.width, box.height) * 2;
const dot = document.createElement("span");
dot.style.cssText = `position:absolute;left:${e.clientX - box.left - size / 2}px;top:${e.clientY - box.top - size / 2}px;width:${size}px;height:${size}px;border-radius:50%;background:currentColor;pointer-events:none`;
el.append(dot);
dot.animate([{ transform: "scale(0)", opacity: 0.35 }, { transform: "scale(1)", opacity: 0 }], { duration: 600, easing: "ease-out" }).onfinish = () => dot.remove();
};
el.addEventListener("pointerdown", down);
return () => el.removeEventListener("pointerdown", down);
}
export function BtnHoverRipple({ children = "Get started", ...props }: React.ComponentProps<"button">) {
const ref = useRef<HTMLButtonElement>(null);
useEffect(() => attach(ref.current!), []);
return (
<button className="btn-hover-ripple" type="button" ref={ref} {...props}>
<span>{children}</span>
</button>
);
}
hover-ripple.html
<button class="btn-hover-ripple relative inline-flex items-center justify-center gap-[0.5em] [box-sizing:border-box] [padding:11px_22px] [font-family:inherit] text-[15px] font-[600] [line-height:1.2] text-[#ffffff] bg-[#7c5cff] [border:0px_solid_#8b6cff] rounded-[10px] [box-shadow:none] cursor-pointer no-underline whitespace-nowrap [transition:all_180ms_ease] overflow-hidden focus-visible:[outline:2px_solid_#7c5cff] focus-visible:[outline-offset:2px] disabled:[opacity:0.5] disabled:[pointer-events:none] motion-reduce:[transition:none!important]" type="button"> <span>Get started</span> </button> <!-- Needs the script from the JS tab. -->