@passui/tanstack 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +76 -2
- package/package.json +165 -5
- package/src/auth-shell/auth-shell.tsx +26 -0
- package/src/auth-shell/index.ts +1 -0
- package/src/document/compose-viewport.tsx +96 -0
- package/src/document/index.ts +1 -0
- package/src/form-fields/fields.tsx +192 -0
- package/src/form-fields/index.ts +2 -0
- package/src/form-fields/selection.tsx +149 -0
- package/src/form-fields/submit.tsx +40 -0
- package/src/forms/contexts.ts +8 -0
- package/src/forms/elements/form-alert.tsx +30 -0
- package/src/forms/elements/submit.tsx +1 -0
- package/src/forms/fields/avatar-picker.tsx +399 -0
- package/src/forms/fields/avatar.tsx +299 -0
- package/src/forms/fields/checkbox.tsx +1 -0
- package/src/forms/fields/index.ts +32 -0
- package/src/forms/fields/input.tsx +1 -0
- package/src/forms/fields/multiselect.tsx +1 -0
- package/src/forms/fields/otp.tsx +1 -0
- package/src/forms/fields/radio-card.tsx +1 -0
- package/src/forms/fields/switch.tsx +1 -0
- package/src/forms/fields/textarea.tsx +1 -0
- package/src/forms/index.ts +30 -0
- package/src/mutation/index.ts +1 -0
- package/src/mutation/use-app-mutation.ts +156 -0
- package/src/route-boundary/default-boundary.tsx +61 -0
- package/src/route-boundary/default-loader.tsx +19 -0
- package/src/route-boundary/index.ts +3 -0
- package/src/route-boundary/not-founder.tsx +50 -0
- package/src/settings/index.ts +1 -0
- package/src/settings/setting-list.tsx +252 -0
- package/src/shared/index.ts +2 -0
- package/src/shared/optimistic.ts +7 -0
- package/src/shared/table.ts +30 -0
- package/src/solid/auth-shell/auth-shell.tsx +20 -0
- package/src/solid/auth-shell/index.ts +1 -0
- package/src/solid/document/compose-viewport.tsx +69 -0
- package/src/solid/document/index.ts +1 -0
- package/src/solid/form-fields/avatar.tsx +370 -0
- package/src/solid/form-fields/fields.tsx +336 -0
- package/src/solid/form-fields/index.ts +3 -0
- package/src/solid/form-fields/submit.tsx +31 -0
- package/src/solid/forms/contexts.ts +7 -0
- package/src/solid/forms/form-alert.tsx +19 -0
- package/src/solid/forms/index.ts +32 -0
- package/src/solid/mutation/index.ts +5 -0
- package/src/solid/mutation/use-app-mutation.ts +171 -0
- package/src/solid/route-boundary/boundaries.tsx +104 -0
- package/src/solid/route-boundary/index.ts +1 -0
- package/src/solid/settings/index.ts +1 -0
- package/src/solid/settings/setting-list.tsx +185 -0
- package/src/solid/styles.css +2 -0
- package/src/solid/tables/columns.tsx +40 -0
- package/src/solid/tables/data-table.tsx +448 -0
- package/src/solid/tables/filter-row.tsx +161 -0
- package/src/solid/tables/index.ts +4 -0
- package/src/solid/tables/pagination.tsx +78 -0
- package/src/styles.css +12 -0
- package/src/tables/columns.tsx +39 -0
- package/src/tables/data-table.tsx +617 -0
- package/src/tables/filter-row.tsx +260 -0
- package/src/tables/index.ts +3 -0
- package/src/tables/pagination.tsx +93 -0
- package/src/tables/utils.ts +5 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
import { cn } from "@passui/react/cn"
|
|
3
|
+
import { Toggle } from "@passui/react/toggle"
|
|
4
|
+
import { useFieldContext } from "../forms/contexts"
|
|
5
|
+
|
|
6
|
+
type Option = {
|
|
7
|
+
label: string
|
|
8
|
+
value: string
|
|
9
|
+
description?: string
|
|
10
|
+
icon?: ReactNode
|
|
11
|
+
disabled?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function FieldRadioCard({
|
|
15
|
+
label,
|
|
16
|
+
description,
|
|
17
|
+
options,
|
|
18
|
+
disabled,
|
|
19
|
+
columns = 1,
|
|
20
|
+
}: {
|
|
21
|
+
label: string
|
|
22
|
+
description?: string
|
|
23
|
+
options: Option[]
|
|
24
|
+
disabled?: boolean
|
|
25
|
+
columns?: 1 | 2
|
|
26
|
+
}) {
|
|
27
|
+
const field = useFieldContext<string>()
|
|
28
|
+
return (
|
|
29
|
+
<fieldset
|
|
30
|
+
disabled={disabled}
|
|
31
|
+
className="flex flex-col gap-2"
|
|
32
|
+
>
|
|
33
|
+
<legend className="text-subheadline font-semibold">
|
|
34
|
+
{label}
|
|
35
|
+
</legend>
|
|
36
|
+
{description && (
|
|
37
|
+
<p className="text-footnote text-label-secondary">
|
|
38
|
+
{description}
|
|
39
|
+
</p>
|
|
40
|
+
)}
|
|
41
|
+
<div
|
|
42
|
+
className={cn(
|
|
43
|
+
"grid gap-2",
|
|
44
|
+
columns === 2 && "md:grid-cols-2"
|
|
45
|
+
)}
|
|
46
|
+
>
|
|
47
|
+
{options.map((option) => (
|
|
48
|
+
<label
|
|
49
|
+
key={option.value}
|
|
50
|
+
className={cn(
|
|
51
|
+
"flex items-start gap-3 rounded-xl border p-3",
|
|
52
|
+
field.state.value === option.value
|
|
53
|
+
? "border-accent-text bg-accent/10"
|
|
54
|
+
: "border-separator bg-elevated",
|
|
55
|
+
(disabled || option.disabled) &&
|
|
56
|
+
"opacity-40"
|
|
57
|
+
)}
|
|
58
|
+
>
|
|
59
|
+
<input
|
|
60
|
+
type="radio"
|
|
61
|
+
name={field.name}
|
|
62
|
+
value={option.value}
|
|
63
|
+
checked={
|
|
64
|
+
field.state.value ===
|
|
65
|
+
option.value
|
|
66
|
+
}
|
|
67
|
+
disabled={option.disabled}
|
|
68
|
+
onBlur={field.handleBlur}
|
|
69
|
+
onChange={() =>
|
|
70
|
+
field.handleChange(
|
|
71
|
+
option.value
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
className="mt-1 accent-accent-text"
|
|
75
|
+
/>
|
|
76
|
+
{option.icon}
|
|
77
|
+
<span className="text-subheadline">
|
|
78
|
+
{option.label}
|
|
79
|
+
{option.description && (
|
|
80
|
+
<span className="block text-footnote text-label-secondary">
|
|
81
|
+
{option.description}
|
|
82
|
+
</span>
|
|
83
|
+
)}
|
|
84
|
+
</span>
|
|
85
|
+
</label>
|
|
86
|
+
))}
|
|
87
|
+
</div>
|
|
88
|
+
</fieldset>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function FieldMultiselect({
|
|
93
|
+
label,
|
|
94
|
+
description,
|
|
95
|
+
options,
|
|
96
|
+
}: {
|
|
97
|
+
label: string
|
|
98
|
+
description?: string
|
|
99
|
+
options: Option[]
|
|
100
|
+
}) {
|
|
101
|
+
const field = useFieldContext<string[]>()
|
|
102
|
+
return (
|
|
103
|
+
<fieldset className="flex flex-col gap-3">
|
|
104
|
+
<legend className="text-subheadline font-semibold">
|
|
105
|
+
{label}
|
|
106
|
+
</legend>
|
|
107
|
+
{description && (
|
|
108
|
+
<p className="text-footnote text-label-secondary">
|
|
109
|
+
{description}
|
|
110
|
+
</p>
|
|
111
|
+
)}
|
|
112
|
+
{options.map((option) => (
|
|
113
|
+
<div key={option.value}>
|
|
114
|
+
<Toggle
|
|
115
|
+
style="checkbox"
|
|
116
|
+
label={option.label}
|
|
117
|
+
disabled={option.disabled}
|
|
118
|
+
checked={field.state.value.includes(
|
|
119
|
+
option.value
|
|
120
|
+
)}
|
|
121
|
+
onChange={(checked) => {
|
|
122
|
+
field.handleChange(
|
|
123
|
+
checked
|
|
124
|
+
? Array.from(
|
|
125
|
+
new Set([
|
|
126
|
+
...field.state
|
|
127
|
+
.value,
|
|
128
|
+
option.value,
|
|
129
|
+
])
|
|
130
|
+
)
|
|
131
|
+
: field.state.value.filter(
|
|
132
|
+
(value) =>
|
|
133
|
+
value !==
|
|
134
|
+
option.value
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
field.handleBlur()
|
|
138
|
+
}}
|
|
139
|
+
/>
|
|
140
|
+
{option.description && (
|
|
141
|
+
<p className="pl-8 text-footnote text-label-secondary">
|
|
142
|
+
{option.description}
|
|
143
|
+
</p>
|
|
144
|
+
)}
|
|
145
|
+
</div>
|
|
146
|
+
))}
|
|
147
|
+
</fieldset>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Button } from "@passui/react/button"
|
|
2
|
+
import { useFormContext } from "../forms/contexts"
|
|
3
|
+
|
|
4
|
+
export function Submit({ label }: { label: string }) {
|
|
5
|
+
const form = useFormContext()
|
|
6
|
+
return (
|
|
7
|
+
<form.Subscribe
|
|
8
|
+
selector={(state) => [
|
|
9
|
+
state.isSubmitting,
|
|
10
|
+
state.canSubmit,
|
|
11
|
+
state.isDirty,
|
|
12
|
+
state.isTouched,
|
|
13
|
+
]}
|
|
14
|
+
>
|
|
15
|
+
{([pending, valid, dirty, touched]) => (
|
|
16
|
+
<Button
|
|
17
|
+
type="submit"
|
|
18
|
+
variant="default"
|
|
19
|
+
size="default"
|
|
20
|
+
aria-busy={pending}
|
|
21
|
+
disabled={
|
|
22
|
+
pending ||
|
|
23
|
+
!valid ||
|
|
24
|
+
!dirty ||
|
|
25
|
+
!touched
|
|
26
|
+
}
|
|
27
|
+
className="w-full"
|
|
28
|
+
>
|
|
29
|
+
{pending && (
|
|
30
|
+
<span
|
|
31
|
+
aria-hidden
|
|
32
|
+
className="size-4 animate-spin rounded-full border-2 border-current border-r-transparent"
|
|
33
|
+
/>
|
|
34
|
+
)}
|
|
35
|
+
{label}
|
|
36
|
+
</Button>
|
|
37
|
+
)}
|
|
38
|
+
</form.Subscribe>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Alert02Icon } from "@hugeicons/core-free-icons"
|
|
2
|
+
import { HugeiconsIcon } from "@hugeicons/react"
|
|
3
|
+
import type { ReactNode } from "react"
|
|
4
|
+
import { cn } from "@passui/react/cn"
|
|
5
|
+
|
|
6
|
+
/** A form-level error people can't miss: announced, tinted, and gently shaken in. */
|
|
7
|
+
export function FormAlert({
|
|
8
|
+
children,
|
|
9
|
+
className,
|
|
10
|
+
}: {
|
|
11
|
+
children: ReactNode
|
|
12
|
+
className?: string
|
|
13
|
+
}) {
|
|
14
|
+
return (
|
|
15
|
+
<div
|
|
16
|
+
role="alert"
|
|
17
|
+
className={cn(
|
|
18
|
+
"flex items-start gap-2.5 rounded-2xl bg-destructive/12 px-4 py-3 text-sm font-medium text-destructive ring-1 ring-destructive/25 ring-inset",
|
|
19
|
+
"motion-safe:animate-in motion-safe:fade-in-0 motion-safe:zoom-in-95 motion-safe:duration-300",
|
|
20
|
+
className
|
|
21
|
+
)}
|
|
22
|
+
>
|
|
23
|
+
<HugeiconsIcon
|
|
24
|
+
icon={Alert02Icon}
|
|
25
|
+
className="mt-px size-4 shrink-0"
|
|
26
|
+
/>
|
|
27
|
+
<span className="min-w-0">{children}</span>
|
|
28
|
+
</div>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Submit } from "../../form-fields/submit"
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Camera01Icon,
|
|
3
|
+
Delete02Icon,
|
|
4
|
+
Image01Icon,
|
|
5
|
+
TextFontIcon,
|
|
6
|
+
} from "@hugeicons/core-free-icons"
|
|
7
|
+
import { HugeiconsIcon } from "@hugeicons/react"
|
|
8
|
+
import { useEffect, useRef, useState } from "react"
|
|
9
|
+
import type { ReactNode } from "react"
|
|
10
|
+
import {
|
|
11
|
+
ProfileImage,
|
|
12
|
+
ProfileImageFallback,
|
|
13
|
+
ProfileImageContent,
|
|
14
|
+
} from "@passui/react/profile-image"
|
|
15
|
+
import { Button } from "@passui/react/button"
|
|
16
|
+
import { Sheet } from "@passui/react/sheet"
|
|
17
|
+
import { createPortal } from "react-dom"
|
|
18
|
+
import { cn } from "@passui/react/cn"
|
|
19
|
+
import { Ambient } from "@passui/react/ambient"
|
|
20
|
+
|
|
21
|
+
export type AvatarPickerLabels = {
|
|
22
|
+
photo: string
|
|
23
|
+
photoDescription: string
|
|
24
|
+
choosePhoto: string
|
|
25
|
+
takePhoto: string
|
|
26
|
+
capture: string
|
|
27
|
+
monogram: string
|
|
28
|
+
removePhoto: string
|
|
29
|
+
cancel: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type AvatarPickerProps = {
|
|
33
|
+
open: boolean
|
|
34
|
+
onOpenChange: (open: boolean) => void
|
|
35
|
+
label: string
|
|
36
|
+
image?: string
|
|
37
|
+
clearable: boolean
|
|
38
|
+
labels: AvatarPickerLabels
|
|
39
|
+
onChooseFile: () => void
|
|
40
|
+
/** A captured frame, still to be framed in Move and Scale. */
|
|
41
|
+
onCapture: (file: File) => void
|
|
42
|
+
/** A finished image that needs no framing, like a monogram. */
|
|
43
|
+
onChoose: (file: File) => void
|
|
44
|
+
onRemove: () => void
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type CameraViewProps = {
|
|
48
|
+
labels: AvatarPickerLabels
|
|
49
|
+
onCapture: (file: File) => void
|
|
50
|
+
onCancel: () => void
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type OptionProps = {
|
|
54
|
+
icon: typeof Image01Icon
|
|
55
|
+
tone?: "default" | "destructive"
|
|
56
|
+
onClick: () => void
|
|
57
|
+
children: ReactNode
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Apple's contact monogram gradients
|
|
61
|
+
const MONOGRAM_GRADIENTS = [
|
|
62
|
+
["#A1A1A6", "#6E6E73"],
|
|
63
|
+
["#FF9F0A", "#FF6A00"],
|
|
64
|
+
["#FF6482", "#FF2D55"],
|
|
65
|
+
["#BF5AF2", "#8944E3"],
|
|
66
|
+
["#64D2FF", "#0A84FF"],
|
|
67
|
+
["#34C759", "#0FA958"],
|
|
68
|
+
] as const
|
|
69
|
+
|
|
70
|
+
function initialsOf(label: string) {
|
|
71
|
+
const parts = label.trim().split(/\s+/).filter(Boolean)
|
|
72
|
+
const letters =
|
|
73
|
+
parts.length > 1
|
|
74
|
+
? [parts[0], parts[parts.length - 1]]
|
|
75
|
+
: parts
|
|
76
|
+
return (
|
|
77
|
+
letters
|
|
78
|
+
.map((part) => part[0]?.toUpperCase() ?? "")
|
|
79
|
+
.join("") || "?"
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function toFile(
|
|
84
|
+
canvas: HTMLCanvasElement,
|
|
85
|
+
name: string,
|
|
86
|
+
type: string
|
|
87
|
+
) {
|
|
88
|
+
const blob = await new Promise<Blob | null>((resolve) =>
|
|
89
|
+
canvas.toBlob(resolve, type, 0.92)
|
|
90
|
+
)
|
|
91
|
+
if (!blob) throw new Error("Could not export the image")
|
|
92
|
+
return new File([blob], name, { type })
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function renderMonogram(
|
|
96
|
+
initials: string,
|
|
97
|
+
[from, to]: readonly [string, string]
|
|
98
|
+
) {
|
|
99
|
+
const size = 512
|
|
100
|
+
const canvas = document.createElement("canvas")
|
|
101
|
+
canvas.width = size
|
|
102
|
+
canvas.height = size
|
|
103
|
+
const context = canvas.getContext("2d")
|
|
104
|
+
if (!context) throw new Error("Canvas is not available")
|
|
105
|
+
const gradient = context.createLinearGradient(
|
|
106
|
+
0,
|
|
107
|
+
0,
|
|
108
|
+
0,
|
|
109
|
+
size
|
|
110
|
+
)
|
|
111
|
+
gradient.addColorStop(0, from)
|
|
112
|
+
gradient.addColorStop(1, to)
|
|
113
|
+
context.fillStyle = gradient
|
|
114
|
+
context.fillRect(0, 0, size, size)
|
|
115
|
+
context.fillStyle = "#fff"
|
|
116
|
+
context.textAlign = "center"
|
|
117
|
+
context.textBaseline = "middle"
|
|
118
|
+
context.font = `600 ${initials.length > 1 ? 196 : 232}px -apple-system, "SF Pro Rounded", "SF Pro Display", system-ui, sans-serif`
|
|
119
|
+
context.fillText(initials, size / 2, size / 2 + 12)
|
|
120
|
+
return toFile(canvas, "monogram.png", "image/png")
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function Option({
|
|
124
|
+
icon,
|
|
125
|
+
tone = "default",
|
|
126
|
+
onClick,
|
|
127
|
+
children,
|
|
128
|
+
}: OptionProps) {
|
|
129
|
+
return (
|
|
130
|
+
<button
|
|
131
|
+
type="button"
|
|
132
|
+
onClick={onClick}
|
|
133
|
+
className={cn(
|
|
134
|
+
"flex w-full items-center gap-3 px-4 py-3 text-left text-sm transition-colors",
|
|
135
|
+
"hover:bg-foreground/[0.04] focus-visible:bg-foreground/[0.06] focus-visible:outline-none",
|
|
136
|
+
tone === "destructive" && "text-destructive"
|
|
137
|
+
)}
|
|
138
|
+
>
|
|
139
|
+
<HugeiconsIcon
|
|
140
|
+
icon={icon}
|
|
141
|
+
className="size-4.5 shrink-0 opacity-80"
|
|
142
|
+
/>
|
|
143
|
+
{children}
|
|
144
|
+
</button>
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The live selfie view: the frame under a circle, an iOS shutter below. */
|
|
149
|
+
function CameraView({
|
|
150
|
+
labels,
|
|
151
|
+
onCapture,
|
|
152
|
+
onCancel,
|
|
153
|
+
}: CameraViewProps) {
|
|
154
|
+
const videoRef = useRef<HTMLVideoElement>(null)
|
|
155
|
+
const [failed, setFailed] = useState(false)
|
|
156
|
+
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
let stream: MediaStream | null = null
|
|
159
|
+
navigator.mediaDevices
|
|
160
|
+
.getUserMedia({
|
|
161
|
+
video: {
|
|
162
|
+
facingMode: "user",
|
|
163
|
+
width: 1280,
|
|
164
|
+
height: 1280,
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
.then((media) => {
|
|
168
|
+
stream = media
|
|
169
|
+
if (videoRef.current)
|
|
170
|
+
videoRef.current.srcObject = media
|
|
171
|
+
})
|
|
172
|
+
.catch(() => setFailed(true))
|
|
173
|
+
return () => {
|
|
174
|
+
for (const track of stream?.getTracks() ?? [])
|
|
175
|
+
track.stop()
|
|
176
|
+
}
|
|
177
|
+
}, [])
|
|
178
|
+
|
|
179
|
+
async function capture() {
|
|
180
|
+
const video = videoRef.current
|
|
181
|
+
if (!video?.videoWidth) return
|
|
182
|
+
const canvas = document.createElement("canvas")
|
|
183
|
+
canvas.width = video.videoWidth
|
|
184
|
+
canvas.height = video.videoHeight
|
|
185
|
+
const context = canvas.getContext("2d")
|
|
186
|
+
if (!context) return
|
|
187
|
+
// the preview is mirrored like a mirror; keep the photo matching it
|
|
188
|
+
context.translate(canvas.width, 0)
|
|
189
|
+
context.scale(-1, 1)
|
|
190
|
+
context.drawImage(video, 0, 0)
|
|
191
|
+
onCapture(
|
|
192
|
+
await toFile(canvas, "photo.jpg", "image/jpeg")
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<div className="flex flex-col items-center gap-5 bg-neutral-950 px-5 pt-8 pb-6">
|
|
198
|
+
<div className="relative size-64 overflow-hidden rounded-full ring-1 ring-white/20">
|
|
199
|
+
{failed ? (
|
|
200
|
+
<div className="flex size-full items-center justify-center p-8 text-center text-xs text-white/70">
|
|
201
|
+
{labels.takePhoto}
|
|
202
|
+
</div>
|
|
203
|
+
) : (
|
|
204
|
+
<video
|
|
205
|
+
ref={videoRef}
|
|
206
|
+
autoPlay
|
|
207
|
+
playsInline
|
|
208
|
+
muted
|
|
209
|
+
className="size-full -scale-x-100 object-cover"
|
|
210
|
+
/>
|
|
211
|
+
)}
|
|
212
|
+
</div>
|
|
213
|
+
<div className="grid w-full grid-cols-3 items-center">
|
|
214
|
+
<Button
|
|
215
|
+
type="button"
|
|
216
|
+
variant="ghost"
|
|
217
|
+
size="sm"
|
|
218
|
+
className="justify-self-start text-white hover:bg-white/10 hover:text-white"
|
|
219
|
+
onClick={onCancel}
|
|
220
|
+
>
|
|
221
|
+
{labels.cancel}
|
|
222
|
+
</Button>
|
|
223
|
+
<button
|
|
224
|
+
type="button"
|
|
225
|
+
aria-label={labels.capture}
|
|
226
|
+
disabled={failed}
|
|
227
|
+
onClick={() => void capture()}
|
|
228
|
+
className="size-16 justify-self-center rounded-full border-4 border-white p-1 transition-transform active:scale-95 disabled:opacity-40"
|
|
229
|
+
>
|
|
230
|
+
<span className="block size-full rounded-full bg-white" />
|
|
231
|
+
</button>
|
|
232
|
+
</div>
|
|
233
|
+
</div>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The photo sheet Apple shows for a contact photo: the current photo lit by
|
|
239
|
+
* its own colors, then the ways to change it.
|
|
240
|
+
*/
|
|
241
|
+
export function AvatarPicker({
|
|
242
|
+
open,
|
|
243
|
+
onOpenChange,
|
|
244
|
+
label,
|
|
245
|
+
image,
|
|
246
|
+
clearable,
|
|
247
|
+
labels,
|
|
248
|
+
onChooseFile,
|
|
249
|
+
onCapture,
|
|
250
|
+
onChoose,
|
|
251
|
+
onRemove,
|
|
252
|
+
}: AvatarPickerProps) {
|
|
253
|
+
const [mode, setMode] = useState<"menu" | "camera">(
|
|
254
|
+
"menu"
|
|
255
|
+
)
|
|
256
|
+
const [monogramOpen, setMonogramOpen] = useState(false)
|
|
257
|
+
const [canUseCamera, setCanUseCamera] = useState(false)
|
|
258
|
+
const initials = initialsOf(label)
|
|
259
|
+
|
|
260
|
+
useEffect(() => {
|
|
261
|
+
setCanUseCamera(
|
|
262
|
+
Boolean(navigator.mediaDevices?.getUserMedia)
|
|
263
|
+
)
|
|
264
|
+
}, [])
|
|
265
|
+
|
|
266
|
+
useEffect(() => {
|
|
267
|
+
if (!open) {
|
|
268
|
+
setMode("menu")
|
|
269
|
+
setMonogramOpen(false)
|
|
270
|
+
}
|
|
271
|
+
}, [open])
|
|
272
|
+
|
|
273
|
+
if (typeof document === "undefined") return null
|
|
274
|
+
return createPortal(
|
|
275
|
+
<Sheet
|
|
276
|
+
open={open}
|
|
277
|
+
onClose={() => onOpenChange(false)}
|
|
278
|
+
label={
|
|
279
|
+
mode === "camera"
|
|
280
|
+
? labels.takePhoto
|
|
281
|
+
: labels.photo
|
|
282
|
+
}
|
|
283
|
+
className="gap-0 overflow-hidden rounded-3xl p-0 sm:max-w-[22rem]"
|
|
284
|
+
>
|
|
285
|
+
{mode === "camera" ? (
|
|
286
|
+
<>
|
|
287
|
+
<h2 className="sr-only">
|
|
288
|
+
{labels.takePhoto}
|
|
289
|
+
</h2>
|
|
290
|
+
<CameraView
|
|
291
|
+
labels={labels}
|
|
292
|
+
onCapture={onCapture}
|
|
293
|
+
onCancel={() => setMode("menu")}
|
|
294
|
+
/>
|
|
295
|
+
</>
|
|
296
|
+
) : (
|
|
297
|
+
<>
|
|
298
|
+
<Ambient
|
|
299
|
+
src={image}
|
|
300
|
+
className="flex flex-col items-center gap-3 px-5 pt-8 pb-6 text-center"
|
|
301
|
+
>
|
|
302
|
+
<ProfileImage className="size-28 shadow-[0_12px_32px_-12px_rgb(0_0_0/0.45)] ring-1 ring-white/50">
|
|
303
|
+
<ProfileImageContent
|
|
304
|
+
src={image}
|
|
305
|
+
alt={label}
|
|
306
|
+
/>
|
|
307
|
+
<ProfileImageFallback className="text-3xl">
|
|
308
|
+
{initials}
|
|
309
|
+
</ProfileImageFallback>
|
|
310
|
+
</ProfileImage>
|
|
311
|
+
<div className="flex flex-col gap-0.5">
|
|
312
|
+
<h2>{labels.photo}</h2>
|
|
313
|
+
<p>{labels.photoDescription}</p>
|
|
314
|
+
</div>
|
|
315
|
+
</Ambient>
|
|
316
|
+
|
|
317
|
+
<div className="m-3 flex flex-col divide-y divide-border/40 overflow-hidden rounded-2xl border border-border/35 bg-card shadow-xs">
|
|
318
|
+
<Option
|
|
319
|
+
icon={Image01Icon}
|
|
320
|
+
onClick={onChooseFile}
|
|
321
|
+
>
|
|
322
|
+
{labels.choosePhoto}
|
|
323
|
+
</Option>
|
|
324
|
+
{canUseCamera && (
|
|
325
|
+
<Option
|
|
326
|
+
icon={Camera01Icon}
|
|
327
|
+
onClick={() =>
|
|
328
|
+
setMode("camera")
|
|
329
|
+
}
|
|
330
|
+
>
|
|
331
|
+
{labels.takePhoto}
|
|
332
|
+
</Option>
|
|
333
|
+
)}
|
|
334
|
+
<Option
|
|
335
|
+
icon={TextFontIcon}
|
|
336
|
+
onClick={() =>
|
|
337
|
+
setMonogramOpen(
|
|
338
|
+
(current) => !current
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
>
|
|
342
|
+
{labels.monogram}
|
|
343
|
+
</Option>
|
|
344
|
+
{monogramOpen && (
|
|
345
|
+
<div className="flex items-center justify-between gap-2 px-4 py-3">
|
|
346
|
+
{MONOGRAM_GRADIENTS.map(
|
|
347
|
+
(gradient) => (
|
|
348
|
+
<button
|
|
349
|
+
key={gradient[0]}
|
|
350
|
+
type="button"
|
|
351
|
+
aria-label={`${labels.monogram} ${gradient[0]}`}
|
|
352
|
+
onClick={() =>
|
|
353
|
+
void renderMonogram(
|
|
354
|
+
initials,
|
|
355
|
+
gradient
|
|
356
|
+
).then(
|
|
357
|
+
onChoose
|
|
358
|
+
)
|
|
359
|
+
}
|
|
360
|
+
className="flex size-10 items-center justify-center rounded-full text-sm font-semibold text-white shadow-xs ring-1 ring-black/5 transition-transform hover:scale-105 active:scale-95"
|
|
361
|
+
style={{
|
|
362
|
+
backgroundImage: `linear-gradient(${gradient[0]}, ${gradient[1]})`,
|
|
363
|
+
}}
|
|
364
|
+
>
|
|
365
|
+
{initials}
|
|
366
|
+
</button>
|
|
367
|
+
)
|
|
368
|
+
)}
|
|
369
|
+
</div>
|
|
370
|
+
)}
|
|
371
|
+
{clearable && image && (
|
|
372
|
+
<Option
|
|
373
|
+
icon={Delete02Icon}
|
|
374
|
+
tone="destructive"
|
|
375
|
+
onClick={onRemove}
|
|
376
|
+
>
|
|
377
|
+
{labels.removePhoto}
|
|
378
|
+
</Option>
|
|
379
|
+
)}
|
|
380
|
+
</div>
|
|
381
|
+
|
|
382
|
+
<div className="px-3 pb-3">
|
|
383
|
+
<Button
|
|
384
|
+
type="button"
|
|
385
|
+
variant="secondary"
|
|
386
|
+
className="w-full rounded-2xl"
|
|
387
|
+
onClick={() =>
|
|
388
|
+
onOpenChange(false)
|
|
389
|
+
}
|
|
390
|
+
>
|
|
391
|
+
{labels.cancel}
|
|
392
|
+
</Button>
|
|
393
|
+
</div>
|
|
394
|
+
</>
|
|
395
|
+
)}
|
|
396
|
+
</Sheet>,
|
|
397
|
+
document.body
|
|
398
|
+
)
|
|
399
|
+
}
|