Morse
import { useEffect, useRef, useState } from 'react';
const css = `
.btn-morse {
display: inline-flex;
flex-direction: column;
gap: 10px;
width: 210px;
padding: 12px 14px 12px;
border: 1px solid light-dark(#dbdde1, #2e3035);
border-radius: 14px;
background: light-dark(#f3f4f7, #1a1c1f);
color: light-dark(#373a40, #f1f2f3);
font-family: inherit;
cursor: pointer;
transition: border-color 0.3s ease;
}
.btn-morse:hover { border-color: light-dark(#6d7179, #7d8087); }
.btn-morse:focus-visible { outline: 2px solid light-dark(#42454c, #e5e6e9); outline-offset: 3px; }
.btn-morse__head {
display: flex;
align-items: center;
gap: 10px;
font-size: 14px;
font-weight: 600;
}
.btn-morse__lamp {
width: 10px;
height: 10px;
border-radius: 50%;
background: light-dark(#dbdde1, #2e3035);
transition: background-color 0.04s linear, box-shadow 0.04s linear;
}
.btn-morse__lamp[data-on] {
background: light-dark(#373a40, #f1f2f3);
box-shadow: 0 0 12px 2px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent);
}
.btn-morse__code {
margin-left: auto;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
font-weight: 500;
color: light-dark(#5f626a, #a7abb2);
}
.btn-morse__tape {
position: relative;
height: 18px;
border-radius: 6px;
background: light-dark(#fbfcfd, #121417);
box-shadow: inset 0 0 0 1px light-dark(#ebedf0, #232529);
overflow: hidden;
mask-image: linear-gradient(90deg, transparent, #000 18%, #000 82%, transparent);
}
.btn-morse__reel { position: absolute; top: 0; bottom: 0; left: 0; will-change: transform; }
.btn-morse__mark {
position: absolute;
top: 6px;
height: 6px;
border-radius: 3px;
background: light-dark(#6d7179, #7d8087);
}
.btn-morse__readhead {
position: absolute;
left: 50%;
top: 2px;
bottom: 2px;
width: 1px;
background: light-dark(#42454c, #e5e6e9);
}
`;
const CODE: Record<string, string> = { S: '...', E: '.', N: '-.', D: '-..' };
const WORD = 'SEND';
const U = 6; // px per Morse time unit
// Marks as [start, length] in units: dot 1, dash 3, 1 between marks, 3 between letters, 7 between words.
const MARKS: [number, number][] = [];
let cursor = 0;
for (const ch of WORD) {
for (const sym of CODE[ch]) {
const len = sym === '.' ? 1 : 3;
MARKS.push([cursor, len]);
cursor += len + 1;
}
cursor += 2;
}
const PERIOD = cursor + 4; // + the rest of a 7-unit word gap
export function MorseButton() {
const reel = useRef<HTMLSpanElement>(null);
const tape = useRef<HTMLSpanElement>(null);
const sending = useRef(false);
const [lamp, setLamp] = useState(false);
useEffect(() => {
let pos = -6;
let last = performance.now();
let raf = 0;
const tick = (now: number) => {
const dt = (now - last) / 1000;
last = now;
if (sending.current) pos += dt * 9; // 9 units per second
const half = (tape.current?.clientWidth ?? 180) / 2;
const p = ((pos % PERIOD) + PERIOD) % PERIOD;
reel.current!.style.transform = `translateX(${(half - p * U).toFixed(1)}px)`;
setLamp(sending.current && MARKS.some(([s, l]) => p >= s && p < s + l));
raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf);
}, []);
const on = () => (sending.current = !matchMedia('(prefers-reduced-motion: reduce)').matches);
const off = () => (sending.current = false);
return (
<>
<style>
{css}
</style>
<button type="button" className="btn-morse" onPointerEnter={on} onPointerLeave={off} onFocus={on} onBlur={off}>
<span className="btn-morse__head">
<span className="btn-morse__lamp" data-on={lamp || undefined} aria-hidden="true" />
Send
<span className="btn-morse__code" aria-hidden="true">
{[...WORD].map((c) => CODE[c].replace(/-/g, '–')).join(' ')}
</span>
</span>
<span ref={tape} className="btn-morse__tape" aria-hidden="true">
<span ref={reel} className="btn-morse__reel">
{/* Three periods laid end to end so the tape never runs out on screen. */}
{[-1, 0, 1].flatMap((k) =>
MARKS.map(([s, l]) => (
<span
key={`${k}-${s}`}
className="btn-morse__mark"
style={{ left: (s + k * PERIOD) * U, width: l * U - 1 }}
/>
)),
)}
</span>
<span className="btn-morse__readhead" />
</span>
</button>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initBtnMorse(el) on the mounted element instead.
<button type="button" class="btn-morse">
<span class="btn-morse__head"><span class="btn-morse__lamp" aria-hidden="true"></span>Send<span class="btn-morse__code" aria-hidden="true">... . –. –..</span></span>
<span class="btn-morse__tape" aria-hidden="true">
<span class="btn-morse__reel">
<span class="btn-morse__mark" style="left:-204px;width:5px"></span>
<span class="btn-morse__mark" style="left:-192px;width:5px"></span>
<span class="btn-morse__mark" style="left:-180px;width:5px"></span>
<span class="btn-morse__mark" style="left:-156px;width:5px"></span>
<span class="btn-morse__mark" style="left:-132px;width:17px"></span>
<span class="btn-morse__mark" style="left:-108px;width:5px"></span>
<span class="btn-morse__mark" style="left:-84px;width:17px"></span>
<span class="btn-morse__mark" style="left:-60px;width:5px"></span>
<span class="btn-morse__mark" style="left:-48px;width:5px"></span>
<span class="btn-morse__mark" style="left:0;width:5px"></span>
<span class="btn-morse__mark" style="left:12px;width:5px"></span>
<span class="btn-morse__mark" style="left:24px;width:5px"></span>
<span class="btn-morse__mark" style="left:48px;width:5px"></span>
<span class="btn-morse__mark" style="left:72px;width:17px"></span>
<span class="btn-morse__mark" style="left:96px;width:5px"></span>
<span class="btn-morse__mark" style="left:120px;width:17px"></span>
<span class="btn-morse__mark" style="left:144px;width:5px"></span>
<span class="btn-morse__mark" style="left:156px;width:5px"></span>
<span class="btn-morse__mark" style="left:204px;width:5px"></span>
<span class="btn-morse__mark" style="left:216px;width:5px"></span>
<span class="btn-morse__mark" style="left:228px;width:5px"></span>
<span class="btn-morse__mark" style="left:252px;width:5px"></span>
<span class="btn-morse__mark" style="left:276px;width:17px"></span>
<span class="btn-morse__mark" style="left:300px;width:5px"></span>
<span class="btn-morse__mark" style="left:324px;width:17px"></span>
<span class="btn-morse__mark" style="left:348px;width:5px"></span>
<span class="btn-morse__mark" style="left:360px;width:5px"></span>
</span>
<span class="btn-morse__readhead"></span>
</span>
</button>.btn-morse {
display: inline-flex;
flex-direction: column;
gap: 10px;
width: 210px;
padding: 12px 14px 12px;
border: 1px solid light-dark(#dbdde1, #2e3035);
border-radius: 14px;
background: light-dark(#f3f4f7, #1a1c1f);
color: light-dark(#373a40, #f1f2f3);
font-family: inherit;
cursor: pointer;
transition: border-color 0.3s ease;
}
.btn-morse:hover { border-color: light-dark(#6d7179, #7d8087); }
.btn-morse:focus-visible { outline: 2px solid light-dark(#42454c, #e5e6e9); outline-offset: 3px; }
.btn-morse__head {
display: flex;
align-items: center;
gap: 10px;
font-size: 14px;
font-weight: 600;
}
.btn-morse__lamp {
width: 10px;
height: 10px;
border-radius: 50%;
background: light-dark(#dbdde1, #2e3035);
transition: background-color 0.04s linear, box-shadow 0.04s linear;
}
.btn-morse__lamp[data-on] {
background: light-dark(#373a40, #f1f2f3);
box-shadow: 0 0 12px 2px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent);
}
.btn-morse__code {
margin-left: auto;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
font-weight: 500;
color: light-dark(#5f626a, #a7abb2);
}
.btn-morse__tape {
position: relative;
height: 18px;
border-radius: 6px;
background: light-dark(#fbfcfd, #121417);
box-shadow: inset 0 0 0 1px light-dark(#ebedf0, #232529);
overflow: hidden;
mask-image: linear-gradient(90deg, transparent, #000 18%, #000 82%, transparent);
}
.btn-morse__reel { position: absolute; top: 0; bottom: 0; left: 0; will-change: transform; }
.btn-morse__mark {
position: absolute;
top: 6px;
height: 6px;
border-radius: 3px;
background: light-dark(#6d7179, #7d8087);
}
.btn-morse__readhead {
position: absolute;
left: 50%;
top: 2px;
bottom: 2px;
width: 1px;
background: light-dark(#42454c, #e5e6e9);
}function initBtnMorse(root) {
var reel = root.querySelector('.btn-morse__reel');
var tape = root.querySelector('.btn-morse__tape');
var lamp = root.querySelector('.btn-morse__lamp');
if (!reel || !tape || !lamp) return;
var CODE = { S: '...', E: '.', N: '-.', D: '-..' };
var U = 6; // px per Morse time unit
// Marks as [start, length] in units: dot 1, dash 3, 1 between marks, 3 between letters, 7 between words.
var MARKS = [];
var at = 0;
'SEND'.split('').forEach(function (ch) {
CODE[ch].split('').forEach(function (sym) {
var len = sym === '.' ? 1 : 3;
MARKS.push([at, len]);
at += len + 1;
});
at += 2;
});
var PERIOD = at + 4;
var sending = false;
var pos = -6;
var last = performance.now();
var raf = 0;
function tick(now) {
var dt = (now - last) / 1000;
last = now;
if (sending) pos += dt * 9; // nine units a second
var p = ((pos % PERIOD) + PERIOD) % PERIOD;
reel.style.transform = 'translateX(' + (tape.clientWidth / 2 - p * U).toFixed(1) + 'px)';
lamp.toggleAttribute('data-on', sending && MARKS.some(function (m) { return p >= m[0] && p < m[0] + m[1]; }));
raf = requestAnimationFrame(tick);
}
function on() { sending = !matchMedia('(prefers-reduced-motion: reduce)').matches; }
function off() { sending = false; }
root.addEventListener('pointerenter', on);
root.addEventListener('pointerleave', off);
root.addEventListener('focus', on);
root.addEventListener('blur', off);
raf = requestAnimationFrame(tick);
return function () {
cancelAnimationFrame(raf);
root.removeEventListener('pointerenter', on);
root.removeEventListener('pointerleave', off);
root.removeEventListener('focus', on);
root.removeEventListener('blur', off);
};
}
document.querySelectorAll('.btn-morse').forEach(initBtnMorse);Goal: Create a "Morse" button.
Visual behavior: Hover and the button transmits its label in Morse code: a paper tape of dots and dashes runs past a read head while a lamp blinks in time with every mark. Leave and the tape stops where it is.
Technique:
- The marks are built from real Morse timing in 6px units: dot 1, dash 3, 1 between marks, 3 between letters, 7 before the word repeats.
- Three copies of the message sit end to end on a reel inside a tape with faded edges (mask-image), so it never runs out on screen.
- While hovered or focused a rAF loop advances the position at nine units a second and translates the reel so the current point sits under the read head.
- The lamp is lit exactly when the read head is inside a mark, so the blink and the tape always agree.
Accessibility: A real <button> with a visible label; decoration is aria-hidden. Focus transmits like hover. Under prefers-reduced-motion the tape never moves.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Morse" button.
Visual behavior: Hover and the button transmits its label in Morse code: a paper tape of dots and dashes runs past a read head while a lamp blinks in time with every mark. Leave and the tape stops where it is.
Technique:
- The marks are built from real Morse timing in 6px units: dot 1, dash 3, 1 between marks, 3 between letters, 7 before the word repeats.
- Three copies of the message sit end to end on a reel inside a tape with faded edges (mask-image), so it never runs out on screen.
- While hovered or focused a rAF loop advances the position at nine units a second and translates the reel so the current point sits under the read head.
- The lamp is lit exactly when the read head is inside a mark, so the blink and the tape always agree.
Accessibility: A real <button> with a visible label; decoration is aria-hidden. Focus transmits like hover. Under prefers-reduced-motion the tape never moves.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
Hover and the button transmits its label in Morse code: a paper tape of dots and dashes runs past a read head while a lamp blinks in time with every mark. Leave and the tape stops where it is.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- hover, continuous, glow
- Techniques
- raf, state machine, css variables
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A real <button> with a visible label; decoration is aria-hidden. Focus transmits like hover. Under prefers-reduced-motion the tape never moves.