Skip to content

Commit b8edca2

Browse files
committed
reactScan to track re-renders on demo site
1 parent d015efc commit b8edca2

File tree

23 files changed

+522
-1228
lines changed

23 files changed

+522
-1228
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { bodyAttributes } from './attributes';
2+
3+
if (typeof window !== 'undefined') {
4+
const toDatasetKey = (dataKey: string) => dataKey.slice(5).replace(/-([a-z])/g, (_, c) => c.toUpperCase());
5+
6+
const act = {
7+
// toggle:theme-test(dark,light)
8+
toggle: (k: string, [on = 'on']: string[]) => {
9+
document.body.dataset[k] = document.body.dataset[k] ? '' : on;
10+
},
11+
// cycle:theme-test(dark,light)
12+
cycle: (k: string, vals: string[]) => {
13+
const cur = document.body.dataset[k] ?? vals[0];
14+
const next = vals[(vals.indexOf(cur) + 1) % vals.length];
15+
document.body.dataset[k] = next;
16+
},
17+
// set:theme-test(dark)
18+
set: (k: string, [v = '']: string[]) => {
19+
document.body.dataset[k] = v;
20+
},
21+
// attr:theme-test(data-theme)
22+
attr: (k: string, [attr]: string[], el: HTMLElement) => {
23+
document.body.dataset[k] = el.getAttribute(attr) ?? '';
24+
},
25+
};
26+
27+
document.addEventListener('click', (e) => {
28+
const el = (e.target as HTMLElement).closest<HTMLElement>('[data-ui]');
29+
if (!el) return;
30+
31+
const [, cmd, key, raw] = el.dataset.ui!.match(/^(\w+):([\w-]+)(?:\((.*?)\))?$/) || [];
32+
if (!cmd || !(`data-${key}` in bodyAttributes)) return;
33+
34+
const dsKey = toDatasetKey(`data-${key}`);
35+
console.log('dsKey: ', dsKey);
36+
act[cmd as keyof typeof act]?.(dsKey, raw ? raw.split(',') : [], el);
37+
});
38+
}

examples/demo/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
"clean": "rm -rf .next"
1616
},
1717
"dependencies": {
18-
"@react-zero-ui/core": "^0.1.0",
18+
"@react-zero-ui/core": "0.1.0",
1919
"@vercel/analytics": "^1.5.0",
2020
"clsx": "^2.1.1",
2121
"motion": "12.18.1",
2222
"next": "^15.3.5",
2323
"react": "^19.0.0",
24-
"react-dom": "^19.0.0"
24+
"react-dom": "^19.0.0",
25+
"react-scan": "^0.4.3"
2526
},
2627
"devDependencies": {
2728
"@tailwindcss/postcss": "^4.1.10",
@@ -32,4 +33,4 @@
3233
"tailwindcss": "^4.1.10",
3334
"typescript": "^5"
3435
}
35-
}
36+
}

examples/demo/src/app/(test)/ReactState.tsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
import { useState } from 'react';
44

5-
import { useRenderTracker } from './ReactTracker';
6-
75
export function TestComponentWithState() {
8-
const ref = useRenderTracker('TestComponentWithState');
96
const [accent, setAccent] = useState<'violet' | 'emerald' | 'amber'>('violet');
107
const [theme, setTheme] = useState<'light' | 'dark'>('light');
118
const [menuOpen, setMenuOpen] = useState<boolean>(false);
129

1310
return (
1411
<div
15-
ref={ref}
1612
className={`flex h-full w-full flex-col justify-between space-y-4 py-8 **:transition-all **:duration-300 ${theme === 'dark' ? 'bg-gray-900' : 'bg-gray-100'}`}>
1713
<Header theme={theme} />
1814
<ThemeSwitcher
@@ -41,12 +37,8 @@ export function TestComponentWithState() {
4137

4238
// Header Component
4339
function Header({ theme }: { theme: 'light' | 'dark' }) {
44-
const ref = useRenderTracker('Header');
45-
4640
return (
47-
<div
48-
ref={ref}
49-
className="space-y-2 text-center">
41+
<div className="space-y-2 text-center">
5042
<h1 className={`text-3xl font-bold ${theme === 'light' ? 'text-gray-900' : 'text-white'}`}>
5143
React State <span className="max-[450px]:hidden">Management</span>
5244
</h1>
@@ -61,12 +53,8 @@ function Header({ theme }: { theme: 'light' | 'dark' }) {
6153

6254
// Theme Switcher Component
6355
function ThemeSwitcher({ theme, setTheme }: { theme: 'light' | 'dark'; setTheme: (t: 'light' | 'dark') => void }) {
64-
const ref = useRenderTracker('ThemeSwitcher');
65-
6656
return (
67-
<div
68-
ref={ref}
69-
className="flex justify-center gap-2">
57+
<div className="flex justify-center gap-2">
7058
<button
7159
aria-label="button"
7260
onClick={() => setTheme('light')}
@@ -93,12 +81,8 @@ function AccentPicker({
9381
setAccent: (a: 'violet' | 'emerald' | 'amber') => void;
9482
theme: 'light' | 'dark';
9583
}) {
96-
const ref = useRenderTracker('AccentPicker');
97-
9884
return (
99-
<div
100-
ref={ref}
101-
className="space-y-4 pb-2">
85+
<div className="space-y-4 pb-2">
10286
<h2 className={`text-center text-lg font-semibold ${theme === 'light' ? 'text-gray-800' : 'text-gray-200'}`}>Choose Accent</h2>
10387
<div className="flex justify-center gap-3">
10488
<button
@@ -133,11 +117,8 @@ function InteractiveCard({
133117
setMenuOpen: (open: boolean) => void;
134118
accent: 'violet' | 'emerald' | 'amber';
135119
}) {
136-
const ref = useRenderTracker('InteractiveCard');
137-
138120
return (
139121
<div
140-
ref={ref}
141122
className={`relative mx-auto max-w-md overflow-hidden rounded-2xl border border-gray-200 shadow-lg transition-all duration-0! ${theme === 'light' ? 'bg-gray-50 shadow-gray-200' : 'bg-gray-700 shadow-black/50'}`}>
142123
<div className="space-y-4 p-6">
143124
<h3 className={`text-xl font-semibold ${theme === 'light' ? 'text-gray-900' : 'text-white'}`}>Open Menu Demo</h3>
@@ -162,12 +143,8 @@ function InteractiveCard({
162143

163144
// State Display Component
164145
function StateDisplay({ theme, accent, menuOpen }: { theme: 'light' | 'dark'; accent: 'violet' | 'emerald' | 'amber'; menuOpen: boolean }) {
165-
const ref = useRenderTracker('StateDisplay');
166-
167146
return (
168-
<div
169-
ref={ref}
170-
className="max-[450px]:hidden">
147+
<div className="max-[450px]:hidden">
171148
<div
172149
className={`mt-5 **:text-nowrap flex justify-center gap-4 space-y-1 text-center font-mono text-sm capitalize ${accent === 'violet' ? 'text-violet-500' : accent === 'emerald' ? 'text-emerald-500' : 'text-amber-500'}`}>
173150
<div className="flex gap-1">theme: {theme} </div>

examples/demo/src/app/(test)/ReactTracker.tsx

Lines changed: 0 additions & 233 deletions
This file was deleted.

0 commit comments

Comments
 (0)