@42-central/ui-react 0.22.0 → 0.23.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.
- package/dist/{chunk-DJAXXLN6.js → chunk-F2FSHVO5.js} +3 -3
- package/dist/{chunk-DJAXXLN6.js.map → chunk-F2FSHVO5.js.map} +1 -1
- package/dist/components/camera/index.js +2 -2
- package/dist/components/data-table/index.js +1 -1
- package/dist/components/geodesic/a11y.d.ts +15 -0
- package/dist/components/geodesic/a11y.d.ts.map +1 -0
- package/dist/components/geodesic/activity-badge.d.ts +5 -0
- package/dist/components/geodesic/activity-badge.d.ts.map +1 -0
- package/dist/components/geodesic/badge-frame.d.ts +60 -0
- package/dist/components/geodesic/badge-frame.d.ts.map +1 -0
- package/dist/components/geodesic/exam-badge.d.ts +5 -0
- package/dist/components/geodesic/exam-badge.d.ts.map +1 -0
- package/dist/components/geodesic/geometry.d.ts +1089 -0
- package/dist/components/geodesic/geometry.d.ts.map +1 -0
- package/dist/components/geodesic/gift-box-badge.d.ts +5 -0
- package/dist/components/geodesic/gift-box-badge.d.ts.map +1 -0
- package/dist/components/geodesic/gift-token.d.ts +23 -0
- package/dist/components/geodesic/gift-token.d.ts.map +1 -0
- package/dist/components/geodesic/glyph-slot.d.ts +8 -0
- package/dist/components/geodesic/glyph-slot.d.ts.map +1 -0
- package/dist/components/geodesic/index.d.ts +12 -0
- package/dist/components/geodesic/index.d.ts.map +1 -0
- package/dist/components/geodesic/index.js +917 -0
- package/dist/components/geodesic/index.js.map +1 -0
- package/dist/components/geodesic/module-badge.d.ts +5 -0
- package/dist/components/geodesic/module-badge.d.ts.map +1 -0
- package/dist/components/geodesic/planet.d.ts +30 -0
- package/dist/components/geodesic/planet.d.ts.map +1 -0
- package/dist/components/geodesic/project-badge.d.ts +5 -0
- package/dist/components/geodesic/project-badge.d.ts.map +1 -0
- package/dist/components/geodesic/skill-badge.d.ts +5 -0
- package/dist/components/geodesic/skill-badge.d.ts.map +1 -0
- package/dist/components/geodesic/social-badge.d.ts +8 -0
- package/dist/components/geodesic/social-badge.d.ts.map +1 -0
- package/dist/components/geodesic/status.d.ts +80 -0
- package/dist/components/geodesic/status.d.ts.map +1 -0
- package/dist/components/geodesic/styles.css +146 -0
- package/dist/components/photo-capture/index.js +2 -2
- package/package.json +2 -1
- package/src/as-child.contract.test.tsx +2 -0
- package/src/components/geodesic/a11y.ts +10 -0
- package/src/components/geodesic/activity-badge.tsx +17 -0
- package/src/components/geodesic/badge-frame.tsx +324 -0
- package/src/components/geodesic/exam-badge.tsx +17 -0
- package/src/components/geodesic/geometry.test.ts +60 -0
- package/src/components/geodesic/geometry.ts +367 -0
- package/src/components/geodesic/gift-box-badge.tsx +17 -0
- package/src/components/geodesic/gift-token.tsx +104 -0
- package/src/components/geodesic/glyph-slot.tsx +28 -0
- package/src/components/geodesic/index.ts +13 -0
- package/src/components/geodesic/module-badge.tsx +17 -0
- package/src/components/geodesic/planet.tsx +131 -0
- package/src/components/geodesic/project-badge.tsx +17 -0
- package/src/components/geodesic/skill-badge.tsx +17 -0
- package/src/components/geodesic/social-badge.tsx +21 -0
- package/src/components/geodesic/status.test.ts +66 -0
- package/src/components/geodesic/status.ts +176 -0
- package/src/components/geodesic/styles.css +146 -0
- package/src/icon-stroke.test.tsx +5 -0
|
@@ -48,6 +48,7 @@ import * as fileTrigger from "./components/file-trigger"
|
|
|
48
48
|
import * as fileUpload from "./components/file-upload"
|
|
49
49
|
import * as flex from "./components/flex"
|
|
50
50
|
import * as gallery from "./components/gallery"
|
|
51
|
+
import * as geodesic from "./components/geodesic"
|
|
51
52
|
import * as grid from "./components/grid"
|
|
52
53
|
import * as hoverCard from "./components/hover-card"
|
|
53
54
|
import * as image from "./components/image"
|
|
@@ -186,6 +187,7 @@ const modules = {
|
|
|
186
187
|
fileUpload,
|
|
187
188
|
flex,
|
|
188
189
|
gallery,
|
|
190
|
+
geodesic,
|
|
189
191
|
grid,
|
|
190
192
|
hoverCard,
|
|
191
193
|
image,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A badge's accessible name lives on its wrapper: with a label it is one image,
|
|
3
|
+
* without one it is decoration and stays out of the accessibility tree. The
|
|
4
|
+
* SVG inside is always hidden — the wrapper already speaks for it.
|
|
5
|
+
*/
|
|
6
|
+
export function imageProps(label: string | undefined) {
|
|
7
|
+
return label
|
|
8
|
+
? ({ role: "img", "aria-label": label } as const)
|
|
9
|
+
: ({ "aria-hidden": true } as const)
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type BadgeBaseProps, BadgeFrame } from "./badge-frame"
|
|
2
|
+
import { GEOMETRY } from "./geometry"
|
|
3
|
+
|
|
4
|
+
export type ActivityBadgeProps = BadgeBaseProps
|
|
5
|
+
|
|
6
|
+
/** An activity (rush, workshop, event): the octahedron in a diamond. Completed is blue — you take part in an activity, you don't validate it. */
|
|
7
|
+
export function ActivityBadge(props: ActivityBadgeProps) {
|
|
8
|
+
return (
|
|
9
|
+
<BadgeFrame
|
|
10
|
+
{...props}
|
|
11
|
+
tile={{ kind: "polygon", points: GEOMETRY.tiles.diamond }}
|
|
12
|
+
solids={[GEOMETRY.activity]}
|
|
13
|
+
doneHue="blue"
|
|
14
|
+
locked="silhouette"
|
|
15
|
+
/>
|
|
16
|
+
)
|
|
17
|
+
}
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { type CSSProperties, type SVGProps, useEffect, useId, useState } from "react"
|
|
4
|
+
import { cn } from "../../lib/cn"
|
|
5
|
+
import { props as attrs } from "../../lib/props"
|
|
6
|
+
import type { WithTestId } from "../../lib/test-id"
|
|
7
|
+
import { imageProps } from "./a11y"
|
|
8
|
+
import {
|
|
9
|
+
type BadgeStatus,
|
|
10
|
+
faceFill,
|
|
11
|
+
fillLevel,
|
|
12
|
+
type Hue,
|
|
13
|
+
hueOf,
|
|
14
|
+
liquidFadeWidth,
|
|
15
|
+
paint,
|
|
16
|
+
type Tone,
|
|
17
|
+
tint,
|
|
18
|
+
} from "./status"
|
|
19
|
+
|
|
20
|
+
/** The shape every generated solid has (see `packages/react/scripts/generate-geometry.ts`). */
|
|
21
|
+
export type SolidData = {
|
|
22
|
+
readonly facets: readonly {
|
|
23
|
+
readonly points: string
|
|
24
|
+
readonly tone: Tone
|
|
25
|
+
readonly rank: number
|
|
26
|
+
}[]
|
|
27
|
+
readonly edges: string
|
|
28
|
+
readonly hidden: string
|
|
29
|
+
readonly hull: string
|
|
30
|
+
readonly details: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type BadgeTile =
|
|
34
|
+
| { readonly kind: "circle" }
|
|
35
|
+
| { readonly kind: "polygon"; readonly points: string }
|
|
36
|
+
|
|
37
|
+
/** Props every badge shares. */
|
|
38
|
+
export type BadgeBaseProps = WithTestId & {
|
|
39
|
+
status: BadgeStatus
|
|
40
|
+
/**
|
|
41
|
+
* Completion while `in-progress`, 0–100: fills the solid from the bottom
|
|
42
|
+
* up, each face from its own base through a soft gradient, animating
|
|
43
|
+
* between values. 0–100 is drawn as 15–100% of the fill, so
|
|
44
|
+
* started work always shows light. Ignored in other statuses. Defaults to 50.
|
|
45
|
+
*/
|
|
46
|
+
progress?: number
|
|
47
|
+
/** Rendered width and height, in px. */
|
|
48
|
+
size?: number
|
|
49
|
+
/** Accessible name. Without one the badge is decorative and hidden from assistive tech. */
|
|
50
|
+
label?: string
|
|
51
|
+
className?: string
|
|
52
|
+
style?: CSSProperties
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type BadgeFrameProps = BadgeBaseProps & {
|
|
56
|
+
tile: BadgeTile
|
|
57
|
+
solids: readonly SolidData[]
|
|
58
|
+
doneHue: Hue
|
|
59
|
+
/**
|
|
60
|
+
* How a locked badge shows its solid: the module keeps its wireframe, the
|
|
61
|
+
* other shapes collapse to a dotted silhouette.
|
|
62
|
+
*/
|
|
63
|
+
locked: "wire" | "silhouette"
|
|
64
|
+
/** Dotted connectors drawn between the solids (the social badge). */
|
|
65
|
+
links?: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const VIEWBOX = "0 0 64 64"
|
|
69
|
+
const DASH = "3 2"
|
|
70
|
+
const DOT = "0.1 1.6"
|
|
71
|
+
|
|
72
|
+
function Tile({ tile, ...paint }: { tile: BadgeTile } & SVGProps<SVGCircleElement>) {
|
|
73
|
+
if (tile.kind === "circle") return <circle cx={32} cy={32} r={29} {...paint} />
|
|
74
|
+
return <polygon points={tile.points} {...(paint as SVGProps<SVGPolygonElement>)} />
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A padlock on a dark plate, set in a soft shade of the page's own body
|
|
79
|
+
* colour (brand-950 on a dark page, brand-25 on a light one) that clears
|
|
80
|
+
* the wireframe or silhouette behind it, so the lock reads on its own. It is
|
|
81
|
+
* a radial gradient rather than a blur filter: it renders the same in every
|
|
82
|
+
* browser and costs nothing to draw.
|
|
83
|
+
*/
|
|
84
|
+
function Lock({ shown }: { shown: boolean }) {
|
|
85
|
+
// useId's characters are not all valid in a `url(#…)` reference.
|
|
86
|
+
const glow = `geodesic-lock-glow-${useId().replace(/[^\w-]/g, "")}`
|
|
87
|
+
const shade = paint("brand", [950, 25])
|
|
88
|
+
// Always mounted, faded in and out, so unlocking eases instead of popping.
|
|
89
|
+
return (
|
|
90
|
+
<g className="geodesic-crossfade geodesic-lock" data-shown={shown}>
|
|
91
|
+
<defs>
|
|
92
|
+
<radialGradient id={glow}>
|
|
93
|
+
<stop offset="0" stopColor={shade} stopOpacity={1} />
|
|
94
|
+
<stop offset="0.55" stopColor={shade} stopOpacity={0.8} />
|
|
95
|
+
<stop offset="1" stopColor={shade} stopOpacity={0} />
|
|
96
|
+
</radialGradient>
|
|
97
|
+
</defs>
|
|
98
|
+
<circle cx={32} cy={33} r={15} fill={`url(#${glow})`} />
|
|
99
|
+
{/* An icon, so a 1.5px stroke at any badge size, like the slotted glyphs. */}
|
|
100
|
+
<g
|
|
101
|
+
fill="none"
|
|
102
|
+
stroke={paint("neutral", [25, 700])}
|
|
103
|
+
strokeWidth={1.5}
|
|
104
|
+
strokeLinecap="round"
|
|
105
|
+
strokeLinejoin="round"
|
|
106
|
+
>
|
|
107
|
+
<rect
|
|
108
|
+
x={27}
|
|
109
|
+
y={31}
|
|
110
|
+
width={10}
|
|
111
|
+
height={8}
|
|
112
|
+
rx={1.6}
|
|
113
|
+
fill={paint("neutral", [950, 25], 85)}
|
|
114
|
+
vectorEffect="non-scaling-stroke"
|
|
115
|
+
/>
|
|
116
|
+
{/* Filled like the body, so the wireframe behind does not show through the loop. */}
|
|
117
|
+
<path
|
|
118
|
+
d="M29 31v-2.4a3 3 0 0 1 6 0V31"
|
|
119
|
+
fill={paint("neutral", [950, 25], 85)}
|
|
120
|
+
vectorEffect="non-scaling-stroke"
|
|
121
|
+
/>
|
|
122
|
+
</g>
|
|
123
|
+
</g>
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function Solid({
|
|
128
|
+
solid,
|
|
129
|
+
status,
|
|
130
|
+
hue,
|
|
131
|
+
progress,
|
|
132
|
+
locked,
|
|
133
|
+
}: {
|
|
134
|
+
solid: SolidData
|
|
135
|
+
status: BadgeStatus
|
|
136
|
+
hue: Hue
|
|
137
|
+
progress: number
|
|
138
|
+
locked: "wire" | "silhouette"
|
|
139
|
+
}) {
|
|
140
|
+
const lit = <LiquidSolid solid={solid} status={status} hue={hue} progress={progress} />
|
|
141
|
+
if (locked === "wire") return lit
|
|
142
|
+
// The silhouette and the solid are both kept mounted and cross-faded, so
|
|
143
|
+
// locking and unlocking ease like every other status change.
|
|
144
|
+
const silhouette = status === "locked"
|
|
145
|
+
return (
|
|
146
|
+
<>
|
|
147
|
+
<polygon
|
|
148
|
+
className="geodesic-crossfade"
|
|
149
|
+
data-shown={silhouette}
|
|
150
|
+
points={solid.hull}
|
|
151
|
+
fill={paint("neutral", [900, 100], 70)}
|
|
152
|
+
stroke={paint("neutral", [400, 400])}
|
|
153
|
+
strokeWidth={0.8}
|
|
154
|
+
strokeDasharray={DOT}
|
|
155
|
+
strokeLinecap="round"
|
|
156
|
+
/>
|
|
157
|
+
<g className="geodesic-crossfade" data-shown={!silhouette}>
|
|
158
|
+
{lit}
|
|
159
|
+
</g>
|
|
160
|
+
</>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type SolidProps = { solid: SolidData; status: BadgeStatus; hue: Hue; progress: number }
|
|
165
|
+
|
|
166
|
+
/** The visible edges, and the hidden ones dotted until the badge is done. */
|
|
167
|
+
function Wires({ solid, status, hue }: Omit<SolidProps, "progress">) {
|
|
168
|
+
const done = status === "done"
|
|
169
|
+
// Done glints over its own faces, the same on either page; the bare
|
|
170
|
+
// wireframe takes one step darker on a light page, just enough to show.
|
|
171
|
+
const wire = done
|
|
172
|
+
? paint(hue, [200, 200], 50)
|
|
173
|
+
: paint(hue, status === "locked" ? [400, 400] : [200, 400])
|
|
174
|
+
return (
|
|
175
|
+
<>
|
|
176
|
+
{!done && solid.hidden && (
|
|
177
|
+
<path
|
|
178
|
+
d={solid.hidden}
|
|
179
|
+
fill="none"
|
|
180
|
+
stroke={wire}
|
|
181
|
+
strokeOpacity={0.55}
|
|
182
|
+
strokeWidth={0.5}
|
|
183
|
+
strokeDasharray={DOT}
|
|
184
|
+
strokeLinecap="round"
|
|
185
|
+
/>
|
|
186
|
+
)}
|
|
187
|
+
<path
|
|
188
|
+
d={solid.edges + solid.details}
|
|
189
|
+
fill="none"
|
|
190
|
+
stroke={wire}
|
|
191
|
+
strokeWidth={0.55}
|
|
192
|
+
strokeLinejoin="round"
|
|
193
|
+
/>
|
|
194
|
+
</>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Starts at `from` and moves to `target` on the next frame, so CSS can transition it. */
|
|
199
|
+
export function useSettled<T>(from: T, target: T) {
|
|
200
|
+
const [value, setValue] = useState(from)
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
const frame = requestAnimationFrame(() => setValue(target))
|
|
203
|
+
return () => cancelAnimationFrame(frame)
|
|
204
|
+
}, [target])
|
|
205
|
+
return value
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* How progress fills a solid: a registered CSS property (`--geodesic-level`),
|
|
210
|
+
* transitioned by CSS, is how many faces along the bottom-up order are lit,
|
|
211
|
+
* and each face fills from its own base to its top through a soft gradient
|
|
212
|
+
* mask. One gradient rule serves every face: a CSS mask on an SVG shape is
|
|
213
|
+
* laid out on that shape's bounding box (see styles.css). The first frame
|
|
214
|
+
* renders nothing lit, so a badge fills up when it appears too.
|
|
215
|
+
*/
|
|
216
|
+
function LiquidSolid({ solid, status, hue, progress }: SolidProps) {
|
|
217
|
+
const faces = solid.facets.length
|
|
218
|
+
const fade = liquidFadeWidth(faces)
|
|
219
|
+
const level = useSettled(0, fillLevel(status, progress, faces, fade))
|
|
220
|
+
return (
|
|
221
|
+
<g
|
|
222
|
+
className="geodesic-solid-smooth"
|
|
223
|
+
style={{ "--geodesic-level": level, "--geodesic-fade": fade } as CSSProperties}
|
|
224
|
+
>
|
|
225
|
+
{solid.facets.map((facet) => (
|
|
226
|
+
<polygon
|
|
227
|
+
key={facet.points}
|
|
228
|
+
className="geodesic-facet-liquid"
|
|
229
|
+
points={facet.points}
|
|
230
|
+
fill={faceFill(hue, status, facet.tone)}
|
|
231
|
+
style={{ "--geodesic-rank": facet.rank } as CSSProperties}
|
|
232
|
+
/>
|
|
233
|
+
))}
|
|
234
|
+
<Wires solid={solid} status={status} hue={hue} />
|
|
235
|
+
</g>
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Draws any statused badge: a tile, one or more solids, and what the status
|
|
241
|
+
* adds (lock, glow). The per-shape components only pick the geometry
|
|
242
|
+
* and the done hue.
|
|
243
|
+
*/
|
|
244
|
+
export function BadgeFrame({
|
|
245
|
+
status,
|
|
246
|
+
size = 64,
|
|
247
|
+
label,
|
|
248
|
+
className,
|
|
249
|
+
style,
|
|
250
|
+
testId,
|
|
251
|
+
tile,
|
|
252
|
+
solids,
|
|
253
|
+
doneHue,
|
|
254
|
+
progress = 50,
|
|
255
|
+
locked,
|
|
256
|
+
links,
|
|
257
|
+
}: BadgeFrameProps) {
|
|
258
|
+
const hue = hueOf(status, doneHue)
|
|
259
|
+
const done = status === "done"
|
|
260
|
+
const tileFill =
|
|
261
|
+
// Locked is a fully transparent tint, not `none`, so the fill can ease in.
|
|
262
|
+
paint(hue, [500, 500], status === "locked" ? 0 : done ? 20 : status === "available" ? 22 : 16)
|
|
263
|
+
const tileStroke =
|
|
264
|
+
status === "locked"
|
|
265
|
+
? paint("neutral", [500, 400])
|
|
266
|
+
: done
|
|
267
|
+
? paint(hue, [400, 400])
|
|
268
|
+
: paint(hue, [400, 400], 65)
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<span
|
|
272
|
+
className={cn("geodesic-badge", className)}
|
|
273
|
+
{...imageProps(label)}
|
|
274
|
+
{...attrs({ "data-testid": testId })}
|
|
275
|
+
style={{
|
|
276
|
+
display: "inline-grid",
|
|
277
|
+
width: size,
|
|
278
|
+
height: size,
|
|
279
|
+
flex: "none",
|
|
280
|
+
filter: done ? `drop-shadow(0 0 ${size * 0.09}px ${tint(hue, 500, 55)})` : undefined,
|
|
281
|
+
...style,
|
|
282
|
+
}}
|
|
283
|
+
>
|
|
284
|
+
<svg
|
|
285
|
+
aria-hidden="true"
|
|
286
|
+
viewBox={VIEWBOX}
|
|
287
|
+
width={size}
|
|
288
|
+
height={size}
|
|
289
|
+
style={{ gridArea: "1 / 1", display: "block" }}
|
|
290
|
+
>
|
|
291
|
+
<Tile
|
|
292
|
+
tile={tile}
|
|
293
|
+
fill={tileFill}
|
|
294
|
+
stroke={tileStroke}
|
|
295
|
+
strokeWidth={1}
|
|
296
|
+
strokeDasharray={done ? undefined : DASH}
|
|
297
|
+
strokeLinejoin="round"
|
|
298
|
+
/>
|
|
299
|
+
{links && (
|
|
300
|
+
<path
|
|
301
|
+
d={links}
|
|
302
|
+
fill="none"
|
|
303
|
+
stroke={paint(hue, [300, 300])}
|
|
304
|
+
strokeOpacity={0.6}
|
|
305
|
+
strokeWidth={0.6}
|
|
306
|
+
strokeDasharray={DOT}
|
|
307
|
+
strokeLinecap="round"
|
|
308
|
+
/>
|
|
309
|
+
)}
|
|
310
|
+
{solids.map((solid) => (
|
|
311
|
+
<Solid
|
|
312
|
+
key={solid.hull}
|
|
313
|
+
solid={solid}
|
|
314
|
+
status={status}
|
|
315
|
+
hue={hue}
|
|
316
|
+
progress={progress}
|
|
317
|
+
locked={locked}
|
|
318
|
+
/>
|
|
319
|
+
))}
|
|
320
|
+
<Lock shown={status === "locked"} />
|
|
321
|
+
</svg>
|
|
322
|
+
</span>
|
|
323
|
+
)
|
|
324
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type BadgeBaseProps, BadgeFrame } from "./badge-frame"
|
|
2
|
+
import { GEOMETRY } from "./geometry"
|
|
3
|
+
|
|
4
|
+
export type ExamBadgeProps = BadgeBaseProps
|
|
5
|
+
|
|
6
|
+
/** An exam: the tetrahedron in a triangle. Validated, gold. */
|
|
7
|
+
export function ExamBadge(props: ExamBadgeProps) {
|
|
8
|
+
return (
|
|
9
|
+
<BadgeFrame
|
|
10
|
+
{...props}
|
|
11
|
+
tile={{ kind: "polygon", points: GEOMETRY.tiles.triangle }}
|
|
12
|
+
solids={[GEOMETRY.exam]}
|
|
13
|
+
doneHue="yellow"
|
|
14
|
+
locked="silhouette"
|
|
15
|
+
/>
|
|
16
|
+
)
|
|
17
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { GEOMETRY } from "./geometry"
|
|
3
|
+
|
|
4
|
+
const parse = (points: string) =>
|
|
5
|
+
points.split(" ").map((p) => {
|
|
6
|
+
const [x = 0, y = 0] = p.split(",").map(Number)
|
|
7
|
+
return [x, y] as const
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
const centroidY = (points: string) => {
|
|
11
|
+
const ys = parse(points).map(([, y]) => y)
|
|
12
|
+
return ys.reduce((a, b) => a + b, 0) / ys.length
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Twice the signed area; positive is clockwise on screen (y points down). */
|
|
16
|
+
const signedArea = (points: string) => {
|
|
17
|
+
const p = parse(points)
|
|
18
|
+
return p.reduce((sum, [x, y], i) => {
|
|
19
|
+
const [nx, ny] = p[(i + 1) % p.length] ?? [x, y]
|
|
20
|
+
return sum + (x * ny - nx * y)
|
|
21
|
+
}, 0)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("geodesic geometry", () => {
|
|
25
|
+
const solids = {
|
|
26
|
+
module: GEOMETRY.module,
|
|
27
|
+
project: GEOMETRY.project,
|
|
28
|
+
exam: GEOMETRY.exam,
|
|
29
|
+
activity: GEOMETRY.activity,
|
|
30
|
+
skill: GEOMETRY.skill,
|
|
31
|
+
gift: GEOMETRY.gift,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
it.each(Object.entries(solids))("%s ranks every facet once, bottom first", (_, solid) => {
|
|
35
|
+
const ranks = solid.facets.map((f) => f.rank).sort((a, b) => a - b)
|
|
36
|
+
expect(ranks).toEqual(solid.facets.map((_, i) => i))
|
|
37
|
+
const heights = [...solid.facets]
|
|
38
|
+
.sort((a, b) => a.rank - b.rank)
|
|
39
|
+
.map((facet) => centroidY(facet.points))
|
|
40
|
+
for (const [i, height] of heights.entries())
|
|
41
|
+
if (i > 0) expect(height).toBeLessThanOrEqual((heights[i - 1] ?? height) + 1e-9)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it.each(Object.entries(solids))("%s keeps only facets turned to the viewer", (_, solid) => {
|
|
45
|
+
const signs = solid.facets.map((f) => Math.sign(signedArea(f.points)))
|
|
46
|
+
expect(new Set(signs).size).toBe(1)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it("fits every solid inside the 64×64 viewBox", () => {
|
|
50
|
+
const all = [...Object.values(solids), ...GEOMETRY.social].flatMap((s) =>
|
|
51
|
+
s.facets.flatMap((f) => parse(f.points)),
|
|
52
|
+
)
|
|
53
|
+
for (const [x, y] of all) {
|
|
54
|
+
expect(x).toBeGreaterThanOrEqual(0)
|
|
55
|
+
expect(x).toBeLessThanOrEqual(64)
|
|
56
|
+
expect(y).toBeGreaterThanOrEqual(0)
|
|
57
|
+
expect(y).toBeLessThanOrEqual(64)
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
})
|