1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466"use client";
import { memo, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
import type { CSSProperties, KeyboardEvent as ReactKeyboardEvent, ReactNode } from "react";
type SchoolId = "stanford" | "ucla" | "michigan" | "texas";
type School = {
id: SchoolId;
name: string;
short: string;
primary: string;
secondary: string;
contact: { name: string; art: string[] };
};
const SCHOOLS: School[] = [
{
id: "stanford", name: "Stanford", short: "Stanford", primary: "#8C1515", secondary: "#8C1515",
contact: {
name: "Cardinal",
art: [
"......#......",
".....###.....",
"....#####....",
".....###.....",
"....#####....",
"...#######...",
"....#####....",
"...#######...",
"..#########..",
".###########.",
"......+......",
"......+......",
".....+++.....",
],
},
},
{
id: "ucla", name: "UCLA", short: "UCLA", primary: "#2774AE", secondary: "#FFD100",
contact: {
name: "Joe",
art: [
".###.....###.",
"#ooo#####ooo#",
"#oo#######oo#",
".###########.",
".###########.",
".##++###++##.",
".##++###++##.",
".####ooo####.",
"..##ooooo##..",
"..##oo+oo##..",
"...##ooo##...",
"....#####....",
],
},
},
{
id: "michigan", name: "University of Michigan", short: "Michigan", primary: "#00274C", secondary: "#FFCB05",
contact: {
name: "Biff",
art: [
".##.......##.",
"#oo#######oo#",
"#ooooooooooo#",
"#o##ooooo##o#",
"#oo##ooo##oo#",
"#okkoooookko#",
"#ooooooooooo#",
"#ooookkkoooo#",
".#oo#####oo#.",
".#o#wwwww#o#.",
"..#ow###wo#..",
"...#######...",
],
},
},
{
id: "texas", name: "The University of Texas", short: "Texas", primary: "#BF5700", secondary: "#333F48",
contact: {
name: "Bevo",
art: [
"#...........#",
"##.........##",
".###.....###.",
"...#######...",
"....#o#o#....",
"....#####....",
".....###.....",
".....###.....",
".....#o#.....",
],
},
},
];
function hex(c: string) {
const n = parseInt(c.slice(1), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255] as const;
}
function shade(c: string, amt: number) {
const t = amt > 0 ? 0 : 255, k = Math.abs(amt);
return `#${hex(c).map((v) => Math.round(v + (t - v) * k).toString(16).padStart(2, "0")).join("")}`;
}
function luma(c: string) {
const [r, g, b] = hex(c);
return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
}
function toHsl(c: string): [number, number, number] {
const n = parseInt(c.slice(1), 16), r = (n >> 16) / 255, g = ((n >> 8) & 255) / 255, b = (n & 255) / 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b), l = (max + min) / 2, d = max - min;
if (!d) return [0, 0, l];
const s = d / (1 - Math.abs(2 * l - 1));
const h = max === r ? ((g - b) / d + 6) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
return [h * 60, s, l];
}
function fromHsl(h: number, s: number, l: number) {
const k = (n: number) => (n + h / 30) % 12, a = s * Math.min(l, 1 - l);
const f = (n: number) => Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1))));
return `#${[f(0), f(8), f(4)].map((v) => v.toString(16).padStart(2, "0")).join("")}`;
}
function lum(c: string) {
const n = parseInt(c.slice(1), 16);
const f = (v: number) => { v /= 255; return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4; };
return 0.2126 * f(n >> 16) + 0.7152 * f((n >> 8) & 255) + 0.0722 * f(n & 255);
}
function band(c: string, lo: number, hi: number, dh = 0, sat = 0.62) {
const [h0, s0, l0] = toHsl(c);
const gray = s0 < 0.08, h = gray ? h0 : (h0 + dh + 360) % 360;
const s = gray ? s0 : Math.max(s0, sat);
let l = l0, x = fromHsl(h, s, l);
for (let i = 0; i < 100 && lum(x) < lo; i++) x = fromHsl(h, s, (l = Math.min(1, l + 0.005)));
for (let i = 0; i < 100 && lum(x) > hi; i++) x = fromHsl(h, s, (l = Math.max(0, l - 0.005)));
return x;
}
const rgba = (c: string, o: number) => { const n = parseInt(c.slice(1), 16); return `rgba(${n >> 16},${(n >> 8) & 255},${n & 255},${o})`; };
function wallCss(primary: string, secondary: string) {
const lifted = secondary.toLowerCase() !== primary.toLowerCase() && luma(secondary) > luma(primary) + 0.1;
const deep = band(shade(primary, 0.3), 0.13, 0.14, -12), base = band(primary, 0.135, 0.15, -5), mid = band(primary, 0.155, 0.175);
const tint = band(primary, 0.18, 0.21, 10), glow = band(lifted ? secondary : primary, 0.4, 0.55, lifted ? 0 : 16);
return [
`linear-gradient(180deg,${rgba(deep, 0.8)} 0%,${rgba(deep, 0)} 24%)`,
`radial-gradient(46% 22% at 100% 62%,${glow} 0%,${rgba(glow, 0.85)} 30%,rgba(255,250,240,.32) 58%,rgba(255,250,240,0) 100%)`,
`conic-gradient(from 160deg at 18% 72%,${deep} 0deg,${base} 60deg,${mid} 120deg,${tint} 170deg,${mid} 220deg,${base} 290deg,${deep} 360deg)`,
].join(",");
}
const WALLS = new Map(SCHOOLS.map((s) => [s.id, wallCss(s.primary, s.secondary)]));
type Who = "me" | "them";
type Beat =
| { k: "step" }
| { k: "stamp"; day: string; time: string }
| { k: "send"; text: string; photo?: boolean }
| { k: "read" }
| { k: "reply"; text: string };
type Msg = { id: string; type: "msg"; at: number; from: Who; text?: string; photo?: boolean };
type Stamp = { id: string; type: "stamp"; at: number; day: string; time: string };
type Item = Msg | Stamp;
type Compose = { start: number; photoAt: number; chars: number[]; send: number; down: number; text: string; photo: boolean };
type Timeline = {
items: Item[]; composes: Compose[]; typings: [number, number][];
receipts: { id: string; delivered: number; read: number; time: string }[];
clock: [number, string][]; end: number;
};
const LEAD = 700;
const KEY = 58;
const HOLD = 1700;
const jitter = (i: number) => ((Math.sin(i * 12.9898) * 43758.5453) % 1 + 1) % 1;
function findLast<T>(a: readonly T[], f: (x: T) => boolean): T | undefined {
for (let i = a.length - 1; i >= 0; i--) if (f(a[i])) return a[i];
return undefined;
}
function compile(beats: Beat[]): Timeline {
const items: Item[] = [], composes: Compose[] = [], typings: [number, number][] = [];
const receipts: Timeline["receipts"] = [], clock: [number, string][] = [[0, "9:41"]];
let t = LEAD, n = 0, steps = 0, time = "9:41 AM";
let kb: Compose | null = null;
const id = () => `m${n++}`;
const keyboardDown = (at: number) => { if (kb) { kb.down = Math.min(kb.down, at); kb = null; } };
for (const b of beats) {
if (b.k === "step") {
if (steps++) t += HOLD;
t += 250;
} else if (b.k === "stamp") {
keyboardDown(t);
time = b.time;
items.push({ id: id(), type: "stamp", at: t, day: b.day, time: b.time });
clock.push([t, b.time.replace(/ [AP]M$/, "")]);
t += 650;
} else if (b.k === "send") {
const start = t;
const photoAt = b.photo ? start + 900 : start;
let at = b.photo ? photoAt + 700 : start + (kb ? 300 : 520);
const chars = [...b.text].map((ch, i) => {
at += KEY * (0.65 + 0.7 * jitter(i + b.text.length)) + (ch === " " ? 25 : 0) + (/[A-Z0-9’',!?]/.test(ch) ? 90 : 0);
return Math.round(at);
});
const send = at + 480;
if (kb) kb.down = start;
const c: Compose = { start, photoAt, chars, send, down: Infinity, text: b.text, photo: !!b.photo };
composes.push(c);
kb = c;
if (b.photo) items.push({ id: id(), type: "msg", at: send, from: "me", photo: true });
const m: Msg = { id: id(), type: "msg", at: send + (b.photo ? 110 : 0), from: "me", text: b.text };
items.push(m);
receipts.push({ id: m.id, delivered: m.at + 420, read: Infinity, time });
t = send + 1100;
} else if (b.k === "read") {
const r = receipts[receipts.length - 1];
r.read = t + 350; r.time = time;
t += 900;
} else {
const think = Math.min(2600, 900 + b.text.length * 13);
typings.push([t, t + think]);
t += think;
if (kb) { kb.down = t + 1300; kb = null; }
items.push({ id: id(), type: "msg", at: t, from: "them", text: b.text });
t += 1200 + b.text.length * 34;
}
}
keyboardDown(t);
return { items, composes, typings, receipts, clock, end: t + HOLD * 2 };
}
function exchange(sent: string, reply: string, photo?: boolean): Timeline {
return compile([
{ k: "step" },
{ k: "stamp", day: "Today", time: "9:41 AM" },
{ k: "send", text: sent, photo },
{ k: "read" },
{ k: "reply", text: reply },
]);
}
type Keys = {
key: string | null;
shift: boolean;
nums: boolean;
words: [string, string, string];
};
type Snap = {
t: number;
items: Item[];
receipt: { id: string; text: string } | null;
typing: boolean;
kbd: boolean;
field: { text: string; photo: boolean };
keys: Keys;
clock: string;
};
const PRED: [string, string, string] = ["I", "The", "I’m"];
const isLetter = (c: string) => /[a-z]/i.test(c);
const within = (t: number, at: number, before = 150) => t >= at - before && t < at;
function norm(ch: string) {
if (ch === "’" || ch === "‘") return "'";
if (ch === "“" || ch === "”") return "\"";
return ch.toLowerCase();
}
function snapshot(tl: Timeline, t: number): Snap {
const items = tl.items.filter((i) => i.at <= t);
let receipt: Snap["receipt"] = null;
for (const r of tl.receipts) {
if (t < r.delivered) continue;
receipt = { id: r.id, text: t >= r.read ? `Read ${r.time}` : "Delivered" };
}
const typing = tl.typings.some(([a, b]) => t >= a && t < b);
const c = findLast(tl.composes, (x) => t >= x.start && t < x.down);
let field: Snap["field"] = { text: "", photo: false };
let keys: Keys = { key: null, shift: true, nums: false, words: [...PRED] };
if (c) {
const typed = c.chars.filter((at) => at <= t).length;
const live = t < c.send;
field = { text: live ? c.text.slice(0, typed) : "", photo: live && c.photo && t >= c.photoAt };
let down = -1;
for (let k = c.chars.length - 1; k >= 0; k--) if (c.chars[k] <= t && t - c.chars[k] < 115) { down = k; break; }
const ch = down >= 0 && live ? c.text[down] : null;
const next = live ? c.text[typed] ?? null : null;
const focus = ch ?? next;
let key: string | null = ch === " " ? "space" : ch ? norm(ch) : null;
if (c.photo && within(t, c.photoAt - 40, 220)) key = "plus";
if (within(t, c.send, 170)) key = "send";
const shift = !!next && isLetter(next) && next === next.toUpperCase();
const nums = !!focus && focus !== " " && !isLetter(focus);
const before = field.text, words = c.text.split(" ");
const at = before.split(" ").length - 1, part = before.split(" ")[at] ?? "";
const strip = (w = "") => w.replace(/[.,!?]+$/, "");
keys = {
key, shift: typed === 0 || !live || shift, nums,
words: !live || typed === 0 ? [...PRED] : part ? [`“${part}”`, strip(words[at]), strip(words[at + 1]) || "the"] : [strip(words[at]) || "the", strip(words[at + 1]) || "and", "I"],
};
}
const clock = findLast(tl.clock, ([at]) => at <= t)?.[1] ?? "9:41";
return { t, items, receipt, typing, kbd: !!c, field, keys, clock };
}
function signature(s: Snap) {
return [
s.items.length, s.receipt?.text ?? "", s.typing, s.kbd, s.field.text, s.field.photo, s.keys.key ?? "", s.keys.shift, s.keys.nums,
s.keys.words.join(), s.clock,
].join("|");
}
type IconName = "wrench" | "id" | "laptop" | "doc" | "help" | "shield" | "siren";
type Case = { id: string; icon: IconName; title: string; desc: string; did: string; sent: string; photo?: boolean; rows: [string, string][]; reply: string };
const CASES: Case[] = [
{ id: "maintenance", icon: "wrench", title: "Maintenance", desc: "Leaks, heat, lights, broken machines.", did: "Filed", sent: "sink in hale 2nd floor bathroom is leaking everywhere", photo: true,
rows: [["Department", "Facilities"], ["Filed as", "Work order"], ["Priority", "Urgent"], ["Photo", "Attached"], ["Follow-up", "Texts you when it's fixed"]], reply: "Filed with Facilities w/ your photo ✅ I'll text you when it's fixed." },
{ id: "lockouts", icon: "id", title: "Lockouts & IDs", desc: "Dorm doors, lost and broken cards.", did: "Card off", sent: "lost my student ID somewhere near the library",
rows: [["Old card", "Turned off"], ["Done in", "Card access system"], ["New card", "Ordered once you say yes"], ["Lost & found", "Watching for a match"], ["Follow-up", "Texts you when it's ready"]], reply: "Turned off your lost card so no one can use it 🔒 Want me to order a new one? It's $25 on your student account." },
{ id: "passwords", icon: "laptop", title: "Passwords & IT", desc: "Passwords, two-factor, Wi-Fi.", did: "Reset", sent: "new phone and now i can't get past 2fa",
rows: [["Verified", "Code to campus email"], ["Done in", "IT help desk"], ["Action", "Reset two-factor"], ["Ticket", "Not needed"], ["Follow-up", "Walks you through setup"]], reply: "Reset ✅ Sign in again and it'll walk you through adding your new phone." },
{ id: "holds", icon: "doc", title: "Holds & deadlines", desc: "Registration holds, forms, due dates.", did: "Hold found", sent: "when do i register for spring",
rows: [["Registration", "Nov 14, 8 AM"], ["Hold", "Missing health form"], ["Source", "Student records"], ["Next step", "Offers the upload link"], ["Follow-up", "Texts when it clears"]], reply: "Nov 14 at 8 AM. A missing health form is holding your registration. Want the upload link?" },
{ id: "questions", icon: "help", title: "Questions", desc: "Hours, offices, where to go.", did: "Answered", sent: "when does the library close tonight",
rows: [["Answer", "2 AM tonight"], ["Source", "Library hours page"], ["Also", "24h room with ID"], ["Filed", "Nothing to file"], ["Follow-up", "None needed"]], reply: "2 AM tonight. First-floor study room is 24h with your ID." },
{ id: "safety", icon: "shield", title: "Safety", desc: "Hazards, dark paths, escorts.", did: "Reported", sent: "the stairs by lot C are totally dark",
rows: [["Department", "Campus Safety"], ["Filed as", "Lighting hazard"], ["Also sent to", "Facilities"], ["Location", "Lot C stairs"], ["Follow-up", "Offers an escort"]], reply: "Reported the dark stairs to Campus Safety. Want an escort to your dorm?" },
{ id: "emergencies", icon: "siren", title: "Emergencies", desc: "Gets you to 911 first.", did: "911 prompted", sent: "someone collapsed outside the gym",
rows: [["First", "Tells you to call 911"], ["Alerted", "Campus Safety"], ["Summary", "Texted to read to 911"], ["Location", "Rec Center"], ["Questions", "None until you're safe"]], reply: "Call 911 now 🚨 Campus Safety has been alerted. Read them the next text." },
];
const DONE = ["Filed with Facilities in 4 s", "Card turned off in 6 s", "Two-factor reset in 9 s", "Hold found in 3 s", "Answered in 2 s", "Reported to Campus Safety in 4 s", "Campus Safety alerted in 2 s"];
const TIMES = CASES.map((c) => {
const tl = exchange(c.sent, c.reply, c.photo);
const msgs = tl.items.filter((x): x is Msg => x.type === "msg" && !!x.text);
const sent = msgs.find((m) => m.from === "me")!.at, reply = msgs.find((m) => m.from === "them")!.at;
const gap = (reply - sent - 900) / c.rows.length;
return { tl, sent, reply, row: (k: number) => sent + 600 + k * gap, settle: reply + 1500, end: reply + 4600 };
});
type Clock = { i: number; ms: number; run: number };
function viewKey(s: Clock) {
const T = TIMES[s.i];
const rows = CASES[s.i].rows.filter((_, k) => s.ms >= T.row(k)).length;
return `${s.i}.${s.run}|${s.ms >= T.sent}|${s.ms >= T.reply}|${rows}|${signature(snapshot(T.tl, s.ms))}`;
}
const PATHS: Record<IconName, string> = {
wrench: "M226.76,69a8,8,0,0,0-12.84-2.88l-40.3,37.19-17.23-3.7-3.7-17.23,37.19-40.3A8,8,0,0,0,187,29.24,72,72,0,0,0,88,96,72.34,72.34,0,0,0,94,124.94L33.79,177c-.15.12-.29.26-.43.39a32,32,0,0,0,45.26,45.26c.13-.13.27-.28.39-.42L131.06,162A72,72,0,0,0,232,96,71.56,71.56,0,0,0,226.76,69ZM160,152a56.14,56.14,0,0,1-27.07-7,8,8,0,0,0-9.92,1.77L67.11,211.51a16,16,0,0,1-22.62-22.62L109.18,133a8,8,0,0,0,1.77-9.93,56,56,0,0,1,58.36-82.31l-31.2,33.81a8,8,0,0,0-1.94,7.1L141.83,108a8,8,0,0,0,6.14,6.14l26.35,5.66a8,8,0,0,0,7.1-1.94l33.81-31.2A56.06,56.06,0,0,1,160,152Z",
id: "M200,112a8,8,0,0,1-8,8H152a8,8,0,0,1,0-16h40A8,8,0,0,1,200,112Zm-8,24H152a8,8,0,0,0,0,16h40a8,8,0,0,0,0-16Zm40-80V200a16,16,0,0,1-16,16H40a16,16,0,0,1-16-16V56A16,16,0,0,1,40,40H216A16,16,0,0,1,232,56ZM216,200V56H40V200H216Zm-80.26-34a8,8,0,1,1-15.5,4c-2.63-10.26-13.06-18-24.25-18s-21.61,7.74-24.25,18a8,8,0,1,1-15.5-4,39.84,39.84,0,0,1,17.19-23.34,32,32,0,1,1,45.12,0A39.76,39.76,0,0,1,135.75,166ZM96,136a16,16,0,1,0-16-16A16,16,0,0,0,96,136Z",
laptop: "M232,168h-8V72a24,24,0,0,0-24-24H56A24,24,0,0,0,32,72v96H24a8,8,0,0,0-8,8v16a24,24,0,0,0,24,24H216a24,24,0,0,0,24-24V176A8,8,0,0,0,232,168ZM48,72a8,8,0,0,1,8-8H200a8,8,0,0,1,8,8v96H48ZM224,192a8,8,0,0,1-8,8H40a8,8,0,0,1-8-8v-8H224ZM152,88a8,8,0,0,1-8,8H112a8,8,0,0,1,0-16h32A8,8,0,0,1,152,88Z",
doc: "M213.66,82.34l-56-56A8,8,0,0,0,152,24H56A16,16,0,0,0,40,40V216a16,16,0,0,0,16,16H200a16,16,0,0,0,16-16V88A8,8,0,0,0,213.66,82.34ZM160,51.31,188.69,80H160ZM200,216H56V40h88V88a8,8,0,0,0,8,8h48V216Zm-32-80a8,8,0,0,1-8,8H96a8,8,0,0,1,0-16h64A8,8,0,0,1,168,136Zm0,32a8,8,0,0,1-8,8H96a8,8,0,0,1,0-16h64A8,8,0,0,1,168,168Z",
help: "M140,180a12,12,0,1,1-12-12A12,12,0,0,1,140,180ZM128,72c-22.06,0-40,16.15-40,36v4a8,8,0,0,0,16,0v-4c0-11,10.77-20,24-20s24,9,24,20-10.77,20-24,20a8,8,0,0,0-8,8v8a8,8,0,0,0,16,0v-.72c18.24-3.35,32-17.9,32-35.28C168,88.15,150.06,72,128,72Zm104,56A104,104,0,1,1,128,24,104.11,104.11,0,0,1,232,128Zm-16,0a88,88,0,1,0-88,88A88.1,88.1,0,0,0,216,128Z",
shield: "M208,40H48A16,16,0,0,0,32,56v56c0,52.72,25.52,84.67,46.93,102.19,23.06,18.86,46,25.26,47,25.53a8,8,0,0,0,4.2,0c1-.27,23.91-6.67,47-25.53C198.48,196.67,224,164.72,224,112V56A16,16,0,0,0,208,40Zm0,72c0,37.07-13.66,67.16-40.6,89.42A129.3,129.3,0,0,1,128,223.62a128.25,128.25,0,0,1-38.92-21.81C61.82,179.51,48,149.3,48,112l0-56,160,0ZM82.34,141.66a8,8,0,0,1,11.32-11.32L112,148.69l50.34-50.35a8,8,0,0,1,11.32,11.32l-56,56a8,8,0,0,1-11.32,0Z",
siren: "M120,16V8a8,8,0,0,1,16,0v8a8,8,0,0,1-16,0Zm80,32a8,8,0,0,0,5.66-2.34l8-8a8,8,0,0,0-11.32-11.32l-8,8A8,8,0,0,0,200,48ZM50.34,45.66A8,8,0,0,0,61.66,34.34l-8-8A8,8,0,0,0,42.34,37.66Zm87,26.45a8,8,0,1,0-2.64,15.78C153.67,91.08,168,108.32,168,128a8,8,0,0,0,16,0C184,100.6,163.93,76.57,137.32,72.11ZM232,176v24a16,16,0,0,1-16,16H40a16,16,0,0,1-16-16V176a16,16,0,0,1,16-16V128a88,88,0,0,1,88.67-88c48.15.36,87.33,40.29,87.33,89v31A16,16,0,0,1,232,176ZM56,160H200V129c0-40-32.05-72.71-71.45-73H128a72,72,0,0,0-72,72Zm160,40V176H40v24H216Z",
};
function Icon({ name }: { name: IconName }) {
return (
<svg viewBox="0 0 256 256" fill="currentColor" aria-hidden="true" focusable="false">
<path d={PATHS[name]} />
</svg>
);
}
const SETS = { upper: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", lower: "abcdefghijklmnopqrstuvwxyz", digit: "0123456789" } as const;
type Kind = keyof typeof SETS;
const kindOf = (c: string): Kind | null => (/^[A-Z]$/.test(c) ? "upper" : /^[a-z]$/.test(c) ? "lower" : /^[0-9]$/.test(c) ? "digit" : null);
const widthCache = new Map<string, Map<string, number>>();
let measureCtx: CanvasRenderingContext2D | null | undefined;
function widths(font: string) {
const hit = widthCache.get(font);
if (hit) return hit;
if (measureCtx === undefined) measureCtx = document.createElement("canvas").getContext("2d");
const m = new Map<string, number>();
if (measureCtx) {
measureCtx.font = font;
for (const set of Object.values(SETS)) for (const g of set) m.set(g, measureCtx.measureText(g).width);
}
if (document.fonts?.status === "loaded") widthCache.set(font, m);
return m;
}
function autoPool(c: string, k: Kind, w: Map<string, number>) {
const set = [...SETS[k]].filter((g) => g !== c);
const t = w.get(c);
if (!t) return set;
const ranked = set.map((g) => [g, Math.abs((w.get(g) ?? t) - t) / t] as const).sort((a, b) => a[1] - b[1]);
const close = ranked.filter(([, d]) => d <= 0.12);
return (close.length >= 5 ? close : ranked.slice(0, 5)).map(([g]) => g);
}
const reduced = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function Scramble({ text, duration, scramble = 0.45, rate = 14 }: { text: string; duration: number; scramble?: number; rate?: number }) {
const box = useRef<HTMLSpanElement>(null);
useEffect(() => {
const el = box.current;
if (!el) return;
if (reduced()) { el.dataset.s = "done"; return; }
const els = Array.from(el.querySelectorAll<HTMLSpanElement>(".md-c"));
const cs = getComputedStyle(el);
const w = widths(`${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`);
const n = els.length, top = Math.max(1, n - 1);
const D = duration * 1000, S = Math.min(scramble * 1000, D * 0.6), t0 = performance.now();
const cells = els.map((c, i) => {
const ch = c.textContent ?? "", k = kindOf(ch);
const start = t0 + (i / top) * (D - S);
return { el: c, pool: k ? autoPool(ch, k, w) : null, start, lock: start + S, tick: -1, phase: Math.random(), state: "", o: "" };
});
const set = (c: (typeof cells)[number], state: string) => { if (c.state !== state) { c.state = state; c.el.className = state ? `md-c ${state}` : "md-c"; } };
let raf = 0;
const frame = (now: number) => {
let busy = false;
for (const c of cells) {
if (now >= c.lock || (!c.pool && now >= c.start)) { set(c, ""); continue; }
busy = true;
if (!c.pool) { set(c, "md-x"); continue; }
set(c, "md-h");
const tick = Math.floor(((now - t0) / 1000) * rate + c.phase);
if (tick !== c.tick) {
c.tick = tick;
let g = c.pool[Math.floor(Math.random() * c.pool.length)];
if (g === c.el.dataset.g && c.pool.length > 1) g = c.pool[(c.pool.indexOf(g) + 1) % c.pool.length];
c.el.dataset.g = g;
}
const p = Math.min(1, Math.max(0, (now - t0) / (c.lock - t0 || 1)));
const o = (0.42 + 0.58 * p * p).toFixed(2);
if (o !== c.o) { c.o = o; c.el.style.setProperty("--md-o", o); }
}
raf = busy ? requestAnimationFrame(frame) : 0;
};
el.dataset.s = "run";
frame(t0);
return () => cancelAnimationFrame(raf);
}, [text, duration, scramble, rate]);
return (
<span className="md-cr">
<span className="md-sr">{text}</span>
<span key={text} ref={box} className="md-cv" data-s="idle" aria-hidden="true">
{Array.from(text).map((g, i) => (/^\s$/.test(g) ? g : <span key={i} className="md-c">{g}</span>))}
</span>
</span>
);
}
const SIREN = (() => {
const PERIOD = 3;
const FRAMES = 72;
const HALF = 22;
const CX = 12;
const H = Math.PI / 2, PI = Math.PI;
type Ring = { y: number; r: number; k: number };
const GLASS = { top: { y: 4.6, r: 5.6, k: 1.6 }, bot: { y: 16.6, r: 6.6, k: 1.7 } };
const LAMP = { top: { y: 6.8, r: 4.3, k: 1.25 }, bot: { y: 14.4, r: 4.95, k: 1.3 } };
const BASE = { top: { y: 17, r: 7.1, k: 1.75 }, bot: { y: 18.5, r: 7.1, k: 1.75 } };
const n = (v: number) => +v.toFixed(2);
const P = ([x, y]: number[]) => `${n(x)} ${n(y)}`;
const rad = (deg: number) => (deg * Math.PI) / 180;
const limb = (deg: number) => Math.max(-90, Math.min(90, deg));
const at = (g: Ring, phi: number) => [CX + g.r * Math.sin(phi), g.y + g.k * Math.cos(phi)];
const arc = (g: Ring, a: number, b: number) => {
const h = (4 / 3) * Math.tan((b - a) / 4);
const [ax, ay] = at(g, a), [bx, by] = at(g, b);
const da = [g.r * Math.cos(a), -g.k * Math.sin(a)], db = [g.r * Math.cos(b), -g.k * Math.sin(b)];
return `C${P([ax + h * da[0], ay + h * da[1]])} ${P([bx - h * db[0], by - h * db[1]])} ${P([bx, by])}`;
};
const { top, bot } = GLASS, B = BASE;
const sideX = (y: number) => CX - (top.r + ((bot.r - top.r) * (y - top.y)) / (bot.y - top.y));
let lo = -PI, hi = -H;
for (let i = 0; i < 30; i++) {
const mid = (lo + hi) / 2, [x, y] = at(B.top, mid);
if (x < sideX(y)) hi = mid; else lo = mid;
}
const j = (lo + hi) / 2, [, jy] = at(B.top, j);
const left = [sideX(jy), jy], right = [2 * CX - left[0], jy];
const SHAPE =
`M${P(at(B.bot, -H))}L${P(at(B.top, -H))}${arc(B.top, -H, j)}L${P(left)}` +
`L${P(at(top, -H))}${arc(top, -H, -PI)}${arc(top, PI, H)}` +
`L${P(right)}L${P(at(B.top, -j))}${arc(B.top, -j, H)}L${P(at(B.bot, H))}${arc(B.bot, H, 0)}${arc(B.bot, 0, -H)}Z`;
const lamp = (phi: number) => {
const a = rad(limb(phi - HALF)), b = rad(limb(phi + HALF));
return `M${P(at(LAMP.top, a))}${arc(LAMP.top, a, b)}L${P(at(LAMP.bot, b))}${arc(LAMP.bot, b, a)}Z`;
};
const PHI = Array.from({ length: FRAMES }, (_, k) => -180 + (360 * k) / FRAMES);
const D = PHI.map(lamp);
const OP = PHI.map((p) => +(Math.max(0, Math.cos(rad(p))) ** 0.6).toFixed(3));
const HALF_W = Math.max(BASE.top.r, BASE.bot.r, GLASS.bot.r);
const TOP = GLASS.top.y - GLASS.top.k, FOOT = BASE.bot.y + BASE.bot.k;
const PAD = (24 - (FOOT - TOP)) / 2;
const VB_W = 2 * (HALF_W + PAD);
const v = (x: number) => +x.toFixed(3);
return {
PERIOD, FRAMES, SHAPE, D, OP, VB_W,
D_VALUES: [...D, D[0]].join(";"),
OP_VALUES: [...OP, OP[0]].join(";"),
VIEWBOX: `${v(CX - HALF_W - PAD)} ${v(TOP - PAD)} ${v(VB_W)} 24`,
};
})();
function Lamp({ turn }: { turn: number }) {
const k = Math.round(turn * SIREN.FRAMES) % SIREN.FRAMES, begin = `${-turn * SIREN.PERIOD}s`;
return (
<path d={SIREN.D[k]} opacity={SIREN.OP[k]} style={{ fill: "var(--md-bg)" }}>
<animate attributeName="d" values={SIREN.D_VALUES} dur={`${SIREN.PERIOD}s`} begin={begin} repeatCount="indefinite" />
<animate attributeName="opacity" values={SIREN.OP_VALUES} dur={`${SIREN.PERIOD}s`} begin={begin} repeatCount="indefinite" />
</path>
);
}
function Siren({ size, live, still }: { size: number; live: boolean; still: boolean }) {
const ref = useRef<SVGSVGElement>(null);
useEffect(() => {
const svg = ref.current;
if (!svg) return;
if (still) { svg.pauseAnimations(); svg.setCurrentTime(0); } else if (live) svg.unpauseAnimations(); else svg.pauseAnimations();
}, [live, still]);
return (
<svg ref={ref} className="md-siren" width={+((size * SIREN.VB_W) / 24).toFixed(3)} height={size} viewBox={SIREN.VIEWBOX} aria-hidden="true" focusable="false">
<path d={SIREN.SHAPE} style={{ fill: "var(--md-accent)" }} />
<Lamp turn={0} />
<Lamp turn={0.5} />
</svg>
);
}
const W = 402, H = 874;
const KBD = 344;
const clamp1 = (v: number) => Math.max(-1, Math.min(1, v));
function Phone({ shown, children }: { shown: boolean; children: ReactNode }) {
const scr = useRef<HTMLDivElement>(null);
const stage = useRef<HTMLDivElement>(null);
const tilt = useRef<HTMLDivElement>(null);
const front = useRef(shown);
const nudge = useRef<() => void>(() => {});
useLayoutEffect(() => {
const el = scr.current;
if (!el) return;
const set = () => el.style.setProperty("--md-k", String(el.clientWidth / W));
set();
const ro = new ResizeObserver(set);
ro.observe(el);
return () => ro.disconnect();
}, []);
useEffect(() => {
const st = stage.current, el = tilt.current;
if (!st || !el) return;
if (!window.matchMedia("(hover: hover) and (pointer: fine)").matches || reduced()) return;
let gx = 0, gy = 0, on = 0, x = 0, y = 0, lift = 0, raf = 0, last = 0;
const tick = (now: number) => {
const dt = last ? Math.min(64, now - last) : 16;
last = now;
const k = 1 - Math.exp(-dt / 120);
const live = front.current ? 1 : 0, tx = gx * live, ty = gy * live, tl = on * live;
x += (tx - x) * k; y += (ty - y) * k; lift += (tl - lift) * k;
const moving = Math.abs(tx - x) + Math.abs(ty - y) + Math.abs(tl - lift) > 0.001;
if (!moving) { x = tx; y = ty; lift = tl; }
el.style.setProperty("--md-hx", x.toFixed(4));
el.style.setProperty("--md-hy", y.toFixed(4));
el.style.setProperty("--md-hl", lift.toFixed(4));
if (moving) raf = requestAnimationFrame(tick);
else { raf = 0; last = 0; }
};
const kick = () => { if (!raf) raf = requestAnimationFrame(tick); };
nudge.current = kick;
const move = (e: PointerEvent) => {
if (e.pointerType !== "mouse") return;
const r = st.getBoundingClientRect();
gx = clamp1((e.clientX - r.left - r.width / 2) / (r.width / 2));
gy = clamp1((e.clientY - r.top - r.height / 2) / (r.height / 2));
on = 1;
kick();
};
const leave = () => { gx = 0; gy = 0; on = 0; kick(); };
st.addEventListener("pointermove", move);
st.addEventListener("pointerleave", leave);
return () => {
cancelAnimationFrame(raf);
nudge.current = () => {};
st.removeEventListener("pointermove", move);
st.removeEventListener("pointerleave", leave);
};
}, []);
useEffect(() => { front.current = shown; nudge.current(); }, [shown]);
return (
<div className="md-ip-stage" ref={stage} aria-hidden="true">
<div className="md-ip-tilt" ref={tilt}>
<div className={`md-ip${shown ? "" : " md-enter"}`}>
<div className="md-ip-screen" ref={scr}>
<div className="md-ios">{children}</div>
</div>
</div>
</div>
</div>
);
}
function StatusBar({ clock }: { clock: string }) {
return (
<div className="md-sb">
<span className="md-sb-time">{clock}</span>
<span className="md-sb-right">
<svg width="19" height="12" viewBox="0 0 19 12"><rect x="0" y="7.5" width="3.2" height="4.5" rx="1" /><rect x="5.2" y="5" width="3.2" height="7" rx="1" /><rect x="10.4" y="2.5" width="3.2" height="9.5" rx="1" /><rect x="15.6" y="0" width="3.2" height="12" rx="1" /></svg>
<svg width="17" height="12" viewBox="0 0 17 12"><path d="M8.5 2.3c2.4 0 4.6.9 6.3 2.5.2.2.5.2.7 0l1.2-1.2c.2-.2.2-.5 0-.7A11.7 11.7 0 0 0 8.5 0 11.7 11.7 0 0 0 .3 2.9c-.2.2-.2.5 0 .7l1.2 1.2c.2.2.5.2.7 0a9 9 0 0 1 6.3-2.5Zm0 3.9c1.3 0 2.5.5 3.5 1.3.2.2.5.2.7 0l1.2-1.2c.2-.2.2-.5 0-.7a7.8 7.8 0 0 0-10.8 0c-.2.2-.2.5 0 .7l1.2 1.2c.2.2.5.2.7 0 1-.8 2.2-1.3 3.5-1.3Zm2.4 2.7c.2-.2.2-.5 0-.7a3.6 3.6 0 0 0-4.8 0c-.2.2-.2.5 0 .7l2 2a.6.6 0 0 0 .8 0l2-2Z" /></svg>
<svg width="27" height="13" viewBox="0 0 27 13"><rect x=".5" y=".5" width="23" height="12" rx="3.8" fill="none" stroke="currentColor" strokeOpacity=".4" /><rect x="2" y="2" width="20" height="9" rx="2.5" /><path d="M25 4.4v4.2c.8-.3 1.4-1.1 1.4-2.1s-.6-1.8-1.4-2.1Z" fillOpacity=".45" /></svg>
</span>
</div>
);
}
const useFresh = (at: number, t: number) => useState(() => t - at < 450)[0];
function Row({ m, t, grouped, tail, receipt, photo }: { m: Msg; t: number; grouped: boolean; tail: boolean; receipt: string | null; photo: string }) {
const fresh = useFresh(m.at, t);
return (
<div className={`md-row md-${m.from}${grouped ? " md-grouped" : ""}${fresh ? " md-fresh" : ""}`}>
<div className="md-wrap">
{m.photo
? <img className="md-photo" src={photo} alt="" width={480} height={320} />
: <div className={`md-b md-${m.from}${tail ? " md-tail" : ""}`}>{m.text}</div>}
</div>
{receipt && (
<div className="md-receipt" key={receipt}>
{receipt.startsWith("Read") ? <><b>Read</b>{receipt.slice(4)}</> : <b>{receipt}</b>}
</div>
)}
</div>
);
}
function StampRow({ s, t }: { s: Stamp; t: number }) {
const fresh = useFresh(s.at, t);
return <div className={`md-stamp${fresh ? " md-fresh" : ""}`}><b>{s.day}</b> {s.time}</div>;
}
function Typing({ grouped }: { grouped: boolean }) {
return <div className={`md-row md-them md-fresh${grouped ? " md-grouped" : ""}`}><div className="md-typing"><i /><i /><i /></div></div>;
}
const ROWS = {
abc: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
num: ["1234567890", "-/:;()$&@\"", ".,?!'"],
};
const KW = (W - 6 - 9 * 6) / 10;
const KH = 43;
const ROW_Y = [52, 106, 160, 214];
type KeyBox = { id: string; label: string; x: number; y: number; w: number; kind: "char" | "fn" };
function layout(nums: boolean): KeyBox[] {
const rows = nums ? ROWS.num : ROWS.abc;
const out: KeyBox[] = [];
const line = (chars: string, y: number, w: number, x0: number) => [...chars].forEach((c, i) => out.push({ id: c, label: c, x: x0 + i * (w + 6), y, w, kind: "char" }));
line(rows[0], ROW_Y[0], KW, 3);
if (nums) line(rows[1], ROW_Y[1], KW, 3);
else line(rows[1], ROW_Y[1], KW, (W - (9 * KW + 8 * 6)) / 2);
out.push({ id: "shift", label: nums ? "#+=" : "", x: 3, y: ROW_Y[2], w: 44, kind: "fn" });
if (nums) line(rows[2], ROW_Y[2], (W - 2 * 63 - 4 * 6) / 5, 63);
else line(rows[2], ROW_Y[2], KW, (W - (7 * KW + 6 * 6)) / 2);
out.push({ id: "del", label: "", x: W - 47, y: ROW_Y[2], w: 44, kind: "fn" });
out.push({ id: "mode", label: nums ? "ABC" : "123", x: 3, y: ROW_Y[3], w: 44, kind: "fn" });
out.push({ id: "emoji", label: "", x: 53, y: ROW_Y[3], w: 44, kind: "fn" });
out.push({ id: "space", label: "", x: 103, y: ROW_Y[3], w: W - 103 - 101, kind: "char" });
out.push({ id: "return", label: "", x: W - 95, y: ROW_Y[3], w: 92, kind: "fn" });
return out;
}
const LAYOUTS = { abc: layout(false), num: layout(true) };
function balloon(k: KeyBox) {
const head = k.w + 24, top = 56, neck = 12, h = top + neck + KH;
const hx = Math.max(-k.x + 1, Math.min((k.w - head) / 2, W - 1 - k.x - head));
const L = hx, R = hx + head, kl = 0, kr = k.w, r = 11, kr2 = 9;
return {
d: `M${L + r} 0H${R - r}Q${R} 0 ${R} ${r}V${top - 8}C${R} ${top + 2} ${kr} ${top + 3} ${kr} ${top + neck}V${h - kr2}Q${kr} ${h} ${kr - kr2} ${h}H${kl + kr2}Q${kl} ${h} ${kl} ${h - kr2}V${top + neck}C${kl} ${top + 3} ${L} ${top + 2} ${L} ${top - 8}V${r}Q${L} 0 ${L + r} 0Z`,
left: L, width: head, height: h, cx: hx + head / 2,
};
}
const KEY_ICONS: Record<string, ReactNode> = {
del: <svg viewBox="0 0 26 20" width="24" height="18"><path d="M8.6 2.5h13.1c1.3 0 2.3 1 2.3 2.3v10.4c0 1.3-1 2.3-2.3 2.3H8.6L2 10l6.6-7.5Z" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinejoin="round" /><path d="m11.8 6.6 6.8 6.8m0-6.8-6.8 6.8" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" /></svg>,
emoji: <svg viewBox="0 0 24 24" width="21" height="21"><circle cx="12" cy="12" r="10" fill="currentColor" /><circle cx="8.8" cy="10" r="1.35" fill="#5b5b5e" /><circle cx="15.2" cy="10" r="1.35" fill="#5b5b5e" /><path d="M7.8 13.6a4.8 4.8 0 0 0 8.4 0" fill="none" stroke="#5b5b5e" strokeWidth="1.6" strokeLinecap="round" /></svg>,
return: <svg viewBox="0 0 24 20" width="22" height="18"><path d="M20 3.5v6.2c0 1.2-1 2.2-2.2 2.2H5m0 0 4.2-4.2M5 11.9l4.2 4.2" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" /></svg>,
};
function Keyboard({ snap }: { snap: Snap }) {
const { keys } = snap;
const keyset = keys.nums ? LAYOUTS.num : LAYOUTS.abc;
const down = keys.key;
const pressed = down ? keyset.find((k) => k.id === down) : undefined;
const pop = pressed && pressed.kind === "char" && pressed.id !== "space" ? balloon(pressed) : null;
const upper = keys.shift && !keys.nums;
return (
<div className={`md-kb${snap.kbd ? " md-up" : ""}`}>
<div className="md-kb-pred">
{keys.words.map((w, i) => <span key={i}>{w}</span>)}
<span className="md-kb-fmt"><svg viewBox="0 0 22 16" width="19" height="14"><path d="M1.5 3.5h6M1.5 8h6M1.5 12.5h6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><path d="m10 13.5 4.2-11h.8l4.2 11M11.6 9.6h6" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" /></svg></span>
</div>
{keyset.map((k) => (
<span key={k.id} className={`md-kb-k${k.kind === "fn" ? " md-fn" : ""}${down === k.id && !pop ? " md-on" : ""}${k.id === "shift" && upper ? " md-lit" : ""}`}
style={{ left: k.x, top: k.y, width: k.w }}>
{k.id === "shift" && !keys.nums
? <svg viewBox="0 0 24 24" width="22" height="22"><path d="M12 3.8 3.4 12.6h4.9v7.6h7.4v-7.6h4.9L12 3.8Z" fill={upper ? "currentColor" : "none"} stroke="currentColor" strokeWidth="1.7" strokeLinejoin="round" /></svg>
: KEY_ICONS[k.id] ?? (k.kind === "char" && upper ? k.label.toUpperCase() : k.label)}
</span>
))}
{pop && pressed && (
<span className="md-kb-pop" style={{ left: pressed.x + pop.left, top: pressed.y + KH - pop.height, width: pop.width, height: pop.height }}>
<svg width={pop.width} height={pop.height} viewBox={`${pop.left} 0 ${pop.width} ${pop.height}`}><path d={pop.d} /></svg>
<b style={{ left: pop.cx - pop.left }}>{upper ? pressed.label.toUpperCase() : pressed.label}</b>
</span>
)}
<span className="md-kb-globe"><svg viewBox="0 0 24 24" width="25" height="25"><circle cx="12" cy="12" r="9.5" fill="none" stroke="currentColor" strokeWidth="1.5" /><path d="M2.5 12h19M12 2.5c-3 3-4.4 6.2-4.4 9.5s1.4 6.5 4.4 9.5c3-3 4.4-6.2 4.4-9.5S15 5.5 12 2.5ZM4.6 6.5h14.8M4.6 17.5h14.8" fill="none" stroke="currentColor" strokeWidth="1.5" /></svg></span>
<span className="md-kb-mic"><svg viewBox="0 0 24 24" width="24" height="24"><rect x="8.6" y="2.6" width="6.8" height="12" rx="3.4" fill="none" stroke="currentColor" strokeWidth="1.6" /><path d="M5.4 11.4a6.6 6.6 0 0 0 13.2 0M12 18v3.4" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" /></svg></span>
</div>
);
}
function Wall({ school }: { school: School }) {
const [layers, setLayers] = useState<School[]>([school]);
if (layers[layers.length - 1] !== school) setLayers([layers[layers.length - 1], school]);
return <>{layers.map((s, i) => <div key={s.id} className={`md-wall${i ? " md-wall-in" : ""}`} style={{ background: WALLS.get(s.id) }} />)}</>;
}
function Mascot({ s }: { s: School }) {
const gid = `md-m-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
const rows = s.contact.art;
const w = Math.max(...rows.map((r) => r.length)), h = rows.length;
const pitch = 74 / Math.max(w, h, 11), ox = 50 - (w * pitch) / 2, oy = 50 - (h * pitch) / 2;
const back = shade(s.primary, luma(s.primary) > 0.3 ? 0.45 : 0.25);
const second = s.secondary.toLowerCase() === s.primary.toLowerCase() || luma(s.secondary) < 0.25 ? "#fff6dc" : s.secondary;
const fill: Record<string, string> = { "#": shade(s.primary, -0.4), o: second, "+": shade(s.primary, -0.12), k: "#0b0b0d", w: "#ffffff" };
const field: ReactNode[] = [];
for (let y = 1; y < 100; y += pitch * 0.7) for (let x = 1; x < 100; x += pitch * 0.7) {
const d = Math.hypot(x - 50, y - 42) / 60;
field.push(<circle key={`${x}.${y}`} cx={x} cy={y} r={0.5 + 0.55 * Math.max(0, 1 - d)} />);
}
return (
<svg viewBox="0 0 100 100" width="100%" height="100%" aria-hidden="true">
<defs><radialGradient id={gid} cx="50%" cy="44%" r="64%"><stop offset="0" stopColor={shade(back, -0.18)} /><stop offset="1" stopColor={shade(back, 0.35)} /></radialGradient></defs>
<rect width="100" height="100" fill={`url(#${gid})`} />
<g fill={shade(back, -0.3)} opacity=".45">{field}</g>
{rows.flatMap((row, y) => row.split("").map((ch, x) => fill[ch]
? <circle key={`${x}.${y}`} cx={ox + (x + 0.5) * pitch} cy={oy + (y + 0.5) * pitch} r={pitch * (ch === "+" ? 0.4 : 0.46)} fill={fill[ch]} />
: null))}
</svg>
);
}
function Messages({ snap, school, photo }: { snap: Snap; school: School; photo: string }) {
const thread = useRef<HTMLDivElement>(null);
const field = useRef<HTMLDivElement>(null);
const t = snap.t;
useLayoutEffect(() => {
const th = thread.current, fd = field.current;
if (!th || !fd) return;
const above = (snap.kbd ? KBD + 8 : 28) + fd.offsetHeight + 14;
const ty = Math.min(0, H - above - th.offsetHeight);
const top = (el: HTMLElement) => { let y = 0, n: HTMLElement | null = el; while (n && n !== th) { y += n.offsetTop; n = n.offsetParent as HTMLElement | null; } return y; };
th.querySelectorAll<HTMLElement>(".md-b.md-me").forEach((b) => {
b.style.setProperty("--md-by", `${top(b)}px`);
b.style.setProperty("--md-bh", `${b.offsetHeight}px`);
});
th.style.setProperty("--md-ty", `${ty}px`);
}, [snap]);
const items = snap.items;
const typing = snap.typing;
const from = (i: number) => { const x = items[i]; return x?.type === "msg" ? x.from : i === items.length && typing ? "them" : null; };
const empty = !snap.field.text && !snap.field.photo;
return (
<>
<Wall school={school} />
<div className="md-thread" ref={thread}>
<div className="md-start"><span>iMessage</span><span><svg viewBox="0 0 10 12" width="7" height="9"><path d="M2.4 5V3.6a2.6 2.6 0 0 1 5.2 0V5" fill="none" stroke="currentColor" strokeWidth="1.3" /><rect x="1" y="5" width="8" height="6.4" rx="1.4" fill="currentColor" /></svg>Encrypted</span></div>
{items.map((it, i) => it.type === "stamp"
? <StampRow key={it.id} s={it} t={t} />
: <Row key={it.id} m={it} t={t} grouped={from(i - 1) === it.from} tail={!it.photo && from(i + 1) !== it.from} receipt={snap.receipt?.id === it.id ? snap.receipt.text : null} photo={photo} />)}
{typing && <Typing key={`ty${items.length}`} grouped={from(items.length - 1) === "them"} />}
</div>
<div className="md-top">
<i className="md-top-fx" />
<span className="md-btn md-glass md-back"><svg viewBox="0 0 12 20" width="11" height="19"><path d="M9.8 2 2 10l7.8 8" fill="none" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round" /></svg></span>
<span className="md-btn md-glass md-video"><svg viewBox="0 0 30 20" width="27" height="18"><rect x="1.2" y="2.2" width="19" height="15.6" rx="4.2" fill="none" stroke="currentColor" strokeWidth="2" /><path d="m21.6 8.2 5.6-3.4c.6-.4 1.4.1 1.4.8v8.8c0 .7-.8 1.2-1.4.8l-5.6-3.4" fill="none" stroke="currentColor" strokeWidth="2" strokeLinejoin="round" /></svg></span>
<span className="md-avatar"><Mascot key={school.id} s={school} /></span>
<span className="md-name md-glass">{school.contact.name}<svg viewBox="0 0 8 14" width="6" height="11"><path d="m1.5 1.5 5 5.5-5 5.5" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" /></svg></span>
</div>
<Keyboard snap={snap} />
<div className={`md-dock${snap.kbd ? " md-up" : ""}`}>
<i className="md-bot-fx" />
<span className={`md-plus md-glass${snap.keys.key === "plus" ? " md-on" : ""}`}><svg viewBox="0 0 20 20" width="19" height="19"><path d="M10 3v14M3 10h14" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" /></svg></span>
<div ref={field} className={`md-field md-glass${snap.field.photo ? " md-att" : ""}`}>
{snap.field.photo && (
<span className="md-attach">
<img src={photo} alt="" width={480} height={320} />
<i><svg viewBox="0 0 10 10" width="8" height="8"><path d="m2 2 6 6M8 2 2 8" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" /></svg></i>
</span>
)}
<span className="md-input">
{snap.field.text
? <>{snap.field.text}{snap.kbd && <i className={`md-caret${snap.keys.key ? " md-typing-now" : ""}`} />}</>
: <>{snap.kbd && <i className="md-caret" />}<span className="md-ph">{snap.field.photo ? "Add comment or Send" : "iMessage"}</span></>}
</span>
{!empty
? <span className={`md-send${snap.keys.key === "send" ? " md-on" : ""}`}><svg viewBox="0 0 16 16" width="15" height="15"><path d="M8 13.5V2.8M3 7.6l5-5 5 5" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" /></svg></span>
: snap.kbd
? <span className="md-wave"><svg viewBox="0 0 24 24" width="19" height="19"><rect x="8.6" y="2.6" width="6.8" height="12" rx="3.4" fill="none" stroke="currentColor" strokeWidth="1.7" /><path d="M5.4 11.4a6.6 6.6 0 0 0 13.2 0M12 18v3.4" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" /></svg></span>
: <span className="md-wave"><svg viewBox="0 0 22 16" width="20" height="15"><path d="M2 6.5v3M6 4v8M10 1.5v13M14 4.5v7M18 6.5v3" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" /></svg></span>}
</div>
</div>
<StatusBar clock={snap.clock} />
<i className="md-home" />
</>
);
}
const CaseScreen = memo(function CaseScreen({ snap, school, photo }: { snap: Snap; sig: string; school: School; photo: string }) {
return <Messages snap={snap} school={school} photo={photo} />;
}, (a, b) => a.sig === b.sig && a.school === b.school && a.photo === b.photo);
const css = `
@property --md-accent{syntax:"<color>";inherits:true;initial-value:#8c1515}
@property --md-ty{syntax:"<length>";inherits:true;initial-value:0px}
.md{--md-bg:#f9f9f9;--md-ink:#262626;--md-muted:rgba(38,38,38,.56);--md-line:#ececec;--md-a4:rgba(0,0,0,.04);--md-ease:cubic-bezier(.3,.7,.2,1);
--md-mono:var(--font-mono,"JetBrains Mono"),ui-monospace,monospace;
--md-heat-4:color-mix(in srgb,var(--md-accent) 4%,transparent);--md-heat-12:color-mix(in srgb,var(--md-accent) 12%,transparent);
container:md/inline-size;position:relative;width:100%;min-height:100%;overflow:hidden;isolation:isolate;
background:var(--md-bg);color:var(--md-ink);font-family:var(--font-inter,Inter),system-ui,sans-serif;font-size:15px;line-height:normal;text-align:left;-webkit-font-smoothing:antialiased;
transition:--md-accent .9s var(--md-ease)}
.md *,.md *::before,.md *::after{box-sizing:border-box}
.md button{font:inherit;color:inherit;-webkit-tap-highlight-color:transparent}
.md-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap;user-select:none}
/* the cases: a tab list on the left, and the open case's demo beside it */
.md-cases{display:grid;grid-template-columns:228px minmax(0,1fr)}
.md-list{position:relative;display:flex;flex-direction:column;border-right:1px solid var(--md-line)}
.md-case{position:relative;display:flex;flex:1 0 auto;min-height:64px;border-bottom:1px solid var(--md-line);transition:background .2s}
.md-case:last-child{border-bottom:none}
@media (hover:hover){.md-case:hover{background:var(--md-a4)}}
.md-case.md-on{background:var(--md-heat-4)}
/* the 3px accent bar on the open case fills top to bottom while its demo plays (--md-p, set by the clock) */
.md-case.md-on::before{content:"";position:absolute;left:0;top:0;bottom:0;width:3px;background:var(--md-heat-12)}
.md-bar{position:absolute;left:0;top:0;bottom:0;z-index:1;width:3px;background:var(--md-accent);transform-origin:top;transform:scaleY(var(--md-p,0));pointer-events:none}
.md-tab{flex:1;display:flex;flex-direction:column;align-items:flex-start;justify-content:center;gap:2px;width:100%;margin:0;padding:11px 22px 11px 24px;border:none;background:none;text-align:left;color:var(--md-ink);cursor:pointer}
.md-tab:focus-visible{outline:2px solid var(--md-accent);outline-offset:-2px}
.md-case-t{display:flex;align-items:center;gap:8px;font-size:15.5px;font-weight:450;line-height:22px;white-space:nowrap;transition:color .15s}
.md-case-t svg{width:20px;height:20px;flex:none;color:#8a8a8a;transition:color .15s}
.md-on .md-case-t,.md-on .md-case-t svg{color:var(--md-accent)}
.md-case-d{font-size:13px;line-height:18px;color:rgba(38,38,38,.48);text-wrap:pretty;hyphens:manual}
.md-view{position:relative;display:flex;flex-direction:column;min-width:0;min-height:536px}
.md-head{flex:none;display:flex;align-items:center;gap:12px;height:56px;padding:0 20px;border-bottom:1px solid var(--md-line);font-size:15px;color:var(--md-ink);white-space:nowrap}
.md-head.md-wait{color:var(--md-muted)}
.md-siren{flex:none;position:relative;top:-1px}
.md-ell::after{content:"";display:inline-block;width:1.2em;text-align:left;animation:md-ell 1.2s steps(1) infinite}
@keyframes md-ell{0%{content:""}25%{content:"."}50%{content:".."}75%{content:"..."}}
.md-table{margin-right:284px}
.md-tr{display:grid;grid-template-columns:118px minmax(0,1fr);align-items:center;height:53px;border-bottom:1px solid var(--md-line);font-size:14px}
.md-tr>span{min-width:0;padding:0 14px 0 20px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.md-tr>span:first-child{align-self:stretch;display:flex;align-items:center;border-right:1px solid var(--md-line);color:var(--md-muted)}
.md-tv{font-family:var(--md-mono);font-size:12.5px;color:var(--md-ink)}
.md-skel{display:block;height:8px;border-radius:4px;background:linear-gradient(90deg,#ececec 30%,#f6f6f6 50%,#ececec 70%) 0 0/300% 100%;animation:md-skel 1.4s linear infinite}
@keyframes md-skel{from{background-position:100% 0}to{background-position:0 0}}
.md-steps{display:flex;margin:28px 284px 0 0;padding:0 20px;font-family:var(--md-mono);font-size:11px;color:#b5b5b5}
.md-steps span{flex:1;display:flex;align-items:center;gap:8px;white-space:nowrap;transition:color .3s}
.md-steps span:last-child{flex:none}
.md-steps span:not(:last-child)::after{content:"";flex:1;min-width:8px;height:1px;margin:0 10px;background:linear-gradient(90deg,var(--md-accent) 50%,var(--md-line) 50%) 100% 0/200% 100%;transition:background-position .6s var(--md-ease)}
.md-steps i{width:7px;height:7px;flex-shrink:0;border-radius:50%;background:#dcdcdc;transition:background .3s,box-shadow .3s}
.md-steps .md-on{color:var(--md-ink)}
.md-steps .md-on i{background:var(--md-accent);box-shadow:0 0 0 3px var(--md-heat-12)}
.md-steps .md-on.md-next::after{background-position:0 0}
/* the campus picker: Alert's segmented tabs, a dot in each school's color */
.md-campus{margin:auto 284px 0 0;padding:24px 16px 22px 20px}
.md-campus-k{display:block;margin:0 0 8px 2px;font-family:var(--md-mono);font-size:10px;letter-spacing:.08em;text-transform:uppercase;color:#a8a8a8}
.md-seg{display:inline-flex;max-width:100%;padding:3px;border-radius:10px;background:var(--md-a4)}
.md-sc{position:relative;display:flex;align-items:center;gap:6px;height:30px;padding:0 8px;border-radius:8px;font-size:12.5px;color:#8c8c8c;white-space:nowrap;cursor:pointer;transition:color .2s,background .2s,box-shadow .2s}
.md-sc input{position:absolute;inset:0;width:100%;height:100%;margin:0;opacity:0;cursor:pointer}
.md-sc i{width:8px;height:8px;flex:none;border-radius:50%;box-shadow:inset 0 0 0 1px rgba(0,0,0,.08)}
.md-sc.md-on{background:#fff;color:var(--md-ink);box-shadow:0 1px 2px rgba(0,0,0,.08),0 0 0 .5px rgba(0,0,0,.05)}
.md-sc:has(input:focus-visible){outline:2px solid var(--md-accent);outline-offset:1px}
@media (hover:hover){.md-sc:not(.md-on):hover{color:#555}}
/* the phone, sized to its box: --md-sh is the screen's height */
.md-phone{position:absolute;z-index:2;top:0;bottom:0;right:24px;width:244px;display:grid;place-items:center;container-type:size;
--md-sh:min(calc(100cqh - 48px),calc((100cqw - 14px) / .45995))}
/* ----- the phone body */
.md-ip-stage{--md-sw:calc(var(--md-sh) * .45995);--md-b:7px;--md-pw:calc(var(--md-sw) + 2 * var(--md-b));--md-phh:calc(var(--md-sh) + 2 * var(--md-b));--md-pr:calc(var(--md-sw) * .13 + var(--md-b));
--md-apple:-apple-system,BlinkMacSystemFont,"SF Pro Text","SF Pro Icons",var(--font-inter,Inter),"Helvetica Neue",Helvetica,Arial,sans-serif;
position:relative;perspective:1900px;perspective-origin:50% 42%}
/* leans toward the cursor: --md-hx/--md-hy run -1 to 1 across the phone, --md-hl is 0 to 1 while hovered */
.md-ip-tilt{transform-style:preserve-3d;transform:translateZ(calc(var(--md-hl,0) * 16px)) rotateX(calc(var(--md-hy,0) * -6deg)) rotateY(calc(var(--md-hx,0) * 9deg));will-change:transform}
.md-ip{position:relative;width:var(--md-pw);height:var(--md-phh);border-radius:var(--md-pr);background:#fff;box-shadow:0 0 0 1px rgba(0,0,0,.06),0 40px 48px -20px rgba(0,0,0,.06),0 32px 32px -20px rgba(0,0,0,.05),0 16px 24px -12px rgba(0,0,0,.05),0 2px 4px rgba(0,0,0,.03);transition:transform .7s var(--md-ease),opacity .6s var(--md-ease)}
.md-ip.md-enter{opacity:0;transform:translateY(24px) scale(.97)}
.md-ip-screen{position:absolute;inset:var(--md-b);border-radius:calc(var(--md-pr) - var(--md-b));overflow:hidden;background:#000;box-shadow:0 0 0 1px var(--md-line);--md-k:.56}
/* ----- the screen, in iPhone points */
.md-ios{position:absolute;left:0;top:0;width:402px;height:874px;overflow:hidden;transform:scale(var(--md-k));transform-origin:0 0;font-family:var(--md-apple);font-weight:400;color:#fff;-webkit-font-smoothing:antialiased;letter-spacing:-.4px;user-select:none;-webkit-user-select:none}
.md-ios svg{display:block}
.md-ios img{display:block;max-width:none}
.md-wall{position:absolute;inset:-60px;filter:blur(34px)}
.md-wall.md-wall-in{animation:md-fade .8s ease both}
.md-thread{position:absolute;left:0;right:0;top:0;padding:146px 0 0;transform:translate3d(0,var(--md-ty),0);transition:--md-ty .55s cubic-bezier(.25,.85,.3,1);will-change:transform}
.md-start{display:flex;flex-direction:column;align-items:center;gap:1px;margin:2px 0 0;font-size:11.5px;line-height:14px;letter-spacing:-.1px;color:rgba(255,255,255,.72)}
.md-start span:last-child{display:flex;align-items:center;gap:3px}
.md-start svg{fill:currentColor}
.md-row{position:relative;display:flex;flex-direction:column;margin-top:10px;padding:0 19px}
.md-row.md-me{align-items:flex-end}
.md-row.md-them{align-items:flex-start}
.md-row.md-grouped{margin-top:2px}
.md-wrap{position:relative;display:flex;flex-direction:column;align-items:flex-start;max-width:282px}
.md-row.md-me .md-wrap{align-items:flex-end}
.md-b{position:relative;padding:7px 13px 8px;border-radius:19px;font-size:17px;line-height:22px;overflow-wrap:break-word}
/* Alert's bubbles are Liquid Glass over the wall: blurred, richer and dimmed so white text stays legible */
.md-b.md-them,.md-typing{-webkit-backdrop-filter:blur(14px) saturate(1.5) brightness(.3);backdrop-filter:blur(14px) saturate(1.5) brightness(.3);box-shadow:inset 0 1px .5px rgba(255,255,255,.34),inset 0 -.5px .5px rgba(255,255,255,.14),inset 0 0 0 .6px rgba(255,255,255,.16),0 4px 14px rgba(0,0,0,.1)}
.md-b.md-them{background:rgba(8,8,14,.3)}
.md-b.md-tail.md-them::before,.md-typing::before,.md-typing::after{-webkit-backdrop-filter:inherit;backdrop-filter:inherit}
/* blue bubbles share one gradient pinned to the screen: lighter near the top, deeper near the bottom */
.md-b.md-me{background:linear-gradient(180deg,#52a8ff 0,#2f8cf7 437px,#1474ec 874px) 0 calc(-1 * (var(--md-by,0px) + var(--md-ty,0px))) / 100% 874px no-repeat}
.md-b.md-tail.md-them{border-bottom-left-radius:0}
.md-b.md-tail.md-me{border-bottom-right-radius:0}
.md-b.md-tail::before{content:"";position:absolute;bottom:0;width:9px;height:14px;background:inherit}
.md-b.md-tail.md-them::before{left:-9px;-webkit-mask:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 14'%3E%3Cpath d='M9 0C9 6 7.6 10.9 1.1 13.5Q0 14 1.5 14H9Z'/%3E%3C/svg%3E") 0 0/100% 100% no-repeat;mask:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 14'%3E%3Cpath d='M9 0C9 6 7.6 10.9 1.1 13.5Q0 14 1.5 14H9Z'/%3E%3C/svg%3E") 0 0/100% 100% no-repeat}
.md-b.md-tail.md-me::before{right:-9px;background-position:0 calc(-1 * (var(--md-by,0px) + var(--md-bh,0px) - 14px + var(--md-ty,0px)));-webkit-mask:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 14'%3E%3Cpath d='M0 0C0 6 1.4 10.9 7.9 13.5Q9 14 7.5 14H0Z'/%3E%3C/svg%3E") 0 0/100% 100% no-repeat;mask:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 14'%3E%3Cpath d='M0 0C0 6 1.4 10.9 7.9 13.5Q9 14 7.5 14H0Z'/%3E%3C/svg%3E") 0 0/100% 100% no-repeat}
.md-photo{display:block;width:222px;height:148px;object-fit:cover;border-radius:18px}
.md-stamp{margin:16px 0 6px;text-align:center;font-size:12px;line-height:16px;letter-spacing:-.1px;color:rgba(255,255,255,.86)}
.md-stamp b,.md-receipt b{font-weight:600}
.md-receipt{margin:4px 2px 0 0;font-size:12px;line-height:16px;letter-spacing:-.1px;color:rgba(255,255,255,.86);animation:md-fade .4s ease both}
.md-typing{position:relative;display:flex;align-items:center;justify-content:center;gap:5px;width:62px;height:38px;border-radius:19px;background:rgba(8,8,14,.3)}
.md-typing::before,.md-typing::after{content:"";position:absolute;border-radius:50%;background:inherit}
.md-typing::before{width:12px;height:12px;left:-5px;bottom:-4px}
.md-typing::after{width:6px;height:6px;left:-10px;bottom:-9px}
.md-typing i{width:8.5px;height:8.5px;border-radius:50%;background:#fff;opacity:.38;animation:md-dot 1.35s ease-in-out infinite}
.md-typing i:nth-child(2){animation-delay:.18s}
.md-typing i:nth-child(3){animation-delay:.36s}
/* top: status bar and the iOS 26 header, with the scroll-edge blur under them */
.md-top{position:absolute;left:0;right:0;top:0;height:150px;z-index:10;pointer-events:none}
.md-top-fx{position:absolute;left:0;right:0;top:0;height:170px;background:linear-gradient(180deg,rgba(44,0,0,.62),rgba(44,0,0,.2) 62%,rgba(44,0,0,0));-webkit-backdrop-filter:blur(9px);backdrop-filter:blur(9px);-webkit-mask-image:linear-gradient(180deg,#000 52%,transparent);mask-image:linear-gradient(180deg,#000 52%,transparent)}
.md-sb{position:absolute;left:0;right:0;top:0;z-index:35;height:54px;color:#fff;pointer-events:none}
.md-sb-time{position:absolute;left:4px;width:138px;top:18px;display:flex;flex-direction:column;align-items:center;font-size:17px;line-height:22px;font-weight:600;letter-spacing:-.4px}
.md-sb-right{position:absolute;left:264px;right:6px;top:21px;display:flex;align-items:center;justify-content:center;gap:6px;fill:currentColor}
.md-glass{background:rgba(255,255,255,.13);-webkit-backdrop-filter:blur(10px) saturate(1.2) brightness(1.04);backdrop-filter:blur(10px) saturate(1.2) brightness(1.04);box-shadow:inset 0 1px .5px rgba(255,255,255,.5),inset 0 -.5px .5px rgba(255,255,255,.22),inset 0 0 0 .6px rgba(255,255,255,.2),0 6px 18px rgba(30,0,0,.16)}
.md-btn{position:absolute;top:58px;width:44px;height:44px;display:grid;place-items:center;border-radius:50%;color:#fff}
.md-back{left:16px}
.md-video{right:16px}
.md-avatar{position:absolute;left:50%;top:52px;width:58px;height:58px;margin-left:-29px;overflow:hidden;border-radius:50%;box-shadow:0 3px 10px rgba(0,0,0,.3),0 0 0 .5px rgba(255,255,255,.18)}
.md-name{position:absolute;left:50%;top:110px;height:30px;display:flex;align-items:center;gap:5px;padding:0 11px 0 14px;border-radius:15px;transform:translateX(-50%);font-size:16.5px;font-weight:600;letter-spacing:-.35px;white-space:nowrap}
.md-name svg{opacity:.55;margin-top:1px}
/* bottom: + button and the text field, which ride up with the keyboard */
.md-dock{position:absolute;left:0;right:0;bottom:28px;z-index:12;display:flex;align-items:flex-end;gap:9px;padding:0 14px 0 15px;transition:transform .5s cubic-bezier(.2,.86,.25,1)}
.md-dock.md-up{transform:translateY(-324px)}
.md-bot-fx{position:absolute;left:0;right:0;top:-12px;bottom:-40px;z-index:-1;background:linear-gradient(180deg,rgba(110,50,0,0),rgba(110,50,0,.2));-webkit-backdrop-filter:blur(5px);backdrop-filter:blur(5px);-webkit-mask-image:linear-gradient(180deg,transparent,#000 22px);mask-image:linear-gradient(180deg,transparent,#000 22px)}
.md-dock.md-up .md-bot-fx{bottom:0}
.md-dock .md-glass{background:rgba(24,18,18,.26);-webkit-backdrop-filter:blur(12px) saturate(.75);backdrop-filter:blur(12px) saturate(.75)}
.md-plus{flex:none;width:38px;height:38px;display:grid;place-items:center;border-radius:50%;color:#fff;transition:transform .18s,background .18s}
.md-plus.md-on{transform:scale(.86);background:rgba(255,255,255,.3)}
.md-field{position:relative;flex:1;min-height:38px;display:flex;flex-direction:column;justify-content:center;padding:8px 44px 8px 15px;border-radius:19px;transition:border-radius .2s}
.md-field.md-att{padding-top:9px;border-radius:21px}
.md-input{display:block;min-height:22px;white-space:pre-wrap;font-size:17px;line-height:22px;color:#fff;overflow-wrap:break-word}
.md-ph{color:rgba(255,255,255,.6)}
.md-caret{display:inline-block;width:2px;height:21px;margin:0 1px -4px;border-radius:1px;background:#3b97ff;animation:md-blink 1.05s steps(1) infinite}
.md-caret.md-typing-now{animation:none}
.md-wave{position:absolute;right:12px;bottom:9.5px;color:rgba(255,255,255,.8)}
.md-send{position:absolute;right:4.5px;bottom:4.5px;width:29px;height:29px;display:grid;place-items:center;border-radius:50%;background:#2a8bf7;color:#fff;animation:md-pop .28s cubic-bezier(.3,1.5,.6,1) both;transition:transform .14s,filter .14s}
.md-send.md-on{transform:scale(.84);filter:brightness(.88)}
.md-attach{position:relative;display:block;width:128px;height:86px;margin:0 0 8px -6px;border-radius:14px;overflow:hidden;animation:md-pop .35s cubic-bezier(.3,1.3,.6,1) both;transform-origin:0 100%}
.md-attach img{display:block;width:100%;height:100%;object-fit:cover}
.md-attach i{position:absolute;top:6px;right:6px;width:20px;height:20px;display:grid;place-items:center;border-radius:50%;background:rgba(0,0,0,.55);color:#fff}
.md-home{position:absolute;left:50%;bottom:8px;z-index:40;width:140px;height:5px;margin-left:-70px;border-radius:3px;background:#fff}
/* keyboard: iOS 26, dark */
.md-kb{position:absolute;left:0;right:0;bottom:0;z-index:11;height:344px;border-radius:32px 32px 0 0;background:rgba(43,43,45,.97);-webkit-backdrop-filter:blur(26px);backdrop-filter:blur(26px);box-shadow:inset 0 .6px 0 rgba(255,255,255,.14);transform:translateY(102%);transition:transform .5s cubic-bezier(.2,.86,.25,1);letter-spacing:0}
.md-kb.md-up{transform:none}
.md-kb-pred{position:absolute;left:8px;right:6px;top:6px;height:40px;display:grid;grid-template-columns:1fr 1fr 1fr 34px;align-items:center}
.md-kb-pred span{position:relative;padding:0 6px;text-align:center;font-size:16.5px;letter-spacing:-.3px;color:#fff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.md-kb-pred span+span::before{content:"";position:absolute;left:0;top:50%;width:1px;height:20px;margin-top:-10px;background:rgba(255,255,255,.17)}
.md-kb-pred .md-kb-fmt{display:grid;place-items:center;padding:0;color:rgba(255,255,255,.85)}
.md-kb-k{position:absolute;height:43px;display:grid;place-items:center;border-radius:9px;background:#5f5f62;color:#fff;font-size:23px;line-height:1;font-weight:400}
.md-kb-k.md-fn{font-size:16.5px}
.md-kb-k.md-on{background:#8a8a8e}
.md-kb-k.md-lit{background:#f2f2f4;color:#1c1c1e}
.md-kb-pop{position:absolute;z-index:2;pointer-events:none;filter:drop-shadow(0 2px 5px rgba(0,0,0,.35))}
.md-kb-pop svg{position:absolute;inset:0;fill:#727276}
.md-kb-pop b{position:absolute;top:8px;transform:translateX(-50%);font-size:37px;font-weight:400;line-height:1}
.md-kb-globe,.md-kb-mic{position:absolute;bottom:32px;color:rgba(255,255,255,.9)}
.md-kb-globe{left:24px}
.md-kb-mic{right:24px}
@keyframes md-dot{0%,55%,100%{opacity:.38}25%{opacity:.95}}
@keyframes md-fade{from{opacity:0}to{opacity:1}}
@keyframes md-pop{from{opacity:0;transform:scale(.6)}to{opacity:1;transform:none}}
@keyframes md-blink{0%{opacity:1}50%{opacity:0}}
@keyframes md-sendin{from{opacity:.4;transform:translateY(60px) scale(.94)}35%{opacity:1}to{opacity:1;transform:none}}
/* Alert's bubbles pop in by scale only: an opacity animation would cut their frosted glass off from the wall */
@keyframes md-popin{from{transform:scale(.6)}70%{transform:scale(1.02)}to{transform:none}}
.md-row.md-me.md-fresh .md-wrap{transform-origin:100% 100%;animation:md-sendin .5s cubic-bezier(.2,.9,.25,1) both}
.md-row.md-them.md-fresh .md-wrap,.md-row.md-them.md-fresh .md-typing{transform-origin:0 100%;animation:md-popin .38s cubic-bezier(.25,.8,.35,1) backwards}
.md-stamp.md-fresh{animation:md-fade .45s ease both}
/* cipher text (Scramble): real letters keep their place, cipher is drawn over them */
.md-cr{position:relative}
.md-c{position:relative}
.md-c.md-x{opacity:0}
.md-c.md-h{-webkit-text-fill-color:transparent}
.md-c.md-h::after{content:attr(data-g);content:attr(data-g) / "";position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);white-space:pre;-webkit-text-fill-color:currentColor;opacity:var(--md-o,.42);pointer-events:none}
@media (prefers-reduced-motion:no-preference){.md-cv[data-s=idle] .md-c{opacity:0}}
/* narrower: the cases become a row of chips over the demo */
@container md (max-width:819.98px){
.md-cases{grid-template-columns:minmax(0,1fr)}
.md-list{flex-direction:row;gap:8px;padding:16px 24px;overflow-x:auto;scrollbar-width:none;border-right:none;border-bottom:1px solid var(--md-line);
-webkit-mask-image:linear-gradient(90deg,transparent,#000 20px,#000 calc(100% - 20px),transparent);mask-image:linear-gradient(90deg,transparent,#000 20px,#000 calc(100% - 20px),transparent)}
.md-list::-webkit-scrollbar{display:none}
.md-case{flex:none;min-height:0;border:none;border-radius:99px;overflow:hidden;background:#fff;box-shadow:inset 0 0 0 1px var(--md-line)}
.md-case:last-child{border:none}
.md-case.md-on{background:var(--md-heat-4);box-shadow:inset 0 0 0 1px var(--md-heat-12)}
.md-case.md-on::before{display:none}
.md-tab{flex-direction:row;align-items:center;height:38px;padding:0 15px 0 12px}
.md-tab:focus-visible{border-radius:99px}
.md-case-t{font-size:14px;line-height:20px;gap:7px}
.md-case-t svg{width:17px;height:17px}
.md-case-d{display:none}
.md-bar{top:auto;left:14px;right:14px;bottom:4px;width:auto;height:2px;border-radius:1px;transform-origin:left;transform:scaleX(var(--md-p,0))}
.md-head{padding:0 24px}
.md-tr>span:first-child{padding-left:24px}
.md-tr{grid-template-columns:122px minmax(0,1fr)}
.md-steps{padding:0 24px}
.md-campus{padding:28px 16px 24px 24px}
}
/* phone width: the phone stacks between the status line and the table */
@container md (max-width:599.98px){
.md-view{min-height:0}
.md-head{height:52px;padding:0 24px}
.md-phone{position:relative;inset:auto;width:auto;height:556px;border-bottom:1px solid var(--md-line)}
.md-table{margin:0}
.md-tr{grid-template-columns:120px minmax(0,1fr);height:46px;font-size:13.5px}
.md-tr>span{padding:0 24px 0 14px}
.md-tr>span:first-child{padding:0 12px 0 24px}
.md-steps{margin:22px 0 0;padding:0 24px}
.md-steps span:not(:last-child)::after{margin:0 8px}
.md-campus{margin:0;padding:26px 24px 24px}
.md-seg{display:flex;width:100%}
.md-sc{flex:1 1 auto;justify-content:center;min-width:0;padding:0 6px;gap:6px;font-size:12.5px}
}
.md[data-off] *,.md[data-off] *::before,.md[data-off] *::after{animation-play-state:paused!important}
@media (prefers-reduced-motion:reduce){
.md,.md *,.md *::before,.md *::after{animation-duration:.01ms!important;animation-iteration-count:1!important;transition-duration:.01ms!important}
}
`;
export default function MessagesDemo({
photoUrl = "/images/play/alert-leaking-sink.jpg",
school: initialSchool = "stanford",
}: {
photoUrl?: string;
school?: SchoolId;
}) {
const uid = useId();
const rootRef = useRef<HTMLDivElement>(null);
const listRef = useRef<HTMLDivElement>(null);
const phoneRef = useRef<HTMLDivElement>(null);
const barRef = useRef<HTMLSpanElement | null>(null);
const [still, setStill] = useState(() => typeof window !== "undefined" && reduced());
const clock = useRef<Clock>({ i: 0, ms: still ? TIMES[0].settle : 0, run: 0 });
const [view, setView] = useState<Clock>(clock.current);
const shownKey = useRef("");
const holds = useRef({ hover: false, touch: false, key: false });
const held = useRef(false);
const pointer = useRef("mouse");
const [inView, setInView] = useState(false);
const [visible, setVisible] = useState(true);
const [phoneSeen, setPhoneSeen] = useState(false);
const [schoolId, setSchoolId] = useState<SchoolId>(initialSchool);
const school = SCHOOLS.find((s) => s.id === schoolId) ?? SCHOOLS[0];
const live = inView && visible;
const paintBar = () => {
const s = clock.current;
barRef.current?.style.setProperty("--md-p", String(held.current ? 1 : Math.min(1, s.ms / TIMES[s.i].end)));
};
const setHold = (k: keyof typeof holds.current, on: boolean) => {
holds.current[k] = on;
const h = holds.current;
held.current = h.hover || h.touch || h.key;
paintBar();
};
const show = (s: Clock) => {
clock.current = s;
const k = viewKey(s);
if (k !== shownKey.current) { shownKey.current = k; setView(s); }
paintBar();
};
const pick = (k: number) => {
if (pointer.current === "touch" || pointer.current === "pen") setHold("touch", true);
show({ i: k, ms: still ? TIMES[k].settle : 0, run: clock.current.run + 1 });
};
useEffect(() => {
const el = rootRef.current;
if (!el) return;
const io = new IntersectionObserver(([e]) => {
setInView(e.intersectionRatio >= 0.25);
el.toggleAttribute("data-off", !e.isIntersecting);
}, { threshold: [0, 0.25] });
io.observe(el);
const vis = () => setVisible(document.visibilityState === "visible");
vis();
document.addEventListener("visibilitychange", vis);
const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
const rm = () => setStill(mq.matches);
rm();
mq.addEventListener("change", rm);
const off = (e: PointerEvent) => {
if (holds.current.touch && !el.contains(e.target as Node)) setHold("touch", false);
};
document.addEventListener("pointerdown", off);
return () => {
io.disconnect();
document.removeEventListener("visibilitychange", vis);
mq.removeEventListener("change", rm);
document.removeEventListener("pointerdown", off);
};
}, []);
useEffect(() => {
const el = phoneRef.current;
if (!el) return;
const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) { setPhoneSeen(true); io.disconnect(); } }, { threshold: 0.25 });
io.observe(el);
return () => io.disconnect();
}, []);
useEffect(() => {
if (!live) return;
let raf = 0, last = performance.now();
const loop = (now: number) => {
const dt = Math.min(64, now - last);
last = now;
const s = clock.current, T = TIMES[s.i];
const m = Math.max(s.ms + dt, still ? T.settle : 0);
if (!held.current && m >= T.end) {
const i = (s.i + 1) % CASES.length;
show({ i, ms: still ? TIMES[i].settle : 0, run: s.run + 1 });
} else show({ i: s.i, ms: m, run: s.run });
raf = requestAnimationFrame(loop);
};
raf = requestAnimationFrame(loop);
return () => cancelAnimationFrame(raf);
}, [live, still]);
useEffect(() => {
const list = listRef.current;
const el = list?.children[view.i] as HTMLElement | undefined;
if (!list || !el || list.scrollWidth <= list.clientWidth) return;
const pad = 24, left = el.offsetLeft - pad, right = el.offsetLeft + el.offsetWidth + pad - list.clientWidth;
const behavior = reduced() ? "auto" : "smooth";
if (list.scrollLeft > left) list.scrollTo({ left, behavior });
else if (list.scrollLeft < right) list.scrollTo({ left: right, behavior });
}, [view.i]);
const onTabKey = (e: ReactKeyboardEvent<HTMLDivElement>) => {
const k = e.key, i = view.i, n = CASES.length;
const next = k === "ArrowDown" || k === "ArrowRight" ? i + 1 : k === "ArrowUp" || k === "ArrowLeft" ? i - 1 : k === "Home" ? 0 : k === "End" ? n - 1 : null;
if (next === null) return;
e.preventDefault();
const j = (next + n) % n;
pointer.current = "keyboard";
show({ i: j, ms: still ? TIMES[j].settle : 0, run: clock.current.run + 1 });
listRef.current?.querySelectorAll<HTMLButtonElement>("[role=tab]")[j]?.focus();
};
const { i, ms } = view;
const c = CASES[i];
const T = TIMES[i];
const sent = ms >= T.sent, replied = ms >= T.reply;
const snap = snapshot(T.tl, ms);
const status = replied ? "done" : sent ? "work" : "wait";
const tabId = (k: number) => `${uid}-tab-${k}`;
const panelId = `${uid}-panel`;
const steps: [string, boolean][] = [["Received", sent], [c.did, ms >= T.row(c.rows.length - 1)], ["Texted you", replied]];
return (
<div
ref={rootRef}
className="md"
style={{ "--md-accent": school.primary } as CSSProperties}
onPointerDown={(e) => { pointer.current = e.pointerType; }}
onPointerEnter={(e) => { if (e.pointerType === "mouse") setHold("hover", true); }}
onPointerLeave={(e) => { if (e.pointerType === "mouse") setHold("hover", false); }}
onFocus={(e) => { if (e.target.matches(":focus-visible")) setHold("key", true); }}
onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setHold("key", false); }}
>
<style href="play-messages-demo" precedence="play">{css}</style>
<div className="md-cases">
<div ref={listRef} className="md-list" role="tablist" aria-label="Use cases" onKeyDown={onTabKey}>
{CASES.map((x, k) => {
const on = k === i;
return (
<div key={x.id} className={`md-case${on ? " md-on" : ""}`}>
{on && <span className="md-bar" ref={(el) => { barRef.current = el; paintBar(); }} />}
<button type="button" role="tab" id={tabId(k)} aria-selected={on} aria-controls={panelId} tabIndex={on ? 0 : -1} className="md-tab" onClick={() => pick(k)}>
<span className="md-case-t"><Icon name={x.icon} />{x.title}</span>
<span className="md-case-d">{x.desc}</span>
</button>
</div>
);
})}
</div>
<div className="md-view" id={panelId} role="tabpanel" aria-labelledby={tabId(i)}>
<div className={`md-head md-${status}`}>
<Siren size={20} live={live} still={still} />
{status === "done"
? <Scramble key={`d${i}.${view.run}`} text={DONE[i]} duration={0.6} />
: <span>{status === "work" ? "Alert is working on it" : "Waiting for a text"}<i className="md-ell" aria-hidden="true" /></span>}
</div>
<div className="md-phone" ref={phoneRef}>
<Phone shown={phoneSeen}>
<CaseScreen key={`${i}.${view.run}`} snap={snap} sig={signature(snap)} school={school} photo={photoUrl} />
</Phone>
</div>
<p className="md-sr">Student texts: “{c.sent}”{c.photo ? ", with a photo" : ""}. Alert replies: “{c.reply}”</p>
<div className="md-table">
{c.rows.map(([k, v], n) => (
<div key={k} className="md-tr">
<span>{k}</span>
<span className="md-tv">
{ms >= T.row(n)
? <Scramble key={`${i}.${view.run}.${k}`} text={v} duration={0.45} />
: <i className="md-skel" aria-hidden="true" style={{ width: `${38 + ((n * 23) % 40)}%` }} />}
</span>
</div>
))}
</div>
<div className="md-steps" aria-hidden="true">
{steps.map(([l, on], k) => (
<span key={k} className={`${on ? "md-on" : ""}${steps[k + 1]?.[1] ? " md-next" : ""}`}><i />{l}</span>
))}
</div>
<div className="md-campus">
<span className="md-campus-k" id={`${uid}-campus`}>Campus</span>
<div className="md-seg" role="radiogroup" aria-labelledby={`${uid}-campus`}>
{SCHOOLS.map((s) => (
<label key={s.id} className={`md-sc${s.id === school.id ? " md-on" : ""}`}>
<input
type="radio"
name={`${uid}-school`}
value={s.id}
checked={s.id === school.id}
onChange={() => { if (pointer.current === "touch" || pointer.current === "pen") setHold("touch", true); setSchoolId(s.id); }}
/>
<i style={{ background: s.primary }} aria-hidden="true" />
{s.short}
</label>
))}
</div>
</div>
</div>
</div>
</div>
);
}