@funstack/ui 0.1.0-alpha.20 → 0.1.0-alpha.21
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/alert-dialog/alert-dialog-root.mjs +2 -1
- package/dist/alert-dialog/alert-dialog-root.mjs.map +1 -1
- package/dist/avatar/avatar-context.mjs +24 -0
- package/dist/avatar/avatar-context.mjs.map +1 -0
- package/dist/avatar/avatar-fallback.d.mts +32 -0
- package/dist/avatar/avatar-fallback.d.mts.map +1 -0
- package/dist/avatar/avatar-fallback.mjs +31 -0
- package/dist/avatar/avatar-fallback.mjs.map +1 -0
- package/dist/avatar/avatar-image.d.mts +37 -0
- package/dist/avatar/avatar-image.d.mts.map +1 -0
- package/dist/avatar/avatar-image.mjs +91 -0
- package/dist/avatar/avatar-image.mjs.map +1 -0
- package/dist/avatar/avatar-root.d.mts +48 -0
- package/dist/avatar/avatar-root.d.mts.map +1 -0
- package/dist/avatar/avatar-root.mjs +81 -0
- package/dist/avatar/avatar-root.mjs.map +1 -0
- package/dist/avatar/index.d.mts +24 -0
- package/dist/avatar/index.d.mts.map +1 -0
- package/dist/avatar/index.mjs +24 -0
- package/dist/avatar/index.mjs.map +1 -0
- package/dist/dialog/dialog-close.d.mts.map +1 -1
- package/dist/dialog/dialog-close.mjs +14 -3
- package/dist/dialog/dialog-close.mjs.map +1 -1
- package/dist/dialog/dialog-context.mjs +7 -1
- package/dist/dialog/dialog-context.mjs.map +1 -1
- package/dist/dialog/dialog-popup.d.mts +1 -0
- package/dist/dialog/dialog-popup.d.mts.map +1 -1
- package/dist/dialog/dialog-popup.mjs +20 -2
- package/dist/dialog/dialog-popup.mjs.map +1 -1
- package/dist/dialog/dialog-root.mjs +6 -4
- package/dist/dialog/dialog-root.mjs.map +1 -1
- package/dist/docs/components/avatar.md +354 -0
- package/dist/docs/components/drawer.md +622 -0
- package/dist/docs/components/otp-field.md +501 -0
- package/dist/docs/components/scroll-area.md +294 -0
- package/dist/docs/index.md +5 -1
- package/dist/docs/roadmap.md +6 -0
- package/dist/drawer/drawer-close.d.mts +26 -0
- package/dist/drawer/drawer-close.d.mts.map +1 -0
- package/dist/drawer/drawer-close.mjs +19 -0
- package/dist/drawer/drawer-close.mjs.map +1 -0
- package/dist/drawer/drawer-description.d.mts +23 -0
- package/dist/drawer/drawer-description.d.mts.map +1 -0
- package/dist/drawer/drawer-description.mjs +18 -0
- package/dist/drawer/drawer-description.mjs.map +1 -0
- package/dist/drawer/drawer-form.d.mts +24 -0
- package/dist/drawer/drawer-form.d.mts.map +1 -0
- package/dist/drawer/drawer-form.mjs +19 -0
- package/dist/drawer/drawer-form.mjs.map +1 -0
- package/dist/drawer/drawer-popup.d.mts +77 -0
- package/dist/drawer/drawer-popup.d.mts.map +1 -0
- package/dist/drawer/drawer-popup.mjs +124 -0
- package/dist/drawer/drawer-popup.mjs.map +1 -0
- package/dist/drawer/drawer-root.d.mts +31 -0
- package/dist/drawer/drawer-root.d.mts.map +1 -0
- package/dist/drawer/drawer-root.mjs +23 -0
- package/dist/drawer/drawer-root.mjs.map +1 -0
- package/dist/drawer/drawer-swipe.d.mts +5 -0
- package/dist/drawer/drawer-swipe.d.mts.map +1 -0
- package/dist/drawer/drawer-swipe.mjs +193 -0
- package/dist/drawer/drawer-swipe.mjs.map +1 -0
- package/dist/drawer/drawer-title.d.mts +23 -0
- package/dist/drawer/drawer-title.d.mts.map +1 -0
- package/dist/drawer/drawer-title.mjs +18 -0
- package/dist/drawer/drawer-title.mjs.map +1 -0
- package/dist/drawer/drawer-trigger.d.mts +23 -0
- package/dist/drawer/drawer-trigger.d.mts.map +1 -0
- package/dist/drawer/drawer-trigger.mjs +18 -0
- package/dist/drawer/drawer-trigger.mjs.map +1 -0
- package/dist/drawer/index.d.mts +34 -0
- package/dist/drawer/index.d.mts.map +1 -0
- package/dist/drawer/index.mjs +33 -0
- package/dist/drawer/index.mjs.map +1 -0
- package/dist/index.d.mts +19 -1
- package/dist/index.mjs +4 -1
- package/dist/internal/swipe-arithmetic.mjs +75 -0
- package/dist/internal/swipe-arithmetic.mjs.map +1 -0
- package/dist/internal/swipe.mjs +169 -0
- package/dist/internal/swipe.mjs.map +1 -0
- package/dist/menu/menu-context-trigger.mjs +1 -1
- package/dist/menu/menu-popup.mjs +1 -1
- package/dist/otp-field/index.d.mts +23 -0
- package/dist/otp-field/index.d.mts.map +1 -0
- package/dist/otp-field/index.mjs +23 -0
- package/dist/otp-field/index.mjs.map +1 -0
- package/dist/otp-field/otp-field-context.mjs +13 -0
- package/dist/otp-field/otp-field-context.mjs.map +1 -0
- package/dist/otp-field/otp-field-filter.d.mts +5 -0
- package/dist/otp-field/otp-field-filter.d.mts.map +1 -0
- package/dist/otp-field/otp-field-filter.mjs +135 -0
- package/dist/otp-field/otp-field-filter.mjs.map +1 -0
- package/dist/otp-field/otp-field-input.d.mts +41 -0
- package/dist/otp-field/otp-field-input.d.mts.map +1 -0
- package/dist/otp-field/otp-field-input.mjs +177 -0
- package/dist/otp-field/otp-field-input.mjs.map +1 -0
- package/dist/otp-field/otp-field-root.d.mts +143 -0
- package/dist/otp-field/otp-field-root.d.mts.map +1 -0
- package/dist/otp-field/otp-field-root.mjs +397 -0
- package/dist/otp-field/otp-field-root.mjs.map +1 -0
- package/dist/otp-field/otp-field-slot.d.mts +52 -0
- package/dist/otp-field/otp-field-slot.d.mts.map +1 -0
- package/dist/otp-field/otp-field-slot.mjs +67 -0
- package/dist/otp-field/otp-field-slot.mjs.map +1 -0
- package/package.json +4 -1
- package/skills/funstack-ui-knowledge/SKILL.md +5 -1
|
@@ -12,7 +12,8 @@ import { useDialogRoot } from "../dialog/dialog-root.mjs";
|
|
|
12
12
|
function AlertDialogRoot(props) {
|
|
13
13
|
return useDialogRoot(props, {
|
|
14
14
|
namespace: "AlertDialog",
|
|
15
|
-
modal: true
|
|
15
|
+
modal: true,
|
|
16
|
+
component: "dialog"
|
|
16
17
|
});
|
|
17
18
|
}
|
|
18
19
|
AlertDialogRoot.displayName = "AlertDialogRoot";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alert-dialog-root.mjs","names":[],"sources":["../../src/alert-dialog/alert-dialog-root.tsx"],"sourcesContent":["\"use client\";\nimport { useDialogRoot } from \"../dialog/dialog-root.js\";\nimport type {\n DialogOpenChangeDetails,\n DialogRootProps,\n DialogRootState,\n} from \"../dialog/dialog-root.js\";\n\n/**\n * Details passed as the second argument of `openChangeAction` and `onOpenChange`: Dialog's.\n * `reason` is `\"activate\"` for the Trigger, a Cancel, an Action or a Form; `\"dismiss\"` for\n * Escape, Android back or an assistive-technology dismiss (there is no light dismiss).\n */\nexport type AlertDialogOpenChangeDetails = DialogOpenChangeDetails;\n\n/** State of `AlertDialog.Root`: Dialog's, with `modal` always `true`. */\nexport type AlertDialogRootState = DialogRootState;\n\n/**\n * Props of `AlertDialog.Root`: `Dialog.Root`'s without `modal` (an alert dialog is always\n * modal). Without `open`, `defaultOpen`, `openChangeAction` and `onOpenChange` it is JS-free.\n *\n * @see https://ui.funstack.work/components/alert-dialog\n */\nexport type AlertDialogRootProps = Omit<DialogRootProps, \"modal\">;\n\n/**\n * `Dialog.Root` with `modal` fixed to `true` (design 2.1). Owns the ids, the mode and the open\n * state; renders no DOM. The confirmed work belongs in `AlertDialog.Action`'s `action`;\n * `openChangeAction` hears the Trigger, Cancel, Action and Form requests, `onOpenChange` the\n * dismissals (Escape: a fact, it means \"cancel\").\n *\n * @see https://ui.funstack.work/components/alert-dialog\n */\nexport function AlertDialogRoot(props: AlertDialogRootProps) {\n // `useDialogRoot` reads only the open-state props and `children`: an untyped `modal` is\n // dropped.\n return useDialogRoot(props, { namespace: \"AlertDialog\", modal: true });\n}\n\nAlertDialogRoot.displayName = \"AlertDialogRoot\";\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,gBAAgB,OAA6B;CAG3D,OAAO,cAAc,OAAO;EAAE,WAAW;EAAe,OAAO;
|
|
1
|
+
{"version":3,"file":"alert-dialog-root.mjs","names":[],"sources":["../../src/alert-dialog/alert-dialog-root.tsx"],"sourcesContent":["\"use client\";\nimport { useDialogRoot } from \"../dialog/dialog-root.js\";\nimport type {\n DialogOpenChangeDetails,\n DialogRootProps,\n DialogRootState,\n} from \"../dialog/dialog-root.js\";\n\n/**\n * Details passed as the second argument of `openChangeAction` and `onOpenChange`: Dialog's.\n * `reason` is `\"activate\"` for the Trigger, a Cancel, an Action or a Form; `\"dismiss\"` for\n * Escape, Android back or an assistive-technology dismiss (there is no light dismiss).\n */\nexport type AlertDialogOpenChangeDetails = DialogOpenChangeDetails;\n\n/** State of `AlertDialog.Root`: Dialog's, with `modal` always `true`. */\nexport type AlertDialogRootState = DialogRootState;\n\n/**\n * Props of `AlertDialog.Root`: `Dialog.Root`'s without `modal` (an alert dialog is always\n * modal). Without `open`, `defaultOpen`, `openChangeAction` and `onOpenChange` it is JS-free.\n *\n * @see https://ui.funstack.work/components/alert-dialog\n */\nexport type AlertDialogRootProps = Omit<DialogRootProps, \"modal\">;\n\n/**\n * `Dialog.Root` with `modal` fixed to `true` (design 2.1). Owns the ids, the mode and the open\n * state; renders no DOM. The confirmed work belongs in `AlertDialog.Action`'s `action`;\n * `openChangeAction` hears the Trigger, Cancel, Action and Form requests, `onOpenChange` the\n * dismissals (Escape: a fact, it means \"cancel\").\n *\n * @see https://ui.funstack.work/components/alert-dialog\n */\nexport function AlertDialogRoot(props: AlertDialogRootProps) {\n // `useDialogRoot` reads only the open-state props and `children`: an untyped `modal` is\n // dropped.\n return useDialogRoot(props, { namespace: \"AlertDialog\", modal: true, component: \"dialog\" });\n}\n\nAlertDialogRoot.displayName = \"AlertDialogRoot\";\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,gBAAgB,OAA6B;CAG3D,OAAO,cAAc,OAAO;EAAE,WAAW;EAAe,OAAO;EAAM,WAAW;CAAS,CAAC;AAC5F;AAEA,gBAAgB,cAAc"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, use } from "react";
|
|
3
|
+
//#region src/avatar/avatar-context.ts
|
|
4
|
+
const AvatarContext = createContext(null);
|
|
5
|
+
function useAvatarContext(part) {
|
|
6
|
+
const context = use(AvatarContext);
|
|
7
|
+
if (context === null) throw new Error(`${part} must be rendered inside an Avatar.Root.`);
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Identifies what a report is about: the Image's `src` and `srcSet`, the two props whose change
|
|
12
|
+
* starts a new request (and a new commit wait in react-dom). `null` when neither is given (an
|
|
13
|
+
* empty string is no source either: react-dom's `maySuspendCommit` reads it the same way).
|
|
14
|
+
*/
|
|
15
|
+
function sourceKey(src, srcSet) {
|
|
16
|
+
const s = src === "" ? void 0 : src;
|
|
17
|
+
const set = srcSet === "" ? void 0 : srcSet;
|
|
18
|
+
if (s === void 0 && set === void 0) return null;
|
|
19
|
+
return `${s ?? ""}\u0000${set ?? ""}`;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { AvatarContext, sourceKey, useAvatarContext };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=avatar-context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-context.mjs","names":[],"sources":["../../src/avatar/avatar-context.ts"],"sourcesContent":["\"use client\";\nimport { createContext, use } from \"react\";\nimport type { AvatarStatus } from \"./avatar-root.js\";\n\n/**\n * What `Avatar.Root` provides to its parts (design/components/avatar.md 4): the status and the\n * source it is about, and the stable functions through which `Avatar.Image` reports.\n */\nexport interface AvatarContextValue {\n readonly status: AvatarStatus;\n /** The source key (`sourceKey`) the status is about; `null` for `\"idle\"`. */\n readonly source: string | null;\n /** Stable. Writes the status for `source` (an urgent `setState`, conventions 2.6). */\n readonly report: (source: string, status: Exclude<AvatarStatus, \"idle\">) => void;\n /** Stable. Back to `\"idle\"` if the status is still about `source` (a source change, unmount). */\n readonly release: (source: string) => void;\n /** Stable. Called from a layout effect of each `Avatar.Image`; returns the unregistration. */\n readonly registerImage: () => () => void;\n}\n\nexport const AvatarContext = createContext<AvatarContextValue | null>(null);\n\nexport function useAvatarContext(part: string): AvatarContextValue {\n const context = use(AvatarContext);\n if (context === null) {\n throw new Error(`${part} must be rendered inside an Avatar.Root.`);\n }\n return context;\n}\n\n/**\n * Identifies what a report is about: the Image's `src` and `srcSet`, the two props whose change\n * starts a new request (and a new commit wait in react-dom). `null` when neither is given (an\n * empty string is no source either: react-dom's `maySuspendCommit` reads it the same way).\n */\nexport function sourceKey(src: string | undefined, srcSet: string | undefined): string | null {\n const s = src === \"\" ? undefined : src;\n const set = srcSet === \"\" ? undefined : srcSet;\n if (s === undefined && set === undefined) return null;\n return `${s ?? \"\"}\\u0000${set ?? \"\"}`;\n}\n"],"mappings":";;;AAoBA,MAAa,gBAAgB,cAAyC,IAAI;AAE1E,SAAgB,iBAAiB,MAAkC;CACjE,MAAM,UAAU,IAAI,aAAa;CACjC,IAAI,YAAY,MACd,MAAM,IAAI,MAAM,GAAG,KAAK,yCAAyC;CAEnE,OAAO;AACT;;;;;;AAOA,SAAgB,UAAU,KAAyB,QAA2C;CAC5F,MAAM,IAAI,QAAQ,KAAK,KAAA,IAAY;CACnC,MAAM,MAAM,WAAW,KAAK,KAAA,IAAY;CACxC,IAAI,MAAM,KAAA,KAAa,QAAQ,KAAA,GAAW,OAAO;CACjD,OAAO,GAAG,KAAK,GAAG,QAAQ,OAAO;AACnC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { AvatarState } from "./avatar-root.mjs";
|
|
3
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
4
|
+
//#region src/avatar/avatar-fallback.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Props of `Avatar.Fallback`.
|
|
7
|
+
*
|
|
8
|
+
* @see https://ui.funstack.work/components/avatar
|
|
9
|
+
*/
|
|
10
|
+
export interface AvatarFallbackProps extends Omit<ComponentPropsWithRef<"span">, "className" | "style" | "children"> {
|
|
11
|
+
/** Class name, or a function of the part's state that returns one. */
|
|
12
|
+
className?: StateValue<string | undefined, AvatarState>;
|
|
13
|
+
/** Inline style, or a function of the part's state that returns one. */
|
|
14
|
+
style?: StateValue<CSSProperties | undefined, AvatarState>;
|
|
15
|
+
/** Content (initials, an icon), or a function of the part's state. */
|
|
16
|
+
children?: StateValue<ReactNode, AvatarState>;
|
|
17
|
+
/** Replace the default `<span>` (element form) or take over rendering (function form). */
|
|
18
|
+
render?: RenderProp<ComponentPropsWithRef<"span">, AvatarState>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* `Avatar.Fallback`: what shows when there is no picture. Rendered in every status but
|
|
22
|
+
* `"loaded"`, from the server render on, **under** the image (the consumer's CSS stacks them):
|
|
23
|
+
* nothing is swapped while the image loads, so there is no delay prop. Owned key: `data-status`.
|
|
24
|
+
*
|
|
25
|
+
* @see https://ui.funstack.work/components/avatar
|
|
26
|
+
*/
|
|
27
|
+
export declare function AvatarFallback({ className, style, children, render, ref, ...rest }: AvatarFallbackProps): ReactNode;
|
|
28
|
+
export declare namespace AvatarFallback {
|
|
29
|
+
var displayName: string;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//# sourceMappingURL=avatar-fallback.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-fallback.d.mts","names":[],"sources":["../../src/avatar/avatar-fallback.tsx"],"mappings":";;;;;;;;;iBAYiB,4BAA4B,KAC3C;;EAIA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,+BAA+B;;;;;;;;;wBAUrC,iBACd,WACA,OACA,UACA,QACA,QACG,QACF,sBAAmB;yBAPN"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { mergeRenderRef, renderElement } from "../internal/render-element.mjs";
|
|
3
|
+
import { useAvatarContext } from "./avatar-context.mjs";
|
|
4
|
+
//#region src/avatar/avatar-fallback.tsx
|
|
5
|
+
/**
|
|
6
|
+
* `Avatar.Fallback`: what shows when there is no picture. Rendered in every status but
|
|
7
|
+
* `"loaded"`, from the server render on, **under** the image (the consumer's CSS stacks them):
|
|
8
|
+
* nothing is swapped while the image loads, so there is no delay prop. Owned key: `data-status`.
|
|
9
|
+
*
|
|
10
|
+
* @see https://ui.funstack.work/components/avatar
|
|
11
|
+
*/
|
|
12
|
+
function AvatarFallback({ className, style, children, render, ref, ...rest }) {
|
|
13
|
+
const { status } = useAvatarContext("Avatar.Fallback");
|
|
14
|
+
if (status === "loaded") return null;
|
|
15
|
+
return renderElement({
|
|
16
|
+
defaultElement: "span",
|
|
17
|
+
render,
|
|
18
|
+
state: { status },
|
|
19
|
+
className,
|
|
20
|
+
style,
|
|
21
|
+
children,
|
|
22
|
+
props: rest,
|
|
23
|
+
libraryProps: { "data-status": status },
|
|
24
|
+
ref: mergeRenderRef(ref, render)
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
AvatarFallback.displayName = "AvatarFallback";
|
|
28
|
+
//#endregion
|
|
29
|
+
export { AvatarFallback };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=avatar-fallback.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-fallback.mjs","names":[],"sources":["../../src/avatar/avatar-fallback.tsx"],"sourcesContent":["\"use client\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { mergeRenderRef, renderElement } from \"../internal/render-element.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useAvatarContext } from \"./avatar-context.js\";\nimport type { AvatarState } from \"./avatar-root.js\";\n\n/**\n * Props of `Avatar.Fallback`.\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport interface AvatarFallbackProps extends Omit<\n ComponentPropsWithRef<\"span\">,\n \"className\" | \"style\" | \"children\"\n> {\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, AvatarState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, AvatarState>;\n /** Content (initials, an icon), or a function of the part's state. */\n children?: StateValue<ReactNode, AvatarState>;\n /** Replace the default `<span>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"span\">, AvatarState>;\n}\n\n/**\n * `Avatar.Fallback`: what shows when there is no picture. Rendered in every status but\n * `\"loaded\"`, from the server render on, **under** the image (the consumer's CSS stacks them):\n * nothing is swapped while the image loads, so there is no delay prop. Owned key: `data-status`.\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport function AvatarFallback({\n className,\n style,\n children,\n render,\n ref,\n ...rest\n}: AvatarFallbackProps) {\n const { status } = useAvatarContext(\"Avatar.Fallback\");\n // Not rendered at all once the image loaded: a function `render` or `children` does not run.\n if (status === \"loaded\") return null;\n return renderElement<ComponentPropsWithRef<\"span\">, AvatarState>({\n defaultElement: \"span\",\n render,\n state: { status },\n className,\n style,\n children,\n props: rest,\n libraryProps: { \"data-status\": status },\n // Passed through to the element, never read during render.\n // oxlint-disable-next-line react/refs\n ref: mergeRenderRef(ref, render),\n });\n}\n\nAvatarFallback.displayName = \"AvatarFallback\";\n"],"mappings":";;;;;;;;;;;AAiCA,SAAgB,eAAe,EAC7B,WACA,OACA,UACA,QACA,KACA,GAAG,QACmB;CACtB,MAAM,EAAE,WAAW,iBAAiB,iBAAiB;CAErD,IAAI,WAAW,UAAU,OAAO;CAChC,OAAO,cAA0D;EAC/D,gBAAgB;EAChB;EACA,OAAO,EAAE,OAAO;EAChB;EACA;EACA;EACA,OAAO;EACP,cAAc,EAAE,eAAe,OAAO;EAGtC,KAAK,eAAe,KAAK,MAAM;CACjC,CAAC;AACH;AAEA,eAAe,cAAc"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { AvatarState } from "./avatar-root.mjs";
|
|
3
|
+
import { CSSProperties, ComponentPropsWithRef } from "react";
|
|
4
|
+
//#region src/avatar/avatar-image.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Props of `Avatar.Image`.
|
|
7
|
+
*
|
|
8
|
+
* @see https://ui.funstack.work/components/avatar
|
|
9
|
+
*/
|
|
10
|
+
export interface AvatarImageProps extends Omit<ComponentPropsWithRef<"img">, "alt" | "className" | "style" | "children"> {
|
|
11
|
+
/** No `src` (and no `srcSet`): the part renders nothing and the Fallback is the avatar. */
|
|
12
|
+
src?: string | undefined;
|
|
13
|
+
/** Class name, or a function of the part's state that returns one. */
|
|
14
|
+
className?: StateValue<string | undefined, AvatarState>;
|
|
15
|
+
/** Inline style, or a function of the part's state that returns one. */
|
|
16
|
+
style?: StateValue<CSSProperties | undefined, AvatarState>;
|
|
17
|
+
/** Replace the default `<img>` (element form) or take over rendering (function form). Must
|
|
18
|
+
* render an `<img>` and pass the ref on. */
|
|
19
|
+
render?: RenderProp<ComponentPropsWithRef<"img">, AvatarState>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `Avatar.Image`: the picture, an `<img alt="">` (the Root carries the name). Rendered from the
|
|
23
|
+
* first render, on the server too, while it has a source that has not failed. Owned keys: `alt`,
|
|
24
|
+
* `data-status`.
|
|
25
|
+
*
|
|
26
|
+
* It listens with native `load` / `error` listeners and reads `img.complete` at mount, and
|
|
27
|
+
* **never renders an `onLoad` prop**: react-dom's `maySuspendCommit` reads one as "this image
|
|
28
|
+
* opts out of the commit wait" under a `<ViewTransition>` (design/components/avatar.md 5).
|
|
29
|
+
*
|
|
30
|
+
* @see https://ui.funstack.work/components/avatar
|
|
31
|
+
*/
|
|
32
|
+
export declare function AvatarImage({ className, style, render, ...rest }: AvatarImageProps): import("react").ReactNode;
|
|
33
|
+
export declare namespace AvatarImage {
|
|
34
|
+
var displayName: string;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//# sourceMappingURL=avatar-image.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-image.d.mts","names":[],"sources":["../../src/avatar/avatar-image.tsx"],"mappings":";;;;;;;;;iBAciB,yBAAyB,KACxC;;EAIA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;;EAG9C,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;wBAcpC,cAAc,WAAW,OAAO,WAAW,QAAQ,mCAAgB;yBAAnE"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { renderElement } from "../internal/render-element.mjs";
|
|
3
|
+
import { useMergedRefs } from "../internal/use-merged-refs.mjs";
|
|
4
|
+
import { sourceKey, useAvatarContext } from "./avatar-context.mjs";
|
|
5
|
+
import { isValidElement, useCallback, useLayoutEffect, useRef } from "react";
|
|
6
|
+
//#region src/avatar/avatar-image.tsx
|
|
7
|
+
/**
|
|
8
|
+
* `Avatar.Image`: the picture, an `<img alt="">` (the Root carries the name). Rendered from the
|
|
9
|
+
* first render, on the server too, while it has a source that has not failed. Owned keys: `alt`,
|
|
10
|
+
* `data-status`.
|
|
11
|
+
*
|
|
12
|
+
* It listens with native `load` / `error` listeners and reads `img.complete` at mount, and
|
|
13
|
+
* **never renders an `onLoad` prop**: react-dom's `maySuspendCommit` reads one as "this image
|
|
14
|
+
* opts out of the commit wait" under a `<ViewTransition>` (design/components/avatar.md 5).
|
|
15
|
+
*
|
|
16
|
+
* @see https://ui.funstack.work/components/avatar
|
|
17
|
+
*/
|
|
18
|
+
function AvatarImage({ className, style, render, ...rest }) {
|
|
19
|
+
const { status, source: reported, report, release, registerImage } = useAvatarContext("Avatar.Image");
|
|
20
|
+
const source = sourceKey(rest.src, rest.srcSet);
|
|
21
|
+
const element = useRef(null);
|
|
22
|
+
const current = useRef(source);
|
|
23
|
+
useLayoutEffect(registerImage, [registerImage]);
|
|
24
|
+
const listen = useCallback((img) => {
|
|
25
|
+
element.current = img;
|
|
26
|
+
const settle = (status) => {
|
|
27
|
+
const key = current.current;
|
|
28
|
+
if (key !== null && img.complete) report(key, status);
|
|
29
|
+
};
|
|
30
|
+
const onLoad = () => settle("loaded");
|
|
31
|
+
const onError = () => settle("error");
|
|
32
|
+
img.addEventListener("load", onLoad);
|
|
33
|
+
img.addEventListener("error", onError);
|
|
34
|
+
return () => {
|
|
35
|
+
img.removeEventListener("load", onLoad);
|
|
36
|
+
img.removeEventListener("error", onError);
|
|
37
|
+
if (element.current === img) element.current = null;
|
|
38
|
+
};
|
|
39
|
+
}, [report]);
|
|
40
|
+
useLayoutEffect(() => {
|
|
41
|
+
current.current = source;
|
|
42
|
+
if (source === null) return;
|
|
43
|
+
const img = element.current;
|
|
44
|
+
if (img === null) return;
|
|
45
|
+
let live = true;
|
|
46
|
+
if (!img.complete) report(source, "loading");
|
|
47
|
+
else if (img.naturalWidth > 0) report(source, "loaded");
|
|
48
|
+
else {
|
|
49
|
+
report(source, "loading");
|
|
50
|
+
const decided = (status) => () => {
|
|
51
|
+
if (live && current.current === source) report(source, status);
|
|
52
|
+
};
|
|
53
|
+
if (typeof img.decode === "function") img.decode().then(decided("loaded"), decided("error"));
|
|
54
|
+
else decided("error")();
|
|
55
|
+
}
|
|
56
|
+
return () => {
|
|
57
|
+
live = false;
|
|
58
|
+
release(source);
|
|
59
|
+
};
|
|
60
|
+
}, [
|
|
61
|
+
source,
|
|
62
|
+
report,
|
|
63
|
+
release
|
|
64
|
+
]);
|
|
65
|
+
const failed = source === null || status === "error" && reported === source;
|
|
66
|
+
const ref = useMergedRefs(listen, rest.ref, elementRefOf(render));
|
|
67
|
+
if (failed) return null;
|
|
68
|
+
return renderElement({
|
|
69
|
+
defaultElement: "img",
|
|
70
|
+
render,
|
|
71
|
+
state: { status },
|
|
72
|
+
className,
|
|
73
|
+
style,
|
|
74
|
+
children: void 0,
|
|
75
|
+
props: rest,
|
|
76
|
+
libraryProps: {
|
|
77
|
+
alt: "",
|
|
78
|
+
"data-status": status
|
|
79
|
+
},
|
|
80
|
+
ref
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
AvatarImage.displayName = "AvatarImage";
|
|
84
|
+
/** The `ref` of a `render` element, attached with the part's own (as `useRender` does). */
|
|
85
|
+
function elementRefOf(render) {
|
|
86
|
+
return isValidElement(render) ? render.props.ref : void 0;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
export { AvatarImage };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=avatar-image.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-image.mjs","names":[],"sources":["../../src/avatar/avatar-image.tsx"],"sourcesContent":["\"use client\";\nimport { isValidElement, useCallback, useLayoutEffect, useRef } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, Ref } from \"react\";\nimport { renderElement } from \"../internal/render-element.js\";\nimport { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { sourceKey, useAvatarContext } from \"./avatar-context.js\";\nimport type { AvatarState } from \"./avatar-root.js\";\n\n/**\n * Props of `Avatar.Image`.\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport interface AvatarImageProps extends Omit<\n ComponentPropsWithRef<\"img\">,\n \"alt\" | \"className\" | \"style\" | \"children\"\n> {\n /** No `src` (and no `srcSet`): the part renders nothing and the Fallback is the avatar. */\n src?: string | undefined;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, AvatarState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, AvatarState>;\n /** Replace the default `<img>` (element form) or take over rendering (function form). Must\n * render an `<img>` and pass the ref on. */\n render?: RenderProp<ComponentPropsWithRef<\"img\">, AvatarState>;\n}\n\n/**\n * `Avatar.Image`: the picture, an `<img alt=\"\">` (the Root carries the name). Rendered from the\n * first render, on the server too, while it has a source that has not failed. Owned keys: `alt`,\n * `data-status`.\n *\n * It listens with native `load` / `error` listeners and reads `img.complete` at mount, and\n * **never renders an `onLoad` prop**: react-dom's `maySuspendCommit` reads one as \"this image\n * opts out of the commit wait\" under a `<ViewTransition>` (design/components/avatar.md 5).\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport function AvatarImage({ className, style, render, ...rest }: AvatarImageProps) {\n const {\n status,\n source: reported,\n report,\n release,\n registerImage,\n } = useAvatarContext(\"Avatar.Image\");\n const source = sourceKey(rest.src, rest.srcSet);\n const element = useRef<HTMLImageElement | null>(null);\n // The source the mounted element is loading, as of the last commit. Events and `decode()`\n // results are attributed to it; a report for an older source is dropped (design 4).\n const current = useRef<string | null>(source);\n\n useLayoutEffect(registerImage, [registerImage]);\n\n // Native listeners through the ref, never `onLoad` (design 5). An event is taken only while\n // the element is `complete`: a late event for a source the element no longer loads finds it\n // incomplete (its new request is running) and is dropped.\n const listen = useCallback(\n (img: HTMLImageElement) => {\n element.current = img;\n const settle = (status: \"loaded\" | \"error\") => {\n const key = current.current;\n if (key !== null && img.complete) report(key, status);\n };\n const onLoad = () => settle(\"loaded\");\n const onError = () => settle(\"error\");\n img.addEventListener(\"load\", onLoad);\n img.addEventListener(\"error\", onError);\n return () => {\n img.removeEventListener(\"load\", onLoad);\n img.removeEventListener(\"error\", onError);\n if (element.current === img) element.current = null;\n };\n },\n [report],\n );\n\n // At mount and on every source change, read the element: an image that finished before\n // hydration (or while react-dom held the commit for it) fired its event before any listener.\n useLayoutEffect(() => {\n current.current = source;\n if (source === null) return;\n const img = element.current;\n // The source failed already: the element is not rendered and the status stays \"error\".\n if (img === null) return;\n let live = true;\n if (!img.complete) {\n report(source, \"loading\");\n } else if (img.naturalWidth > 0) {\n report(source, \"loaded\");\n } else {\n // Complete with no natural width: broken, or an image with no intrinsic size (some SVGs).\n // `decode()` separates them without a request.\n report(source, \"loading\");\n const decided = (status: \"loaded\" | \"error\") => () => {\n if (live && current.current === source) report(source, status);\n };\n if (typeof img.decode === \"function\") {\n img.decode().then(decided(\"loaded\"), decided(\"error\"));\n } else {\n decided(\"error\")();\n }\n }\n return () => {\n live = false;\n release(source);\n };\n }, [source, report, release]);\n\n const failed = source === null || (status === \"error\" && reported === source);\n\n // Stable, so that React does not re-attach the listeners on every status change. `useRender`'s\n // merge, inlined: once its source failed the element is not rendered at all, and a function\n // `render` must not run then.\n const ref = useMergedRefs<HTMLImageElement>(listen, rest.ref, elementRefOf(render));\n\n if (failed) return null;\n return renderElement<ComponentPropsWithRef<\"img\">, AvatarState>({\n defaultElement: \"img\",\n render,\n state: { status },\n className,\n style,\n children: undefined,\n props: rest,\n libraryProps: { alt: \"\", \"data-status\": status },\n ref: ref as Ref<unknown>,\n });\n}\n\nAvatarImage.displayName = \"AvatarImage\";\n\n/** The `ref` of a `render` element, attached with the part's own (as `useRender` does). */\nfunction elementRefOf(render: AvatarImageProps[\"render\"]): Ref<HTMLImageElement> | undefined {\n return isValidElement<{ ref?: Ref<HTMLImageElement> }>(render) ? render.props.ref : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAwCA,SAAgB,YAAY,EAAE,WAAW,OAAO,QAAQ,GAAG,QAA0B;CACnF,MAAM,EACJ,QACA,QAAQ,UACR,QACA,SACA,kBACE,iBAAiB,cAAc;CACnC,MAAM,SAAS,UAAU,KAAK,KAAK,KAAK,MAAM;CAC9C,MAAM,UAAU,OAAgC,IAAI;CAGpD,MAAM,UAAU,OAAsB,MAAM;CAE5C,gBAAgB,eAAe,CAAC,aAAa,CAAC;CAK9C,MAAM,SAAS,aACZ,QAA0B;EACzB,QAAQ,UAAU;EAClB,MAAM,UAAU,WAA+B;GAC7C,MAAM,MAAM,QAAQ;GACpB,IAAI,QAAQ,QAAQ,IAAI,UAAU,OAAO,KAAK,MAAM;EACtD;EACA,MAAM,eAAe,OAAO,QAAQ;EACpC,MAAM,gBAAgB,OAAO,OAAO;EACpC,IAAI,iBAAiB,QAAQ,MAAM;EACnC,IAAI,iBAAiB,SAAS,OAAO;EACrC,aAAa;GACX,IAAI,oBAAoB,QAAQ,MAAM;GACtC,IAAI,oBAAoB,SAAS,OAAO;GACxC,IAAI,QAAQ,YAAY,KAAK,QAAQ,UAAU;EACjD;CACF,GACA,CAAC,MAAM,CACT;CAIA,sBAAsB;EACpB,QAAQ,UAAU;EAClB,IAAI,WAAW,MAAM;EACrB,MAAM,MAAM,QAAQ;EAEpB,IAAI,QAAQ,MAAM;EAClB,IAAI,OAAO;EACX,IAAI,CAAC,IAAI,UACP,OAAO,QAAQ,SAAS;OACnB,IAAI,IAAI,eAAe,GAC5B,OAAO,QAAQ,QAAQ;OAClB;GAGL,OAAO,QAAQ,SAAS;GACxB,MAAM,WAAW,iBAAqC;IACpD,IAAI,QAAQ,QAAQ,YAAY,QAAQ,OAAO,QAAQ,MAAM;GAC/D;GACA,IAAI,OAAO,IAAI,WAAW,YACxB,IAAI,OAAO,CAAC,CAAC,KAAK,QAAQ,QAAQ,GAAG,QAAQ,OAAO,CAAC;QAErD,QAAQ,OAAO,CAAC,CAAC;EAErB;EACA,aAAa;GACX,OAAO;GACP,QAAQ,MAAM;EAChB;CACF,GAAG;EAAC;EAAQ;EAAQ;CAAO,CAAC;CAE5B,MAAM,SAAS,WAAW,QAAS,WAAW,WAAW,aAAa;CAKtE,MAAM,MAAM,cAAgC,QAAQ,KAAK,KAAK,aAAa,MAAM,CAAC;CAElF,IAAI,QAAQ,OAAO;CACnB,OAAO,cAAyD;EAC9D,gBAAgB;EAChB;EACA,OAAO,EAAE,OAAO;EAChB;EACA;EACA,UAAU,KAAA;EACV,OAAO;EACP,cAAc;GAAE,KAAK;GAAI,eAAe;EAAO;EAC1C;CACP,CAAC;AACH;AAEA,YAAY,cAAc;;AAG1B,SAAS,aAAa,QAAuE;CAC3F,OAAO,eAAgD,MAAM,IAAI,OAAO,MAAM,MAAM,KAAA;AACtF"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
3
|
+
//#region src/avatar/avatar-root.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The load state of the avatar's image (design/components/avatar.md 4): `"idle"` until an image
|
|
6
|
+
* with a source reports (the server render and the hydration render too), `"loading"` while it
|
|
7
|
+
* is not complete, `"loaded"` once it is available, `"error"` when its current source failed.
|
|
8
|
+
*/
|
|
9
|
+
export type AvatarStatus = "idle" | "loading" | "loaded" | "error";
|
|
10
|
+
/** The state every Avatar part receives. */
|
|
11
|
+
export interface AvatarState {
|
|
12
|
+
/** The load state of the image the Root last heard from. */
|
|
13
|
+
readonly status: AvatarStatus;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Props of `Avatar.Root`.
|
|
17
|
+
*
|
|
18
|
+
* @see https://ui.funstack.work/components/avatar
|
|
19
|
+
*/
|
|
20
|
+
export interface AvatarRootProps extends Omit<ComponentPropsWithRef<"span">, "role" | "aria-label" | "aria-labelledby" | "aria-hidden" | "className" | "style" | "children"> {
|
|
21
|
+
/**
|
|
22
|
+
* The avatar's text alternative, with the contract of `<img alt>`: who or what is pictured, or
|
|
23
|
+
* `""` when the avatar is decoration next to text that already says it. Non-empty: the Root is
|
|
24
|
+
* `role="img"` with this `aria-label`, in every status. Empty: `aria-hidden="true"`.
|
|
25
|
+
*/
|
|
26
|
+
alt: string;
|
|
27
|
+
/** Class name, or a function of the part's state that returns one. */
|
|
28
|
+
className?: StateValue<string | undefined, AvatarState>;
|
|
29
|
+
/** Inline style, or a function of the part's state that returns one. */
|
|
30
|
+
style?: StateValue<CSSProperties | undefined, AvatarState>;
|
|
31
|
+
/** Content (an `Avatar.Image` and an `Avatar.Fallback`), or a function of the part's state. */
|
|
32
|
+
children?: StateValue<ReactNode, AvatarState>;
|
|
33
|
+
/** Replace the default `<span>` (element form) or take over rendering (function form). */
|
|
34
|
+
render?: RenderProp<ComponentPropsWithRef<"span">, AvatarState>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* `Avatar.Root`: a `span` that names the avatar and holds the image's status. Owned keys: `role`,
|
|
38
|
+
* `aria-label`, `aria-hidden`, `data-status`. The status is a mirror of a browser-owned fact,
|
|
39
|
+
* written urgently (conventions 2.6), never inside a transition.
|
|
40
|
+
*
|
|
41
|
+
* @see https://ui.funstack.work/components/avatar
|
|
42
|
+
*/
|
|
43
|
+
export declare function AvatarRoot({ alt, className, style, children, render, ...rest }: AvatarRootProps): import("react").JSX.Element;
|
|
44
|
+
export declare namespace AvatarRoot {
|
|
45
|
+
var displayName: string;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//# sourceMappingURL=avatar-root.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-root.d.mts","names":[],"sources":["../../src/avatar/avatar-root.tsx"],"mappings":";;;;;;;;YAgBY;;iBAGK;;WAEN,QAAQ;;;;;;;iBAQF,wBAAwB,KACvC;;;;;;EAQA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,+BAA+B;;;;;;;;;wBAiBrC,aAAa,KAAK,WAAW,OAAO,UAAU,WAAW,QAAQ,kCAAe,IAAA;yBAAhF"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRender } from "../internal/use-render.mjs";
|
|
3
|
+
import { AvatarContext } from "./avatar-context.mjs";
|
|
4
|
+
import { useCallback, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
//#region src/avatar/avatar-root.tsx
|
|
7
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
8
|
+
const IDLE = {
|
|
9
|
+
status: "idle",
|
|
10
|
+
source: null
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* `Avatar.Root`: a `span` that names the avatar and holds the image's status. Owned keys: `role`,
|
|
14
|
+
* `aria-label`, `aria-hidden`, `data-status`. The status is a mirror of a browser-owned fact,
|
|
15
|
+
* written urgently (conventions 2.6), never inside a transition.
|
|
16
|
+
*
|
|
17
|
+
* @see https://ui.funstack.work/components/avatar
|
|
18
|
+
*/
|
|
19
|
+
function AvatarRoot({ alt, className, style, children, render, ...rest }) {
|
|
20
|
+
const [report, setReport] = useState(IDLE);
|
|
21
|
+
const images = useRef(0);
|
|
22
|
+
const warned = useRef(false);
|
|
23
|
+
const write = useCallback((source, status) => {
|
|
24
|
+
setReport((prev) => prev.source === source && prev.status === status ? prev : {
|
|
25
|
+
status,
|
|
26
|
+
source
|
|
27
|
+
});
|
|
28
|
+
}, []);
|
|
29
|
+
const release = useCallback((source) => {
|
|
30
|
+
setReport((prev) => prev.source === source ? IDLE : prev);
|
|
31
|
+
}, []);
|
|
32
|
+
const registerImage = useCallback(() => {
|
|
33
|
+
images.current += 1;
|
|
34
|
+
if (isDev && images.current > 1 && !warned.current) {
|
|
35
|
+
warned.current = true;
|
|
36
|
+
console.error("Avatar.Root: more than one Avatar.Image is rendered in one Avatar.Root. The status follows whichever image reported last; render one Avatar.Image per Root.");
|
|
37
|
+
}
|
|
38
|
+
return () => {
|
|
39
|
+
images.current -= 1;
|
|
40
|
+
};
|
|
41
|
+
}, []);
|
|
42
|
+
const context = useMemo(() => ({
|
|
43
|
+
status: report.status,
|
|
44
|
+
source: report.source,
|
|
45
|
+
report: write,
|
|
46
|
+
release,
|
|
47
|
+
registerImage
|
|
48
|
+
}), [
|
|
49
|
+
report,
|
|
50
|
+
write,
|
|
51
|
+
release,
|
|
52
|
+
registerImage
|
|
53
|
+
]);
|
|
54
|
+
const state = useMemo(() => ({ status: report.status }), [report.status]);
|
|
55
|
+
const named = alt !== "";
|
|
56
|
+
const libraryProps = {
|
|
57
|
+
role: named ? "img" : void 0,
|
|
58
|
+
"aria-label": named ? alt : void 0,
|
|
59
|
+
"aria-hidden": named ? void 0 : true,
|
|
60
|
+
"data-status": report.status
|
|
61
|
+
};
|
|
62
|
+
const element = useRender({
|
|
63
|
+
defaultElement: "span",
|
|
64
|
+
render,
|
|
65
|
+
state,
|
|
66
|
+
className,
|
|
67
|
+
style,
|
|
68
|
+
children,
|
|
69
|
+
props: rest,
|
|
70
|
+
libraryProps
|
|
71
|
+
});
|
|
72
|
+
return /* @__PURE__ */ jsx(AvatarContext, {
|
|
73
|
+
value: context,
|
|
74
|
+
children: element
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
AvatarRoot.displayName = "AvatarRoot";
|
|
78
|
+
//#endregion
|
|
79
|
+
export { AvatarRoot };
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=avatar-root.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"avatar-root.mjs","names":[],"sources":["../../src/avatar/avatar-root.tsx"],"sourcesContent":["\"use client\";\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport type { DataAttributes } from \"../internal/render-element.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport { AvatarContext } from \"./avatar-context.js\";\nimport type { AvatarContextValue } from \"./avatar-context.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * The load state of the avatar's image (design/components/avatar.md 4): `\"idle\"` until an image\n * with a source reports (the server render and the hydration render too), `\"loading\"` while it\n * is not complete, `\"loaded\"` once it is available, `\"error\"` when its current source failed.\n */\nexport type AvatarStatus = \"idle\" | \"loading\" | \"loaded\" | \"error\";\n\n/** The state every Avatar part receives. */\nexport interface AvatarState {\n /** The load state of the image the Root last heard from. */\n readonly status: AvatarStatus;\n}\n\n/**\n * Props of `Avatar.Root`.\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport interface AvatarRootProps extends Omit<\n ComponentPropsWithRef<\"span\">,\n \"role\" | \"aria-label\" | \"aria-labelledby\" | \"aria-hidden\" | \"className\" | \"style\" | \"children\"\n> {\n /**\n * The avatar's text alternative, with the contract of `<img alt>`: who or what is pictured, or\n * `\"\"` when the avatar is decoration next to text that already says it. Non-empty: the Root is\n * `role=\"img\"` with this `aria-label`, in every status. Empty: `aria-hidden=\"true\"`.\n */\n alt: string;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, AvatarState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, AvatarState>;\n /** Content (an `Avatar.Image` and an `Avatar.Fallback`), or a function of the part's state. */\n children?: StateValue<ReactNode, AvatarState>;\n /** Replace the default `<span>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"span\">, AvatarState>;\n}\n\ninterface Report {\n readonly status: AvatarStatus;\n readonly source: string | null;\n}\n\nconst IDLE: Report = { status: \"idle\", source: null };\n\n/**\n * `Avatar.Root`: a `span` that names the avatar and holds the image's status. Owned keys: `role`,\n * `aria-label`, `aria-hidden`, `data-status`. The status is a mirror of a browser-owned fact,\n * written urgently (conventions 2.6), never inside a transition.\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport function AvatarRoot({ alt, className, style, children, render, ...rest }: AvatarRootProps) {\n const [report, setReport] = useState<Report>(IDLE);\n const images = useRef(0);\n const warned = useRef(false);\n\n const write = useCallback((source: string, status: Exclude<AvatarStatus, \"idle\">) => {\n setReport((prev) =>\n prev.source === source && prev.status === status ? prev : { status, source },\n );\n }, []);\n const release = useCallback((source: string) => {\n setReport((prev) => (prev.source === source ? IDLE : prev));\n }, []);\n const registerImage = useCallback(() => {\n images.current += 1;\n if (isDev && images.current > 1 && !warned.current) {\n warned.current = true;\n console.error(\n \"Avatar.Root: more than one Avatar.Image is rendered in one Avatar.Root. The status \" +\n \"follows whichever image reported last; render one Avatar.Image per Root.\",\n );\n }\n return () => {\n images.current -= 1;\n };\n }, []);\n\n const context = useMemo<AvatarContextValue>(\n () => ({\n status: report.status,\n source: report.source,\n report: write,\n release,\n registerImage,\n }),\n [report, write, release, registerImage],\n );\n\n const state: AvatarState = useMemo(() => ({ status: report.status }), [report.status]);\n\n const named = alt !== \"\";\n const libraryProps: Partial<ComponentPropsWithRef<\"span\">> & DataAttributes = {\n role: named ? \"img\" : undefined,\n \"aria-label\": named ? alt : undefined,\n \"aria-hidden\": named ? undefined : true,\n \"data-status\": report.status,\n };\n\n const element = useRender<ComponentPropsWithRef<\"span\">, AvatarState>({\n defaultElement: \"span\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps,\n });\n\n return <AvatarContext value={context}>{element}</AvatarContext>;\n}\n\nAvatarRoot.displayName = \"AvatarRoot\";\n"],"mappings":";;;;;;AASA,MAAM,QAAQ,QAAQ,IAAI,aAAa;AA6CvC,MAAM,OAAe;CAAE,QAAQ;CAAQ,QAAQ;AAAK;;;;;;;;AASpD,SAAgB,WAAW,EAAE,KAAK,WAAW,OAAO,UAAU,QAAQ,GAAG,QAAyB;CAChG,MAAM,CAAC,QAAQ,aAAa,SAAiB,IAAI;CACjD,MAAM,SAAS,OAAO,CAAC;CACvB,MAAM,SAAS,OAAO,KAAK;CAE3B,MAAM,QAAQ,aAAa,QAAgB,WAA0C;EACnF,WAAW,SACT,KAAK,WAAW,UAAU,KAAK,WAAW,SAAS,OAAO;GAAE;GAAQ;EAAO,CAC7E;CACF,GAAG,CAAC,CAAC;CACL,MAAM,UAAU,aAAa,WAAmB;EAC9C,WAAW,SAAU,KAAK,WAAW,SAAS,OAAO,IAAK;CAC5D,GAAG,CAAC,CAAC;CACL,MAAM,gBAAgB,kBAAkB;EACtC,OAAO,WAAW;EAClB,IAAI,SAAS,OAAO,UAAU,KAAK,CAAC,OAAO,SAAS;GAClD,OAAO,UAAU;GACjB,QAAQ,MACN,6JAEF;EACF;EACA,aAAa;GACX,OAAO,WAAW;EACpB;CACF,GAAG,CAAC,CAAC;CAEL,MAAM,UAAU,eACP;EACL,QAAQ,OAAO;EACf,QAAQ,OAAO;EACf,QAAQ;EACR;EACA;CACF,IACA;EAAC;EAAQ;EAAO;EAAS;CAAa,CACxC;CAEA,MAAM,QAAqB,eAAe,EAAE,QAAQ,OAAO,OAAO,IAAI,CAAC,OAAO,MAAM,CAAC;CAErF,MAAM,QAAQ,QAAQ;CACtB,MAAM,eAAwE;EAC5E,MAAM,QAAQ,QAAQ,KAAA;EACtB,cAAc,QAAQ,MAAM,KAAA;EAC5B,eAAe,QAAQ,KAAA,IAAY;EACnC,eAAe,OAAO;CACxB;CAEA,MAAM,UAAU,UAAsD;EACpE,gBAAgB;EAChB;EACA;EACA;EACA;EACA;EACA,OAAO;EACP;CACF,CAAC;CAED,OAAO,oBAAC,eAAD;EAAe,OAAO;EAAU,UAAA;CAAuB,CAAA;AAChE;AAEA,WAAW,cAAc"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "../internal/platform-attributes.mjs";
|
|
2
|
+
import { AvatarRoot, AvatarRootProps, AvatarState, AvatarStatus } from "./avatar-root.mjs";
|
|
3
|
+
import { AvatarFallback, AvatarFallbackProps } from "./avatar-fallback.mjs";
|
|
4
|
+
import { AvatarImage, AvatarImageProps } from "./avatar-image.mjs";
|
|
5
|
+
//#region src/avatar/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Avatar: a picture of a person or a thing, with something to show in its place. `Root` (a
|
|
8
|
+
* `span` that carries the name: `role="img"` + `aria-label` from the required `alt`, or
|
|
9
|
+
* `aria-hidden` for `alt=""`), `Image` (an `<img alt="">` in the server HTML from the first
|
|
10
|
+
* render) and `Fallback` (rendered under the image until it has loaded). `data-status` /
|
|
11
|
+
* `state.status` is `"idle" | "loading" | "loaded" | "error"`, an urgent mirror of the image. No
|
|
12
|
+
* action, nothing pending; the image never gets an `onLoad` prop, so React's commit wait for
|
|
13
|
+
* images under a `<ViewTransition>` applies to it.
|
|
14
|
+
*
|
|
15
|
+
* @see https://ui.funstack.work/components/avatar
|
|
16
|
+
*/
|
|
17
|
+
export declare const Avatar: {
|
|
18
|
+
readonly Root: typeof AvatarRoot;
|
|
19
|
+
readonly Image: typeof AvatarImage;
|
|
20
|
+
readonly Fallback: typeof AvatarFallback;
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export type { AvatarFallbackProps, AvatarImageProps, AvatarRootProps, AvatarState, AvatarStatus };
|
|
24
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/avatar/index.ts"],"mappings":";;;;;;;;;;;;;;;;qBAkBa;WACX,aAAI;WACJ,cAAK;WACL,iBAAQ"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AvatarFallback } from "./avatar-fallback.mjs";
|
|
2
|
+
import { AvatarImage } from "./avatar-image.mjs";
|
|
3
|
+
import { AvatarRoot } from "./avatar-root.mjs";
|
|
4
|
+
//#region src/avatar/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Avatar: a picture of a person or a thing, with something to show in its place. `Root` (a
|
|
7
|
+
* `span` that carries the name: `role="img"` + `aria-label` from the required `alt`, or
|
|
8
|
+
* `aria-hidden` for `alt=""`), `Image` (an `<img alt="">` in the server HTML from the first
|
|
9
|
+
* render) and `Fallback` (rendered under the image until it has loaded). `data-status` /
|
|
10
|
+
* `state.status` is `"idle" | "loading" | "loaded" | "error"`, an urgent mirror of the image. No
|
|
11
|
+
* action, nothing pending; the image never gets an `onLoad` prop, so React's commit wait for
|
|
12
|
+
* images under a `<ViewTransition>` applies to it.
|
|
13
|
+
*
|
|
14
|
+
* @see https://ui.funstack.work/components/avatar
|
|
15
|
+
*/
|
|
16
|
+
const Avatar = {
|
|
17
|
+
Root: AvatarRoot,
|
|
18
|
+
Image: AvatarImage,
|
|
19
|
+
Fallback: AvatarFallback
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
export { Avatar };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/avatar/index.ts"],"sourcesContent":["// Ships the platform attribute typings with this subpath (conventions 7.4). Type-only: the\n// built JS has no import of it.\nimport \"../internal/platform-attributes.js\";\nimport { AvatarFallback } from \"./avatar-fallback.js\";\nimport { AvatarImage } from \"./avatar-image.js\";\nimport { AvatarRoot } from \"./avatar-root.js\";\n\n/**\n * Avatar: a picture of a person or a thing, with something to show in its place. `Root` (a\n * `span` that carries the name: `role=\"img\"` + `aria-label` from the required `alt`, or\n * `aria-hidden` for `alt=\"\"`), `Image` (an `<img alt=\"\">` in the server HTML from the first\n * render) and `Fallback` (rendered under the image until it has loaded). `data-status` /\n * `state.status` is `\"idle\" | \"loading\" | \"loaded\" | \"error\"`, an urgent mirror of the image. No\n * action, nothing pending; the image never gets an `onLoad` prop, so React's commit wait for\n * images under a `<ViewTransition>` applies to it.\n *\n * @see https://ui.funstack.work/components/avatar\n */\nexport const Avatar = {\n Root: AvatarRoot,\n Image: AvatarImage,\n Fallback: AvatarFallback,\n} as const;\n\nexport type { AvatarRootProps, AvatarState, AvatarStatus } from \"./avatar-root.js\";\nexport type { AvatarImageProps } from \"./avatar-image.js\";\nexport type { AvatarFallbackProps } from \"./avatar-fallback.js\";\n"],"mappings":";;;;;;;;;;;;;;;AAkBA,MAAa,SAAS;CACpB,MAAM;CACN,OAAO;CACP,UAAU;AACZ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog-close.d.mts","names":[],"sources":["../../src/dialog/dialog-close.tsx"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"dialog-close.d.mts","names":[],"sources":["../../src/dialog/dialog-close.tsx"],"mappings":";;;;;;;;iBAsBiB;;WAEN;;WAEA;;;iBAIM,2BAA2B;;WAEjC;;;;;;;iBAQM,yBAAyB,KACxC;;;;;;;;;;;;EAcA,SAAS,QAAQ,SAAS;;;;;;;;;;EAU1B,eAAe;;EAEf;;;;;;;;EAQA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,iCAAiC;;;;;;;;;;;;;;;;;;wBAmBvC,YAAY,OAAO,mBAAgB;yBAAnC"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { createActionDetails } from "../internal/action/action-details.mjs";
|
|
3
|
+
import { useMergedRefs } from "../internal/use-merged-refs.mjs";
|
|
3
4
|
import { useOverlayClose } from "../internal/overlay/use-overlay-close.mjs";
|
|
4
|
-
import { useDialogContext } from "./dialog-context.mjs";
|
|
5
|
+
import { DialogCloseRegistry, useDialogContext } from "./dialog-context.mjs";
|
|
6
|
+
import { use, useCallback } from "react";
|
|
5
7
|
//#region src/dialog/dialog-close.tsx
|
|
8
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
6
9
|
/**
|
|
7
10
|
* A close button: `<button type="button" commandfor command="request-close">`, or a plain
|
|
8
11
|
* `<button type="button">` with `action` (it does nothing before hydration). In JS-free mode
|
|
@@ -27,8 +30,13 @@ DialogClose.displayName = "DialogClose";
|
|
|
27
30
|
* The body of `Dialog.Close`, also run by `AlertDialog.Cancel` and `AlertDialog.Action`
|
|
28
31
|
* (alert-dialog.md 8).
|
|
29
32
|
*/
|
|
30
|
-
function useDialogClose({ action, whilePending, value, disabled = false, className, style, children, render, ...rest }, { part, ownProps }) {
|
|
33
|
+
function useDialogClose({ action, whilePending, value, disabled = false, className, style, children, render, ref, ...rest }, { part, ownProps }) {
|
|
31
34
|
const ctx = useDialogContext(part);
|
|
35
|
+
const registerClose = use(DialogCloseRegistry);
|
|
36
|
+
const reportClose = useCallback((element) => {
|
|
37
|
+
if (element !== null) registerClose?.(element);
|
|
38
|
+
}, [registerClose]);
|
|
39
|
+
const mergedRef = useMergedRefs(ref, isDev ? reportClose : void 0);
|
|
32
40
|
const { popupRef, recordCloseIntent } = ctx;
|
|
33
41
|
const isLibrary = ctx.mode === "library";
|
|
34
42
|
return useOverlayClose({
|
|
@@ -64,7 +72,10 @@ function useDialogClose({ action, whilePending, value, disabled = false, classNa
|
|
|
64
72
|
style,
|
|
65
73
|
children,
|
|
66
74
|
render,
|
|
67
|
-
rest
|
|
75
|
+
rest: {
|
|
76
|
+
...rest,
|
|
77
|
+
ref: mergedRef
|
|
78
|
+
}
|
|
68
79
|
});
|
|
69
80
|
}
|
|
70
81
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialog-close.mjs","names":[],"sources":["../../src/dialog/dialog-close.tsx"],"sourcesContent":["\"use client\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action/action-details.js\";\nimport type {\n Action,\n ActionDetails,\n RenderProp,\n StateValue,\n WhilePending,\n} from \"../internal/types.js\";\nimport { useOverlayClose } from \"../internal/overlay/use-overlay-close.js\";\nimport { useDialogContext } from \"./dialog-context.js\";\nimport type { DialogNamespace } from \"./dialog-context.js\";\n\n/**\n * State of `Dialog.Close`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface DialogCloseState {\n /** The `action` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/** Details passed to `Dialog.Close`'s `action`. */\nexport interface DialogCloseDetails extends ActionDetails {\n /** The `value` the dialog closes with (`dialog.returnValue`); `\"\"` without `value`. */\n readonly returnValue: string;\n}\n\n/**\n * Props of `Dialog.Close`.\n *\n * @see https://ui.funstack.work/components/dialog\n */\nexport interface DialogCloseProps extends Omit<\n ComponentPropsWithRef<\"button\">,\n \"onClick\" | \"value\" | \"className\" | \"style\" | \"children\"\n> {\n /**\n * Runs inside this part's own transition (`data-pending` on the button), then closes the\n * dialog with `value` as `returnValue`: in library mode through the Root\n * (`openChangeAction(false)` awaited, view transition if set), in JS-free mode with\n * `close(value)`. A rejection reaches the nearest Error Boundary above the Close part and\n * leaves the dialog open. If the dialog was dismissed and opened again while the action ran,\n * the close is dropped: it belonged to the earlier open. With `action` the button is no native\n * close invoker (no `commandfor` / `command`), so a click before hydration does nothing\n * instead of closing the dialog without running the action. Without `action` in JS-free mode\n * the button is a native `command=\"request-close\"` invoker.\n */\n action?: Action<[details: DialogCloseDetails]>;\n /**\n * What a click does while this part's `action` is pending. `\"ignore\"`: nothing (the click is\n * cancelled, `aria-disabled=\"true\"` meanwhile). `\"run\"`: the action runs again (`data-pending`\n * until every run has settled); the dialog closes once, when the last run settles and only if\n * the latest run resolved (a rejection leaves it open, as in `\"ignore\"` mode). No effect\n * without `action`.\n *\n * @defaultValue `\"ignore\"`\n */\n whilePending?: WhilePending;\n /** Becomes `dialog.returnValue` (and `details.returnValue` of `openChangeAction(false)`). */\n value?: string;\n /**\n * Never closes. Native `disabled` on a `<button>`; owned, so a `render` element cannot\n * re-enable it. With a link or component `render` whose click this part handles (library mode,\n * or with `action`), `aria-disabled=\"true\"` instead: the element stays focusable.\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, DialogCloseState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, DialogCloseState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, DialogCloseState>;\n /** Replace the default `<button>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, DialogCloseState>;\n}\n\n/**\n * A close button: `<button type=\"button\" commandfor command=\"request-close\">`, or a plain\n * `<button type=\"button\">` with `action` (it does nothing before hydration). In JS-free mode\n * without `action` the browser closes the dialog (a close request: `cancel`, then `close`), with\n * `value` as the `returnValue`. In library mode the click is always cancelled and the close goes\n * through the Root (`openChangeAction(false)` awaited, then `close(value)` in the commit, inside\n * the view transition if the Popup's `transition` is set). With `action` the action runs first in\n * this part's own transition (`data-pending` and `aria-disabled` on the button; a second click\n * meanwhile is ignored unless `whilePending=\"run\"`); a rejection leaves the dialog open.\n *\n * Owned keys (conventions 1.2): `type` (on a `<button>` only), `disabled`, `commandfor`,\n * `command` (neither rendered with `action`), `value`, `onClick` (library mode, or with `action`), `aria-disabled`, `data-pending`,\n * `data-disabled`.\n *\n * @see https://ui.funstack.work/components/dialog\n */\nexport function DialogClose(props: DialogCloseProps) {\n return useDialogClose(props, { part: \"Dialog.Close\" });\n}\n\nDialogClose.displayName = \"DialogClose\";\n\n/** What a caller of `useDialogClose` sets (design/components/alert-dialog.md 8). */\nexport interface DialogCloseOptions {\n /** Public name of the part (`\"AlertDialog.Cancel\"`). */\n readonly part: `${DialogNamespace}.${string}`;\n /** Extra owned keys (AlertDialog.Cancel's `autoFocus`). */\n readonly ownProps?: ComponentPropsWithRef<\"button\">;\n}\n\n/**\n * The body of `Dialog.Close`, also run by `AlertDialog.Cancel` and `AlertDialog.Action`\n * (alert-dialog.md 8).\n */\nexport function useDialogClose(\n {\n action,\n whilePending,\n value,\n disabled = false,\n className,\n style,\n children,\n render,\n ...rest\n }: DialogCloseProps,\n { part, ownProps }: DialogCloseOptions,\n): ReactNode {\n const ctx = useDialogContext(part);\n const { popupRef, recordCloseIntent } = ctx;\n const isLibrary = ctx.mode === \"library\";\n return useOverlayClose({\n part,\n noun: \"dialog\",\n isLibrary,\n popupId: ctx.ids.popup,\n command: \"request-close\",\n openGeneration: ctx.openGeneration,\n action,\n whilePending,\n details: (event) => createActionDetails(event, { returnValue: value ?? \"\" }),\n close: (event) => {\n const returnValue = value ?? \"\";\n if (isLibrary) {\n // A library-owned close (design 2.3): awaited, then `close(returnValue)` in the commit. A\n // no-op when `false` is already intended (a repeated click, or a dismissal meanwhile).\n ctx.requestClose(event, \"activate\", returnValue);\n return;\n }\n // JS-free mode with `action`: a browser-owned close; there is no Root state to go through.\n const dialog = popupRef.current;\n if (dialog === null || !dialog.open) return;\n recordCloseIntent({ reason: \"activate\", event });\n dialog.close(returnValue);\n },\n ownProps: { value, ...ownProps },\n disabled,\n className,\n style,\n children,\n render,\n rest,\n });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"dialog-close.mjs","names":[],"sources":["../../src/dialog/dialog-close.tsx"],"sourcesContent":["\"use client\";\nimport { use, useCallback } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action/action-details.js\";\nimport type {\n Action,\n ActionDetails,\n RenderProp,\n StateValue,\n WhilePending,\n} from \"../internal/types.js\";\nimport { useOverlayClose } from \"../internal/overlay/use-overlay-close.js\";\nimport { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport { DialogCloseRegistry, useDialogContext } from \"./dialog-context.js\";\nimport type { DialogNamespace } from \"./dialog-context.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * State of `Dialog.Close`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface DialogCloseState {\n /** The `action` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/** Details passed to `Dialog.Close`'s `action`. */\nexport interface DialogCloseDetails extends ActionDetails {\n /** The `value` the dialog closes with (`dialog.returnValue`); `\"\"` without `value`. */\n readonly returnValue: string;\n}\n\n/**\n * Props of `Dialog.Close`.\n *\n * @see https://ui.funstack.work/components/dialog\n */\nexport interface DialogCloseProps extends Omit<\n ComponentPropsWithRef<\"button\">,\n \"onClick\" | \"value\" | \"className\" | \"style\" | \"children\"\n> {\n /**\n * Runs inside this part's own transition (`data-pending` on the button), then closes the\n * dialog with `value` as `returnValue`: in library mode through the Root\n * (`openChangeAction(false)` awaited, view transition if set), in JS-free mode with\n * `close(value)`. A rejection reaches the nearest Error Boundary above the Close part and\n * leaves the dialog open. If the dialog was dismissed and opened again while the action ran,\n * the close is dropped: it belonged to the earlier open. With `action` the button is no native\n * close invoker (no `commandfor` / `command`), so a click before hydration does nothing\n * instead of closing the dialog without running the action. Without `action` in JS-free mode\n * the button is a native `command=\"request-close\"` invoker.\n */\n action?: Action<[details: DialogCloseDetails]>;\n /**\n * What a click does while this part's `action` is pending. `\"ignore\"`: nothing (the click is\n * cancelled, `aria-disabled=\"true\"` meanwhile). `\"run\"`: the action runs again (`data-pending`\n * until every run has settled); the dialog closes once, when the last run settles and only if\n * the latest run resolved (a rejection leaves it open, as in `\"ignore\"` mode). No effect\n * without `action`.\n *\n * @defaultValue `\"ignore\"`\n */\n whilePending?: WhilePending;\n /** Becomes `dialog.returnValue` (and `details.returnValue` of `openChangeAction(false)`). */\n value?: string;\n /**\n * Never closes. Native `disabled` on a `<button>`; owned, so a `render` element cannot\n * re-enable it. With a link or component `render` whose click this part handles (library mode,\n * or with `action`), `aria-disabled=\"true\"` instead: the element stays focusable.\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, DialogCloseState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, DialogCloseState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, DialogCloseState>;\n /** Replace the default `<button>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, DialogCloseState>;\n}\n\n/**\n * A close button: `<button type=\"button\" commandfor command=\"request-close\">`, or a plain\n * `<button type=\"button\">` with `action` (it does nothing before hydration). In JS-free mode\n * without `action` the browser closes the dialog (a close request: `cancel`, then `close`), with\n * `value` as the `returnValue`. In library mode the click is always cancelled and the close goes\n * through the Root (`openChangeAction(false)` awaited, then `close(value)` in the commit, inside\n * the view transition if the Popup's `transition` is set). With `action` the action runs first in\n * this part's own transition (`data-pending` and `aria-disabled` on the button; a second click\n * meanwhile is ignored unless `whilePending=\"run\"`); a rejection leaves the dialog open.\n *\n * Owned keys (conventions 1.2): `type` (on a `<button>` only), `disabled`, `commandfor`,\n * `command` (neither rendered with `action`), `value`, `onClick` (library mode, or with `action`), `aria-disabled`, `data-pending`,\n * `data-disabled`.\n *\n * @see https://ui.funstack.work/components/dialog\n */\nexport function DialogClose(props: DialogCloseProps) {\n return useDialogClose(props, { part: \"Dialog.Close\" });\n}\n\nDialogClose.displayName = \"DialogClose\";\n\n/** What a caller of `useDialogClose` sets (design/components/alert-dialog.md 8). */\nexport interface DialogCloseOptions {\n /** Public name of the part (`\"AlertDialog.Cancel\"`). */\n readonly part: `${DialogNamespace}.${string}`;\n /** Extra owned keys (AlertDialog.Cancel's `autoFocus`). */\n readonly ownProps?: ComponentPropsWithRef<\"button\">;\n}\n\n/**\n * The body of `Dialog.Close`, also run by `AlertDialog.Cancel` and `AlertDialog.Action`\n * (alert-dialog.md 8).\n */\nexport function useDialogClose(\n {\n action,\n whilePending,\n value,\n disabled = false,\n className,\n style,\n children,\n render,\n ref,\n ...rest\n }: DialogCloseProps,\n { part, ownProps }: DialogCloseOptions,\n): ReactNode {\n const ctx = useDialogContext(part);\n // A Drawer.Popup's development check that the drawer has a Close (drawer.md 7).\n const registerClose = use(DialogCloseRegistry);\n const reportClose = useCallback(\n (element: HTMLElement | null) => {\n if (element !== null) registerClose?.(element);\n },\n [registerClose],\n );\n // Production passes the consumer's ref through untouched.\n const mergedRef = useMergedRefs<HTMLButtonElement | null>(ref, isDev ? reportClose : undefined);\n const { popupRef, recordCloseIntent } = ctx;\n const isLibrary = ctx.mode === \"library\";\n return useOverlayClose({\n part,\n noun: \"dialog\",\n isLibrary,\n popupId: ctx.ids.popup,\n command: \"request-close\",\n openGeneration: ctx.openGeneration,\n action,\n whilePending,\n details: (event) => createActionDetails(event, { returnValue: value ?? \"\" }),\n close: (event) => {\n const returnValue = value ?? \"\";\n if (isLibrary) {\n // A library-owned close (design 2.3): awaited, then `close(returnValue)` in the commit. A\n // no-op when `false` is already intended (a repeated click, or a dismissal meanwhile).\n ctx.requestClose(event, \"activate\", returnValue);\n return;\n }\n // JS-free mode with `action`: a browser-owned close; there is no Root state to go through.\n const dialog = popupRef.current;\n if (dialog === null || !dialog.open) return;\n recordCloseIntent({ reason: \"activate\", event });\n dialog.close(returnValue);\n },\n ownProps: { value, ...ownProps },\n disabled,\n className,\n style,\n children,\n render,\n rest: { ...rest, ref: mergedRef },\n });\n}\n"],"mappings":";;;;;;;AAgBA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;;;;;;;;;;;;;AAsFvC,SAAgB,YAAY,OAAyB;CACnD,OAAO,eAAe,OAAO,EAAE,MAAM,eAAe,CAAC;AACvD;AAEA,YAAY,cAAc;;;;;AAc1B,SAAgB,eACd,EACE,QACA,cACA,OACA,WAAW,OACX,WACA,OACA,UACA,QACA,KACA,GAAG,QAEL,EAAE,MAAM,YACG;CACX,MAAM,MAAM,iBAAiB,IAAI;CAEjC,MAAM,gBAAgB,IAAI,mBAAmB;CAC7C,MAAM,cAAc,aACjB,YAAgC;EAC/B,IAAI,YAAY,MAAM,gBAAgB,OAAO;CAC/C,GACA,CAAC,aAAa,CAChB;CAEA,MAAM,YAAY,cAAwC,KAAK,QAAQ,cAAc,KAAA,CAAS;CAC9F,MAAM,EAAE,UAAU,sBAAsB;CACxC,MAAM,YAAY,IAAI,SAAS;CAC/B,OAAO,gBAAgB;EACrB;EACA,MAAM;EACN;EACA,SAAS,IAAI,IAAI;EACjB,SAAS;EACT,gBAAgB,IAAI;EACpB;EACA;EACA,UAAU,UAAU,oBAAoB,OAAO,EAAE,aAAa,SAAS,GAAG,CAAC;EAC3E,QAAQ,UAAU;GAChB,MAAM,cAAc,SAAS;GAC7B,IAAI,WAAW;IAGb,IAAI,aAAa,OAAO,YAAY,WAAW;IAC/C;GACF;GAEA,MAAM,SAAS,SAAS;GACxB,IAAI,WAAW,QAAQ,CAAC,OAAO,MAAM;GACrC,kBAAkB;IAAE,QAAQ;IAAY;GAAM,CAAC;GAC/C,OAAO,MAAM,WAAW;EAC1B;EACA,UAAU;GAAE;GAAO,GAAG;EAAS;EAC/B;EACA;EACA;EACA;EACA;EACA,MAAM;GAAE,GAAG;GAAM,KAAK;EAAU;CAClC,CAAC;AACH"}
|
|
@@ -3,6 +3,12 @@ import { createContext, use } from "react";
|
|
|
3
3
|
//#region src/dialog/dialog-context.ts
|
|
4
4
|
const DialogContext = createContext(null);
|
|
5
5
|
/**
|
|
6
|
+
* Development only: where a Close part inside a `Drawer.Popup` reports its element, for the
|
|
7
|
+
* Popup's "another way to close" warning (design/components/drawer.md 7). `null` outside a
|
|
8
|
+
* Drawer. The Close parts of every namespace report: a `Dialog.Close` closes a drawer too.
|
|
9
|
+
*/
|
|
10
|
+
const DialogCloseRegistry = createContext(null);
|
|
11
|
+
/**
|
|
6
12
|
* The parts of `Dialog` and `AlertDialog` (design/components/alert-dialog.md 8) share this
|
|
7
13
|
* context. `part` is the public name (`"AlertDialog.Cancel"`); the error names the Root of the
|
|
8
14
|
* same namespace.
|
|
@@ -20,6 +26,6 @@ function rootPartOf(part) {
|
|
|
20
26
|
return `${part.slice(0, part.indexOf("."))}.Root`;
|
|
21
27
|
}
|
|
22
28
|
//#endregion
|
|
23
|
-
export { DialogContext, rootPartOf, useDialogContext };
|
|
29
|
+
export { DialogCloseRegistry, DialogContext, rootPartOf, useDialogContext };
|
|
24
30
|
|
|
25
31
|
//# sourceMappingURL=dialog-context.mjs.map
|