Add to Cart
Add-to-cart button that confirms with a tick while a copy of the product picture flies into the cart icon and its count rolls up by one. Rolls back if the request fails.
View codeLike button whose heart fills with ink, a small wave riding the surface, and drains when the like is taken back. The count rolls digit by digit with carries (999 → 1,000) and rolls back if the request fails.
With your AI
Say it to your AI. It gets the exact code below, not its best guess, and knows where it goes.
Add the Kodza Ink Like to my page.
claude mcp add -t http kodza https://kodza.dev/api/mcp
<button class="kodza-ink-like" type="button" aria-pressed="false" aria-label="Like, 999 likes" data-count="999">
<svg class="kodza-ink-like__icon" viewBox="0 0 24 24" aria-hidden="true">
<g class="kodza-ink-like__ink">
<g class="kodza-ink-like__level">
<g class="kodza-ink-like__surface">
<path class="kodza-ink-like__wave" d="M-48 2q6-2.4 12 0t12 0t12 0t12 0t12 0t12 0t12 0t12 0V40H-48z" />
</g>
</g>
</g>
<path class="kodza-ink-like__shape" d="M12 20.5C10.5 19.3 2.5 13.8 2.5 8.6 2.5 5.8 4.6 3.8 7.2 3.8 9.2 3.8 10.9 5 12 6.7 13.1 5 14.8 3.8 16.8 3.8 19.4 3.8 21.5 5.8 21.5 8.6 21.5 13.8 13.5 19.3 12 20.5Z" />
</svg>
<span class="kodza-ink-like__count" aria-hidden="true">999</span>
</button>
<button class="kodza-ink-like kodza-ink-like--bare" type="button" aria-pressed="true" aria-label="Like, 128 likes" data-count="128">
<svg class="kodza-ink-like__icon" viewBox="0 0 24 24" aria-hidden="true">
<g class="kodza-ink-like__ink">
<g class="kodza-ink-like__level">
<g class="kodza-ink-like__surface">
<path class="kodza-ink-like__wave" d="M-48 2q6-2.4 12 0t12 0t12 0t12 0t12 0t12 0t12 0t12 0V40H-48z" />
</g>
</g>
</g>
<path class="kodza-ink-like__shape" d="M12 20.5C10.5 19.3 2.5 13.8 2.5 8.6 2.5 5.8 4.6 3.8 7.2 3.8 9.2 3.8 10.9 5 12 6.7 13.1 5 14.8 3.8 16.8 3.8 19.4 3.8 21.5 5.8 21.5 8.6 21.5 13.8 13.5 19.3 12 20.5Z" />
</svg>
<span class="kodza-ink-like__count" aria-hidden="true">128</span>
</button>
/* Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license */
/* Kodza — Ink Like
A like button whose heart fills with ink. Liked, the ink rises with a small wave riding its
surface; taken back, it drains through the tip. The count rolls digit by digit, carries
included (999 → 1,000). The state lives in aria-pressed and the total in data-count: set them
from your code and the button follows. Under the pointer, a drop of ink waits in the tip.
From your code: listen to "kodza:like" and pass your request to event.detail.wait(promise);
if it fails, the like rolls back, and if it resolves to a number, the count rolls to it.
Everything is tuned with the --kz-ink-like-* variables. */
.kodza-ink-like {
--kz-ink-like-fg: #0a0a0a;
--kz-ink-like-bg: rgba(10, 10, 10, 0.06);
--kz-ink-like-bg-hover: rgba(10, 10, 10, 0.1);
--kz-ink-like-height: 52px;
--kz-ink-like-pad-x: 20px;
--kz-ink-like-icon: 22px;
--kz-ink-like-ease: cubic-bezier(0.22, 1, 0.36, 1);
position: relative;
display: inline-flex;
align-items: center;
gap: 10px;
box-sizing: border-box;
height: var(--kz-ink-like-height);
margin: 0;
padding: 0 calc(var(--kz-ink-like-pad-x) + 2px) 0 var(--kz-ink-like-pad-x);
border: 0;
border-radius: 999px;
background: var(--kz-ink-like-bg);
color: var(--kz-ink-like-fg);
font: inherit;
font-weight: 500;
line-height: 1;
letter-spacing: -0.01em;
white-space: nowrap;
cursor: pointer;
user-select: none;
-webkit-tap-highlight-color: transparent;
transition: background-color 0.2s ease;
}
.kodza-ink-like:focus-visible {
outline: 2px solid var(--kz-ink-like-fg);
outline-offset: 3px;
}
/* Bare: the heart and its count alone, for a row of actions under a post */
.kodza-ink-like--bare {
--kz-ink-like-bg: transparent;
--kz-ink-like-bg-hover: transparent;
--kz-ink-like-pad-x: 8px;
}
/* The heart: its outline, and the ink cut to the same shape by the script */
.kodza-ink-like__icon {
display: block;
flex: none;
width: var(--kz-ink-like-icon);
height: var(--kz-ink-like-icon);
overflow: hidden;
transition: transform 0.25s var(--kz-ink-like-ease);
}
.kodza-ink-like:active .kodza-ink-like__icon {
transform: scale(0.86);
}
.kodza-ink-like__shape {
fill: none;
stroke: currentColor;
stroke-width: 1.6;
stroke-linejoin: round;
}
.kodza-ink-like:not(.is-ready) .kodza-ink-like__ink {
visibility: hidden;
}
/* The level: just under the tip when empty, just over the top when liked, so the whole rise
shows. It rises fast and settles in the lobes, and drains faster, gathering speed as it goes */
.kodza-ink-like__level,
.kodza-ink-like__surface,
.kodza-ink-like__wave {
transform-box: view-box;
}
.kodza-ink-like__level {
transform: translateY(19px);
transition: transform 420ms cubic-bezier(0.6, 0, 0.8, 0.4);
}
.kodza-ink-like[aria-pressed="true"] .kodza-ink-like__level {
transform: translateY(1px);
transition-duration: 760ms;
transition-timing-function: cubic-bezier(0.3, 0.7, 0.3, 1);
}
/* The surface: calm at rest, stirred by the script while the ink moves */
.kodza-ink-like__surface {
transform: scaleY(0.7);
transform-origin: 12px 2px;
}
.kodza-ink-like__wave {
fill: currentColor;
animation: kodza-ink-like-wave 1.1s linear infinite paused;
}
.kodza-ink-like.is-flowing .kodza-ink-like__wave {
animation-play-state: running;
}
@keyframes kodza-ink-like-wave {
to {
transform: translateX(24px);
}
}
@media (hover: hover) {
.kodza-ink-like:hover {
background: var(--kz-ink-like-bg-hover);
}
/* A drop of ink waits in the tip, until the click */
.kodza-ink-like:not(.is-spent):not([aria-pressed="true"]):hover .kodza-ink-like__level {
transform: translateY(16px);
transition-duration: 380ms;
transition-timing-function: var(--kz-ink-like-ease);
}
.kodza-ink-like:not(.is-spent):hover .kodza-ink-like__wave {
animation-play-state: running;
}
}
/* The count: one cell per character, the old digit leaving as the new one comes in */
.kodza-ink-like__count {
display: inline-flex;
height: 1.25em;
line-height: 1.25em;
font-variant-numeric: tabular-nums;
-webkit-mask-image: linear-gradient(transparent, #000 22%, #000 78%, transparent);
mask-image: linear-gradient(transparent, #000 22%, #000 78%, transparent);
}
.kodza-ink-like__digit {
position: relative;
display: block;
height: 1.25em;
overflow: hidden;
}
.kodza-ink-like__digit > span {
display: block;
}
.kodza-ink-like__digit > span + span {
position: absolute;
top: 0;
left: 0;
}
/* What a screen reader hears after a click */
.kodza-ink-like__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}
@media (prefers-reduced-motion: reduce) {
.kodza-ink-like,
.kodza-ink-like__icon,
.kodza-ink-like__level {
transition: none;
}
.kodza-ink-like__wave {
animation: none;
}
}
/* kodza-guard de1c4773ddac — generated by Kodza, do not edit.
Theme guard: keeps a site's own CSS (WordPress themes, Bootstrap, CSS resets…) off this
component. :not(#_) is always true and weighs as much as an id, so these lines win over
theme rules made of classes and tags. Customise the component through its --kz-* variables. */
/* What the browser gives, where the component sets it only in some states: at the lowest weight, so its own rules below put it back */
:where(.kodza-ink-like):not(#_) {
outline: revert;
outline-offset: revert;
}
/* What the browser gives, on everything the component leaves to it */
.kodza-ink-like:not(#_) {
text-decoration: revert;
list-style: revert;
box-shadow: revert;
appearance: revert;
max-height: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
min-height: revert;
}
.kodza-ink-like__icon:not(#_) {
border: revert;
margin: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
color: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-ink-like__ink:not(#_),
.kodza-ink-like__level:not(#_),
.kodza-ink-like__surface:not(#_),
.kodza-ink-like__wave:not(#_),
.kodza-ink-like__shape:not(#_),
.kodza-ink-like__count:not(#_),
.kodza-ink-like__digit:not(#_),
.kodza-ink-like__digit span:where(:not([class*="kodza-"])):not(#_) {
box-sizing: revert;
}
/* The component's own rules, one step stronger: real buttons and fields, what it sets on tags themes style (links, lists, headings, images…), and the root width (a theme may narrow it, never widen it) */
.kodza-ink-like:not(#_) {
position: relative;
display: inline-flex;
align-items: center;
gap: 10px;
box-sizing: border-box;
height: var(--kz-ink-like-height);
margin: 0;
padding: 0 calc(var(--kz-ink-like-pad-x) + 2px) 0 var(--kz-ink-like-pad-x);
border: 0;
border-radius: 999px;
background: var(--kz-ink-like-bg);
color: var(--kz-ink-like-fg);
font: inherit;
font-weight: 500;
line-height: 1;
letter-spacing: -0.01em;
white-space: nowrap;
cursor: pointer;
user-select: none;
-webkit-tap-highlight-color: transparent;
transition: background-color 0.2s ease;
}
.kodza-ink-like:focus-visible:not(#_) {
outline: 2px solid var(--kz-ink-like-fg);
outline-offset: 3px;
}
.kodza-ink-like__icon:not(#_) {
display: block;
transition: transform 0.25s var(--kz-ink-like-ease);
}
@media (hover: hover) {
.kodza-ink-like:hover:not(#_) {
background: var(--kz-ink-like-bg-hover);
}
}
@media (prefers-reduced-motion: reduce) {
.kodza-ink-like:not(#_) {
transition: none;
}
}
@media (prefers-reduced-motion: reduce) {
.kodza-ink-like__icon:not(#_) {
transition: none;
}
}
// Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license
// Kodza — Ink Like
// A like button whose heart fills with ink: it rises with a small wave riding its surface and
// drains through the tip when the like is taken back; a click halfway turns it round where it
// is. The count rolls digit by digit 380 ms later, carries included (999 → 1,000). The state is
// aria-pressed and the total data-count: change them from your code and the button follows.
// "kodza:like" bubbles from the button and can be cancelled: pass your request to
// event.detail.wait(promise); if it rejects the like rolls back, if it resolves to a number (or
// { count }) the count rolls to it. data-say-liked, data-say-unliked and data-say-refused set
// what a screen reader hears.
(() => {
const SELECTOR = '.kodza-ink-like';
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const ease = 'cubic-bezier(0.22, 1, 0.36, 1)';
const LAG_UP = 380;
const LAG_DOWN = 120;
const format = (n) => new Intl.NumberFormat(document.documentElement.lang || undefined).format(n);
let made = 0;
// One instance. Everything it starts is tied to `signal`, so kodza:destroy stops it all
function mount(root, signal) {
const svg = root.querySelector('.kodza-ink-like__icon');
const ink = root.querySelector('.kodza-ink-like__ink');
const surface = root.querySelector('.kodza-ink-like__surface');
const count = root.querySelector('.kodza-ink-like__count');
if (!svg || !ink) return;
// The ink is cut to the heart: a clip path made of the outline, with an id of its own
const ns = 'http://www.w3.org/2000/svg';
const defs = document.createElementNS(ns, 'defs');
const clip = document.createElementNS(ns, 'clipPath');
clip.id = `kodza-ink-like-${Math.random().toString(36).slice(2, 8)}-${++made}`;
svg.querySelectorAll('.kodza-ink-like__shape').forEach((shape) => {
const copy = shape.cloneNode(true);
copy.removeAttribute('class');
clip.append(copy);
});
defs.append(clip);
svg.prepend(defs);
ink.setAttribute('clip-path', `url(#${clip.id})`);
const countOf = () => Math.max(0, parseInt(root.dataset.count ?? count?.textContent.replace(/\D/g, ''), 10) || 0);
let liked = root.getAttribute('aria-pressed') === 'true';
let total = countOf();
let shown = total;
const label = (n) => {
const text = root.getAttribute('aria-label');
if (!text) return;
root.setAttribute('aria-label', text.replace(/\d(?:[\d., ]*\d)?(\s+likes?\b)?/, (m, word) => format(n) + (word ? word.replace(/likes?/, n === 1 ? 'like' : 'likes') : '')));
};
// The spoken status sits right after the button, and leaves with it
const status = Object.assign(document.createElement('span'), { className: 'kodza-ink-like__sr' });
status.setAttribute('role', 'status');
root.after(status);
const words = {
liked: root.dataset.sayLiked || 'Liked',
unliked: root.dataset.sayUnliked || 'Like removed',
refused: root.dataset.sayRefused || 'Not saved. Try again.',
};
let sayTimer = 0;
const say = (message) => {
status.textContent = '';
clearTimeout(sayTimer);
sayTimer = setTimeout(() => { status.textContent = message; }, 60);
};
// The count: one cell per character, aligned on the right. A digit that changes rolls: up and
// units first when the count grows, down and from the left when it shrinks (the same roll
// played backwards). A digit that appears or goes rolls, and opens or closes its room, along
// with the first digit that turns, so the count moves in one gesture. Taken back halfway, the
// roll plays backwards from where it is
const paint = (n) => {
if (!count) return;
count.replaceChildren(...[...format(n)].map((ch) => {
const cell = Object.assign(document.createElement('span'), { className: 'kodza-ink-like__digit' });
cell.append(Object.assign(document.createElement('span'), { textContent: ch }));
return cell;
}));
};
let run = null;
const settle = (current) => {
Promise.all(current.anims.map((a) => a.finished)).then(() => {
if (run !== current) return;
run = null;
paint(current.to);
}, () => {});
};
const draw = (from, to) => {
const a = format(from);
const b = format(to);
const n = Math.max(a.length, b.length);
const was = a.padStart(n);
const now = b.padStart(n);
const k = to > from ? 1 : -1;
const delay = (i) => (k > 0 ? n - 1 - i : i) * 55;
const anims = [];
const rooms = [];
const turns = [...now].map((ch, i) => (ch !== was[i] && ch.trim() && was[i].trim() ? delay(i) : Infinity));
const start = Math.min(...turns);
count.textContent = '';
for (let i = 0; i < n; i++) {
const cell = Object.assign(document.createElement('span'), { className: 'kodza-ink-like__digit' });
const next = Object.assign(document.createElement('span'), { textContent: now[i].trim() });
cell.append(next);
count.append(cell);
if (was[i] === now[i]) continue;
const old = Object.assign(document.createElement('span'), { textContent: was[i].trim() });
cell.append(old);
const timing = { duration: 480, delay: turns[i] === Infinity && start !== Infinity ? start : delay(i), easing: ease, fill: 'both' };
anims.push(
next.animate([{ transform: `translateY(${100 * k}%)` }, { transform: 'none' }], timing),
old.animate([{ transform: 'none' }, { transform: `translateY(${-100 * k}%)` }], timing),
);
// Widths from the layout, to the subpixel (a transform on a parent leaves them alone): the
// last frame is exactly the count painted after it
const wide = parseFloat(getComputedStyle(next).width);
const narrow = parseFloat(getComputedStyle(old).width);
if (wide !== narrow) rooms.push([cell, narrow, wide]);
}
const timing = { duration: 480, delay: start === Infinity ? 0 : start, easing: ease, fill: 'both' };
rooms.forEach(([cell, narrow, wide]) => {
anims.push(cell.animate([{ width: `${narrow}px` }, { width: `${wide}px` }], timing));
});
run = { from, to, anims };
settle(run);
};
const roll = (to) => {
if (to === shown || !count) return;
if (run && run.from === to) {
run.anims.forEach((a) => a.reverse());
run = { from: run.to, to, anims: run.anims };
settle(run);
} else {
run?.anims.forEach((a) => a.cancel());
run = null;
if (reduceMotion) paint(to);
else draw(shown, to);
}
shown = to;
};
let lag = 0;
const later = (ms) => {
clearTimeout(lag);
lag = setTimeout(() => {
lag = 0;
roll(total);
}, reduceMotion ? 0 : ms);
};
// The surface sloshes while the ink moves: it tilts against the flow, overshoots, settles
let slosh = null;
let flowTimer = 0;
const flow = (up) => {
if (reduceMotion || !surface) return;
root.classList.add('is-flowing');
clearTimeout(flowTimer);
flowTimer = setTimeout(() => root.classList.remove('is-flowing'), up ? 1000 : 520);
const from = getComputedStyle(surface).transform;
slosh?.cancel();
const s = up ? 1 : -1;
slosh = surface.animate([
{ transform: from === 'none' ? 'scaleY(0.7)' : from },
{ transform: `rotate(${-5 * s}deg) scaleY(1.6)`, offset: 0.3 },
{ transform: `rotate(${2 * s}deg) scaleY(1.1)`, offset: 0.65 },
{ transform: 'rotate(0deg) scaleY(0.7)' },
], { duration: up ? 900 : 480, easing: 'ease-out' });
};
// The heart says no: a short shake when the request is refused
const shake = () => {
if (reduceMotion) return;
svg.animate([
{ transform: 'none' },
{ transform: 'translateX(-2px)', offset: 0.2 },
{ transform: 'translateX(2px)', offset: 0.45 },
{ transform: 'translateX(-1px)', offset: 0.7 },
{ transform: 'none' },
], { duration: 360, easing: 'ease-out' });
};
// The level follows aria-pressed in CSS, so a click halfway turns it round where it is
const apply = (next, n) => {
const turned = next !== liked;
liked = next;
total = n;
root.setAttribute('aria-pressed', String(next));
root.dataset.count = String(n);
label(n);
if (turned) flow(next);
later(next ? LAG_UP : LAG_DOWN);
};
let seq = 0;
root.addEventListener('click', () => {
const before = { liked, total };
const next = !liked;
const n = Math.max(0, total + (next ? 1 : -1));
let request = null;
const allowed = root.dispatchEvent(new CustomEvent('kodza:like', {
bubbles: true,
cancelable: true,
detail: { liked: next, count: n, wait: (promise) => { request = Promise.resolve(promise); } },
}));
if (!allowed) return;
const mine = ++seq;
root.classList.add('is-spent');
apply(next, n);
say(next ? words.liked : words.unliked);
request?.then((value) => {
const real = typeof value === 'number' ? value : value?.count;
if (signal.aborted || mine !== seq || !Number.isFinite(real)) return;
total = Math.max(0, Math.round(real));
root.dataset.count = String(total);
label(total);
if (!lag) roll(total);
}, () => {
if (signal.aborted || mine !== seq) return;
apply(before.liked, before.total);
shake();
say(words.refused);
});
}, { signal });
// After a click the drop under the pointer waits for the next visit
root.addEventListener('pointerleave', () => root.classList.remove('is-spent'), { signal });
// Set from your code: aria-pressed fills or drains the heart, data-count rolls the count
const observer = new MutationObserver(() => {
const pressed = root.getAttribute('aria-pressed') === 'true';
const n = countOf();
if (pressed !== liked) {
liked = pressed;
flow(liked);
}
if (n !== total) {
total = n;
label(n);
later(LAG_DOWN);
}
});
observer.observe(root, { attributes: true, attributeFilter: ['aria-pressed', 'data-count'] });
paint(shown);
label(shown);
signal.addEventListener('abort', () => {
clearTimeout(lag);
clearTimeout(flowTimer);
clearTimeout(sayTimer);
observer.disconnect();
slosh?.cancel();
status.remove();
});
}
// Kodza lifecycle, the same in every component: `kodza:init` starts the instances inside
// event.detail.root (the whole page by default), `kodza:destroy` stops them and puts their
// markup back. For themes and apps that swap content without a page load. Each running root
// keeps its markup as written under KODZA, shared by every Kodza script on the page: a Kodza
// component inside another one is saved as written, not half-started, and stopped with it.
const KODZA = Symbol.for('kodza');
const instances = new WeakMap();
const find = (scope = document) => [scope, ...scope.querySelectorAll(SELECTOR)].filter((el) => el.matches?.(SELECTOR));
const attrsOf = (el) => [...el.attributes].map((a) => [a.name, a.value]);
const put = (el, saved) => {
[...el.attributes].forEach((a) => el.removeAttribute(a.name));
saved.attrs.forEach(([name, value]) => el.setAttribute(name, value));
el.innerHTML = saved.html;
};
// The markup inside a root as written: Kodza components already running in it give back their own
const written = (root) => {
const all = [...root.querySelectorAll('*')];
if (!all.some((el) => el[KODZA])) return root.innerHTML;
const copy = root.cloneNode(true);
const twins = copy.querySelectorAll('*');
let last = null;
all.forEach((el, i) => {
if (!el[KODZA] || last?.contains(el)) return;
last = el;
put(twins[i], el[KODZA]);
});
return copy.innerHTML;
};
const init = (scope) => find(scope).forEach((root) => {
if (root.classList.contains('is-ready')) return; // already running, maybe from another copy of this script
const controller = new AbortController();
const saved = { html: written(root), attrs: attrsOf(root), stop: () => destroy(root) };
instances.set(root, { controller, saved });
root[KODZA] = saved;
mount(root, controller.signal);
root.classList.add('is-ready');
});
const destroy = (scope) => find(scope).forEach((root) => {
const instance = instances.get(root);
if (!instance) return;
instances.delete(root);
// The Kodza components running inside it stop first: once its markup is put back, their own
// script could not find them any more
root.querySelectorAll('*').forEach((el) => el[KODZA]?.stop());
instance.controller.abort();
delete root[KODZA];
put(root, instance.saved);
});
document.addEventListener('kodza:init', (event) => init(event.detail?.root));
document.addEventListener('kodza:destroy', (event) => destroy(event.detail?.root));
init();
// Loaded in <head>, the script runs before the markup exists: once more when it is parsed
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => init(), { once: true });
})();
'use client';
// Kodza — Ink Like, for React. From https://kodza.dev/components/ink-like/
// Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license
// Next to this file: ink-like.css and ink-like.js, the component's CSS and JS from Kodza, as they are.
// Yours to edit: the markup is plain JSX below.
// The script changes this markup once React has rendered it (it adds layers, splits text, moves
// nodes), so React must not patch it afterwards. When a prop changes, the key gives React a fresh
// element: kodza:destroy stops the script on the old one, kodza:init starts it on the new one.
// Built from your data (tabs, rows, options)? Put what the markup is made of in the key, as plain
// values (ids, text, numbers): a change starts the script again on fresh markup. What the visitor
// changes (the open tab, the sort, what they type) is the script's: keep it out of the key, follow
// it with onClick or onInput on the root, and change it from your code as a visitor would (click
// the tab; set the field's value, then dispatch a bubbling input event).
import { useRef, useEffect, type ComponentPropsWithoutRef, type ComponentRef, type CSSProperties } from 'react';
import './ink-like.css';
type Variant = "bare";
export type InkLikeProps = Omit<ComponentPropsWithoutRef<'button'>, "children" | "variant" | "vars" | "count" | "onLike"> & {
/** Modifier class(es): .kodza-ink-like--bare */
variant?: Variant | Variant[];
/** data-count on the root (default: 999) */
count?: "999" | "128";
/** Settings, as CSS variables on the root */
vars?: {
/** default: #0a0a0a */
"--kz-ink-like-fg"?: string | number;
/** default: rgba(10, 10, 10, 0.06) */
"--kz-ink-like-bg"?: string | number;
/** default: rgba(10, 10, 10, 0.1) */
"--kz-ink-like-bg-hover"?: string | number;
/** default: 52px */
"--kz-ink-like-height"?: string | number;
/** default: 20px */
"--kz-ink-like-pad-x"?: string | number;
/** default: 22px */
"--kz-ink-like-icon"?: string | number;
/** default: cubic-bezier(0.22, 1, 0.36, 1) */
"--kz-ink-like-ease"?: string | number;
};
/** The text, plain: the script reads it and rebuilds the markup around it */
children?: string;
/** "kodza:like", fired by the script (bubbles from the root) */
onLike?: (event: CustomEvent) => void;
className?: string;
};
export function InkLike({ variant, count = "999", vars, children = "999", onLike, className, style, ...rest }: InkLikeProps) {
const ref = useRef<ComponentRef<'button'>>(null);
const classes = ["kodza-ink-like", ...([] as string[]).concat(variant ?? []).map((v) => `kodza-ink-like--${v}`), className].filter(Boolean).join(' ');
// Everything the markup is made of: when it changes, React gets a fresh element (see the top)
const key = JSON.stringify([classes, count, vars, children, style, rest]);
// Starts the script on this element once React has rendered it, stops it when it goes
useEffect(() => {
const root = ref.current;
let live = true;
// @ts-ignore: a plain script, no types to find
import('./ink-like.js').then(() => {
if (live) document.dispatchEvent(new CustomEvent('kodza:init', { detail: { root } }));
});
return () => {
live = false;
document.dispatchEvent(new CustomEvent('kodza:destroy', { detail: { root } }));
};
}, [key]);
useEffect(() => {
const root = ref.current;
if (!root || !onLike) return;
const listener = (event: Event) => onLike(event as CustomEvent);
root.addEventListener("kodza:like", listener);
return () => root.removeEventListener("kodza:like", listener);
}, [onLike, key]);
return (
<button type="button" aria-pressed="false" aria-label="Like, 999 likes" key={key} {...rest} className={classes} data-count={count} style={{ ...vars, ...style } as CSSProperties} ref={ref}>
<svg className="kodza-ink-like__icon" viewBox="0 0 24 24" aria-hidden="true">
<g className="kodza-ink-like__ink">
<g className="kodza-ink-like__level">
<g className="kodza-ink-like__surface">
<path className="kodza-ink-like__wave" d="M-48 2q6-2.4 12 0t12 0t12 0t12 0t12 0t12 0t12 0t12 0V40H-48z" />
</g>
</g>
</g>
<path className="kodza-ink-like__shape" d="M12 20.5C10.5 19.3 2.5 13.8 2.5 8.6 2.5 5.8 4.6 3.8 7.2 3.8 9.2 3.8 10.9 5 12 6.7 13.1 5 14.8 3.8 16.8 3.8 19.4 3.8 21.5 5.8 21.5 8.6 21.5 13.8 13.5 19.3 12 20.5Z" />
</svg>
{' '}
<span className="kodza-ink-like__count" aria-hidden="true">{children}</span>
</button>
);
}
Checked
Robots poked at it before you did. Every component goes through the same checks, and again each time it changes.