Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DeviceDuoControls.tsx

111 lines3.4 KB

1import {
2 duoFoldState,
3 type DuoCommand,
4 type DuoControlState,
5} from "@t3tools/client-runtime/device/duo-control";
6import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream";
7import { DeviceDuoGlyph } from "./DeviceDuoGlyph";
8import { Button } from "~/components/ui/button";
9import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
10​
11const FOLDS = [
12 { id: "closed", angle: 0 },
13 { id: "half", angle: 90 },
14 { id: "open", angle: 180 },
15] as const;
16const STANDS = [
17 { id: "laptop", label: "Laptop stand" },
18 { id: "tent", label: "Tent stand" },
19] as const;
20​
21/**
22 * Fold shapes move only the hinge, so the device opens around whichever edge it
23 * currently rests on: a vertical phone opens as a book into a landscape tablet,
24 * a horizontal one as a laptop into a portrait tablet. Stands are native presets
25 * that also place the device. Pinching supplies continuous hinge control.
26 */
27export function DeviceDuoControls(props: {
28 screen: DeviceScreenSize;
29 state: DuoControlState;
30 enabled: boolean;
31 onCommand: (command: DuoCommand) => void;
32}) {
33 const { screen } = props;
34 const { fold, stand, phoneVertical } = duoFoldState(screen);
35 const foldLabels = {
36 closed: "Closed",
37 half: phoneVertical ? "Book" : "Laptop",
38 open: "Open",
39 };
40 const button = (
41 key: string,
42 label: string,
43 pressed: boolean,
44 onClick: () => void,
45 glyph: React.ReactNode,
46 ) => (
47 <Tooltip key={key}>
48 <TooltipTrigger
49 render={
50 <Button
51 size="icon"
52 variant={pressed ? "secondary" : "ghost"}
53 disabled={!props.enabled}
54 aria-label={label}
55 aria-pressed={pressed}
56 data-pressed={pressed ? "" : undefined}
57 onClick={onClick}
58 />
59 }
60 >
61 {glyph}
62 </TooltipTrigger>
63 <TooltipPopup side="left">{label}</TooltipPopup>
64 </Tooltip>
65 );
66 const group =
67 "pointer-events-auto flex shrink-0 flex-col items-center gap-1 rounded-full border border-border/50 bg-background/80 p-1 shadow-sm";
68 return (
69 <div aria-label="iPhone Duo stands" className="flex flex-col items-center gap-2">
70 <div role="group" aria-label="Fold shape" className={group}>
71 {FOLDS.map(({ id, angle: value }) =>
72 button(
73 id,
74 foldLabels[id],
75 !stand && fold === id,
76 () => props.onCommand({ control: "angle", value }),
77 <DeviceDuoGlyph pose={id === "half" ? "book" : id} rotated={!phoneVertical} />,
78 ),
79 )}
80 </div>
81 <div role="group" aria-label="Device stance" className={group}>
82 {STANDS.map(({ id, label }) =>
83 button(
84 id,
85 label,
86 screen.hingePose === id,
87 () => props.onCommand({ control: "pose", value: id }),
88 <DeviceDuoGlyph pose={id} />,
89 ),
90 )}
91 </div>
92 {props.state.error ? (
93 <Tooltip>
94 <TooltipTrigger
95 render={
96 <span
97 tabIndex={0}
98 role="alert"
99 aria-label={props.state.error}
100 className="pointer-events-auto text-xs text-destructive"
101 >
102 !
103 </span>
104 }
105 />
106 <TooltipPopup side="left">{props.state.error}</TooltipPopup>
107 </Tooltip>
108 ) : null}
109 </div>
110 );