@zag-js/avatar 2.0.0-next.2 → 2.0.0-next.4

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.
@@ -1,134 +1,86 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ const require_avatar_dom = require("./avatar.dom.js");
6
+ let _zag_js_core = require("@zag-js/core");
30
7
  // src/avatar.machine.ts
31
- var avatar_machine_exports = {};
32
- __export(avatar_machine_exports, {
33
- machine: () => machine
8
+ const imageObserver = /* @__PURE__ */ (0, require("@zag-js/dom-query").createSharedMutationObserver)({
9
+ childList: true,
10
+ subtree: true,
11
+ attributeFilter: ["src", "srcset"]
34
12
  });
35
- module.exports = __toCommonJS(avatar_machine_exports);
36
- var import_dom_query = require("@zag-js/dom-query");
37
- var import_core = require("@zag-js/core");
38
- var dom = __toESM(require("./avatar.dom.js"));
39
- var machine = (0, import_core.createMachine)({
40
- initialState() {
41
- return "loading";
42
- },
43
- effects: ["trackImageRemoval", "trackSrcChange"],
44
- on: {
45
- "src.change": {
46
- target: "loading"
47
- },
48
- "img.unmount": {
49
- target: "error"
50
- }
51
- },
52
- states: {
53
- loading: {
54
- entry: ["checkImageStatus"],
55
- on: {
56
- "img.loaded": {
57
- target: "loaded",
58
- actions: ["invokeOnLoad"]
59
- },
60
- "img.error": {
61
- target: "error",
62
- actions: ["invokeOnError"]
63
- }
64
- }
65
- },
66
- error: {
67
- on: {
68
- "img.loaded": {
69
- target: "loaded",
70
- actions: ["invokeOnLoad"]
71
- }
72
- }
73
- },
74
- loaded: {
75
- on: {
76
- "img.error": {
77
- target: "error",
78
- actions: ["invokeOnError"]
79
- }
80
- }
81
- }
82
- },
83
- implementations: {
84
- actions: {
85
- invokeOnLoad({ prop }) {
86
- prop("onStatusChange")?.({ status: "loaded" });
87
- },
88
- invokeOnError({ prop }) {
89
- prop("onStatusChange")?.({ status: "error" });
90
- },
91
- checkImageStatus({ send, scope, prop }) {
92
- const imageEl = dom.getImageEl(scope);
93
- if (!imageEl?.complete) {
94
- prop("onStatusChange")?.({ status: "loading" });
95
- return;
96
- }
97
- const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
98
- send({ type, src: "ssr" });
99
- }
100
- },
101
- effects: {
102
- trackImageRemoval({ send, scope }) {
103
- const rootEl = dom.getRootEl(scope);
104
- return (0, import_dom_query.observeChildren)(rootEl, {
105
- callback(records) {
106
- const removedNodes = Array.from(records[0].removedNodes);
107
- const removed = removedNodes.find(
108
- (node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-avatar-image]")
109
- );
110
- if (removed) {
111
- send({ type: "img.unmount" });
112
- }
113
- }
114
- });
115
- },
116
- trackSrcChange({ send, scope }) {
117
- const imageEl = dom.getImageEl(scope);
118
- return (0, import_dom_query.observeAttributes)(imageEl, {
119
- attributes: ["src", "srcset"],
120
- callback() {
121
- send({ type: "src.change" });
122
- }
123
- });
124
- }
125
- }
126
- }
13
+ const machine = (0, _zag_js_core.createMachine)({
14
+ initialState() {
15
+ return "loading";
16
+ },
17
+ effects: ["trackImage"],
18
+ on: {
19
+ "src.change": { target: "loading" },
20
+ "img.unmount": { target: "error" }
21
+ },
22
+ states: {
23
+ loading: {
24
+ entry: ["checkImageStatus"],
25
+ on: {
26
+ "img.loaded": {
27
+ target: "loaded",
28
+ actions: ["invokeOnLoad"]
29
+ },
30
+ "img.error": {
31
+ target: "error",
32
+ actions: ["invokeOnError"]
33
+ }
34
+ }
35
+ },
36
+ error: { on: { "img.loaded": {
37
+ target: "loaded",
38
+ actions: ["invokeOnLoad"]
39
+ } } },
40
+ loaded: { on: { "img.error": {
41
+ target: "error",
42
+ actions: ["invokeOnError"]
43
+ } } }
44
+ },
45
+ implementations: {
46
+ actions: {
47
+ invokeOnLoad({ prop }) {
48
+ prop("onStatusChange")?.({ status: "loaded" });
49
+ },
50
+ invokeOnError({ prop }) {
51
+ prop("onStatusChange")?.({ status: "error" });
52
+ },
53
+ checkImageStatus({ send, scope, prop }) {
54
+ const imageEl = require_avatar_dom.getImageEl(scope);
55
+ if (!imageEl?.complete) {
56
+ prop("onStatusChange")?.({ status: "loading" });
57
+ return;
58
+ }
59
+ send({
60
+ type: hasLoaded(imageEl) ? "img.loaded" : "img.error",
61
+ src: "ssr"
62
+ });
63
+ }
64
+ },
65
+ effects: { trackImage({ send, scope }) {
66
+ const rootEl = require_avatar_dom.getRootEl(scope);
67
+ if (!rootEl) return;
68
+ let imageEl = require_avatar_dom.getImageEl(scope);
69
+ return imageObserver.observe(rootEl, (records) => {
70
+ const nextEl = require_avatar_dom.getImageEl(scope);
71
+ const prevEl = imageEl;
72
+ imageEl = nextEl;
73
+ if (!nextEl) {
74
+ if (prevEl) send({ type: "img.unmount" });
75
+ return;
76
+ }
77
+ const srcChanged = records.some((record) => record.type === "attributes" && record.target === nextEl);
78
+ if (nextEl !== prevEl || srcChanged) send({ type: "src.change" });
79
+ });
80
+ } }
81
+ }
127
82
  });
128
83
  function hasLoaded(image) {
129
- return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
84
+ return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
130
85
  }
131
- // Annotate the CommonJS export names for ESM import in node:
132
- 0 && (module.exports = {
133
- machine
134
- });
86
+ exports.machine = machine;
@@ -1,99 +1,83 @@
1
- // src/avatar.machine.ts
2
- import { observeAttributes, observeChildren } from "@zag-js/dom-query";
1
+ import { getImageEl, getRootEl } from "./avatar.dom.mjs";
3
2
  import { createMachine } from "@zag-js/core";
4
- import * as dom from "./avatar.dom.mjs";
5
- var machine = createMachine({
6
- initialState() {
7
- return "loading";
8
- },
9
- effects: ["trackImageRemoval", "trackSrcChange"],
10
- on: {
11
- "src.change": {
12
- target: "loading"
13
- },
14
- "img.unmount": {
15
- target: "error"
16
- }
17
- },
18
- states: {
19
- loading: {
20
- entry: ["checkImageStatus"],
21
- on: {
22
- "img.loaded": {
23
- target: "loaded",
24
- actions: ["invokeOnLoad"]
25
- },
26
- "img.error": {
27
- target: "error",
28
- actions: ["invokeOnError"]
29
- }
30
- }
31
- },
32
- error: {
33
- on: {
34
- "img.loaded": {
35
- target: "loaded",
36
- actions: ["invokeOnLoad"]
37
- }
38
- }
39
- },
40
- loaded: {
41
- on: {
42
- "img.error": {
43
- target: "error",
44
- actions: ["invokeOnError"]
45
- }
46
- }
47
- }
48
- },
49
- implementations: {
50
- actions: {
51
- invokeOnLoad({ prop }) {
52
- prop("onStatusChange")?.({ status: "loaded" });
53
- },
54
- invokeOnError({ prop }) {
55
- prop("onStatusChange")?.({ status: "error" });
56
- },
57
- checkImageStatus({ send, scope, prop }) {
58
- const imageEl = dom.getImageEl(scope);
59
- if (!imageEl?.complete) {
60
- prop("onStatusChange")?.({ status: "loading" });
61
- return;
62
- }
63
- const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
64
- send({ type, src: "ssr" });
65
- }
66
- },
67
- effects: {
68
- trackImageRemoval({ send, scope }) {
69
- const rootEl = dom.getRootEl(scope);
70
- return observeChildren(rootEl, {
71
- callback(records) {
72
- const removedNodes = Array.from(records[0].removedNodes);
73
- const removed = removedNodes.find(
74
- (node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-avatar-image]")
75
- );
76
- if (removed) {
77
- send({ type: "img.unmount" });
78
- }
79
- }
80
- });
81
- },
82
- trackSrcChange({ send, scope }) {
83
- const imageEl = dom.getImageEl(scope);
84
- return observeAttributes(imageEl, {
85
- attributes: ["src", "srcset"],
86
- callback() {
87
- send({ type: "src.change" });
88
- }
89
- });
90
- }
91
- }
92
- }
3
+ import { createSharedMutationObserver } from "@zag-js/dom-query";
4
+ // src/avatar.machine.ts
5
+ const imageObserver = /* @__PURE__ */ createSharedMutationObserver({
6
+ childList: true,
7
+ subtree: true,
8
+ attributeFilter: ["src", "srcset"]
9
+ });
10
+ const machine = createMachine({
11
+ initialState() {
12
+ return "loading";
13
+ },
14
+ effects: ["trackImage"],
15
+ on: {
16
+ "src.change": { target: "loading" },
17
+ "img.unmount": { target: "error" }
18
+ },
19
+ states: {
20
+ loading: {
21
+ entry: ["checkImageStatus"],
22
+ on: {
23
+ "img.loaded": {
24
+ target: "loaded",
25
+ actions: ["invokeOnLoad"]
26
+ },
27
+ "img.error": {
28
+ target: "error",
29
+ actions: ["invokeOnError"]
30
+ }
31
+ }
32
+ },
33
+ error: { on: { "img.loaded": {
34
+ target: "loaded",
35
+ actions: ["invokeOnLoad"]
36
+ } } },
37
+ loaded: { on: { "img.error": {
38
+ target: "error",
39
+ actions: ["invokeOnError"]
40
+ } } }
41
+ },
42
+ implementations: {
43
+ actions: {
44
+ invokeOnLoad({ prop }) {
45
+ prop("onStatusChange")?.({ status: "loaded" });
46
+ },
47
+ invokeOnError({ prop }) {
48
+ prop("onStatusChange")?.({ status: "error" });
49
+ },
50
+ checkImageStatus({ send, scope, prop }) {
51
+ const imageEl = getImageEl(scope);
52
+ if (!imageEl?.complete) {
53
+ prop("onStatusChange")?.({ status: "loading" });
54
+ return;
55
+ }
56
+ send({
57
+ type: hasLoaded(imageEl) ? "img.loaded" : "img.error",
58
+ src: "ssr"
59
+ });
60
+ }
61
+ },
62
+ effects: { trackImage({ send, scope }) {
63
+ const rootEl = getRootEl(scope);
64
+ if (!rootEl) return;
65
+ let imageEl = getImageEl(scope);
66
+ return imageObserver.observe(rootEl, (records) => {
67
+ const nextEl = getImageEl(scope);
68
+ const prevEl = imageEl;
69
+ imageEl = nextEl;
70
+ if (!nextEl) {
71
+ if (prevEl) send({ type: "img.unmount" });
72
+ return;
73
+ }
74
+ const srcChanged = records.some((record) => record.type === "attributes" && record.target === nextEl);
75
+ if (nextEl !== prevEl || srcChanged) send({ type: "src.change" });
76
+ });
77
+ } }
78
+ }
93
79
  });
94
80
  function hasLoaded(image) {
95
- return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
81
+ return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
96
82
  }
97
- export {
98
- machine
99
- };
83
+ export { machine };
@@ -1,8 +1,3 @@
1
- import { AvatarProps } from './avatar.types.mjs';
2
- import '@zag-js/core';
3
- import '@zag-js/types';
4
-
5
- declare const props: (keyof AvatarProps)[];
6
- declare const splitProps: <Props extends Partial<AvatarProps>>(props: Props) => [Partial<AvatarProps>, Omit<Props, keyof AvatarProps>];
7
-
8
- export { props, splitProps };
1
+ import { AvatarProps } from "./avatar.types.mjs";
2
+ export declare const props: (keyof AvatarProps)[];
3
+ export declare const splitProps: <Props extends Partial<AvatarProps>>(props: Props) => [Partial<AvatarProps>, Omit<Props, keyof AvatarProps>];
@@ -1,8 +1,3 @@
1
- import { AvatarProps } from './avatar.types.js';
2
- import '@zag-js/core';
3
- import '@zag-js/types';
4
-
5
- declare const props: (keyof AvatarProps)[];
6
- declare const splitProps: <Props extends Partial<AvatarProps>>(props: Props) => [Partial<AvatarProps>, Omit<Props, keyof AvatarProps>];
7
-
8
- export { props, splitProps };
1
+ import { AvatarProps } from "./avatar.types.js";
2
+ export declare const props: (keyof AvatarProps)[];
3
+ export declare const splitProps: <Props extends Partial<AvatarProps>>(props: Props) => [Partial<AvatarProps>, Omit<Props, keyof AvatarProps>];
@@ -1,35 +1,17 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/avatar.props.ts
21
- var avatar_props_exports = {};
22
- __export(avatar_props_exports, {
23
- props: () => props,
24
- splitProps: () => splitProps
25
- });
26
- module.exports = __toCommonJS(avatar_props_exports);
27
- var import_types = require("@zag-js/types");
28
- var import_utils = require("@zag-js/utils");
29
- var props = (0, import_types.createProps)()(["dir", "id", "ids", "onStatusChange", "getRootNode"]);
30
- var splitProps = (0, import_utils.createSplitProps)(props);
31
- // Annotate the CommonJS export names for ESM import in node:
32
- 0 && (module.exports = {
33
- props,
34
- splitProps
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
35
4
  });
5
+ let _zag_js_types = require("@zag-js/types");
6
+ let _zag_js_utils = require("@zag-js/utils");
7
+ // src/avatar.props.ts
8
+ const props = (0, _zag_js_types.createProps)()([
9
+ "dir",
10
+ "id",
11
+ "ids",
12
+ "onStatusChange",
13
+ "getRootNode"
14
+ ]);
15
+ const splitProps = (0, _zag_js_utils.createSplitProps)(props);
16
+ exports.props = props;
17
+ exports.splitProps = splitProps;
@@ -1,9 +1,12 @@
1
- // src/avatar.props.ts
2
1
  import { createProps } from "@zag-js/types";
3
2
  import { createSplitProps } from "@zag-js/utils";
4
- var props = createProps()(["dir", "id", "ids", "onStatusChange", "getRootNode"]);
5
- var splitProps = createSplitProps(props);
6
- export {
7
- props,
8
- splitProps
9
- };
3
+ // src/avatar.props.ts
4
+ const props = createProps()([
5
+ "dir",
6
+ "id",
7
+ "ids",
8
+ "onStatusChange",
9
+ "getRootNode"
10
+ ]);
11
+ const splitProps = createSplitProps(props);
12
+ export { props, splitProps };
@@ -1,67 +1,74 @@
1
- import { Machine, Service } from '@zag-js/core';
2
- import { CommonProperties, DirectionProperty, PropTypes } from '@zag-js/types';
3
-
4
- type LoadStatus = "loading" | "error" | "loaded";
5
- interface StatusChangeDetails {
6
- status: LoadStatus;
1
+ import { Machine, Service } from "@zag-js/core";
2
+ import { CommonProperties, DirectionProperty, PropTypes } from "@zag-js/types";
3
+ export type LoadStatus = "loading" | "error" | "loaded";
4
+ export interface StatusChangeDetails {
5
+ status: LoadStatus;
7
6
  }
8
- interface ElementIds {
9
- root?: string | undefined;
10
- image?: string | undefined;
11
- fallback?: string | undefined;
7
+ export interface ElementIds {
8
+ root?: string | undefined;
9
+ image?: string | undefined;
10
+ fallback?: string | undefined;
12
11
  }
13
- interface AvatarProps extends CommonProperties, DirectionProperty {
14
- /**
15
- * Functional called when the image loading status changes.
16
- */
17
- onStatusChange?: ((details: StatusChangeDetails) => void) | undefined;
18
- /**
19
- * The ids of the elements in the avatar. Useful for composition.
20
- */
21
- ids?: ElementIds | undefined;
12
+ export interface AvatarProps extends CommonProperties, DirectionProperty {
13
+ /**
14
+ * Functional called when the image loading status changes.
15
+ */
16
+ onStatusChange?: ((details: StatusChangeDetails) => void) | undefined;
17
+ /**
18
+ * The ids of the elements in the avatar. Useful for composition.
19
+ */
20
+ ids?: ElementIds | undefined;
22
21
  }
23
- interface AvatarSchema {
24
- props: AvatarProps;
25
- defaultPropKey: never;
26
- context: any;
27
- initial: "loading";
28
- effect: "trackImageRemoval" | "trackSrcChange";
29
- action: "invokeOnLoad" | "invokeOnError" | "checkImageStatus";
30
- event: {
31
- type: "img.loaded";
32
- src?: string | undefined;
33
- } | {
34
- type: "img.error";
35
- src?: string | undefined;
36
- } | {
37
- type: "img.unmount";
38
- } | {
39
- type: "src.change";
40
- };
41
- state: "loading" | "error" | "loaded";
22
+ export interface AvatarSchema {
23
+ props: AvatarProps;
24
+ defaultPropKey: never;
25
+ context: any;
26
+ initial: "loading";
27
+ effect: "trackImage";
28
+ action: "invokeOnLoad" | "invokeOnError" | "checkImageStatus";
29
+ event: {
30
+ type: "img.loaded";
31
+ src?: string | undefined;
32
+ } | {
33
+ type: "img.error";
34
+ src?: string | undefined;
35
+ } | {
36
+ type: "img.unmount";
37
+ } | {
38
+ type: "src.change";
39
+ };
40
+ state: "loading" | "error" | "loaded";
42
41
  }
43
- type AvatarService = Service<AvatarSchema>;
44
- type AvatarMachine = Machine<AvatarSchema>;
45
- interface AvatarApi<T extends PropTypes = PropTypes> {
46
- /**
47
- * The current image loading status.
48
- */
49
- status: LoadStatus;
50
- /**
51
- * Whether the image is loaded.
52
- */
53
- loaded: boolean;
54
- /**
55
- * Function to set loaded state.
56
- */
57
- setLoaded: VoidFunction;
58
- /**
59
- * Function to set error state.
60
- */
61
- setError: VoidFunction;
62
- getRootProps: () => T["element"];
63
- getImageProps: () => T["img"];
64
- getFallbackProps: () => T["element"];
42
+ export type AvatarService = Service<AvatarSchema>;
43
+ export type AvatarMachine = Machine<AvatarSchema>;
44
+ export interface RootState {
45
+ /**
46
+ * The current image loading status.
47
+ */
48
+ status: LoadStatus;
65
49
  }
66
-
67
- export type { AvatarApi, AvatarMachine, AvatarProps, AvatarSchema, AvatarService, ElementIds, LoadStatus, StatusChangeDetails };
50
+ export interface AvatarApi<T extends PropTypes = PropTypes> {
51
+ /**
52
+ * The current image loading status.
53
+ */
54
+ status: LoadStatus;
55
+ /**
56
+ * Whether the image is loaded.
57
+ */
58
+ loaded: boolean;
59
+ /**
60
+ * Function to set loaded state.
61
+ */
62
+ setLoaded: VoidFunction;
63
+ /**
64
+ * Function to set error state.
65
+ */
66
+ setError: VoidFunction;
67
+ /**
68
+ * Returns the state of the root.
69
+ */
70
+ getRootState: () => RootState;
71
+ getRootProps: () => T["element"];
72
+ getImageProps: () => T["img"];
73
+ getFallbackProps: () => T["element"];
74
+ }