@sereno-ds/ui 0.36.0 → 0.36.2
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/_internal/Field.d.ts +4 -1
- package/dist/_internal/Field.js +2 -0
- package/dist/_internal/Field.js.map +1 -1
- package/dist/_internal/fieldA11y.d.ts +34 -0
- package/dist/_internal/fieldA11y.js +15 -0
- package/dist/_internal/fieldA11y.js.map +1 -0
- package/dist/feedback/Dialog.d.ts +9 -1
- package/dist/feedback/Dialog.js +3 -1
- package/dist/feedback/Dialog.js.map +1 -1
- package/dist/forms/AvatarUpload.js +153 -120
- package/dist/forms/AvatarUpload.js.map +1 -1
- package/dist/forms/Checkbox.js +1 -0
- package/dist/forms/Checkbox.js.map +1 -1
- package/dist/forms/DatePicker.js +2 -0
- package/dist/forms/DatePicker.js.map +1 -1
- package/dist/forms/FileUpload.js +7 -2
- package/dist/forms/FileUpload.js.map +1 -1
- package/dist/forms/Input.js +5 -0
- package/dist/forms/Input.js.map +1 -1
- package/dist/forms/Select.js +4 -0
- package/dist/forms/Select.js.map +1 -1
- package/dist/forms/Textarea.js +22 -1
- package/dist/forms/Textarea.js.map +1 -1
- package/dist/styles.css +3 -0
- package/package.json +1 -1
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Label + hint/error wrapper shared by the form controls. The hint / error line gets an
|
|
5
|
+
* `id` (see `fieldA11y`) so the control can name it in `aria-describedby`.
|
|
6
|
+
*/
|
|
4
7
|
interface FieldProps {
|
|
5
8
|
label?: string;
|
|
6
9
|
hint?: string;
|
package/dist/_internal/Field.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { sx } from "./style.js";
|
|
4
|
+
import { fieldMessageId } from "./fieldA11y.js";
|
|
4
5
|
function Field({ label, hint, error, required, htmlFor, counter, preserveHelperSpace = false, children, style }) {
|
|
5
6
|
return (
|
|
6
7
|
// No `gap` here — the label→field and field→helper gaps are deliberately
|
|
@@ -42,6 +43,7 @@ function Field({ label, hint, error, required, htmlFor, counter, preserveHelperS
|
|
|
42
43
|
(error || hint) && /* @__PURE__ */ jsx(
|
|
43
44
|
"span",
|
|
44
45
|
{
|
|
46
|
+
id: htmlFor ? fieldMessageId(htmlFor) : void 0,
|
|
45
47
|
style: sx({
|
|
46
48
|
fontFamily: "var(--font-body)",
|
|
47
49
|
fontSize: "var(--text-xs)",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/_internal/Field.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { sx } from './style';\n\n
|
|
1
|
+
{"version":3,"sources":["../../src/_internal/Field.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { sx } from './style';\nimport { fieldMessageId } from './fieldA11y';\n\n/**\n * Label + hint/error wrapper shared by the form controls. The hint / error line gets an\n * `id` (see `fieldA11y`) so the control can name it in `aria-describedby`.\n */\nexport interface FieldProps {\n label?: string;\n hint?: string;\n error?: string;\n required?: boolean;\n htmlFor?: string;\n /** Right-aligned node on the hint row (e.g. a character counter). */\n counter?: React.ReactNode;\n /**\n * Reserve the hint/error row's height even when there's nothing to show —\n * keeps the field's own height stable as `error`/`hint` come and go (e.g.\n * several fields in a form invalidating at once), instead of every field\n * growing the moment a message appears. Off by default: most fields don't\n * need the empty gap when there's nothing under them.\n */\n preserveHelperSpace?: boolean;\n children?: React.ReactNode;\n style?: React.CSSProperties;\n}\n\nexport function Field({ label, hint, error, required, htmlFor, counter, preserveHelperSpace = false, children, style }: FieldProps) {\n return (\n // No `gap` here — the label→field and field→helper gaps are deliberately\n // different sizes (below), not one uniform rhythm.\n <div style={sx({ display: 'flex', flexDirection: 'column', ...style })}>\n {label && (\n <label\n htmlFor={htmlFor}\n style={sx({\n fontFamily: 'var(--font-body)',\n fontSize: 'var(--text-sm)',\n fontWeight: 'var(--weight-semibold)',\n color: 'var(--text-primary)',\n letterSpacing: 'var(--tracking-snug)',\n marginBottom: 'var(--space-2)',\n })}\n >\n {label}\n {required && <span style={sx({ color: 'var(--interactive-error)', marginLeft: 3 })}>*</span>}\n </label>\n )}\n {children}\n {(error || hint || counter || preserveHelperSpace) && (\n <div\n style={sx({\n display: 'flex',\n alignItems: 'baseline',\n gap: 'var(--space-3)',\n marginTop: 'var(--space-1)',\n // One line's worth of height, reserved up front — so text\n // appearing/disappearing never changes the row's own size.\n minHeight: preserveHelperSpace ? 'calc(var(--text-xs) * 1.45)' : undefined,\n justifyContent: error || hint ? 'space-between' : 'flex-end',\n })}\n >\n {(error || hint) && (\n <span\n id={htmlFor ? fieldMessageId(htmlFor) : undefined}\n style={sx({\n fontFamily: 'var(--font-body)',\n fontSize: 'var(--text-xs)',\n lineHeight: 1.45,\n color: error ? 'var(--interactive-error)' : 'var(--text-muted)',\n })}\n >\n {error || hint}\n </span>\n )}\n {counter}\n </div>\n )}\n </div>\n );\n}"],"mappings":";AAoCQ,SAYe,KAZf;AAjCR,SAAS,UAAU;AACnB,SAAS,sBAAsB;AA0BxB,SAAS,MAAM,EAAE,OAAO,MAAM,OAAO,UAAU,SAAS,SAAS,sBAAsB,OAAO,UAAU,MAAM,GAAe;AAClI;AAAA;AAAA;AAAA,IAGE,qBAAC,SAAI,OAAO,GAAG,EAAE,SAAS,QAAQ,eAAe,UAAU,GAAG,MAAM,CAAC,GAClE;AAAA,eACC;AAAA,QAAC;AAAA;AAAA,UACC;AAAA,UACA,OAAO,GAAG;AAAA,YACR,YAAY;AAAA,YACZ,UAAU;AAAA,YACV,YAAY;AAAA,YACZ,OAAO;AAAA,YACP,eAAe;AAAA,YACf,cAAc;AAAA,UAChB,CAAC;AAAA,UAEA;AAAA;AAAA,YACA,YAAY,oBAAC,UAAK,OAAO,GAAG,EAAE,OAAO,4BAA4B,YAAY,EAAE,CAAC,GAAG,eAAC;AAAA;AAAA;AAAA,MACvF;AAAA,MAED;AAAA,OACC,SAAS,QAAQ,WAAW,wBAC5B;AAAA,QAAC;AAAA;AAAA,UACC,OAAO,GAAG;AAAA,YACR,SAAS;AAAA,YACT,YAAY;AAAA,YACZ,KAAK;AAAA,YACL,WAAW;AAAA;AAAA;AAAA,YAGX,WAAW,sBAAsB,gCAAgC;AAAA,YACjE,gBAAgB,SAAS,OAAO,kBAAkB;AAAA,UACpD,CAAC;AAAA,UAEC;AAAA,sBAAS,SACT;AAAA,cAAC;AAAA;AAAA,gBACC,IAAI,UAAU,eAAe,OAAO,IAAI;AAAA,gBACxC,OAAO,GAAG;AAAA,kBACR,YAAY;AAAA,kBACZ,UAAU;AAAA,kBACV,YAAY;AAAA,kBACZ,OAAO,QAAQ,6BAA6B;AAAA,gBAC9C,CAAC;AAAA,gBAEA,mBAAS;AAAA;AAAA,YACZ;AAAA,YAED;AAAA;AAAA;AAAA,MACH;AAAA,OAEJ;AAAA;AAEJ;","names":[]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `id` of the hint / error line `Field` renders for the control whose own id is
|
|
5
|
+
* `controlId`. `Field` stamps it on the line and `fieldA11y` points the control at it, both
|
|
6
|
+
* through this one function, so the two can never drift apart.
|
|
7
|
+
*/
|
|
8
|
+
declare function fieldMessageId(controlId: string): string;
|
|
9
|
+
interface FieldA11yInput {
|
|
10
|
+
hint?: string;
|
|
11
|
+
error?: string;
|
|
12
|
+
/** The consumer's own `aria-describedby`, kept (never replaced). */
|
|
13
|
+
describedBy?: string;
|
|
14
|
+
/** The consumer's own `aria-invalid`, used only when there is no `error` to say so. */
|
|
15
|
+
invalid?: React.AriaAttributes['aria-invalid'];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The two attributes that tie a form control to its `Field` message, so a screen reader
|
|
19
|
+
* says the control is invalid and reads the error (or the hint) when it takes focus:
|
|
20
|
+
*
|
|
21
|
+
* - `aria-invalid` is `true` when there is an `error`, and otherwise whatever the consumer
|
|
22
|
+
* passed (nothing, by default, so a valid field has no attribute at all).
|
|
23
|
+
* - `aria-describedby` lists the message's id while there is an `error` or a `hint` (the
|
|
24
|
+
* error already replaces the hint, so it is one id either way), then the consumer's own
|
|
25
|
+
* ids. Absent when there is nothing to point at.
|
|
26
|
+
*
|
|
27
|
+
* Spread the result on the element that takes focus, after any `{...rest}`.
|
|
28
|
+
*/
|
|
29
|
+
declare function fieldA11y(controlId: string, { hint, error, describedBy, invalid }: FieldA11yInput): {
|
|
30
|
+
readonly 'aria-invalid': boolean | "true" | "false" | "grammar" | "spelling" | undefined;
|
|
31
|
+
readonly 'aria-describedby': string | undefined;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { type FieldA11yInput, fieldA11y, fieldMessageId };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function fieldMessageId(controlId) {
|
|
2
|
+
return `${controlId}-message`;
|
|
3
|
+
}
|
|
4
|
+
function fieldA11y(controlId, { hint, error, describedBy, invalid }) {
|
|
5
|
+
const ids = [error || hint ? fieldMessageId(controlId) : "", describedBy?.trim() ?? ""].filter(Boolean).join(" ");
|
|
6
|
+
return {
|
|
7
|
+
"aria-invalid": error ? true : invalid,
|
|
8
|
+
"aria-describedby": ids || void 0
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
fieldA11y,
|
|
13
|
+
fieldMessageId
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=fieldA11y.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/_internal/fieldA11y.ts"],"sourcesContent":["import type * as React from 'react';\n\n/**\n * The `id` of the hint / error line `Field` renders for the control whose own id is\n * `controlId`. `Field` stamps it on the line and `fieldA11y` points the control at it, both\n * through this one function, so the two can never drift apart.\n */\nexport function fieldMessageId(controlId: string): string {\n return `${controlId}-message`;\n}\n\nexport interface FieldA11yInput {\n hint?: string;\n error?: string;\n /** The consumer's own `aria-describedby`, kept (never replaced). */\n describedBy?: string;\n /** The consumer's own `aria-invalid`, used only when there is no `error` to say so. */\n invalid?: React.AriaAttributes['aria-invalid'];\n}\n\n/**\n * The two attributes that tie a form control to its `Field` message, so a screen reader\n * says the control is invalid and reads the error (or the hint) when it takes focus:\n *\n * - `aria-invalid` is `true` when there is an `error`, and otherwise whatever the consumer\n * passed (nothing, by default, so a valid field has no attribute at all).\n * - `aria-describedby` lists the message's id while there is an `error` or a `hint` (the\n * error already replaces the hint, so it is one id either way), then the consumer's own\n * ids. Absent when there is nothing to point at.\n *\n * Spread the result on the element that takes focus, after any `{...rest}`.\n */\nexport function fieldA11y(controlId: string, { hint, error, describedBy, invalid }: FieldA11yInput) {\n const ids = [error || hint ? fieldMessageId(controlId) : '', describedBy?.trim() ?? ''].filter(Boolean).join(' ');\n return {\n 'aria-invalid': error ? true : invalid,\n 'aria-describedby': ids || undefined,\n } as const;\n}\n"],"mappings":"AAOO,SAAS,eAAe,WAA2B;AACxD,SAAO,GAAG,SAAS;AACrB;AAuBO,SAAS,UAAU,WAAmB,EAAE,MAAM,OAAO,aAAa,QAAQ,GAAmB;AAClG,QAAM,MAAM,CAAC,SAAS,OAAO,eAAe,SAAS,IAAI,IAAI,aAAa,KAAK,KAAK,EAAE,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG;AAChH,SAAO;AAAA,IACL,gBAAgB,QAAQ,OAAO;AAAA,IAC/B,oBAAoB,OAAO;AAAA,EAC7B;AACF;","names":[]}
|
|
@@ -55,11 +55,19 @@ interface DialogBodyProps {
|
|
|
55
55
|
interface DialogFooterProps {
|
|
56
56
|
/** Action buttons, right-aligned. */
|
|
57
57
|
children?: React.ReactNode;
|
|
58
|
+
/**
|
|
59
|
+
* Let the buttons share the row and grow to fill it, and each line if they wrap, instead
|
|
60
|
+
* of hugging the right edge at their natural width. For a footer with three actions or
|
|
61
|
+
* long (translated) labels, where a plain wrap leaves the last button, usually the
|
|
62
|
+
* primary one, alone, small and pushed right on a second line. Off by default, so no
|
|
63
|
+
* existing dialog changes.
|
|
64
|
+
*/
|
|
65
|
+
fill?: boolean;
|
|
58
66
|
}
|
|
59
67
|
declare function DialogRoot({ open, children, onClose, variant, size, dividers, dismissible, width, style, ...rest }: DialogProps): React.ReactPortal | null;
|
|
60
68
|
declare function Header({ title, description, children }: DialogHeaderProps): React.JSX.Element;
|
|
61
69
|
declare function Body({ children }: DialogBodyProps): React.JSX.Element;
|
|
62
|
-
declare function Footer({ children }: DialogFooterProps): React.JSX.Element;
|
|
70
|
+
declare function Footer({ children, fill }: DialogFooterProps): React.JSX.Element;
|
|
63
71
|
declare function Close(): React.JSX.Element;
|
|
64
72
|
declare const Dialog: typeof DialogRoot & {
|
|
65
73
|
Header: typeof Header;
|
package/dist/feedback/Dialog.js
CHANGED
|
@@ -190,13 +190,15 @@ function Body({ children }) {
|
|
|
190
190
|
}
|
|
191
191
|
);
|
|
192
192
|
}
|
|
193
|
-
function Footer({ children }) {
|
|
193
|
+
function Footer({ children, fill = false }) {
|
|
194
194
|
const { dividers } = useDialogContext("Footer");
|
|
195
195
|
const pad = "var(--space-5)";
|
|
196
196
|
const hairline = "var(--border-width-hairline) solid var(--border-default)";
|
|
197
197
|
return /* @__PURE__ */ jsx(
|
|
198
198
|
"div",
|
|
199
199
|
{
|
|
200
|
+
className: "sereno-dialog-footer",
|
|
201
|
+
"data-fill": fill || void 0,
|
|
200
202
|
style: sx({
|
|
201
203
|
flex: "0 0 auto",
|
|
202
204
|
display: "flex",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/feedback/Dialog.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createPortal } from 'react-dom';\nimport { X } from 'lucide-react';\nimport { sx } from '../_internal/style';\nimport { Typography } from '../core/Typography';\n\n/**\n * Modal (desktop), bottom sheet (mobile) or full-screen — a **compound\n * component**. Portalled to `<body>` and fixed to the viewport, so no\n * ancestor's `overflow`/`transform`/positioning can trap it. While open it\n * locks page scroll and closes on `Escape`. Host needs the `sereno-pop` /\n * `sereno-slide-up` keyframes (see globals.css).\n *\n * ```tsx\n * <Dialog open={open} onClose={close} footer={false}>\n * <Dialog.Header title=\"Cancel booking?\" description=\"…\">\n * <Dialog.Close />\n * </Dialog.Header>\n * <Dialog.Body>…</Dialog.Body>\n * <Dialog.Footer>\n * <Button variant=\"ghost\" onClick={close}>Back</Button>\n * <Button variant=\"error\" onClick={confirm}>Cancel booking</Button>\n * </Dialog.Footer>\n * </Dialog>\n * ```\n *\n * `Dialog.Header` / `Dialog.Footer` are both optional — a dialog can be just\n * a `Dialog.Body`. `Dialog.Close` goes wherever you put it (typically inside\n * `Dialog.Header`); nothing renders a close button unless you add one.\n */\nexport interface DialogProps extends React.HTMLAttributes<HTMLDivElement> {\n open?: boolean;\n onClose?: () => void;\n /** `sheet` slides up from the bottom (mobile default); `fullscreen` fills the viewport. */\n variant?: 'center' | 'sheet' | 'fullscreen';\n /** Max width of the centered modal. Ignored for `sheet` / `fullscreen`. */\n size?: 'sm' | 'md' | 'lg' | 'xl';\n /** Hairline rules between header / body / footer, MUI-style. The body scrolls on its own. */\n dividers?: boolean;\n /**\n * When `false`, a scrim click and `Escape` no longer close the dialog — only\n * `Dialog.Close`, a footer action, or `open={false}` do. Use it for a choice\n * the user must make explicitly. Defaults to `true`.\n */\n dismissible?: boolean;\n /** Explicit pixel width — overrides `size`. */\n width?: number;\n children?: React.ReactNode;\n}\n\nexport interface DialogHeaderProps {\n title?: string;\n description?: string;\n /** Extra content below the description — or the whole header, if you skip `title`/`description`. Put `Dialog.Close` here. */\n children?: React.ReactNode;\n}\n\nexport interface DialogBodyProps {\n children?: React.ReactNode;\n}\n\nexport interface DialogFooterProps {\n /** Action buttons, right-aligned. */\n children?: React.ReactNode;\n}\n\ninterface DialogContextValue {\n titleId: string;\n onClose?: () => void;\n full: boolean;\n dividers: boolean;\n hasHeader: boolean;\n hasFooter: boolean;\n}\n\nconst SIZE_W = { sm: 440, md: 600, lg: 800, xl: 1000 } as const;\n\n// Cheap, dependency-free focusable check — matches what most hand-rolled\n// focus traps use. No visibility filtering: nothing inside a Dialog is ever\n// conditionally hidden today, and jsdom doesn't compute layout (offsetParent\n// is always null there), so a visibility check would be untestable dead\n// weight rather than a real safeguard.\nconst FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex=\"-1\"])';\nfunction focusableIn(el: HTMLElement): HTMLElement[] {\n return Array.from(el.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR));\n}\n\nconst DialogContext = React.createContext<DialogContextValue | null>(null);\n\nfunction useDialogContext(component: string): DialogContextValue {\n const ctx = React.useContext(DialogContext);\n if (!ctx) throw new Error(`<Dialog.${component}> must be rendered inside <Dialog>.`);\n return ctx;\n}\n\nfunction DialogRoot({\n open = true,\n children,\n onClose,\n variant = 'center',\n size = 'sm',\n dividers = false,\n dismissible = true,\n width,\n style,\n ...rest\n}: DialogProps) {\n const sheet = variant === 'sheet';\n const full = variant === 'fullscreen';\n const rid = React.useId();\n const titleId = `${rid}-title`;\n\n // Portal target is client-only.\n const [mounted, setMounted] = React.useState(false);\n // eslint-disable-next-line react-hooks/set-state-in-effect -- portal target is client-only\n React.useEffect(() => setMounted(true), []);\n\n // While open: lock page scroll, bind Escape to close, trap Tab inside the\n // panel, and restore focus to whatever had it before opening once closed.\n // `onClose` / `dismissible` are read through refs so the effect only\n // re-runs when `open` flips.\n const onCloseRef = React.useRef(onClose);\n const dismissibleRef = React.useRef(dismissible);\n React.useEffect(() => {\n onCloseRef.current = onClose;\n dismissibleRef.current = dismissible;\n });\n const panelRef = React.useRef<HTMLDivElement>(null);\n React.useEffect(() => {\n if (!open) return;\n const previouslyFocused = document.activeElement as HTMLElement | null;\n const root = document.documentElement;\n const prev = { h: root.style.overflow, b: document.body.style.overflow };\n root.style.overflow = 'hidden';\n document.body.style.overflow = 'hidden';\n const onKey = (e: KeyboardEvent) => {\n if (e.key === 'Escape') {\n if (dismissibleRef.current) onCloseRef.current?.();\n return;\n }\n if (e.key !== 'Tab') return;\n const panel = panelRef.current;\n if (!panel) return;\n const focusables = focusableIn(panel);\n // No focusable content (a bare Dialog.Body with plain text): keep\n // focus pinned on the panel itself rather than leaking to the page.\n if (focusables.length === 0) {\n e.preventDefault();\n return;\n }\n const first = focusables[0];\n const last = focusables[focusables.length - 1];\n if (e.shiftKey && document.activeElement === first) {\n e.preventDefault();\n last.focus();\n } else if (!e.shiftKey && document.activeElement === last) {\n e.preventDefault();\n first.focus();\n }\n };\n document.addEventListener('keydown', onKey);\n return () => {\n root.style.overflow = prev.h;\n document.body.style.overflow = prev.b;\n document.removeEventListener('keydown', onKey);\n // The trigger may itself have unmounted (e.g. a row it lived in was\n // removed) — focus() on a detached element is a silent no-op, not a\n // throw, so no need to guard beyond the null check.\n previouslyFocused?.focus();\n };\n }, [open]);\n\n // Pull focus into the dialog when it opens.\n React.useEffect(() => {\n if (open && mounted) panelRef.current?.focus();\n }, [open, mounted]);\n\n if (!open || !mounted) return null;\n\n // Body/Footer padding depends on whether a Header/Footer is actually\n // present — read straight off the children, the same technique\n // SidebarNav.Item / Stepper use for data they need before render.\n const items = React.Children.toArray(children);\n const hasHeader = items.some((c) => React.isValidElement(c) && c.type === Header);\n const hasFooter = items.some((c) => React.isValidElement(c) && c.type === Footer);\n\n const ctx: DialogContextValue = { titleId, onClose, full, dividers, hasHeader, hasFooter };\n\n return createPortal(\n <div\n style={sx({\n position: 'fixed',\n inset: 0,\n zIndex: 1000,\n display: 'flex',\n alignItems: sheet ? 'flex-end' : 'center',\n justifyContent: 'center',\n background: 'var(--bg-overlay)',\n backdropFilter: 'blur(2px)',\n padding: sheet || full ? 0 : 'var(--space-5)',\n })}\n onClick={dismissible ? onClose : undefined}\n >\n <div\n ref={panelRef}\n role=\"dialog\"\n aria-modal=\"true\"\n aria-labelledby={hasHeader ? titleId : undefined}\n tabIndex={-1}\n onClick={(e) => e.stopPropagation()}\n {...rest}\n style={sx({\n width: full || sheet ? '100%' : 'min(100%,' + (width ?? SIZE_W[size]) + 'px)',\n height: full ? '100%' : undefined,\n maxHeight: full ? undefined : sheet ? '90dvh' : 'calc(100dvh - var(--space-6))',\n display: 'flex',\n flexDirection: 'column',\n overflow: 'hidden',\n outline: 'none',\n background: 'var(--bg-surface)',\n borderRadius: full ? 0 : sheet ? 'var(--radius-sheet) var(--radius-sheet) 0 0' : 'var(--radius-lg)',\n border: full ? 'none' : 'var(--border-width-hairline) solid var(--border-default)',\n boxShadow: full ? 'none' : sheet ? 'var(--shadow-sheet)' : 'var(--shadow-lg)',\n animation: sheet ? 'sereno-slide-up var(--duration-sheet) var(--ease-gentle)' : 'sereno-pop var(--duration-normal) var(--ease-out)',\n ...style,\n })}\n >\n {sheet && (\n <span\n aria-hidden\n style={sx({ width: 36, height: 4, borderRadius: '999px', background: 'var(--border-strong)', alignSelf: 'center', marginTop: 'var(--space-2)', flex: '0 0 auto' })}\n />\n )}\n <DialogContext.Provider value={ctx}>{children}</DialogContext.Provider>\n </div>\n </div>,\n document.body,\n );\n}\n\nfunction Header({ title, description, children }: DialogHeaderProps) {\n const { titleId, dividers } = useDialogContext('Header');\n const pad = 'var(--space-5)';\n const hairline = 'var(--border-width-hairline) solid var(--border-default)';\n\n const items = React.Children.toArray(children);\n const closeEl = items.find((c) => React.isValidElement(c) && c.type === Close);\n const extra = items.filter((c) => c !== closeEl);\n\n return (\n <div\n style={sx({\n flex: '0 0 auto',\n display: 'flex',\n alignItems: 'flex-start',\n gap: 'var(--space-3)',\n padding: dividers ? pad : `${pad} ${pad} var(--space-3)`,\n borderBottom: dividers ? hairline : undefined,\n })}\n >\n <div id={titleId} style={sx({ flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', gap: 'var(--space-2)' })}>\n {title && (\n <Typography as=\"h3\" variant=\"h2\" style={{ letterSpacing: 'var(--tracking-tight)' }}>\n {title}\n </Typography>\n )}\n {description && (\n <Typography variant=\"body\" color=\"secondary\">\n {description}\n </Typography>\n )}\n {extra}\n </div>\n {closeEl}\n </div>\n );\n}\n\nfunction Body({ children }: DialogBodyProps) {\n const { dividers, hasHeader, hasFooter, full } = useDialogContext('Body');\n const pad = 'var(--space-5)';\n return (\n <div\n style={sx({\n flex: full ? '1 1 auto' : '0 1 auto',\n minHeight: 0,\n overflowY: 'auto',\n padding: dividers ? pad : hasHeader ? `0 ${pad} ${hasFooter ? 'var(--space-3)' : pad}` : pad,\n })}\n >\n {children}\n </div>\n );\n}\n\nfunction Footer({ children }: DialogFooterProps) {\n const { dividers } = useDialogContext('Footer');\n const pad = 'var(--space-5)';\n const hairline = 'var(--border-width-hairline) solid var(--border-default)';\n return (\n <div\n style={sx({\n flex: '0 0 auto',\n display: 'flex',\n gap: 'var(--space-3)',\n justifyContent: 'flex-end',\n flexWrap: 'wrap',\n padding: dividers ? pad : `0 ${pad} ${pad}`,\n borderTop: dividers ? hairline : undefined,\n })}\n >\n {children}\n </div>\n );\n}\n\nfunction Close() {\n const { onClose, full } = useDialogContext('Close');\n return (\n <button\n type=\"button\"\n className=\"sereno-dialog-close\"\n aria-label=\"Fechar\"\n onClick={onClose}\n style={sx({\n flex: '0 0 auto',\n display: 'inline-flex',\n alignItems: 'center',\n justifyContent: 'center',\n width: full ? 40 : 34,\n height: full ? 40 : 34,\n margin: full ? 0 : '-3px -5px 0 0',\n border: 'none',\n borderRadius: 'var(--radius-md)',\n background: full ? 'var(--bg-subtle)' : 'transparent',\n color: 'var(--text-secondary)',\n cursor: 'pointer',\n })}\n >\n <X size={full ? 22 : 18} strokeWidth={2} />\n </button>\n );\n}\n\nexport const Dialog = Object.assign(DialogRoot, { Header, Body, Footer, Close });\n"],"mappings":";AA6MM,SAyBI,KAzBJ;AA3MN,YAAY,WAAW;AACvB,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAClB,SAAS,UA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3F,CAAC;AAAA,MAEA;AAAA;AAAA,EACH;AAEJ;AAEA,SAAS,OAAO,EAAE,SAAS,GAAsB;AAC/C,QAAM,EAAE,SAAS,IAAI,iBAAiB,QAAQ;AAC9C,QAAM,MAAM;AACZ,QAAM,WAAW;AACjB,SACE;AAAA,IAAC;AAAA;AAAA,MACC,OAAO,GAAG;AAAA,QACR,MAAM;AAAA,QACN,SAAS;AAAA,QACT,KAAK;AAAA,QACL,gBAAgB;AAAA,QAChB,UAAU;AAAA,QACV,SAAS,WAAW,MAAM,KAAK,GAAG,IAAI,GAAG;AAAA,QACzC,WAAW,WAAW,WAAW;AAAA,MACnC,CAAC;AAAA,MAEA;AAAA;AAAA,EACH;AAEJ;AAEA,SAAS,QAAQ;AACf,QAAM,EAAE,SAAS,KAAK,IAAI,iBAAiB,OAAO;AAClD,SACE;AAAA,IAAC;AAAA;AAAA,MACC,MAAK;AAAA,MACL,WAAU;AAAA,MACV,cAAW;AAAA,MACX,SAAS;AAAA,MACT,OAAO,GAAG;AAAA,QACR,MAAM;AAAA,QACN,SAAS;AAAA,QACT,YAAY;AAAA,QACZ,gBAAgB;AAAA,QAChB,OAAO,OAAO,KAAK;AAAA,QACnB,QAAQ,OAAO,KAAK;AAAA,QACpB,QAAQ,OAAO,IAAI;AAAA,QACnB,QAAQ;AAAA,QACR,cAAc;AAAA,QACd,YAAY,OAAO,qBAAqB;AAAA,QACxC,OAAO;AAAA,QACP,QAAQ;AAAA,MACV,CAAC;AAAA,MAED,8BAAC,KAAE,MAAM,OAAO,KAAK,IAAI,aAAa,GAAG;AAAA;AAAA,EAC3C;AAEJ;AAEO,MAAM,SAAS,OAAO,OAAO,YAAY,EAAE,QAAQ,MAAM,QAAQ,MAAM,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/feedback/Dialog.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createPortal } from 'react-dom';\nimport { X } from 'lucide-react';\nimport { sx } from '../_internal/style';\nimport { Typography } from '../core/Typography';\n\n/**\n * Modal (desktop), bottom sheet (mobile) or full-screen — a **compound\n * component**. Portalled to `<body>` and fixed to the viewport, so no\n * ancestor's `overflow`/`transform`/positioning can trap it. While open it\n * locks page scroll and closes on `Escape`. Host needs the `sereno-pop` /\n * `sereno-slide-up` keyframes (see globals.css).\n *\n * ```tsx\n * <Dialog open={open} onClose={close} footer={false}>\n * <Dialog.Header title=\"Cancel booking?\" description=\"…\">\n * <Dialog.Close />\n * </Dialog.Header>\n * <Dialog.Body>…</Dialog.Body>\n * <Dialog.Footer>\n * <Button variant=\"ghost\" onClick={close}>Back</Button>\n * <Button variant=\"error\" onClick={confirm}>Cancel booking</Button>\n * </Dialog.Footer>\n * </Dialog>\n * ```\n *\n * `Dialog.Header` / `Dialog.Footer` are both optional — a dialog can be just\n * a `Dialog.Body`. `Dialog.Close` goes wherever you put it (typically inside\n * `Dialog.Header`); nothing renders a close button unless you add one.\n */\nexport interface DialogProps extends React.HTMLAttributes<HTMLDivElement> {\n open?: boolean;\n onClose?: () => void;\n /** `sheet` slides up from the bottom (mobile default); `fullscreen` fills the viewport. */\n variant?: 'center' | 'sheet' | 'fullscreen';\n /** Max width of the centered modal. Ignored for `sheet` / `fullscreen`. */\n size?: 'sm' | 'md' | 'lg' | 'xl';\n /** Hairline rules between header / body / footer, MUI-style. The body scrolls on its own. */\n dividers?: boolean;\n /**\n * When `false`, a scrim click and `Escape` no longer close the dialog — only\n * `Dialog.Close`, a footer action, or `open={false}` do. Use it for a choice\n * the user must make explicitly. Defaults to `true`.\n */\n dismissible?: boolean;\n /** Explicit pixel width — overrides `size`. */\n width?: number;\n children?: React.ReactNode;\n}\n\nexport interface DialogHeaderProps {\n title?: string;\n description?: string;\n /** Extra content below the description — or the whole header, if you skip `title`/`description`. Put `Dialog.Close` here. */\n children?: React.ReactNode;\n}\n\nexport interface DialogBodyProps {\n children?: React.ReactNode;\n}\n\nexport interface DialogFooterProps {\n /** Action buttons, right-aligned. */\n children?: React.ReactNode;\n /**\n * Let the buttons share the row and grow to fill it, and each line if they wrap, instead\n * of hugging the right edge at their natural width. For a footer with three actions or\n * long (translated) labels, where a plain wrap leaves the last button, usually the\n * primary one, alone, small and pushed right on a second line. Off by default, so no\n * existing dialog changes.\n */\n fill?: boolean;\n}\n\ninterface DialogContextValue {\n titleId: string;\n onClose?: () => void;\n full: boolean;\n dividers: boolean;\n hasHeader: boolean;\n hasFooter: boolean;\n}\n\nconst SIZE_W = { sm: 440, md: 600, lg: 800, xl: 1000 } as const;\n\n// Cheap, dependency-free focusable check — matches what most hand-rolled\n// focus traps use. No visibility filtering: nothing inside a Dialog is ever\n// conditionally hidden today, and jsdom doesn't compute layout (offsetParent\n// is always null there), so a visibility check would be untestable dead\n// weight rather than a real safeguard.\nconst FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex=\"-1\"])';\nfunction focusableIn(el: HTMLElement): HTMLElement[] {\n return Array.from(el.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR));\n}\n\nconst DialogContext = React.createContext<DialogContextValue | null>(null);\n\nfunction useDialogContext(component: string): DialogContextValue {\n const ctx = React.useContext(DialogContext);\n if (!ctx) throw new Error(`<Dialog.${component}> must be rendered inside <Dialog>.`);\n return ctx;\n}\n\nfunction DialogRoot({\n open = true,\n children,\n onClose,\n variant = 'center',\n size = 'sm',\n dividers = false,\n dismissible = true,\n width,\n style,\n ...rest\n}: DialogProps) {\n const sheet = variant === 'sheet';\n const full = variant === 'fullscreen';\n const rid = React.useId();\n const titleId = `${rid}-title`;\n\n // Portal target is client-only.\n const [mounted, setMounted] = React.useState(false);\n // eslint-disable-next-line react-hooks/set-state-in-effect -- portal target is client-only\n React.useEffect(() => setMounted(true), []);\n\n // While open: lock page scroll, bind Escape to close, trap Tab inside the\n // panel, and restore focus to whatever had it before opening once closed.\n // `onClose` / `dismissible` are read through refs so the effect only\n // re-runs when `open` flips.\n const onCloseRef = React.useRef(onClose);\n const dismissibleRef = React.useRef(dismissible);\n React.useEffect(() => {\n onCloseRef.current = onClose;\n dismissibleRef.current = dismissible;\n });\n const panelRef = React.useRef<HTMLDivElement>(null);\n React.useEffect(() => {\n if (!open) return;\n const previouslyFocused = document.activeElement as HTMLElement | null;\n const root = document.documentElement;\n const prev = { h: root.style.overflow, b: document.body.style.overflow };\n root.style.overflow = 'hidden';\n document.body.style.overflow = 'hidden';\n const onKey = (e: KeyboardEvent) => {\n if (e.key === 'Escape') {\n if (dismissibleRef.current) onCloseRef.current?.();\n return;\n }\n if (e.key !== 'Tab') return;\n const panel = panelRef.current;\n if (!panel) return;\n const focusables = focusableIn(panel);\n // No focusable content (a bare Dialog.Body with plain text): keep\n // focus pinned on the panel itself rather than leaking to the page.\n if (focusables.length === 0) {\n e.preventDefault();\n return;\n }\n const first = focusables[0];\n const last = focusables[focusables.length - 1];\n if (e.shiftKey && document.activeElement === first) {\n e.preventDefault();\n last.focus();\n } else if (!e.shiftKey && document.activeElement === last) {\n e.preventDefault();\n first.focus();\n }\n };\n document.addEventListener('keydown', onKey);\n return () => {\n root.style.overflow = prev.h;\n document.body.style.overflow = prev.b;\n document.removeEventListener('keydown', onKey);\n // The trigger may itself have unmounted (e.g. a row it lived in was\n // removed) — focus() on a detached element is a silent no-op, not a\n // throw, so no need to guard beyond the null check.\n previouslyFocused?.focus();\n };\n }, [open]);\n\n // Pull focus into the dialog when it opens.\n React.useEffect(() => {\n if (open && mounted) panelRef.current?.focus();\n }, [open, mounted]);\n\n if (!open || !mounted) return null;\n\n // Body/Footer padding depends on whether a Header/Footer is actually\n // present — read straight off the children, the same technique\n // SidebarNav.Item / Stepper use for data they need before render.\n const items = React.Children.toArray(children);\n const hasHeader = items.some((c) => React.isValidElement(c) && c.type === Header);\n const hasFooter = items.some((c) => React.isValidElement(c) && c.type === Footer);\n\n const ctx: DialogContextValue = { titleId, onClose, full, dividers, hasHeader, hasFooter };\n\n return createPortal(\n <div\n style={sx({\n position: 'fixed',\n inset: 0,\n zIndex: 1000,\n display: 'flex',\n alignItems: sheet ? 'flex-end' : 'center',\n justifyContent: 'center',\n background: 'var(--bg-overlay)',\n backdropFilter: 'blur(2px)',\n padding: sheet || full ? 0 : 'var(--space-5)',\n })}\n onClick={dismissible ? onClose : undefined}\n >\n <div\n ref={panelRef}\n role=\"dialog\"\n aria-modal=\"true\"\n aria-labelledby={hasHeader ? titleId : undefined}\n tabIndex={-1}\n onClick={(e) => e.stopPropagation()}\n {...rest}\n style={sx({\n width: full || sheet ? '100%' : 'min(100%,' + (width ?? SIZE_W[size]) + 'px)',\n height: full ? '100%' : undefined,\n maxHeight: full ? undefined : sheet ? '90dvh' : 'calc(100dvh - var(--space-6))',\n display: 'flex',\n flexDirection: 'column',\n overflow: 'hidden',\n outline: 'none',\n background: 'var(--bg-surface)',\n borderRadius: full ? 0 : sheet ? 'var(--radius-sheet) var(--radius-sheet) 0 0' : 'var(--radius-lg)',\n border: full ? 'none' : 'var(--border-width-hairline) solid var(--border-default)',\n boxShadow: full ? 'none' : sheet ? 'var(--shadow-sheet)' : 'var(--shadow-lg)',\n animation: sheet ? 'sereno-slide-up var(--duration-sheet) var(--ease-gentle)' : 'sereno-pop var(--duration-normal) var(--ease-out)',\n ...style,\n })}\n >\n {sheet && (\n <span\n aria-hidden\n style={sx({ width: 36, height: 4, borderRadius: '999px', background: 'var(--border-strong)', alignSelf: 'center', marginTop: 'var(--space-2)', flex: '0 0 auto' })}\n />\n )}\n <DialogContext.Provider value={ctx}>{children}</DialogContext.Provider>\n </div>\n </div>,\n document.body,\n );\n}\n\nfunction Header({ title, description, children }: DialogHeaderProps) {\n const { titleId, dividers } = useDialogContext('Header');\n const pad = 'var(--space-5)';\n const hairline = 'var(--border-width-hairline) solid var(--border-default)';\n\n const items = React.Children.toArray(children);\n const closeEl = items.find((c) => React.isValidElement(c) && c.type === Close);\n const extra = items.filter((c) => c !== closeEl);\n\n return (\n <div\n style={sx({\n flex: '0 0 auto',\n display: 'flex',\n alignItems: 'flex-start',\n gap: 'var(--space-3)',\n padding: dividers ? pad : `${pad} ${pad} var(--space-3)`,\n borderBottom: dividers ? hairline : undefined,\n })}\n >\n <div id={titleId} style={sx({ flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', gap: 'var(--space-2)' })}>\n {title && (\n <Typography as=\"h3\" variant=\"h2\" style={{ letterSpacing: 'var(--tracking-tight)' }}>\n {title}\n </Typography>\n )}\n {description && (\n <Typography variant=\"body\" color=\"secondary\">\n {description}\n </Typography>\n )}\n {extra}\n </div>\n {closeEl}\n </div>\n );\n}\n\nfunction Body({ children }: DialogBodyProps) {\n const { dividers, hasHeader, hasFooter, full } = useDialogContext('Body');\n const pad = 'var(--space-5)';\n return (\n <div\n style={sx({\n flex: full ? '1 1 auto' : '0 1 auto',\n minHeight: 0,\n overflowY: 'auto',\n padding: dividers ? pad : hasHeader ? `0 ${pad} ${hasFooter ? 'var(--space-3)' : pad}` : pad,\n })}\n >\n {children}\n </div>\n );\n}\n\nfunction Footer({ children, fill = false }: DialogFooterProps) {\n const { dividers } = useDialogContext('Footer');\n const pad = 'var(--space-5)';\n const hairline = 'var(--border-width-hairline) solid var(--border-default)';\n return (\n <div\n // `fill` (see styles.css) makes each child `flex: 1 1 auto`. It can't be inline: it's on the children.\n className=\"sereno-dialog-footer\"\n data-fill={fill || undefined}\n style={sx({\n flex: '0 0 auto',\n display: 'flex',\n gap: 'var(--space-3)',\n justifyContent: 'flex-end',\n flexWrap: 'wrap',\n padding: dividers ? pad : `0 ${pad} ${pad}`,\n borderTop: dividers ? hairline : undefined,\n })}\n >\n {children}\n </div>\n );\n}\n\nfunction Close() {\n const { onClose, full } = useDialogContext('Close');\n return (\n <button\n type=\"button\"\n className=\"sereno-dialog-close\"\n aria-label=\"Fechar\"\n onClick={onClose}\n style={sx({\n flex: '0 0 auto',\n display: 'inline-flex',\n alignItems: 'center',\n justifyContent: 'center',\n width: full ? 40 : 34,\n height: full ? 40 : 34,\n margin: full ? 0 : '-3px -5px 0 0',\n border: 'none',\n borderRadius: 'var(--radius-md)',\n background: full ? 'var(--bg-subtle)' : 'transparent',\n color: 'var(--text-secondary)',\n cursor: 'pointer',\n })}\n >\n <X size={full ? 22 : 18} strokeWidth={2} />\n </button>\n );\n}\n\nexport const Dialog = Object.assign(DialogRoot, { Header, Body, Footer, Close });\n"],"mappings":";AAqNM,SAyBI,KAzBJ;AAnNN,YAAY,WAAW;AACvB,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAClB,SAAS,UAAU;AACnB,SAAS,kBAAkB;AA+E3B,MAAM,SAAS,EAAE,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,IAAK;AAOrD,MAAM,qBAAqB;AAC3B,SAAS,YAAY,IAAgC;AACnD,SAAO,MAAM,KAAK,GAAG,iBAA8B,kBAAkB,CAAC;AACxE;AAEA,MAAM,gBAAgB,MAAM,cAAyC,IAAI;AAEzE,SAAS,iBAAiB,WAAuC;AAC/D,QAAM,MAAM,MAAM,WAAW,aAAa;AAC1C,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,WAAW,SAAS,qCAAqC;AACnF,SAAO;AACT;AAEA,SAAS,WAAW;AAAA,EAClB,OAAO;AAAA,EACP;AAAA,EACA;AAAA,EACA,UAAU;AAAA,EACV,OAAO;AAAA,EACP,WAAW;AAAA,EACX,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EACA,GAAG;AACL,GAAgB;AACd,QAAM,QAAQ,YAAY;AAC1B,QAAM,OAAO,YAAY;AACzB,QAAM,MAAM,MAAM,MAAM;AACxB,QAAM,UAAU,GAAG,GAAG;AAGtB,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,KAAK;AAElD,QAAM,UAAU,MAAM,WAAW,IAAI,GAAG,CAAC,CAAC;AAM1C,QAAM,aAAa,MAAM,OAAO,OAAO;AACvC,QAAM,iBAAiB,MAAM,OAAO,WAAW;AAC/C,QAAM,UAAU,MAAM;AACpB,eAAW,U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Y,OAAO,qBAAqB;AAAA,QACxC,OAAO;AAAA,QACP,QAAQ;AAAA,MACV,CAAC;AAAA,MAED,8BAAC,KAAE,MAAM,OAAO,KAAK,IAAI,aAAa,GAAG;AAAA;AAAA,EAC3C;AAEJ;AAEO,MAAM,SAAS,OAAO,OAAO,YAAY,EAAE,QAAQ,MAAM,QAAQ,MAAM,CAAC;","names":[]}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import { createPortal } from "react-dom";
|
|
5
5
|
import { Camera, Check, ImagePlus, Pencil, RotateCcw, Trash2, X, ZoomIn, ZoomOut } from "lucide-react";
|
|
6
6
|
import { sx } from "../_internal/style.js";
|
|
7
7
|
import { Field } from "../_internal/Field.js";
|
|
8
|
+
import { fieldA11y } from "../_internal/fieldA11y.js";
|
|
8
9
|
import { mergeRefs } from "../_internal/mergeRefs.js";
|
|
9
10
|
import { Dialog } from "../feedback/Dialog.js";
|
|
10
11
|
import { Button } from "../core/Button.js";
|
|
@@ -114,7 +115,8 @@ const AvatarUpload = React.forwardRef(function AvatarUpload2({
|
|
|
114
115
|
window.removeEventListener("resize", close);
|
|
115
116
|
};
|
|
116
117
|
}, [menuOpen]);
|
|
117
|
-
const
|
|
118
|
+
const step = cropSrc ? "crop" : allowCamera && cameraOpen ? "camera" : null;
|
|
119
|
+
const modalUp = step !== null;
|
|
118
120
|
React.useEffect(() => {
|
|
119
121
|
if (!modalUp) return;
|
|
120
122
|
const root = document.documentElement;
|
|
@@ -190,7 +192,9 @@ const AvatarUpload = React.forwardRef(function AvatarUpload2({
|
|
|
190
192
|
setCameraOpen(true);
|
|
191
193
|
};
|
|
192
194
|
const cameraBtn = Math.max(30, Math.round(size * 0.34));
|
|
193
|
-
|
|
195
|
+
const shownError = error || rejected || void 0;
|
|
196
|
+
const a11y = fieldA11y(rid, { hint, error: shownError });
|
|
197
|
+
return /* @__PURE__ */ jsxs(Field, { label, hint, error: shownError, required, htmlFor: rid, style: containerStyle, preserveHelperSpace, children: [
|
|
194
198
|
/* @__PURE__ */ jsx("input", { ref: mergeRefs(libRef, forwardedRef), id: rid, type: "file", accept: "image/*", disabled, onChange: (e) => pick(e.target.files), style: { display: "none" } }),
|
|
195
199
|
/* @__PURE__ */ jsxs("div", { style: sx({ position: "relative", width: size, height: size, flex: "0 0 auto", opacity: disabled ? 0.6 : 1 }), children: [
|
|
196
200
|
/* @__PURE__ */ jsx(
|
|
@@ -227,6 +231,7 @@ const AvatarUpload = React.forwardRef(function AvatarUpload2({
|
|
|
227
231
|
"aria-label": t.trigger,
|
|
228
232
|
"aria-haspopup": "menu",
|
|
229
233
|
"aria-expanded": menuOpen,
|
|
234
|
+
...a11y,
|
|
230
235
|
onClick: toggleMenu,
|
|
231
236
|
style: sx({
|
|
232
237
|
position: "absolute",
|
|
@@ -309,25 +314,19 @@ const AvatarUpload = React.forwardRef(function AvatarUpload2({
|
|
|
309
314
|
),
|
|
310
315
|
document.body
|
|
311
316
|
),
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
{
|
|
315
|
-
labels: t,
|
|
316
|
-
onClose: () => setCameraOpen(false),
|
|
317
|
-
onCapture: onCameraShot,
|
|
318
|
-
onError: () => {
|
|
319
|
-
setCameraOpen(false);
|
|
320
|
-
setRejected(t.cameraError);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
),
|
|
324
|
-
cropSrc && /* @__PURE__ */ jsx(
|
|
325
|
-
CropModal,
|
|
317
|
+
step && /* @__PURE__ */ jsx(
|
|
318
|
+
CaptureDialog,
|
|
326
319
|
{
|
|
320
|
+
step,
|
|
327
321
|
src: cropSrc,
|
|
328
322
|
outputSize,
|
|
329
323
|
labels: t,
|
|
330
|
-
onClose: onCropCancel,
|
|
324
|
+
onClose: step === "camera" ? () => setCameraOpen(false) : onCropCancel,
|
|
325
|
+
onCameraShot,
|
|
326
|
+
onCameraError: () => {
|
|
327
|
+
setCameraOpen(false);
|
|
328
|
+
setRejected(t.cameraError);
|
|
329
|
+
},
|
|
331
330
|
onSave: onCropSave,
|
|
332
331
|
onRetake: cropFromCamera ? onRetake : void 0
|
|
333
332
|
}
|
|
@@ -369,6 +368,8 @@ function MenuItem({ icon, label, onClick, danger }) {
|
|
|
369
368
|
);
|
|
370
369
|
}
|
|
371
370
|
const DEFAULT_STAGE = 280;
|
|
371
|
+
const MODAL_WIDTH = 440;
|
|
372
|
+
const ZOOM_ROW_HEIGHT = 24;
|
|
372
373
|
function CropStage({ children, onPointerDown, onPointerMove, onPointerUp, onWheel, stageRef }) {
|
|
373
374
|
return /* @__PURE__ */ jsxs(
|
|
374
375
|
"div",
|
|
@@ -409,15 +410,61 @@ function CropStage({ children, onPointerDown, onPointerMove, onPointerUp, onWhee
|
|
|
409
410
|
}
|
|
410
411
|
);
|
|
411
412
|
}
|
|
412
|
-
function
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
})
|
|
420
|
-
|
|
413
|
+
function useCamera(active, onError) {
|
|
414
|
+
const videoRef = React.useRef(null);
|
|
415
|
+
const streamRef = React.useRef(null);
|
|
416
|
+
const [ready, setReady] = React.useState(false);
|
|
417
|
+
const onErrorRef = React.useRef(onError);
|
|
418
|
+
React.useEffect(() => {
|
|
419
|
+
onErrorRef.current = onError;
|
|
420
|
+
});
|
|
421
|
+
React.useEffect(() => {
|
|
422
|
+
if (!active) return;
|
|
423
|
+
let cancelled = false;
|
|
424
|
+
let started = null;
|
|
425
|
+
const md = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
426
|
+
if (!md?.getUserMedia) {
|
|
427
|
+
onErrorRef.current();
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
md.getUserMedia({ video: { facingMode: "user", width: { ideal: 1280 }, height: { ideal: 1280 } }, audio: false }).then((s) => {
|
|
431
|
+
if (cancelled) {
|
|
432
|
+
s.getTracks().forEach((tr) => tr.stop());
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
started = s;
|
|
436
|
+
streamRef.current = s;
|
|
437
|
+
if (videoRef.current) videoRef.current.srcObject = s;
|
|
438
|
+
setReady(true);
|
|
439
|
+
}).catch(() => onErrorRef.current());
|
|
440
|
+
return () => {
|
|
441
|
+
cancelled = true;
|
|
442
|
+
started?.getTracks().forEach((tr) => tr.stop());
|
|
443
|
+
streamRef.current = null;
|
|
444
|
+
setReady(false);
|
|
445
|
+
};
|
|
446
|
+
}, [active]);
|
|
447
|
+
const attachVideo = React.useCallback((el) => {
|
|
448
|
+
videoRef.current = el;
|
|
449
|
+
if (el && streamRef.current) el.srcObject = streamRef.current;
|
|
450
|
+
}, []);
|
|
451
|
+
const shoot = (onShot) => {
|
|
452
|
+
const v = videoRef.current;
|
|
453
|
+
if (!v || !v.videoWidth) return;
|
|
454
|
+
const s = Math.min(v.videoWidth, v.videoHeight);
|
|
455
|
+
const canvas = document.createElement("canvas");
|
|
456
|
+
canvas.width = canvas.height = s;
|
|
457
|
+
const ctx = canvas.getContext("2d");
|
|
458
|
+
if (!ctx) return;
|
|
459
|
+
ctx.translate(s, 0);
|
|
460
|
+
ctx.scale(-1, 1);
|
|
461
|
+
ctx.drawImage(v, (v.videoWidth - s) / 2, (v.videoHeight - s) / 2, s, s, 0, 0, s, s);
|
|
462
|
+
canvas.toBlob((b) => b && onShot(b), "image/jpeg", 0.9);
|
|
463
|
+
};
|
|
464
|
+
return { attachVideo, ready, shoot };
|
|
465
|
+
}
|
|
466
|
+
function useCropper(src, outputSize) {
|
|
467
|
+
const [imgEl, setImgEl] = React.useState(null);
|
|
421
468
|
const [stageEl, setStageEl] = React.useState(null);
|
|
422
469
|
const [V, setV] = React.useState(DEFAULT_STAGE);
|
|
423
470
|
const prevV = React.useRef(V);
|
|
@@ -425,6 +472,13 @@ function CropModal({
|
|
|
425
472
|
const [zoom, setZoom] = React.useState(1);
|
|
426
473
|
const [offset, setOffset] = React.useState({ x: 0, y: 0 });
|
|
427
474
|
const drag = React.useRef(null);
|
|
475
|
+
const [seenSrc, setSeenSrc] = React.useState(src);
|
|
476
|
+
if (src !== seenSrc) {
|
|
477
|
+
setSeenSrc(src);
|
|
478
|
+
setNat(null);
|
|
479
|
+
setZoom(1);
|
|
480
|
+
setOffset({ x: 0, y: 0 });
|
|
481
|
+
}
|
|
428
482
|
React.useEffect(() => {
|
|
429
483
|
if (!stageEl) return;
|
|
430
484
|
const ro = new ResizeObserver((entries) => {
|
|
@@ -449,9 +503,7 @@ function CropModal({
|
|
|
449
503
|
}),
|
|
450
504
|
[V, dispW, dispH]
|
|
451
505
|
);
|
|
452
|
-
const onImgLoad = () => {
|
|
453
|
-
const el = imgRef.current;
|
|
454
|
-
if (!el) return;
|
|
506
|
+
const onImgLoad = (el) => {
|
|
455
507
|
const w = el.naturalWidth;
|
|
456
508
|
const h = el.naturalHeight;
|
|
457
509
|
setNat({ w, h });
|
|
@@ -485,107 +537,88 @@ function CropModal({
|
|
|
485
537
|
const onPointerUp = () => {
|
|
486
538
|
drag.current = null;
|
|
487
539
|
};
|
|
488
|
-
const save = () => {
|
|
489
|
-
|
|
490
|
-
if (!el || !nat) return;
|
|
540
|
+
const save = (onSave) => {
|
|
541
|
+
if (!imgEl || !nat) return;
|
|
491
542
|
const canvas = document.createElement("canvas");
|
|
492
543
|
canvas.width = canvas.height = outputSize;
|
|
493
544
|
const ctx = canvas.getContext("2d");
|
|
494
545
|
if (!ctx) return;
|
|
495
546
|
const sSize = V / scale;
|
|
496
|
-
ctx.drawImage(
|
|
547
|
+
ctx.drawImage(imgEl, -offset.x / scale, -offset.y / scale, sSize, sSize, 0, 0, outputSize, outputSize);
|
|
497
548
|
canvas.toBlob((b) => b && onSave(b), "image/jpeg", 0.85);
|
|
498
549
|
};
|
|
499
|
-
return
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
550
|
+
return { setImgEl, setStageEl, zoom, setZoomAt, offset, dispW, dispH, onImgLoad, onPointerDown, onPointerMove, onPointerUp, save };
|
|
551
|
+
}
|
|
552
|
+
function CaptureDialog({
|
|
553
|
+
step,
|
|
554
|
+
src,
|
|
555
|
+
outputSize,
|
|
556
|
+
labels,
|
|
557
|
+
onClose,
|
|
558
|
+
onCameraShot,
|
|
559
|
+
onCameraError,
|
|
560
|
+
onSave,
|
|
561
|
+
onRetake
|
|
562
|
+
}) {
|
|
563
|
+
const isCamera = step === "camera";
|
|
564
|
+
const camera = useCamera(isCamera, onCameraError);
|
|
565
|
+
const crop = useCropper(src, outputSize);
|
|
566
|
+
const [contentEl, setContentEl] = React.useState(null);
|
|
567
|
+
React.useEffect(() => {
|
|
568
|
+
contentEl?.closest('[role="dialog"]')?.focus();
|
|
569
|
+
}, [step, contentEl]);
|
|
570
|
+
return /* @__PURE__ */ jsxs(Dialog, { width: MODAL_WIDTH, onClose, children: [
|
|
571
|
+
/* @__PURE__ */ jsx(Dialog.Header, { title: isCamera ? labels.cameraTitle : labels.cropTitle, description: isCamera ? labels.cameraHint : labels.cropHint }),
|
|
572
|
+
/* @__PURE__ */ jsx(Dialog.Body, { children: /* @__PURE__ */ jsxs("div", { ref: setContentEl, children: [
|
|
573
|
+
isCamera ? /* @__PURE__ */ jsx(CropStage, { children: /* @__PURE__ */ jsx("video", { ref: camera.attachVideo, autoPlay: true, playsInline: true, muted: true, style: sx({ width: "100%", height: "100%", objectFit: "cover", transform: "scaleX(-1)" }) }) }, "camera") : /* @__PURE__ */ jsx(
|
|
574
|
+
CropStage,
|
|
575
|
+
{
|
|
576
|
+
stageRef: crop.setStageEl,
|
|
577
|
+
onPointerDown: crop.onPointerDown,
|
|
578
|
+
onPointerMove: crop.onPointerMove,
|
|
579
|
+
onPointerUp: crop.onPointerUp,
|
|
580
|
+
onWheel: (e) => crop.setZoomAt(crop.zoom - e.deltaY * 2e-3),
|
|
581
|
+
children: src && // eslint-disable-next-line @next/next/no-img-element
|
|
520
582
|
/* @__PURE__ */ jsx(
|
|
521
|
-
"
|
|
583
|
+
"img",
|
|
522
584
|
{
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
"aria-label": labels.zoom,
|
|
530
|
-
style: sx({ flex: 1, accentColor: "var(--interactive-primary)" })
|
|
585
|
+
ref: crop.setImgEl,
|
|
586
|
+
src,
|
|
587
|
+
alt: "",
|
|
588
|
+
draggable: false,
|
|
589
|
+
onLoad: (e) => crop.onImgLoad(e.currentTarget),
|
|
590
|
+
style: sx({ position: "absolute", left: crop.offset.x, top: crop.offset.y, width: crop.dispW, height: crop.dispH, maxWidth: "none", userSelect: "none", pointerEvents: "none" })
|
|
531
591
|
}
|
|
532
|
-
)
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
/* @__PURE__ */ jsxs(
|
|
537
|
-
|
|
538
|
-
/* @__PURE__ */ jsx(
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
560
|
-
streamRef.current = stream;
|
|
561
|
-
if (videoRef.current) videoRef.current.srcObject = stream;
|
|
562
|
-
setReady(true);
|
|
563
|
-
}).catch(onError);
|
|
564
|
-
return () => {
|
|
565
|
-
cancelled = true;
|
|
566
|
-
streamRef.current?.getTracks().forEach((tr) => tr.stop());
|
|
567
|
-
};
|
|
568
|
-
}, [onError]);
|
|
569
|
-
const shoot = () => {
|
|
570
|
-
const v = videoRef.current;
|
|
571
|
-
if (!v || !v.videoWidth) return;
|
|
572
|
-
const s = Math.min(v.videoWidth, v.videoHeight);
|
|
573
|
-
const canvas = document.createElement("canvas");
|
|
574
|
-
canvas.width = canvas.height = s;
|
|
575
|
-
const ctx = canvas.getContext("2d");
|
|
576
|
-
if (!ctx) return;
|
|
577
|
-
ctx.translate(s, 0);
|
|
578
|
-
ctx.scale(-1, 1);
|
|
579
|
-
ctx.drawImage(v, (v.videoWidth - s) / 2, (v.videoHeight - s) / 2, s, s, 0, 0, s, s);
|
|
580
|
-
canvas.toBlob((b) => b && onCapture(b), "image/jpeg", 0.9);
|
|
581
|
-
};
|
|
582
|
-
return /* @__PURE__ */ jsxs(Dialog, { width: 352, onClose, children: [
|
|
583
|
-
/* @__PURE__ */ jsx(Dialog.Header, { title: labels.cameraTitle, description: labels.cameraHint }),
|
|
584
|
-
/* @__PURE__ */ jsx(Dialog.Body, { children: /* @__PURE__ */ jsx(CropStage, { children: /* @__PURE__ */ jsx("video", { ref: videoRef, autoPlay: true, playsInline: true, muted: true, style: sx({ width: "100%", height: "100%", objectFit: "cover", transform: "scaleX(-1)" }) }) }) }),
|
|
585
|
-
/* @__PURE__ */ jsxs(Dialog.Footer, { children: [
|
|
592
|
+
)
|
|
593
|
+
},
|
|
594
|
+
"crop"
|
|
595
|
+
),
|
|
596
|
+
/* @__PURE__ */ jsx("div", { style: sx({ display: "flex", alignItems: "center", gap: "var(--space-3)", marginTop: "var(--space-4)", minHeight: ZOOM_ROW_HEIGHT, color: "var(--text-muted)" }), children: !isCamera && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
597
|
+
/* @__PURE__ */ jsx(ZoomOut, { size: 16, strokeWidth: 1.75, style: { flex: "0 0 auto" } }),
|
|
598
|
+
/* @__PURE__ */ jsx(
|
|
599
|
+
"input",
|
|
600
|
+
{
|
|
601
|
+
type: "range",
|
|
602
|
+
min: 1,
|
|
603
|
+
max: 3,
|
|
604
|
+
step: 0.01,
|
|
605
|
+
value: crop.zoom,
|
|
606
|
+
onChange: (e) => crop.setZoomAt(Number(e.target.value)),
|
|
607
|
+
"aria-label": labels.zoom,
|
|
608
|
+
style: sx({ flex: 1, accentColor: "var(--interactive-primary)" })
|
|
609
|
+
}
|
|
610
|
+
),
|
|
611
|
+
/* @__PURE__ */ jsx(ZoomIn, { size: 16, strokeWidth: 1.75, style: { flex: "0 0 auto" } })
|
|
612
|
+
] }) })
|
|
613
|
+
] }) }),
|
|
614
|
+
/* @__PURE__ */ jsx(Dialog.Footer, { fill: !isCamera && Boolean(onRetake), children: isCamera ? /* @__PURE__ */ jsxs(React.Fragment, { children: [
|
|
615
|
+
/* @__PURE__ */ jsx(Button, { variant: "secondary", iconLeft: /* @__PURE__ */ jsx(X, { size: 16, strokeWidth: 2 }), onClick: onClose, children: labels.cancel }),
|
|
616
|
+
/* @__PURE__ */ jsx(Button, { iconLeft: /* @__PURE__ */ jsx(Camera, { size: 16, strokeWidth: 2 }), onClick: () => camera.shoot(onCameraShot), disabled: !camera.ready, children: labels.capture })
|
|
617
|
+
] }, "camera") : /* @__PURE__ */ jsxs(React.Fragment, { children: [
|
|
618
|
+
onRetake && /* @__PURE__ */ jsx(Button, { variant: "secondary", iconLeft: /* @__PURE__ */ jsx(RotateCcw, { size: 16, strokeWidth: 2 }), onClick: onRetake, children: labels.retake }),
|
|
586
619
|
/* @__PURE__ */ jsx(Button, { variant: "secondary", iconLeft: /* @__PURE__ */ jsx(X, { size: 16, strokeWidth: 2 }), onClick: onClose, children: labels.cancel }),
|
|
587
|
-
/* @__PURE__ */ jsx(Button, { iconLeft: /* @__PURE__ */ jsx(
|
|
588
|
-
] })
|
|
620
|
+
/* @__PURE__ */ jsx(Button, { iconLeft: /* @__PURE__ */ jsx(Check, { size: 16, strokeWidth: 2.5 }), onClick: () => crop.save(onSave), children: labels.save })
|
|
621
|
+
] }, "crop") })
|
|
589
622
|
] });
|
|
590
623
|
}
|
|
591
624
|
export {
|