@jcsoftdev/ui 0.7.1 → 0.9.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/CHANGELOG.md +60 -0
- package/README.md +4 -0
- package/dist/atoms/elapsed-time/elapsed-time.d.ts +29 -0
- package/dist/atoms/elapsed-time/elapsed-time.js +65 -0
- package/dist/atoms/elapsed-time/index.d.ts +2 -0
- package/dist/atoms/elapsed-time/index.js +9 -0
- package/dist/atoms/elapsed-time/model.d.ts +17 -0
- package/dist/atoms/elapsed-time/model.js +37 -0
- package/dist/atoms/elapsed-time/ticker.d.ts +5 -0
- package/dist/atoms/elapsed-time/ticker.js +37 -0
- package/dist/atoms/index.d.ts +2 -0
- package/dist/atoms/index.js +2 -0
- package/dist/atoms/table-glyph/index.d.ts +2 -0
- package/dist/atoms/table-glyph/index.js +14 -0
- package/dist/atoms/table-glyph/layout.d.ts +38 -0
- package/dist/atoms/table-glyph/layout.js +152 -0
- package/dist/atoms/table-glyph/table-glyph.d.ts +32 -0
- package/dist/atoms/table-glyph/table-glyph.js +131 -0
- package/dist/containers/floor-plan/edit-model.d.ts +33 -0
- package/dist/containers/floor-plan/edit-model.js +82 -0
- package/dist/containers/floor-plan/floor-plan.d.ts +83 -0
- package/dist/containers/floor-plan/floor-plan.js +278 -0
- package/dist/containers/floor-plan/index.d.ts +3 -0
- package/dist/containers/floor-plan/index.js +21 -0
- package/dist/containers/floor-plan/model.d.ts +36 -0
- package/dist/containers/floor-plan/model.js +52 -0
- package/dist/containers/floor-plan/use-floor-plan-edit.d.ts +26 -0
- package/dist/containers/floor-plan/use-floor-plan-edit.js +97 -0
- package/dist/containers/index.d.ts +2 -0
- package/dist/containers/index.js +2 -0
- package/dist/containers/order-ticket/index.d.ts +2 -0
- package/dist/containers/order-ticket/index.js +16 -0
- package/dist/containers/order-ticket/model.d.ts +18 -0
- package/dist/containers/order-ticket/model.js +36 -0
- package/dist/containers/order-ticket/order-ticket.d.ts +34 -0
- package/dist/containers/order-ticket/order-ticket.js +390 -0
- package/dist/glass/crystal-map.d.ts +3 -1
- package/dist/glass/crystal-map.js +13 -2
- package/dist/i18n/strings.d.ts +104 -0
- package/dist/i18n/strings.js +62 -0
- package/dist/icons/alarm-clock.d.ts +2 -0
- package/dist/icons/alarm-clock.js +15 -0
- package/dist/icons/hourglass.d.ts +2 -0
- package/dist/icons/hourglass.js +13 -0
- package/dist/icons/index.d.ts +2 -0
- package/dist/icons/index.js +4 -0
- package/icons/svg/alarm-clock.svg +8 -0
- package/icons/svg/hourglass.svg +6 -0
- package/package.json +2 -2
- package/src/atoms/elapsed-time/elapsed-time.tsx +101 -0
- package/src/atoms/elapsed-time/index.ts +6 -0
- package/src/atoms/elapsed-time/model.ts +54 -0
- package/src/atoms/elapsed-time/ticker.ts +39 -0
- package/src/atoms/index.ts +2 -0
- package/src/atoms/table-glyph/index.ts +14 -0
- package/src/atoms/table-glyph/layout.ts +187 -0
- package/src/atoms/table-glyph/table-glyph.tsx +161 -0
- package/src/containers/floor-plan/edit-model.ts +115 -0
- package/src/containers/floor-plan/floor-plan.tsx +393 -0
- package/src/containers/floor-plan/index.ts +20 -0
- package/src/containers/floor-plan/model.ts +81 -0
- package/src/containers/floor-plan/use-floor-plan-edit.ts +135 -0
- package/src/containers/index.ts +2 -0
- package/src/containers/order-ticket/index.ts +15 -0
- package/src/containers/order-ticket/model.ts +55 -0
- package/src/containers/order-ticket/order-ticket.tsx +463 -0
- package/src/glass/crystal-map.ts +21 -3
- package/src/i18n/strings.ts +104 -0
- package/src/icons/alarm-clock.tsx +16 -0
- package/src/icons/hourglass.tsx +14 -0
- package/src/icons/index.ts +2 -0
package/dist/icons/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { type IconComponent, type IconProps, type LucideIcon } from "./icon-base.js";
|
|
2
|
+
export { AlarmClock } from "./alarm-clock.js";
|
|
2
3
|
export { AlertTriangle } from "./alert-triangle.js";
|
|
3
4
|
export { Archive } from "./archive.js";
|
|
4
5
|
export { Armchair } from "./armchair.js";
|
|
@@ -69,6 +70,7 @@ export { Funnel } from "./funnel.js";
|
|
|
69
70
|
export { GraduationCap } from "./graduation-cap.js";
|
|
70
71
|
export { GripVertical } from "./grip-vertical.js";
|
|
71
72
|
export { Heading2 } from "./heading-2.js";
|
|
73
|
+
export { Hourglass } from "./hourglass.js";
|
|
72
74
|
export { House } from "./house.js";
|
|
73
75
|
export { Inbox } from "./inbox.js";
|
|
74
76
|
export { Info } from "./info.js";
|
package/dist/icons/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {} from "./icon-base.js";
|
|
2
|
+
import { AlarmClock } from "./alarm-clock.js";
|
|
2
3
|
import { AlertTriangle } from "./alert-triangle.js";
|
|
3
4
|
import { Archive } from "./archive.js";
|
|
4
5
|
import { Armchair } from "./armchair.js";
|
|
@@ -69,6 +70,7 @@ import { Funnel } from "./funnel.js";
|
|
|
69
70
|
import { GraduationCap } from "./graduation-cap.js";
|
|
70
71
|
import { GripVertical } from "./grip-vertical.js";
|
|
71
72
|
import { Heading2 } from "./heading-2.js";
|
|
73
|
+
import { Hourglass } from "./hourglass.js";
|
|
72
74
|
import { House } from "./house.js";
|
|
73
75
|
import { Inbox } from "./inbox.js";
|
|
74
76
|
import { Info } from "./info.js";
|
|
@@ -150,6 +152,7 @@ import { X } from "./x.js";
|
|
|
150
152
|
import { XCircle } from "./xcircle.js";
|
|
151
153
|
import { XOctagon } from "./xoctagon.js";
|
|
152
154
|
export {
|
|
155
|
+
AlarmClock,
|
|
153
156
|
AlertTriangle,
|
|
154
157
|
Archive,
|
|
155
158
|
Armchair,
|
|
@@ -220,6 +223,7 @@ export {
|
|
|
220
223
|
GraduationCap,
|
|
221
224
|
GripVertical,
|
|
222
225
|
Heading2,
|
|
226
|
+
Hourglass,
|
|
223
227
|
House,
|
|
224
228
|
Inbox,
|
|
225
229
|
Info,
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
2
|
+
<circle cx="12" cy="13" r="8" />
|
|
3
|
+
<path d="M12 9v4l2 2" />
|
|
4
|
+
<path d="M5 3 2 6" />
|
|
5
|
+
<path d="m22 6-3-3" />
|
|
6
|
+
<path d="M6.38 18.7 4 21" />
|
|
7
|
+
<path d="M17.64 18.67 20 21" />
|
|
8
|
+
</svg>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
2
|
+
<path d="M5 22h14" />
|
|
3
|
+
<path d="M5 2h14" />
|
|
4
|
+
<path d="M17 22v-4.172a2 2 0 0 0-.586-1.414L12 12l-4.414 4.414A2 2 0 0 0 7 17.828V22" />
|
|
5
|
+
<path d="M7 2v4.172a2 2 0 0 0 .586 1.414L12 12l4.414-4.414A2 2 0 0 0 17 6.172V2" />
|
|
6
|
+
</svg>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jcsoftdev/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Token-driven React component library: React 19, Tailwind v4.",
|
|
5
5
|
"author": "jcsoftdev",
|
|
6
6
|
"license": "MIT",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"typescript": "^5.5.4",
|
|
103
103
|
"vite": "7.3.6",
|
|
104
104
|
"vitest": "4.1.11",
|
|
105
|
-
"@jcsoftdev/ui-tokens": "^0.
|
|
105
|
+
"@jcsoftdev/ui-tokens": "^0.9.0"
|
|
106
106
|
},
|
|
107
107
|
"publishConfig": {
|
|
108
108
|
"access": "public"
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { type ComponentPropsWithoutRef, useSyncExternalStore } from "react";
|
|
4
|
+
import { useUiStrings } from "../../i18n/index.js";
|
|
5
|
+
import type { ClassNames } from "../../utils/class-names.js";
|
|
6
|
+
import { cn } from "../../utils/cn.js";
|
|
7
|
+
import { usePrefersReducedMotion } from "../../utils/reduced-motion.js";
|
|
8
|
+
import { type ElapsedTone, formatClock, isoDuration, readElapsed, toneFor } from "./model.js";
|
|
9
|
+
import { getTickerSnapshot, subscribeTicker } from "./ticker.js";
|
|
10
|
+
|
|
11
|
+
export type ElapsedTimePart = "root" | "text";
|
|
12
|
+
|
|
13
|
+
export interface ElapsedTimeProps extends Omit<ComponentPropsWithoutRef<"time">, "children"> {
|
|
14
|
+
/** Moment the timer counts from. */
|
|
15
|
+
since: Date | string;
|
|
16
|
+
/** Server clock minus the client clock in ms, so a skewed device still shows the server's elapsed time. */
|
|
17
|
+
serverOffsetMs?: number;
|
|
18
|
+
/**
|
|
19
|
+
* `mm:ss` (default) shows a ticking clock; `minutes` shows whole minutes. With reduced motion
|
|
20
|
+
* requested, `mm:ss` is shown as whole minutes so the digits stop ticking.
|
|
21
|
+
*/
|
|
22
|
+
format?: "mm:ss" | "minutes";
|
|
23
|
+
/** Minutes from which the text turns warning (counting up), or down to which it does (countdown). */
|
|
24
|
+
warnAt?: number;
|
|
25
|
+
/** Minutes from which the text turns danger (counting up), or down to which it does (countdown). A countdown past its target is always danger. */
|
|
26
|
+
dangerAt?: number;
|
|
27
|
+
/** Turns the timer into a countdown to this moment; `since` is then only the fallback reference. */
|
|
28
|
+
countdownTo?: Date | string;
|
|
29
|
+
/** Per-part classes merged with `cn`; consumer classes win. `className` still targets the root and is merged last. */
|
|
30
|
+
classNames?: ClassNames<ElapsedTimePart>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const toneClasses: Record<ElapsedTone, string> = {
|
|
34
|
+
neutral: "text-ink-2",
|
|
35
|
+
warning: "font-bold text-warning",
|
|
36
|
+
danger: "font-bold text-danger",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function fill(template: string, key: string, value: string): string {
|
|
40
|
+
return template.replace(`{${key}}`, value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A live timer in tabular numerals. Every instance shares one ticker (a single timer, aligned to
|
|
45
|
+
* the second, running only while something is mounted). It renders `<time>` with an ISO duration
|
|
46
|
+
* in `dateTime`, exposes plain text such as "12 minutes" to assistive tech and is never an
|
|
47
|
+
* `aria-live` region, so it does not interrupt anyone.
|
|
48
|
+
*/
|
|
49
|
+
export function ElapsedTime({
|
|
50
|
+
since,
|
|
51
|
+
serverOffsetMs,
|
|
52
|
+
format = "mm:ss",
|
|
53
|
+
warnAt,
|
|
54
|
+
dangerAt,
|
|
55
|
+
countdownTo,
|
|
56
|
+
className,
|
|
57
|
+
classNames,
|
|
58
|
+
...props
|
|
59
|
+
}: ElapsedTimeProps) {
|
|
60
|
+
const strings = useUiStrings();
|
|
61
|
+
const reducedMotion = usePrefersReducedMotion();
|
|
62
|
+
const now = useSyncExternalStore(subscribeTicker, getTickerSnapshot, getTickerSnapshot);
|
|
63
|
+
const countdown = countdownTo !== undefined;
|
|
64
|
+
const reading = readElapsed({ since, now, serverOffsetMs, countdownTo });
|
|
65
|
+
const tone = toneFor(reading, countdown, warnAt, dangerAt);
|
|
66
|
+
const minutes = Math.floor(reading.seconds / 60);
|
|
67
|
+
const asClock = format === "mm:ss" && !reducedMotion;
|
|
68
|
+
const visible = asClock
|
|
69
|
+
? formatClock(reading.seconds)
|
|
70
|
+
: fill(strings.minutesShort, "count", String(minutes));
|
|
71
|
+
const spoken = fill(
|
|
72
|
+
minutes === 1 ? strings.elapsedMinutesOne : strings.elapsedMinutes,
|
|
73
|
+
"count",
|
|
74
|
+
String(minutes),
|
|
75
|
+
);
|
|
76
|
+
const announced = countdown
|
|
77
|
+
? fill(reading.over ? strings.countdownOver : strings.countdownLeft, "time", spoken)
|
|
78
|
+
: spoken;
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<time
|
|
82
|
+
dateTime={isoDuration(reading.seconds)}
|
|
83
|
+
data-tone={tone === "neutral" ? undefined : tone}
|
|
84
|
+
data-countdown={countdown ? "" : undefined}
|
|
85
|
+
className={cn(
|
|
86
|
+
"inline-block text-data tabular-nums transition-colors motion-reduce:transition-none",
|
|
87
|
+
toneClasses[tone],
|
|
88
|
+
classNames?.root,
|
|
89
|
+
className,
|
|
90
|
+
)}
|
|
91
|
+
{...props}
|
|
92
|
+
>
|
|
93
|
+
<span aria-hidden="true" suppressHydrationWarning className={classNames?.text}>
|
|
94
|
+
{visible}
|
|
95
|
+
</span>
|
|
96
|
+
<span className="sr-only" suppressHydrationWarning>
|
|
97
|
+
{announced}
|
|
98
|
+
</span>
|
|
99
|
+
</time>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type ElapsedTone = "neutral" | "warning" | "danger";
|
|
2
|
+
|
|
3
|
+
export interface ElapsedReading {
|
|
4
|
+
/** Whole seconds away from the reference: elapsed since it, or remaining until the target. */
|
|
5
|
+
seconds: number;
|
|
6
|
+
/** True once a countdown passed its target. */
|
|
7
|
+
over: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function toMs(value: Date | string | number): number {
|
|
11
|
+
return value instanceof Date ? value.getTime() : new Date(value).getTime();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function readElapsed(options: {
|
|
15
|
+
since: Date | string;
|
|
16
|
+
now: number;
|
|
17
|
+
serverOffsetMs?: number | undefined;
|
|
18
|
+
countdownTo?: Date | string | undefined;
|
|
19
|
+
}): ElapsedReading {
|
|
20
|
+
const { since, now, serverOffsetMs = 0, countdownTo } = options;
|
|
21
|
+
const clock = now + serverOffsetMs;
|
|
22
|
+
if (countdownTo !== undefined) {
|
|
23
|
+
const diff = Math.floor((toMs(countdownTo) - clock) / 1000);
|
|
24
|
+
return { seconds: Math.abs(diff), over: diff < 0 };
|
|
25
|
+
}
|
|
26
|
+
return { seconds: Math.max(0, Math.floor((clock - toMs(since)) / 1000)), over: false };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function toneFor(
|
|
30
|
+
reading: ElapsedReading,
|
|
31
|
+
countdown: boolean,
|
|
32
|
+
warnAt?: number,
|
|
33
|
+
dangerAt?: number,
|
|
34
|
+
): ElapsedTone {
|
|
35
|
+
const minutes = reading.seconds / 60;
|
|
36
|
+
if (countdown) {
|
|
37
|
+
if (reading.over || (dangerAt !== undefined && minutes <= dangerAt)) return "danger";
|
|
38
|
+
if (warnAt !== undefined && minutes <= warnAt) return "warning";
|
|
39
|
+
return "neutral";
|
|
40
|
+
}
|
|
41
|
+
if (dangerAt !== undefined && minutes >= dangerAt) return "danger";
|
|
42
|
+
if (warnAt !== undefined && minutes >= warnAt) return "warning";
|
|
43
|
+
return "neutral";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
47
|
+
|
|
48
|
+
export function formatClock(seconds: number): string {
|
|
49
|
+
return `${pad(Math.floor(seconds / 60))}:${pad(seconds % 60)}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function isoDuration(seconds: number): string {
|
|
53
|
+
return `PT${Math.floor(seconds / 60)}M${seconds % 60}S`;
|
|
54
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const listeners = new Set<() => void>();
|
|
2
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
3
|
+
|
|
4
|
+
function stamp(): number {
|
|
5
|
+
return Math.floor(Date.now() / 1000) * 1000;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function schedule() {
|
|
9
|
+
timer = setTimeout(
|
|
10
|
+
() => {
|
|
11
|
+
for (const listener of [...listeners]) listener();
|
|
12
|
+
if (listeners.size > 0) schedule();
|
|
13
|
+
else timer = undefined;
|
|
14
|
+
},
|
|
15
|
+
1000 - (Date.now() % 1000) + 5,
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Subscribes to the one-second ticker every `ElapsedTime` shares; the timer runs only while someone listens. */
|
|
20
|
+
export function subscribeTicker(listener: () => void): () => void {
|
|
21
|
+
listeners.add(listener);
|
|
22
|
+
if (timer === undefined) schedule();
|
|
23
|
+
return () => {
|
|
24
|
+
listeners.delete(listener);
|
|
25
|
+
if (listeners.size === 0 && timer !== undefined) {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
timer = undefined;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Wall clock floored to the second, stable between ticks so it is a valid external-store snapshot. */
|
|
33
|
+
export function getTickerSnapshot(): number {
|
|
34
|
+
return stamp();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function tickerListenerCount(): number {
|
|
38
|
+
return listeners.size;
|
|
39
|
+
}
|
package/src/atoms/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from "./checkbox/index.js";
|
|
|
6
6
|
export * from "./chip/index.js";
|
|
7
7
|
export * from "./divider/index.js";
|
|
8
8
|
export * from "./due-chip/index.js";
|
|
9
|
+
export * from "./elapsed-time/index.js";
|
|
9
10
|
export * from "./icon/index.js";
|
|
10
11
|
export * from "./icon-button/index.js";
|
|
11
12
|
export * from "./kbd/index.js";
|
|
@@ -19,4 +20,5 @@ export * from "./spinner/index.js";
|
|
|
19
20
|
export * from "./status-dot/index.js";
|
|
20
21
|
export * from "./status-pill/index.js";
|
|
21
22
|
export * from "./switch/index.js";
|
|
23
|
+
export * from "./table-glyph/index.js";
|
|
22
24
|
export * from "./trend-chip/index.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type Chair,
|
|
3
|
+
glyphLayout,
|
|
4
|
+
rectSideCounts,
|
|
5
|
+
squareSideCounts,
|
|
6
|
+
type TableShape,
|
|
7
|
+
} from "./layout.js";
|
|
8
|
+
export {
|
|
9
|
+
TableGlyph,
|
|
10
|
+
type TableGlyphPart,
|
|
11
|
+
type TableGlyphPattern,
|
|
12
|
+
type TableGlyphProps,
|
|
13
|
+
type TableGlyphSize,
|
|
14
|
+
} from "./table-glyph.js";
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
export type TableShape = "round" | "square" | "rect" | "bar";
|
|
2
|
+
|
|
3
|
+
export interface Chair {
|
|
4
|
+
cx: number;
|
|
5
|
+
cy: number;
|
|
6
|
+
rotate: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
circle: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface GlyphLayout {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
body: { x: number; y: number; width: number; height: number; round: boolean };
|
|
16
|
+
chairs: Chair[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const UNIT = 100;
|
|
20
|
+
export const CHAIR_DEPTH = 18;
|
|
21
|
+
export const CHAIR_WIDTH = 26;
|
|
22
|
+
export const CHAIR_GAP = 6;
|
|
23
|
+
const INSET = CHAIR_DEPTH + CHAIR_GAP;
|
|
24
|
+
|
|
25
|
+
type Side = "top" | "bottom" | "left" | "right";
|
|
26
|
+
|
|
27
|
+
function spread(count: number, length: number): number[] {
|
|
28
|
+
return Array.from({ length: count }, (_, i) => ((i + 0.5) / count) * length);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function chairWidthFor(count: number, length: number): number {
|
|
32
|
+
if (count === 0) return CHAIR_WIDTH;
|
|
33
|
+
return Math.max(6, Math.min(CHAIR_WIDTH, length / count - CHAIR_GAP));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function sideChair(side: Side, along: number, w: number, h: number, size: number): Chair {
|
|
37
|
+
const horizontal = side === "top" || side === "bottom";
|
|
38
|
+
const half = CHAIR_DEPTH / 2;
|
|
39
|
+
const cx = horizontal ? along : side === "left" ? half : w - half;
|
|
40
|
+
const cy = horizontal ? (side === "top" ? half : h - half) : along;
|
|
41
|
+
return {
|
|
42
|
+
cx,
|
|
43
|
+
cy,
|
|
44
|
+
rotate: horizontal ? 0 : 90,
|
|
45
|
+
width: size,
|
|
46
|
+
height: CHAIR_DEPTH,
|
|
47
|
+
circle: false,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Chairs per side of a square table: one per side up to four seats, then spread evenly with the remainder going top, bottom, left, right. */
|
|
52
|
+
export function squareSideCounts(seats: number): Record<Side, number> {
|
|
53
|
+
const order: Side[] = ["top", "bottom", "left", "right"];
|
|
54
|
+
const counts: Record<Side, number> = { top: 0, bottom: 0, left: 0, right: 0 };
|
|
55
|
+
const base = Math.floor(seats / 4);
|
|
56
|
+
const extra = seats % 4;
|
|
57
|
+
order.forEach((side, i) => {
|
|
58
|
+
counts[side] = base + (i < extra ? 1 : 0);
|
|
59
|
+
});
|
|
60
|
+
return counts;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Chairs of a rect table: one at each end, the rest split between the two long sides (top gets the odd one). */
|
|
64
|
+
export function rectSideCounts(seats: number): Record<Side, number> {
|
|
65
|
+
const ends = Math.min(seats, 2);
|
|
66
|
+
const rest = seats - ends;
|
|
67
|
+
return {
|
|
68
|
+
left: ends >= 1 ? 1 : 0,
|
|
69
|
+
right: ends >= 2 ? 1 : 0,
|
|
70
|
+
top: Math.ceil(rest / 2),
|
|
71
|
+
bottom: Math.floor(rest / 2),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function sidedChairs(counts: Record<Side, number>, w: number, h: number): Chair[] {
|
|
76
|
+
const out: Chair[] = [];
|
|
77
|
+
for (const side of ["top", "bottom", "left", "right"] as const) {
|
|
78
|
+
const horizontal = side === "top" || side === "bottom";
|
|
79
|
+
const length = horizontal ? w - 2 * INSET : h - 2 * INSET;
|
|
80
|
+
const offset = INSET;
|
|
81
|
+
const size = chairWidthFor(counts[side], length);
|
|
82
|
+
for (const along of spread(counts[side], length)) {
|
|
83
|
+
out.push(sideChair(side, offset + along, w, h, size));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function roundLayout(seats: number, w: number, h: number): GlyphLayout {
|
|
90
|
+
const cx = w / 2;
|
|
91
|
+
const cy = h / 2;
|
|
92
|
+
const radius = Math.max(8, Math.min(w, h) / 2 - INSET);
|
|
93
|
+
const orbit = radius + CHAIR_GAP + CHAIR_DEPTH / 2;
|
|
94
|
+
const size = chairWidthFor(seats, 2 * Math.PI * orbit);
|
|
95
|
+
const chairs = Array.from({ length: seats }, (_, i) => {
|
|
96
|
+
const angle = -Math.PI / 2 + (i * 2 * Math.PI) / seats;
|
|
97
|
+
return {
|
|
98
|
+
cx: cx + orbit * Math.cos(angle),
|
|
99
|
+
cy: cy + orbit * Math.sin(angle),
|
|
100
|
+
rotate: (angle * 180) / Math.PI + 90,
|
|
101
|
+
width: size,
|
|
102
|
+
height: CHAIR_DEPTH,
|
|
103
|
+
circle: false,
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
width: w,
|
|
108
|
+
height: h,
|
|
109
|
+
body: { x: cx - radius, y: cy - radius, width: 2 * radius, height: 2 * radius, round: true },
|
|
110
|
+
chairs,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function barLayout(seats: number, w: number, h: number): GlyphLayout {
|
|
115
|
+
const counterHeight = Math.max(14, h * 0.4);
|
|
116
|
+
const stool = Math.min(
|
|
117
|
+
CHAIR_WIDTH * 0.8,
|
|
118
|
+
h - counterHeight - CHAIR_GAP,
|
|
119
|
+
w / Math.max(seats, 1) - 4,
|
|
120
|
+
);
|
|
121
|
+
const diameter = Math.max(6, stool);
|
|
122
|
+
const chairs = spread(seats, w - 2 * CHAIR_GAP).map((x) => ({
|
|
123
|
+
cx: CHAIR_GAP + x,
|
|
124
|
+
cy: counterHeight + CHAIR_GAP + diameter / 2,
|
|
125
|
+
rotate: 0,
|
|
126
|
+
width: diameter,
|
|
127
|
+
height: diameter,
|
|
128
|
+
circle: true,
|
|
129
|
+
}));
|
|
130
|
+
return {
|
|
131
|
+
width: w,
|
|
132
|
+
height: h,
|
|
133
|
+
body: { x: 0, y: 0, width: w, height: counterHeight, round: false },
|
|
134
|
+
chairs,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function transpose(layout: GlyphLayout): GlyphLayout {
|
|
139
|
+
return {
|
|
140
|
+
width: layout.height,
|
|
141
|
+
height: layout.width,
|
|
142
|
+
body: {
|
|
143
|
+
x: layout.body.y,
|
|
144
|
+
y: layout.body.x,
|
|
145
|
+
width: layout.body.height,
|
|
146
|
+
height: layout.body.width,
|
|
147
|
+
round: layout.body.round,
|
|
148
|
+
},
|
|
149
|
+
chairs: layout.chairs.map((c) => ({
|
|
150
|
+
...c,
|
|
151
|
+
cx: c.cy,
|
|
152
|
+
cy: c.cx,
|
|
153
|
+
rotate: c.circle ? 0 : c.rotate === 0 ? 90 : 0,
|
|
154
|
+
})),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function framed(counts: Record<Side, number>, w: number, h: number): GlyphLayout {
|
|
159
|
+
return {
|
|
160
|
+
width: w,
|
|
161
|
+
height: h,
|
|
162
|
+
body: { x: INSET, y: INSET, width: w - 2 * INSET, height: h - 2 * INSET, round: false },
|
|
163
|
+
chairs: sidedChairs(counts, w, h),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Geometry of a table glyph in a `width` x `height` box (100 units per grid cell). Round tables
|
|
169
|
+
* space chairs radially; square ones put a chair per side up to four, then spread; rect tables
|
|
170
|
+
* seat one at each end and split the rest along the long sides; bars line stools up in a row.
|
|
171
|
+
* Rect and bar follow the longer axis of the box.
|
|
172
|
+
*/
|
|
173
|
+
export function glyphLayout(
|
|
174
|
+
shape: TableShape,
|
|
175
|
+
seats: number,
|
|
176
|
+
width: number,
|
|
177
|
+
height: number,
|
|
178
|
+
): GlyphLayout {
|
|
179
|
+
const count = Math.max(0, Math.floor(seats));
|
|
180
|
+
if (shape === "round") return roundLayout(count, width, height);
|
|
181
|
+
if (shape === "square") return framed(squareSideCounts(count), width, height);
|
|
182
|
+
const vertical = height > width;
|
|
183
|
+
const w = vertical ? height : width;
|
|
184
|
+
const h = vertical ? width : height;
|
|
185
|
+
const layout = shape === "rect" ? framed(rectSideCounts(count), w, h) : barLayout(count, w, h);
|
|
186
|
+
return vertical ? transpose(layout) : layout;
|
|
187
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import type { ClassNames } from "../../utils/class-names.js";
|
|
3
|
+
import { cn } from "../../utils/cn.js";
|
|
4
|
+
import { glyphLayout, type TableShape, UNIT } from "./layout.js";
|
|
5
|
+
|
|
6
|
+
export type TableGlyphSize = "sm" | "md" | "lg" | "fill";
|
|
7
|
+
|
|
8
|
+
export type TableGlyphPattern = "dashed" | "solid" | "soft" | "strong";
|
|
9
|
+
|
|
10
|
+
export type TableGlyphPart = "root" | "body" | "chair";
|
|
11
|
+
|
|
12
|
+
export interface TableGlyphProps extends Omit<ComponentPropsWithoutRef<"svg">, "children"> {
|
|
13
|
+
shape: TableShape;
|
|
14
|
+
/** Chairs drawn around the table. */
|
|
15
|
+
seats: number;
|
|
16
|
+
/** How many chairs are filled, in drawing order. Clamped to `seats`. */
|
|
17
|
+
occupied?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Height of a one-unit box from the control-height tokens, so it follows density; wider shapes
|
|
20
|
+
* grow from it. `fill` takes the size of the parent instead.
|
|
21
|
+
*/
|
|
22
|
+
size?: TableGlyphSize;
|
|
23
|
+
/** Box in grid units (1 unit is one square cell). Defaults to 1x1, rect 2x1 and bar 3x1. */
|
|
24
|
+
width?: number;
|
|
25
|
+
height?: number;
|
|
26
|
+
/** Table surface: `dashed` outline, `solid` outline on the surface, `soft` or `strong` tint. Defaults to `solid`. */
|
|
27
|
+
pattern?: TableGlyphPattern;
|
|
28
|
+
/** Per-part classes merged with `cn`; consumer classes win. `className` still targets the root and is merged last. */
|
|
29
|
+
classNames?: ClassNames<TableGlyphPart>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const sizeClasses: Record<TableGlyphSize, string> = {
|
|
33
|
+
sm: "h-control w-auto",
|
|
34
|
+
md: "h-control-lg w-auto",
|
|
35
|
+
lg: "h-[calc(var(--control-height-lg)*1.5)] w-auto",
|
|
36
|
+
fill: "size-full",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const patternClasses: Record<TableGlyphPattern, string> = {
|
|
40
|
+
dashed: "fill-transparent",
|
|
41
|
+
solid: "fill-surface",
|
|
42
|
+
soft: "fill-current/10",
|
|
43
|
+
strong: "fill-current/35",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const defaultBox: Record<TableShape, readonly [number, number]> = {
|
|
47
|
+
round: [1, 1],
|
|
48
|
+
square: [1, 1],
|
|
49
|
+
rect: [2, 1],
|
|
50
|
+
bar: [3, 1],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const STROKE = 1.5;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* An SVG table with its chairs placed from `seats` and `shape`: radial for round, one per side
|
|
57
|
+
* (then spread) for square, ends plus long sides for rect, a row of stools for bar. It draws with
|
|
58
|
+
* `currentColor` and the surface token, so it follows the text colour and the theme. Decorative
|
|
59
|
+
* (`aria-hidden`) unless an `aria-label` is given.
|
|
60
|
+
*/
|
|
61
|
+
export function TableGlyph({
|
|
62
|
+
shape,
|
|
63
|
+
seats,
|
|
64
|
+
occupied = 0,
|
|
65
|
+
size = "md",
|
|
66
|
+
width,
|
|
67
|
+
height,
|
|
68
|
+
pattern = "solid",
|
|
69
|
+
className,
|
|
70
|
+
classNames,
|
|
71
|
+
"aria-label": ariaLabel,
|
|
72
|
+
...props
|
|
73
|
+
}: TableGlyphProps) {
|
|
74
|
+
const [defaultW, defaultH] = defaultBox[shape];
|
|
75
|
+
const w = (width ?? defaultW) * UNIT;
|
|
76
|
+
const h = (height ?? defaultH) * UNIT;
|
|
77
|
+
const layout = glyphLayout(shape, seats, w, h);
|
|
78
|
+
const filled = Math.max(0, Math.min(Math.floor(occupied), layout.chairs.length));
|
|
79
|
+
const { body } = layout;
|
|
80
|
+
const labelled = ariaLabel !== undefined;
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<svg
|
|
84
|
+
viewBox={`0 0 ${w} ${h}`}
|
|
85
|
+
role={labelled ? "img" : undefined}
|
|
86
|
+
aria-label={ariaLabel}
|
|
87
|
+
aria-hidden={labelled ? undefined : true}
|
|
88
|
+
focusable="false"
|
|
89
|
+
data-shape={shape}
|
|
90
|
+
data-seats={layout.chairs.length}
|
|
91
|
+
data-occupied={filled}
|
|
92
|
+
data-pattern={pattern}
|
|
93
|
+
preserveAspectRatio="xMidYMid meet"
|
|
94
|
+
className={cn("shrink-0 text-ink-2", sizeClasses[size], classNames?.root, className)}
|
|
95
|
+
{...props}
|
|
96
|
+
>
|
|
97
|
+
{body.round ? (
|
|
98
|
+
<circle
|
|
99
|
+
cx={body.x + body.width / 2}
|
|
100
|
+
cy={body.y + body.height / 2}
|
|
101
|
+
r={body.width / 2}
|
|
102
|
+
data-part="body"
|
|
103
|
+
className={cn("stroke-current", patternClasses[pattern], classNames?.body)}
|
|
104
|
+
strokeWidth={STROKE}
|
|
105
|
+
strokeDasharray={pattern === "dashed" ? "5 4" : undefined}
|
|
106
|
+
vectorEffect="non-scaling-stroke"
|
|
107
|
+
/>
|
|
108
|
+
) : (
|
|
109
|
+
<rect
|
|
110
|
+
x={body.x}
|
|
111
|
+
y={body.y}
|
|
112
|
+
width={body.width}
|
|
113
|
+
height={body.height}
|
|
114
|
+
rx={6}
|
|
115
|
+
data-part="body"
|
|
116
|
+
className={cn("stroke-current", patternClasses[pattern], classNames?.body)}
|
|
117
|
+
strokeWidth={STROKE}
|
|
118
|
+
strokeDasharray={pattern === "dashed" ? "5 4" : undefined}
|
|
119
|
+
vectorEffect="non-scaling-stroke"
|
|
120
|
+
/>
|
|
121
|
+
)}
|
|
122
|
+
{layout.chairs.map((chair, index) => {
|
|
123
|
+
const taken = index < filled;
|
|
124
|
+
const chairClass = cn(
|
|
125
|
+
"stroke-current",
|
|
126
|
+
taken ? "fill-current" : "fill-surface",
|
|
127
|
+
classNames?.chair,
|
|
128
|
+
);
|
|
129
|
+
const key = `${chair.cx.toFixed(2)}-${chair.cy.toFixed(2)}`;
|
|
130
|
+
return chair.circle ? (
|
|
131
|
+
<circle
|
|
132
|
+
key={key}
|
|
133
|
+
cx={chair.cx}
|
|
134
|
+
cy={chair.cy}
|
|
135
|
+
r={chair.width / 2}
|
|
136
|
+
data-part="chair"
|
|
137
|
+
data-occupied={taken ? "" : undefined}
|
|
138
|
+
className={chairClass}
|
|
139
|
+
strokeWidth={STROKE}
|
|
140
|
+
vectorEffect="non-scaling-stroke"
|
|
141
|
+
/>
|
|
142
|
+
) : (
|
|
143
|
+
<rect
|
|
144
|
+
key={key}
|
|
145
|
+
x={chair.cx - chair.width / 2}
|
|
146
|
+
y={chair.cy - chair.height / 2}
|
|
147
|
+
width={chair.width}
|
|
148
|
+
height={chair.height}
|
|
149
|
+
rx={5}
|
|
150
|
+
transform={`rotate(${chair.rotate.toFixed(2)} ${chair.cx.toFixed(2)} ${chair.cy.toFixed(2)})`}
|
|
151
|
+
data-part="chair"
|
|
152
|
+
data-occupied={taken ? "" : undefined}
|
|
153
|
+
className={chairClass}
|
|
154
|
+
strokeWidth={STROKE}
|
|
155
|
+
vectorEffect="non-scaling-stroke"
|
|
156
|
+
/>
|
|
157
|
+
);
|
|
158
|
+
})}
|
|
159
|
+
</svg>
|
|
160
|
+
);
|
|
161
|
+
}
|