@a16n/web-sim 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/README.md +13 -2
  2. package/bin/cli.ts +255 -0
  3. package/bin/web-sim.js +39 -0
  4. package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
  5. package/overrides/@react-native-firebase/app/overrides.ts +34 -0
  6. package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
  7. package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
  8. package/overrides/expo-application/ExpoApplication.ts +81 -0
  9. package/overrides/expo-application/overrides.ts +16 -0
  10. package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
  11. package/overrides/expo-background-task/overrides.ts +9 -0
  12. package/overrides/expo-constants/ExponentConstants.ts +66 -0
  13. package/overrides/expo-constants/overrides.ts +9 -0
  14. package/overrides/expo-device/ExpoDevice.ts +104 -0
  15. package/overrides/expo-device/overrides.ts +9 -0
  16. package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
  17. package/overrides/expo-glass-effect/GlassView.tsx +95 -0
  18. package/overrides/expo-glass-effect/availability.ts +14 -0
  19. package/overrides/expo-glass-effect/overrides.ts +14 -0
  20. package/overrides/expo-haptics/Haptics.ts +67 -0
  21. package/overrides/expo-haptics/overrides.ts +5 -0
  22. package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
  23. package/overrides/expo-mail-composer/overrides.ts +9 -0
  24. package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
  25. package/overrides/expo-mesh-gradient/overrides.ts +9 -0
  26. package/overrides/expo-modules-core/overrides.ts +12 -0
  27. package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
  28. package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
  29. package/overrides/expo-navigation-bar/overrides.ts +12 -0
  30. package/overrides/expo-router/NativeStackView.tsx +74 -0
  31. package/overrides/expo-router/NativeTabsView.tsx +263 -0
  32. package/overrides/expo-router/SafeAreaProvider.ts +6 -0
  33. package/overrides/expo-router/overrides.ts +23 -0
  34. package/overrides/expo-router/statusBarRequests.ts +51 -0
  35. package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
  36. package/overrides/expo-screen-orientation/overrides.ts +21 -0
  37. package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
  38. package/overrides/expo-sharing/overrides.ts +9 -0
  39. package/overrides/expo-sms/ExpoSMS.ts +24 -0
  40. package/overrides/expo-sms/overrides.ts +9 -0
  41. package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
  42. package/overrides/expo-splash-screen/overrides.ts +5 -0
  43. package/overrides/expo-status-bar/StatusBar.tsx +44 -0
  44. package/overrides/expo-status-bar/overrides.ts +5 -0
  45. package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
  46. package/overrides/expo-store-review/overrides.ts +9 -0
  47. package/overrides/expo-symbols/SymbolView.tsx +192 -0
  48. package/overrides/expo-symbols/overrides.ts +9 -0
  49. package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
  50. package/overrides/expo-system-ui/overrides.ts +9 -0
  51. package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
  52. package/overrides/expo-task-manager/overrides.ts +9 -0
  53. package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
  54. package/overrides/expo-tracking-transparency/overrides.ts +11 -0
  55. package/overrides/expo-video/ExpoVideo.ts +16 -0
  56. package/overrides/expo-video/overrides.ts +9 -0
  57. package/overrides/lottie-react-native/LottieView.tsx +87 -0
  58. package/overrides/lottie-react-native/overrides.ts +9 -0
  59. package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
  60. package/overrides/react-native-pager-view/overrides.ts +9 -0
  61. package/overrides/react-native-reanimated/overrides.ts +28 -0
  62. package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
  63. package/overrides/react-native-safe-area-context/overrides.ts +12 -0
  64. package/overrides/react-native-share/NativeRNShare.ts +22 -0
  65. package/overrides/react-native-share/overrides.ts +12 -0
  66. package/overrides/react-native-share/platform.ts +14 -0
  67. package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
  68. package/overrides/react-native-turbo-image/overrides.ts +9 -0
  69. package/overrides/react-native-web/RefreshControl.tsx +57 -0
  70. package/overrides/react-native-web/overrides.ts +153 -0
  71. package/overrides/react-native-web/useElementLayout.ts +140 -0
  72. package/package.json +78 -4
  73. package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
  74. package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
  75. package/plugins/battery/plugin.ts +8 -0
  76. package/plugins/battery/shell.tsx +56 -0
  77. package/plugins/battery/state.ts +11 -0
  78. package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
  79. package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
  80. package/plugins/biometrics/plugin.ts +8 -0
  81. package/plugins/biometrics/shell.tsx +88 -0
  82. package/plugins/biometrics/state.ts +41 -0
  83. package/plugins/brightness/app.ts +5 -0
  84. package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
  85. package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
  86. package/plugins/brightness/plugin.ts +9 -0
  87. package/plugins/brightness/shell.tsx +55 -0
  88. package/plugins/brightness/state.ts +19 -0
  89. package/plugins/calendar/calendar.ts +299 -0
  90. package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
  91. package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
  92. package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
  93. package/plugins/calendar/plugin.ts +8 -0
  94. package/plugins/calendar/shell.tsx +123 -0
  95. package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
  96. package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
  97. package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
  98. package/plugins/camera/plugin.ts +9 -0
  99. package/plugins/camera/shell.tsx +107 -0
  100. package/plugins/camera/state.ts +61 -0
  101. package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
  102. package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
  103. package/plugins/clipboard/plugin.ts +8 -0
  104. package/plugins/clipboard/shell.tsx +122 -0
  105. package/plugins/clipboard/state.ts +93 -0
  106. package/plugins/clock/app.ts +25 -0
  107. package/plugins/clock/plugin.ts +8 -0
  108. package/plugins/clock/shell.tsx +225 -0
  109. package/plugins/clock/state.ts +12 -0
  110. package/plugins/contacts/contacts.ts +125 -0
  111. package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
  112. package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
  113. package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
  114. package/plugins/contacts/plugin.ts +8 -0
  115. package/plugins/contacts/shell.tsx +109 -0
  116. package/plugins/home-indicator/app.ts +5 -0
  117. package/plugins/home-indicator/plugin.ts +9 -0
  118. package/plugins/home-indicator/requests.ts +48 -0
  119. package/plugins/home-indicator/shell.tsx +68 -0
  120. package/plugins/home-indicator/state.ts +31 -0
  121. package/plugins/keep-awake/app.ts +5 -0
  122. package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
  123. package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
  124. package/plugins/keep-awake/plugin.ts +9 -0
  125. package/plugins/keep-awake/shell.tsx +80 -0
  126. package/plugins/keep-awake/state.ts +12 -0
  127. package/plugins/keyboard/Keyboard.tsx +354 -0
  128. package/plugins/keyboard/app.ts +90 -0
  129. package/plugins/keyboard/controller.ts +384 -0
  130. package/plugins/keyboard/fields.ts +244 -0
  131. package/plugins/keyboard/icons.tsx +283 -0
  132. package/plugins/keyboard/keyboard.css +362 -0
  133. package/plugins/keyboard/layouts/android.ts +175 -0
  134. package/plugins/keyboard/layouts/index.ts +10 -0
  135. package/plugins/keyboard/layouts/ios.ts +219 -0
  136. package/plugins/keyboard/layouts/types.ts +80 -0
  137. package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
  138. package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
  139. package/plugins/keyboard/plugin.ts +13 -0
  140. package/plugins/keyboard/shell.tsx +133 -0
  141. package/plugins/keyboard/state.ts +92 -0
  142. package/plugins/keyboard/suggestions.ts +63 -0
  143. package/plugins/keyboard/taps.ts +77 -0
  144. package/plugins/keyboard/typing.ts +112 -0
  145. package/plugins/locale/app.ts +232 -0
  146. package/plugins/locale/locale.ts +143 -0
  147. package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
  148. package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
  149. package/plugins/locale/plugin.ts +9 -0
  150. package/plugins/locale/shell.tsx +147 -0
  151. package/plugins/locale/state.ts +36 -0
  152. package/plugins/location/geo.ts +56 -0
  153. package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
  154. package/plugins/location/overrides/expo-location/overrides.ts +11 -0
  155. package/plugins/location/places.ts +38 -0
  156. package/plugins/location/plugin.ts +8 -0
  157. package/plugins/location/shell.tsx +188 -0
  158. package/plugins/location/state.ts +51 -0
  159. package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
  160. package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
  161. package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
  162. package/plugins/motion/plugin.ts +8 -0
  163. package/plugins/motion/sensors.ts +116 -0
  164. package/plugins/motion/shell.tsx +136 -0
  165. package/plugins/motion/state.ts +46 -0
  166. package/plugins/network/app.ts +400 -0
  167. package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
  168. package/plugins/network/overrides/expo-network/overrides.ts +9 -0
  169. package/plugins/network/plugin.ts +8 -0
  170. package/plugins/network/shell.tsx +67 -0
  171. package/plugins/network/state.ts +29 -0
  172. package/plugins/notifications/center.ts +255 -0
  173. package/plugins/notifications/content.ts +36 -0
  174. package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
  175. package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
  176. package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
  177. package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
  178. package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
  179. package/plugins/notifications/plugin.ts +8 -0
  180. package/plugins/notifications/shell.tsx +192 -0
  181. package/plugins/notifications/state.ts +56 -0
  182. package/plugins/notifications/tokens.ts +47 -0
  183. package/plugins/notifications/trigger.ts +107 -0
  184. package/plugins/permissions/plugin.ts +6 -0
  185. package/plugins/permissions/shell.tsx +50 -0
  186. package/plugins/permissions/state.ts +38 -0
  187. package/plugins/photos/appLibrary.ts +114 -0
  188. package/plugins/photos/library.ts +322 -0
  189. package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
  190. package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
  191. package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
  192. package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
  193. package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
  194. package/plugins/photos/plugin.ts +8 -0
  195. package/plugins/photos/samples.ts +367 -0
  196. package/plugins/photos/shell.tsx +164 -0
  197. package/plugins/photos/state.ts +88 -0
  198. package/plugins/storage/files.ts +351 -0
  199. package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
  200. package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
  201. package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
  202. package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
  203. package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
  204. package/plugins/storage/plugin.ts +8 -0
  205. package/plugins/storage/reset.ts +136 -0
  206. package/plugins/storage/secure.ts +71 -0
  207. package/plugins/storage/shell.tsx +365 -0
  208. package/plugins/storage/state.ts +13 -0
  209. package/src/client/app/appDocument.ts +113 -0
  210. package/src/client/app/appFocus.ts +97 -0
  211. package/src/client/app/console.ts +118 -0
  212. package/src/client/app/fonts.ts +59 -0
  213. package/src/client/app/main.tsx +99 -0
  214. package/src/client/app/routeContext.ts +45 -0
  215. package/src/client/app/touch.ts +640 -0
  216. package/src/client/entry.ts +20 -0
  217. package/src/client/modules.d.ts +8 -0
  218. package/src/client/react-native/app.ts +57 -0
  219. package/src/client/react-native/deep.ts +13 -0
  220. package/src/client/react-native/keyboard.tsx +192 -0
  221. package/src/client/react-native/library.ts +256 -0
  222. package/src/client/shell/Cookies.tsx +95 -0
  223. package/src/client/shell/LogDrawer.tsx +415 -0
  224. package/src/client/shell/PhotoPicker.tsx +174 -0
  225. package/src/client/shell/PluginOverlays.tsx +42 -0
  226. package/src/client/shell/PluginPanels.tsx +82 -0
  227. package/src/client/shell/ResetApp.tsx +87 -0
  228. package/src/client/shell/Resizer.tsx +99 -0
  229. package/src/client/shell/Shell.tsx +531 -0
  230. package/src/client/shell/SystemBars.tsx +376 -0
  231. package/src/client/shell/SystemCamera.tsx +181 -0
  232. package/src/client/shell/SystemOverlay.tsx +422 -0
  233. package/src/client/shell/SystemSheets.tsx +462 -0
  234. package/src/client/shell/backdrop.ts +229 -0
  235. package/src/client/shell/control.ts +91 -0
  236. package/src/client/shell/events.ts +226 -0
  237. package/src/client/shell/geometry.ts +68 -0
  238. package/src/client/shell/host.ts +492 -0
  239. package/src/client/shell/icons.tsx +34 -0
  240. package/src/client/shell/logFeed.ts +102 -0
  241. package/src/client/shell/main.tsx +22 -0
  242. package/src/client/shell/permissionCopy.ts +169 -0
  243. package/src/client/shell/plugins.ts +266 -0
  244. package/src/client/shell/popout.ts +185 -0
  245. package/src/client/shell/popover.ts +27 -0
  246. package/src/client/shell/shell.css +878 -0
  247. package/src/client/shell/statusTime.ts +31 -0
  248. package/src/client/shell/tooltips.ts +104 -0
  249. package/src/client/virtual.d.ts +21 -0
  250. package/src/devices/catalog.ts +223 -0
  251. package/src/devices/types.ts +83 -0
  252. package/src/node/appInfo.ts +186 -0
  253. package/src/node/appSource.ts +138 -0
  254. package/src/node/config.ts +180 -0
  255. package/src/node/control.ts +144 -0
  256. package/src/node/doctor.ts +199 -0
  257. package/src/node/events/cli.ts +206 -0
  258. package/src/node/events/shared.ts +238 -0
  259. package/src/node/events/store.ts +266 -0
  260. package/src/node/index.ts +5 -0
  261. package/src/node/overrides/define.ts +56 -0
  262. package/src/node/overrides/packages.ts +136 -0
  263. package/src/node/overrides/platformPatch.ts +56 -0
  264. package/src/node/overrides/plugin.ts +166 -0
  265. package/src/node/overrides/registry.ts +258 -0
  266. package/src/node/paths.ts +18 -0
  267. package/src/node/platform.ts +171 -0
  268. package/src/node/plugins.ts +98 -0
  269. package/src/node/proxy.ts +421 -0
  270. package/src/node/server.ts +253 -0
  271. package/src/plugin/index.ts +33 -0
  272. package/src/plugin/types.ts +127 -0
  273. package/src/plugin/ui.tsx +562 -0
  274. package/src/runtime/clock.ts +112 -0
  275. package/src/runtime/draw.ts +196 -0
  276. package/src/runtime/index.ts +513 -0
  277. package/src/runtime/media.ts +379 -0
  278. package/src/runtime/records.ts +76 -0
  279. package/src/runtime/timezone.ts +81 -0
@@ -0,0 +1,462 @@
1
+ import { ArrowLeftIcon, ArrowUpIcon, BluetoothIcon, BroadcastIcon, ChatCircleIcon, CopyIcon, EnvelopeIcon, FileIcon, FolderIcon, LinkIcon, NoteIcon, PaperPlaneRightIcon, PaperclipIcon, PlusIcon, PrinterIcon, StarIcon, XIcon } from '@phosphor-icons/react'
2
+ import { type ReactNode, useState } from 'react'
3
+ import { app as appInfo, insets, useSimState, type Dialog, type DialogResult, type Platform } from '../../runtime/index.ts'
4
+
5
+ type SheetDialog = Extract<Dialog, { kind: 'store-review' | 'share' | 'compose' }>
6
+
7
+ /**
8
+ * System sheets other apps put over the app: the store's rating prompt, the share sheet, and the message and mail
9
+ * compose sheets. Each in the platform's style, with generic targets in place of other apps' brands.
10
+ */
11
+ export function SystemSheet({ dialog, platform, respond }: { dialog: SheetDialog; platform: Platform; respond: (result: DialogResult) => void }) {
12
+ switch (dialog.kind) {
13
+ case 'store-review':
14
+ return platform === 'ios' ? <IosReview respond={respond} /> : <PlayReview respond={respond} />
15
+ case 'share':
16
+ return platform === 'ios' ? <IosShare dialog={dialog} respond={respond} /> : <AndroidShare dialog={dialog} respond={respond} />
17
+ case 'compose':
18
+ if (dialog.channel === 'sms') {
19
+ return platform === 'ios' ? <IosMessage dialog={dialog} respond={respond} /> : <AndroidMessage dialog={dialog} respond={respond} />
20
+ }
21
+ return platform === 'ios' ? <IosMail dialog={dialog} respond={respond} /> : <AndroidMail dialog={dialog} respond={respond} />
22
+ }
23
+ }
24
+
25
+ function AppIcon({ size, radius }: { size: number; radius: number }) {
26
+ const icon = appInfo().icon
27
+ return icon ? (
28
+ <img className="ws-sheet-app-icon" src={icon} alt="" style={{ width: size, height: size, borderRadius: radius }} />
29
+ ) : (
30
+ <span className="ws-sheet-app-icon" style={{ width: size, height: size, borderRadius: radius }} />
31
+ )
32
+ }
33
+
34
+ function Stars({ value, onChange, size }: { value: number; onChange: (stars: number) => void; size: number }) {
35
+ return (
36
+ <div className="ws-review-stars">
37
+ {[1, 2, 3, 4, 5].map((stars) => {
38
+ const label = stars === 1 ? '1 star' : `${stars} stars`
39
+ return (
40
+ <button key={stars} type="button" aria-label={label} onClick={() => onChange(stars)}>
41
+ <StarIcon size={size} weight={stars <= value ? 'fill' : 'regular'} />
42
+ </button>
43
+ )
44
+ })}
45
+ </div>
46
+ )
47
+ }
48
+
49
+ /** SKStoreReviewController's prompt: Not Now until a star is tapped, then Cancel and Submit. */
50
+ function IosReview({ respond }: { respond: (result: DialogResult) => void }) {
51
+ const [stars, setStars] = useState(0)
52
+ return (
53
+ <div className="ws-scrim ws-scrim-center">
54
+ <div className="ws-ios-alert ws-review-ios" role="alertdialog">
55
+ <div className="ws-ios-alert-text">
56
+ <AppIcon size={60} radius={13} />
57
+ <div className="ws-ios-alert-title">Enjoying {appInfo().name}?</div>
58
+ <div className="ws-ios-alert-message">Tap a star to rate it on the App Store.</div>
59
+ </div>
60
+ <Stars value={stars} onChange={setStars} size={30} />
61
+ <div className="ws-ios-alert-buttons">
62
+ {stars === 0 ? (
63
+ <button type="button" className="ws-ios-alert-button" onClick={() => respond({ button: 0 })}>
64
+ Not Now
65
+ </button>
66
+ ) : (
67
+ <>
68
+ <button type="button" className="ws-ios-alert-button" onClick={() => respond({ button: 0 })}>
69
+ Cancel
70
+ </button>
71
+ <button type="button" className="ws-ios-alert-button ws-bold" onClick={() => respond({ button: 1, text: String(stars) })}>
72
+ Submit
73
+ </button>
74
+ </>
75
+ )}
76
+ </div>
77
+ </div>
78
+ </div>
79
+ )
80
+ }
81
+
82
+ /** Google Play's in-app review sheet. */
83
+ function PlayReview({ respond }: { respond: (result: DialogResult) => void }) {
84
+ const [stars, setStars] = useState(0)
85
+ const [review, setReview] = useState('')
86
+ return (
87
+ <div className="ws-scrim ws-scrim-bottom" onClick={() => respond({ button: 0 })}>
88
+ <div className="ws-m3-sheet ws-review-play" onClick={(event) => event.stopPropagation()}>
89
+ <div className="ws-review-play-app">
90
+ <AppIcon size={40} radius={10} />
91
+ <span>{appInfo().name}</span>
92
+ </div>
93
+ <div className="ws-m3-sheet-title">Rate this app</div>
94
+ <div className="ws-m3-message">Tell others what you think</div>
95
+ <Stars value={stars} onChange={setStars} size={36} />
96
+ {stars > 0 && (
97
+ <textarea
98
+ className="ws-m3-input ws-review-play-text"
99
+ placeholder="Describe your experience (optional)"
100
+ value={review}
101
+ onChange={(event) => setReview(event.target.value)}
102
+ />
103
+ )}
104
+ <div className="ws-m3-actions">
105
+ <button type="button" className="ws-m3-button" onClick={() => respond({ button: 0 })}>
106
+ Not now
107
+ </button>
108
+ <button type="button" className="ws-m3-button ws-m3-filled" disabled={stars === 0} onClick={() => respond({ button: 1, text: String(stars) })}>
109
+ Submit
110
+ </button>
111
+ </div>
112
+ </div>
113
+ </div>
114
+ )
115
+ }
116
+
117
+ type Target = { label: string; icon: ReactNode; color: string }
118
+
119
+ const IOS_TARGETS: Target[] = [
120
+ { label: 'Nearby', icon: <BroadcastIcon size={28} />, color: '#2f9bff' },
121
+ { label: 'Messages', icon: <ChatCircleIcon size={28} />, color: '#34c759' },
122
+ { label: 'Mail', icon: <EnvelopeIcon size={28} />, color: '#1a8cff' },
123
+ { label: 'Notes', icon: <NoteIcon size={28} />, color: '#f5c400' },
124
+ { label: 'Files', icon: <FolderIcon size={28} />, color: '#1e7cf2' },
125
+ ]
126
+
127
+ const IOS_ACTIONS: Target[] = [
128
+ { label: 'Copy', icon: <CopyIcon size={20} />, color: '' },
129
+ { label: 'Save to Files', icon: <FolderIcon size={20} />, color: '' },
130
+ { label: 'Print', icon: <PrinterIcon size={20} />, color: '' },
131
+ ]
132
+
133
+ const ANDROID_TARGETS: Target[] = [
134
+ { label: 'Messages', icon: <ChatCircleIcon size={24} />, color: '#1a73e8' },
135
+ { label: 'Email', icon: <EnvelopeIcon size={24} />, color: '#d93025' },
136
+ { label: 'Files', icon: <FolderIcon size={24} />, color: '#188038' },
137
+ { label: 'Nearby', icon: <BroadcastIcon size={24} />, color: '#5f6368' },
138
+ { label: 'Bluetooth', icon: <BluetoothIcon size={24} />, color: '#1967d2' },
139
+ { label: 'Notes', icon: <NoteIcon size={24} />, color: '#f9ab00' },
140
+ ]
141
+
142
+ /** What is shared, as the sheet's header names it: a file's name, or a link's host. */
143
+ function describe(dialog: Extract<Dialog, { kind: 'share' }>): { name: string; detail: string; link: boolean } {
144
+ try {
145
+ const url = new URL(dialog.url)
146
+ if (url.protocol === 'http:' || url.protocol === 'https:') {
147
+ return { name: url.host, detail: dialog.url, link: true }
148
+ }
149
+ } catch {
150
+ // A bare path.
151
+ }
152
+ const name = decodeURIComponent(dialog.url.split(/[/\\]/).pop() || dialog.url)
153
+ const extension = name.includes('.') ? name.split('.').pop()!.toUpperCase() : ''
154
+ return { name, detail: dialog.mimeType ?? (extension ? `${extension} file` : 'File'), link: false }
155
+ }
156
+
157
+ function copy(text: string): void {
158
+ void navigator.clipboard?.writeText(text).catch(() => undefined)
159
+ }
160
+
161
+ function IosShare({ dialog, respond }: { dialog: Extract<Dialog, { kind: 'share' }>; respond: (result: DialogResult) => void }) {
162
+ const what = describe(dialog)
163
+ const pick = (index: number) => respond({ button: index })
164
+ return (
165
+ <div className="ws-scrim ws-scrim-bottom" onClick={() => respond({ button: -1 })}>
166
+ <div className="ws-share-ios" onClick={(event) => event.stopPropagation()}>
167
+ <div className="ws-share-ios-header">
168
+ <span className="ws-share-thumb">{what.link ? <LinkIcon size={20} /> : <FileIcon size={20} />}</span>
169
+ <span className="ws-share-what">
170
+ <span className="ws-share-name">{what.name}</span>
171
+ <span className="ws-share-detail">{what.detail}</span>
172
+ </span>
173
+ <button type="button" className="ws-share-close" aria-label="Close" onClick={() => respond({ button: -1 })}>
174
+ <XIcon size={15} weight="bold" />
175
+ </button>
176
+ </div>
177
+ <div className="ws-share-ios-targets">
178
+ {IOS_TARGETS.map((target, index) => (
179
+ <button key={target.label} type="button" className="ws-share-target" onClick={() => pick(index)}>
180
+ <span className="ws-share-target-icon" style={{ background: target.color }}>
181
+ {target.icon}
182
+ </span>
183
+ <span>{target.label}</span>
184
+ </button>
185
+ ))}
186
+ </div>
187
+ <div className="ws-share-ios-actions">
188
+ {IOS_ACTIONS.map((action, index) => (
189
+ <button
190
+ key={action.label}
191
+ type="button"
192
+ onClick={() => {
193
+ if (action.label === 'Copy') {
194
+ copy(dialog.url)
195
+ }
196
+ pick(IOS_TARGETS.length + index)
197
+ }}
198
+ >
199
+ <span>{action.label}</span>
200
+ {action.icon}
201
+ </button>
202
+ ))}
203
+ </div>
204
+ </div>
205
+ </div>
206
+ )
207
+ }
208
+
209
+ function AndroidShare({ dialog, respond }: { dialog: Extract<Dialog, { kind: 'share' }>; respond: (result: DialogResult) => void }) {
210
+ const what = describe(dialog)
211
+ return (
212
+ <div className="ws-scrim ws-scrim-bottom" onClick={() => respond({ button: -1 })}>
213
+ <div className="ws-m3-sheet ws-share-android" onClick={(event) => event.stopPropagation()}>
214
+ <div className="ws-m3-sheet-handle" />
215
+ {dialog.title && <div className="ws-share-android-title">{dialog.title}</div>}
216
+ <div className="ws-share-android-preview">
217
+ <span className="ws-share-thumb">{what.link ? <LinkIcon size={20} /> : <FileIcon size={20} />}</span>
218
+ <span className="ws-share-what">
219
+ <span className="ws-share-name">{what.name}</span>
220
+ <span className="ws-share-detail">{what.detail}</span>
221
+ </span>
222
+ <button
223
+ type="button"
224
+ className="ws-share-copy"
225
+ aria-label="Copy"
226
+ onClick={() => {
227
+ copy(dialog.url)
228
+ respond({ button: ANDROID_TARGETS.length })
229
+ }}
230
+ >
231
+ <CopyIcon size={18} />
232
+ </button>
233
+ </div>
234
+ <div className="ws-share-android-apps">
235
+ {ANDROID_TARGETS.map((target, index) => (
236
+ <button key={target.label} type="button" className="ws-share-target" onClick={() => respond({ button: index })}>
237
+ <span className="ws-share-target-icon" style={{ background: target.color }}>
238
+ {target.icon}
239
+ </span>
240
+ <span>{target.label}</span>
241
+ </button>
242
+ ))}
243
+ </div>
244
+ </div>
245
+ </div>
246
+ )
247
+ }
248
+
249
+ type Compose = Extract<Dialog, { kind: 'compose' }>
250
+
251
+ function Recipients({ list }: { list: string[] }) {
252
+ return (
253
+ <span className="ws-compose-recipients">
254
+ {list.map((recipient, index) => (
255
+ <span key={index} className="ws-compose-chip">
256
+ {recipient}
257
+ </span>
258
+ ))}
259
+ </span>
260
+ )
261
+ }
262
+
263
+ function fileName(uri: string): string {
264
+ return decodeURIComponent(uri.split(/[/\\]/).pop() || uri)
265
+ }
266
+
267
+ function Attachments({ list }: { list?: string[] }) {
268
+ if (list === undefined || list.length === 0) {
269
+ return null
270
+ }
271
+ return (
272
+ <div className="ws-compose-attachments">
273
+ {list.map((uri, index) => (
274
+ <span key={index} className="ws-compose-attachment">
275
+ <PaperclipIcon size={13} />
276
+ {fileName(uri)}
277
+ </span>
278
+ ))}
279
+ </div>
280
+ )
281
+ }
282
+
283
+ /** Text as it shows in the compose sheet: HTML bodies without their tags. */
284
+ function plain(body: string | undefined): string {
285
+ if (body === undefined) {
286
+ return ''
287
+ }
288
+ if (!/<[a-z][\s\S]*>/i.test(body)) {
289
+ return body
290
+ }
291
+ const parsed = new DOMParser().parseFromString(body, 'text/html')
292
+ return parsed.body.innerText ?? parsed.body.textContent ?? ''
293
+ }
294
+
295
+ /** A card sheet, as iOS presents a compose view controller: below the status bar, the app peeking behind. */
296
+ function useCardTop(): number {
297
+ const state = useSimState()
298
+ return insets(state).top + 10
299
+ }
300
+
301
+ function IosMessage({ dialog, respond }: { dialog: Compose; respond: (result: DialogResult) => void }) {
302
+ const [body, setBody] = useState(dialog.body ?? '')
303
+ const top = useCardTop()
304
+ return (
305
+ <div className="ws-scrim ws-scrim-bottom">
306
+ <div className="ws-compose ws-compose-ios" style={{ top }}>
307
+ <div className="ws-compose-ios-nav">
308
+ <span />
309
+ <span className="ws-compose-ios-title">New Message</span>
310
+ <button type="button" className="ws-compose-ios-link" onClick={() => respond({ button: 0 })}>
311
+ Cancel
312
+ </button>
313
+ </div>
314
+ <div className="ws-compose-row">
315
+ <span className="ws-compose-label">To:</span>
316
+ <Recipients list={dialog.recipients} />
317
+ </div>
318
+ <div className="ws-compose-thread" />
319
+ <Attachments list={dialog.attachments} />
320
+ <div className="ws-compose-ios-bar">
321
+ <span className="ws-compose-ios-plus">
322
+ <PlusIcon size={18} />
323
+ </span>
324
+ <span className="ws-compose-ios-field">
325
+ <textarea rows={1} placeholder="Text Message" value={body} onChange={(event) => setBody(event.target.value)} />
326
+ <button type="button" className="ws-compose-ios-send" aria-label="Send" disabled={body.trim() === '' && !dialog.attachments?.length} onClick={() => respond({ button: 1 })}>
327
+ <ArrowUpIcon size={16} weight="bold" />
328
+ </button>
329
+ </span>
330
+ </div>
331
+ </div>
332
+ </div>
333
+ )
334
+ }
335
+
336
+ function IosMail({ dialog, respond }: { dialog: Compose; respond: (result: DialogResult) => void }) {
337
+ const [subject, setSubject] = useState(dialog.subject ?? '')
338
+ const [body, setBody] = useState(plain(dialog.body))
339
+ const [closing, setClosing] = useState(false)
340
+ const top = useCardTop()
341
+ const copies = [...(dialog.ccRecipients ?? []), ...(dialog.bccRecipients ?? [])]
342
+ return (
343
+ <div className="ws-scrim ws-scrim-bottom">
344
+ <div className="ws-compose ws-compose-ios" style={{ top }}>
345
+ <div className="ws-compose-ios-nav">
346
+ <button type="button" className="ws-compose-ios-link" onClick={() => setClosing(true)}>
347
+ Cancel
348
+ </button>
349
+ <span className="ws-compose-ios-title">{subject || 'New Message'}</span>
350
+ <button type="button" className="ws-compose-ios-send ws-compose-ios-send-large" aria-label="Send" disabled={dialog.recipients.length === 0} onClick={() => respond({ button: 1 })}>
351
+ <ArrowUpIcon size={18} weight="bold" />
352
+ </button>
353
+ </div>
354
+ <div className="ws-compose-row">
355
+ <span className="ws-compose-label">To:</span>
356
+ <Recipients list={dialog.recipients} />
357
+ </div>
358
+ <div className="ws-compose-row">
359
+ <span className="ws-compose-label">Cc/Bcc:</span>
360
+ <Recipients list={copies} />
361
+ </div>
362
+ <div className="ws-compose-row">
363
+ <span className="ws-compose-label">Subject:</span>
364
+ <input className="ws-compose-input" value={subject} onChange={(event) => setSubject(event.target.value)} />
365
+ </div>
366
+ <textarea className="ws-compose-body" value={body} onChange={(event) => setBody(event.target.value)} />
367
+ <Attachments list={dialog.attachments} />
368
+ {closing && (
369
+ <div className="ws-scrim ws-scrim-bottom" onClick={() => setClosing(false)}>
370
+ <div className="ws-ios-sheet" onClick={(event) => event.stopPropagation()}>
371
+ <div className="ws-ios-sheet-group">
372
+ <button type="button" className="ws-ios-sheet-button ws-destructive" onClick={() => respond({ button: 0 })}>
373
+ Delete Draft
374
+ </button>
375
+ <button type="button" className="ws-ios-sheet-button" onClick={() => respond({ button: 2 })}>
376
+ Save Draft
377
+ </button>
378
+ </div>
379
+ <button type="button" className="ws-ios-sheet-group ws-ios-sheet-button ws-bold" onClick={() => setClosing(false)}>
380
+ Cancel
381
+ </button>
382
+ </div>
383
+ </div>
384
+ )}
385
+ </div>
386
+ </div>
387
+ )
388
+ }
389
+
390
+ /** Android hands composing to another app, full screen under the status bar. */
391
+ function AndroidScreen({ children }: { children: ReactNode }) {
392
+ const state = useSimState()
393
+ const { top, bottom } = insets(state)
394
+ return (
395
+ <div className="ws-compose ws-compose-android" style={{ paddingTop: top, paddingBottom: bottom }}>
396
+ {children}
397
+ </div>
398
+ )
399
+ }
400
+
401
+ function AndroidMessage({ dialog, respond }: { dialog: Compose; respond: (result: DialogResult) => void }) {
402
+ const [body, setBody] = useState(dialog.body ?? '')
403
+ return (
404
+ <AndroidScreen>
405
+ <div className="ws-compose-android-bar">
406
+ <button type="button" className="ws-compose-android-icon" aria-label="Back" onClick={() => respond({ button: 0 })}>
407
+ <ArrowLeftIcon size={22} />
408
+ </button>
409
+ <span className="ws-compose-android-title">{dialog.recipients.join(', ')}</span>
410
+ </div>
411
+ <div className="ws-compose-thread" />
412
+ <Attachments list={dialog.attachments} />
413
+ <div className="ws-compose-android-input">
414
+ <span className="ws-compose-android-field">
415
+ <textarea rows={1} placeholder="Text message" value={body} onChange={(event) => setBody(event.target.value)} />
416
+ </span>
417
+ <button type="button" className="ws-compose-android-send" aria-label="Send" disabled={body.trim() === ''} onClick={() => respond({ button: 1 })}>
418
+ <PaperPlaneRightIcon size={20} />
419
+ </button>
420
+ </div>
421
+ </AndroidScreen>
422
+ )
423
+ }
424
+
425
+ function AndroidMail({ dialog, respond }: { dialog: Compose; respond: (result: DialogResult) => void }) {
426
+ const [subject, setSubject] = useState(dialog.subject ?? '')
427
+ const [body, setBody] = useState(plain(dialog.body))
428
+ return (
429
+ <AndroidScreen>
430
+ <div className="ws-compose-android-bar">
431
+ <button type="button" className="ws-compose-android-icon" aria-label="Close" onClick={() => respond({ button: 0 })}>
432
+ <XIcon size={22} />
433
+ </button>
434
+ <span className="ws-compose-android-title">Compose</span>
435
+ <button type="button" className="ws-compose-android-icon" aria-label="Send" disabled={dialog.recipients.length === 0} onClick={() => respond({ button: 1 })}>
436
+ <PaperPlaneRightIcon size={20} />
437
+ </button>
438
+ </div>
439
+ <div className="ws-compose-row">
440
+ <span className="ws-compose-label">To</span>
441
+ <Recipients list={dialog.recipients} />
442
+ </div>
443
+ {(dialog.ccRecipients?.length ?? 0) > 0 && (
444
+ <div className="ws-compose-row">
445
+ <span className="ws-compose-label">Cc</span>
446
+ <Recipients list={dialog.ccRecipients!} />
447
+ </div>
448
+ )}
449
+ {(dialog.bccRecipients?.length ?? 0) > 0 && (
450
+ <div className="ws-compose-row">
451
+ <span className="ws-compose-label">Bcc</span>
452
+ <Recipients list={dialog.bccRecipients!} />
453
+ </div>
454
+ )}
455
+ <div className="ws-compose-row">
456
+ <input className="ws-compose-input" placeholder="Subject" value={subject} onChange={(event) => setSubject(event.target.value)} />
457
+ </div>
458
+ <textarea className="ws-compose-body" placeholder="Compose email" value={body} onChange={(event) => setBody(event.target.value)} />
459
+ <Attachments list={dialog.attachments} />
460
+ </AndroidScreen>
461
+ )
462
+ }
@@ -0,0 +1,229 @@
1
+ import { type RefObject, useEffect, useState } from 'react'
2
+
3
+ /** How often the content under a bar is looked at again. */
4
+ const SAMPLE_MS = 300
5
+
6
+ /**
7
+ * Relative luminance (0 to 1) past which the content counts as light. There are two, so content near the middle
8
+ * doesn't flip the indicator back and forth.
9
+ */
10
+ const TO_DARK = 0.25
11
+ const TO_LIGHT = 0.15
12
+
13
+ type Rgba = [r: number, g: number, b: number, a: number]
14
+
15
+ /** Where to look, as fractions of the target's box: across its middle, unless the caller says. */
16
+ const ACROSS: ReadonlyArray<readonly [x: number, y: number]> = [
17
+ [0.2, 0.5],
18
+ [0.5, 0.5],
19
+ [0.8, 0.5],
20
+ ]
21
+
22
+ /**
23
+ * The colour iOS gives the home indicator, and the status bar in its default style: dark over light content, light
24
+ * over dark, as the content under it changes. Reads what is drawn at `points` of `target` a few times a second: the shell's own UI over the app
25
+ * (the keyboard, sheets), then the app's elements, then the screen. Each layer's background colour counts as far as it
26
+ * is opaque; an image counts by its average colour once it has been read. Content it can't read (a cross-origin image,
27
+ * a canvas) leaves the colour as it was.
28
+ *
29
+ * `null` when there is nothing to follow (not enabled, or nothing read yet), so the caller falls back to the
30
+ * appearance's colour.
31
+ */
32
+ export function useBackdropTone(
33
+ target: RefObject<HTMLElement | null>,
34
+ enabled: boolean,
35
+ base: string,
36
+ points: ReadonlyArray<readonly [x: number, y: number]> = ACROSS,
37
+ ): 'light' | 'dark' | null {
38
+ const [tone, setTone] = useState<'light' | 'dark' | null>(null)
39
+ useEffect(() => {
40
+ if (!enabled) {
41
+ setTone(null)
42
+ return
43
+ }
44
+ let current: 'light' | 'dark' | null = null
45
+ const sample = () => {
46
+ const element = target.current
47
+ if (element === null) {
48
+ return
49
+ }
50
+ const rect = element.getBoundingClientRect()
51
+ if (rect.width === 0) {
52
+ return
53
+ }
54
+ const values = points
55
+ .map(([x, y]) => luminanceAt(element.ownerDocument, rect.left + rect.width * x, rect.top + rect.height * y, base)).filter((value): value is number => value !== null)
56
+ if (values.length === 0) {
57
+ return
58
+ }
59
+ const luminance = values.reduce((sum, value) => sum + value, 0) / values.length
60
+ // Content colour: dark on light content.
61
+ const next = current === null ? (luminance > (TO_DARK + TO_LIGHT) / 2 ? 'dark' : 'light') : current === 'light' ? (luminance > TO_DARK ? 'dark' : 'light') : luminance < TO_LIGHT ? 'light' : 'dark'
62
+ if (next !== current) {
63
+ current = next
64
+ setTone(next)
65
+ }
66
+ }
67
+ sample()
68
+ const timer = setInterval(sample, SAMPLE_MS)
69
+ return () => clearInterval(timer)
70
+ }, [target, enabled, base, points])
71
+ return tone
72
+ }
73
+
74
+ /** The luminance of what is drawn at a point of `doc`, through the app's frame, over `base`; null if unreadable. */
75
+ function luminanceAt(doc: Document, x: number, y: number, base: string): number | null {
76
+ const layers: Rgba[] = []
77
+ if (!collect(doc, x, y, layers)) {
78
+ return null
79
+ }
80
+ layers.push(parseColor(base) ?? [255, 255, 255, 1])
81
+ // Front to back: each layer shows through what is left uncovered above it.
82
+ let left = 1
83
+ const color = [0, 0, 0]
84
+ for (const [r, g, b, a] of layers) {
85
+ const share = left * a
86
+ color[0] += r * share
87
+ color[1] += g * share
88
+ color[2] += b * share
89
+ left -= share
90
+ if (left < 0.02) {
91
+ break
92
+ }
93
+ }
94
+ return luminance(color[0], color[1], color[2])
95
+ }
96
+
97
+ /** Adds the colours drawn at a point, topmost first, until one is opaque. False when something can't be read. */
98
+ function collect(doc: Document, x: number, y: number, layers: Rgba[]): boolean {
99
+ for (const element of doc.elementsFromPoint(x, y)) {
100
+ if (element instanceof (doc.defaultView?.HTMLIFrameElement ?? HTMLIFrameElement)) {
101
+ let inner: Document | null = null
102
+ try {
103
+ inner = element.contentDocument
104
+ } catch {
105
+ return false
106
+ }
107
+ if (inner === null) {
108
+ return false
109
+ }
110
+ const rect = element.getBoundingClientRect()
111
+ const scale = element.offsetWidth > 0 ? rect.width / element.offsetWidth : 1
112
+ if (!collect(inner, (x - rect.left) / scale, (y - rect.top) / scale, layers)) {
113
+ return false
114
+ }
115
+ if (opaque(layers)) {
116
+ return true
117
+ }
118
+ continue
119
+ }
120
+ const style = doc.defaultView?.getComputedStyle(element)
121
+ if (style === undefined) {
122
+ continue
123
+ }
124
+ const image = imageOf(element, style)
125
+ if (image !== null) {
126
+ const average = averageColor(image)
127
+ if (average === undefined) {
128
+ return false
129
+ }
130
+ if (average !== null) {
131
+ layers.push(average)
132
+ return true
133
+ }
134
+ }
135
+ const background = parseColor(style.backgroundColor)
136
+ if (background !== null && background[3] > 0) {
137
+ const opacity = Number(style.opacity)
138
+ layers.push([background[0], background[1], background[2], background[3] * (Number.isFinite(opacity) ? opacity : 1)])
139
+ if (opaque(layers)) {
140
+ return true
141
+ }
142
+ }
143
+ }
144
+ return true
145
+ }
146
+
147
+ function opaque(layers: Rgba[]): boolean {
148
+ return layers.some((layer) => layer[3] >= 0.98)
149
+ }
150
+
151
+ /** The image an element draws: an `<img>`'s source, or a CSS background image's first URL. */
152
+ function imageOf(element: Element, style: CSSStyleDeclaration): string | null {
153
+ if (element.tagName === 'IMG') {
154
+ const source = (element as HTMLImageElement).currentSrc || (element as HTMLImageElement).src
155
+ return source === '' ? null : source
156
+ }
157
+ const match = /url\(["']?([^"')]+)["']?\)/.exec(style.backgroundImage)
158
+ return match === null ? null : new URL(match[1], element.ownerDocument.baseURI).href
159
+ }
160
+
161
+ /** Images' average colours, by URL: null while loading or for one that can't be read (cross-origin). */
162
+ const averages = new Map<string, Rgba | null | 'loading'>()
163
+
164
+ /** An image's average colour; null to skip it (unreadable), undefined while it loads. */
165
+ function averageColor(url: string): Rgba | null | undefined {
166
+ const known = averages.get(url)
167
+ if (known === 'loading') {
168
+ return undefined
169
+ }
170
+ if (known !== undefined) {
171
+ return known
172
+ }
173
+ averages.set(url, 'loading')
174
+ const image = new Image()
175
+ image.crossOrigin = 'anonymous'
176
+ image.onload = () => {
177
+ try {
178
+ const canvas = document.createElement('canvas')
179
+ canvas.width = 8
180
+ canvas.height = 8
181
+ const context = canvas.getContext('2d', { willReadFrequently: true })
182
+ if (context === null) {
183
+ averages.set(url, null)
184
+ return
185
+ }
186
+ context.drawImage(image, 0, 0, 8, 8)
187
+ const data = context.getImageData(0, 0, 8, 8).data
188
+ let r = 0
189
+ let g = 0
190
+ let b = 0
191
+ let a = 0
192
+ for (let index = 0; index < data.length; index += 4) {
193
+ const alpha = data[index + 3] / 255
194
+ r += data[index] * alpha
195
+ g += data[index + 1] * alpha
196
+ b += data[index + 2] * alpha
197
+ a += alpha
198
+ }
199
+ averages.set(url, a === 0 ? null : [r / a, g / a, b / a, a / 64])
200
+ } catch {
201
+ averages.set(url, null)
202
+ }
203
+ }
204
+ image.onerror = () => averages.set(url, null)
205
+ image.src = url
206
+ return undefined
207
+ }
208
+
209
+ function parseColor(value: string): Rgba | null {
210
+ const match = /rgba?\(([^)]+)\)/.exec(value)
211
+ if (match !== null) {
212
+ const parts = match[1].split(/[\s,/]+/).filter(Boolean).map(Number)
213
+ return [parts[0], parts[1], parts[2], parts[3] ?? 1]
214
+ }
215
+ const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(value)
216
+ if (hex !== null) {
217
+ const digits = hex[1].length === 3 ? [...hex[1]].map((digit) => digit + digit).join('') : hex[1]
218
+ return [parseInt(digits.slice(0, 2), 16), parseInt(digits.slice(2, 4), 16), parseInt(digits.slice(4, 6), 16), 1]
219
+ }
220
+ return null
221
+ }
222
+
223
+ function luminance(r: number, g: number, b: number): number {
224
+ const linear = (channel: number) => {
225
+ const value = channel / 255
226
+ return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
227
+ }
228
+ return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b)
229
+ }