Skip to content

Commit 9794cf7

Browse files
lxsmnsycclaude
andcommitted
chore(demo): add a ticker that updates every frame
A counter and a moving dot driven by a signal and requestAnimationFrame, with a pause button, to watch the update highlight while scrolling or resizing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d39e400 commit 9794cf7

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎examples/demo/src/App.tsx‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createEffect, createMemo, createSignal, For, Loading, Show } from 'solid-js';
1+
import { createEffect, createMemo, createSignal, For, Loading, onSettled, Show } from 'solid-js';
22
import { createOrder, SEED_ORDERS, type Order, type OrderStatus } from './orders.js';
33
import { reportCall } from './report-call.js';
44
import './styles.css';
@@ -24,6 +24,41 @@ function Boom(): never {
2424
throw new Error('The demo threw this on purpose.');
2525
}
2626

27+
/**
28+
* Updates on every animation frame while it runs: a counter for a text
29+
* change, and a dot whose style changes for an element change. Useful to
30+
* watch the update highlight while scrolling or resizing.
31+
*/
32+
function Ticker() {
33+
const [running, setRunning] = createSignal(true, { name: 'ticker-running' });
34+
const [frame, setFrame] = createSignal(0, { name: 'ticker-frame' });
35+
36+
onSettled(() => {
37+
let id = requestAnimationFrame(function tick() {
38+
if (running()) setFrame((value) => value + 1);
39+
id = requestAnimationFrame(tick);
40+
});
41+
return () => cancelAnimationFrame(id);
42+
});
43+
44+
return (
45+
<article class="stat">
46+
<span class="stat-label">Frames</span>
47+
<span class="stat-value">{frame()}</span>
48+
<div class="ticker-track">
49+
<div class="ticker-dot" style={{ left: `${(frame() % 120) / 1.2}%` }} />
50+
</div>
51+
<button
52+
type="button"
53+
class="link ticker-toggle"
54+
onClick={() => setRunning((value) => !value)}
55+
>
56+
{running() ? 'Pause' : 'Play'}
57+
</button>
58+
</article>
59+
);
60+
}
61+
2762
export default function App() {
2863
const [orders, setOrders] = createSignal<Order[]>(SEED_ORDERS, { name: 'orders' });
2964
const [query, setQuery] = createSignal('', { name: 'query' });
@@ -143,6 +178,7 @@ export default function App() {
143178
</Loading>
144179
<span class="stat-note">server function through an async memo</span>
145180
</article>
181+
<Ticker />
146182
</section>
147183

148184
<section class="controls">

‎examples/demo/src/styles.css‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ button.link {
125125
font-size: 1.25rem;
126126
}
127127

128+
.ticker-track {
129+
position: relative;
130+
height: 0.5rem;
131+
margin: 0.25rem 0;
132+
border-radius: 999px;
133+
background: var(--border);
134+
}
135+
136+
.ticker-dot {
137+
position: absolute;
138+
top: 0;
139+
width: 0.5rem;
140+
height: 0.5rem;
141+
border-radius: 999px;
142+
background: var(--accent);
143+
}
144+
145+
.ticker-toggle {
146+
align-self: flex-start;
147+
padding: 0;
148+
}
149+
128150
.stat-note {
129151
color: var(--muted);
130152
font-size: 0.6875rem;

0 commit comments

Comments
 (0)