@zag-js/avatar 1.43.0 → 2.0.0-next.1

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.
@@ -36,9 +36,11 @@ module.exports = __toCommonJS(avatar_connect_exports);
36
36
  var import_avatar = require("./avatar.anatomy.js");
37
37
  var dom = __toESM(require("./avatar.dom.js"));
38
38
  function connect(service, normalize) {
39
- const { state, send, prop, scope } = service;
40
- const loaded = state.matches("loaded");
39
+ const { send, prop, scope } = service;
40
+ const status = getStatus(service);
41
+ const loaded = status === "loaded";
41
42
  return {
43
+ status,
42
44
  loaded,
43
45
  setSrc(src) {
44
46
  const img = dom.getImageEl(scope);
@@ -52,17 +54,15 @@ function connect(service, normalize) {
52
54
  },
53
55
  getRootProps() {
54
56
  return normalize.element({
55
- ...import_avatar.parts.root.attrs,
56
- dir: prop("dir"),
57
- id: dom.getRootId(scope)
57
+ ...import_avatar.parts.root.attrs(scope.id),
58
+ dir: prop("dir")
58
59
  });
59
60
  },
60
61
  getImageProps() {
61
62
  return normalize.img({
62
- ...import_avatar.parts.image.attrs,
63
+ ...import_avatar.parts.image.attrs(scope.id),
63
64
  hidden: !loaded,
64
65
  dir: prop("dir"),
65
- id: dom.getImageId(scope),
66
66
  "data-state": loaded ? "visible" : "hidden",
67
67
  onLoad() {
68
68
  send({ type: "img.loaded", src: "element" });
@@ -74,15 +74,20 @@ function connect(service, normalize) {
74
74
  },
75
75
  getFallbackProps() {
76
76
  return normalize.element({
77
- ...import_avatar.parts.fallback.attrs,
77
+ ...import_avatar.parts.fallback.attrs(scope.id),
78
78
  dir: prop("dir"),
79
- id: dom.getFallbackId(scope),
80
79
  hidden: loaded,
81
80
  "data-state": loaded ? "hidden" : "visible"
82
81
  });
83
82
  }
84
83
  };
85
84
  }
85
+ function getStatus(service) {
86
+ const { state } = service;
87
+ if (state.matches("error")) return "error";
88
+ if (state.matches("loaded")) return "loaded";
89
+ return "loading";
90
+ }
86
91
  // Annotate the CommonJS export names for ESM import in node:
87
92
  0 && (module.exports = {
88
93
  connect
@@ -2,9 +2,11 @@
2
2
  import { parts } from "./avatar.anatomy.mjs";
3
3
  import * as dom from "./avatar.dom.mjs";
4
4
  function connect(service, normalize) {
5
- const { state, send, prop, scope } = service;
6
- const loaded = state.matches("loaded");
5
+ const { send, prop, scope } = service;
6
+ const status = getStatus(service);
7
+ const loaded = status === "loaded";
7
8
  return {
9
+ status,
8
10
  loaded,
9
11
  setSrc(src) {
10
12
  const img = dom.getImageEl(scope);
@@ -18,17 +20,15 @@ function connect(service, normalize) {
18
20
  },
19
21
  getRootProps() {
20
22
  return normalize.element({
21
- ...parts.root.attrs,
22
- dir: prop("dir"),
23
- id: dom.getRootId(scope)
23
+ ...parts.root.attrs(scope.id),
24
+ dir: prop("dir")
24
25
  });
25
26
  },
26
27
  getImageProps() {
27
28
  return normalize.img({
28
- ...parts.image.attrs,
29
+ ...parts.image.attrs(scope.id),
29
30
  hidden: !loaded,
30
31
  dir: prop("dir"),
31
- id: dom.getImageId(scope),
32
32
  "data-state": loaded ? "visible" : "hidden",
33
33
  onLoad() {
34
34
  send({ type: "img.loaded", src: "element" });
@@ -40,15 +40,20 @@ function connect(service, normalize) {
40
40
  },
41
41
  getFallbackProps() {
42
42
  return normalize.element({
43
- ...parts.fallback.attrs,
43
+ ...parts.fallback.attrs(scope.id),
44
44
  dir: prop("dir"),
45
- id: dom.getFallbackId(scope),
46
45
  hidden: loaded,
47
46
  "data-state": loaded ? "hidden" : "visible"
48
47
  });
49
48
  }
50
49
  };
51
50
  }
51
+ function getStatus(service) {
52
+ const { state } = service;
53
+ if (state.matches("error")) return "error";
54
+ if (state.matches("loaded")) return "loaded";
55
+ return "loading";
56
+ }
52
57
  export {
53
58
  connect
54
59
  };
@@ -27,11 +27,12 @@ __export(avatar_dom_exports, {
27
27
  getRootId: () => getRootId
28
28
  });
29
29
  module.exports = __toCommonJS(avatar_dom_exports);
30
- var getRootId = (ctx) => ctx.ids?.root ?? `avatar:${ctx.id}`;
31
- var getImageId = (ctx) => ctx.ids?.image ?? `avatar:${ctx.id}:image`;
32
- var getFallbackId = (ctx) => ctx.ids?.fallback ?? `avatar:${ctx.id}:fallback`;
33
- var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
34
- var getImageEl = (ctx) => ctx.getById(getImageId(ctx));
30
+ var import_avatar = require("./avatar.anatomy.js");
31
+ var getRootId = (ctx) => ctx.ids?.root ?? `${ctx.id}`;
32
+ var getImageId = (ctx) => ctx.ids?.image ?? `${ctx.id}:image`;
33
+ var getFallbackId = (ctx) => ctx.ids?.fallback ?? `${ctx.id}:fallback`;
34
+ var getRootEl = (ctx) => ctx.query(ctx.selector(import_avatar.parts.root));
35
+ var getImageEl = (ctx) => ctx.query(ctx.selector(import_avatar.parts.image));
35
36
  // Annotate the CommonJS export names for ESM import in node:
36
37
  0 && (module.exports = {
37
38
  getFallbackId,
@@ -1,9 +1,10 @@
1
1
  // src/avatar.dom.ts
2
- var getRootId = (ctx) => ctx.ids?.root ?? `avatar:${ctx.id}`;
3
- var getImageId = (ctx) => ctx.ids?.image ?? `avatar:${ctx.id}:image`;
4
- var getFallbackId = (ctx) => ctx.ids?.fallback ?? `avatar:${ctx.id}:fallback`;
5
- var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
6
- var getImageEl = (ctx) => ctx.getById(getImageId(ctx));
2
+ import { parts } from "./avatar.anatomy.mjs";
3
+ var getRootId = (ctx) => ctx.ids?.root ?? `${ctx.id}`;
4
+ var getImageId = (ctx) => ctx.ids?.image ?? `${ctx.id}:image`;
5
+ var getFallbackId = (ctx) => ctx.ids?.fallback ?? `${ctx.id}:fallback`;
6
+ var getRootEl = (ctx) => ctx.query(ctx.selector(parts.root));
7
+ var getImageEl = (ctx) => ctx.query(ctx.selector(parts.image));
7
8
  export {
8
9
  getFallbackId,
9
10
  getImageEl,
@@ -88,9 +88,12 @@ var machine = (0, import_core.createMachine)({
88
88
  invokeOnError({ prop }) {
89
89
  prop("onStatusChange")?.({ status: "error" });
90
90
  },
91
- checkImageStatus({ send, scope }) {
91
+ checkImageStatus({ send, scope, prop }) {
92
92
  const imageEl = dom.getImageEl(scope);
93
- if (!imageEl?.complete) return;
93
+ if (!imageEl?.complete) {
94
+ prop("onStatusChange")?.({ status: "loading" });
95
+ return;
96
+ }
94
97
  const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
95
98
  send({ type, src: "ssr" });
96
99
  }
@@ -102,7 +105,7 @@ var machine = (0, import_core.createMachine)({
102
105
  callback(records) {
103
106
  const removedNodes = Array.from(records[0].removedNodes);
104
107
  const removed = removedNodes.find(
105
- (node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
108
+ (node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-avatar-image]")
106
109
  );
107
110
  if (removed) {
108
111
  send({ type: "img.unmount" });
@@ -54,9 +54,12 @@ var machine = createMachine({
54
54
  invokeOnError({ prop }) {
55
55
  prop("onStatusChange")?.({ status: "error" });
56
56
  },
57
- checkImageStatus({ send, scope }) {
57
+ checkImageStatus({ send, scope, prop }) {
58
58
  const imageEl = dom.getImageEl(scope);
59
- if (!imageEl?.complete) return;
59
+ if (!imageEl?.complete) {
60
+ prop("onStatusChange")?.({ status: "loading" });
61
+ return;
62
+ }
60
63
  const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
61
64
  send({ type, src: "ssr" });
62
65
  }
@@ -68,7 +71,7 @@ var machine = createMachine({
68
71
  callback(records) {
69
72
  const removedNodes = Array.from(records[0].removedNodes);
70
73
  const removed = removedNodes.find(
71
- (node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
74
+ (node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-avatar-image]")
72
75
  );
73
76
  if (removed) {
74
77
  send({ type: "img.unmount" });
@@ -1,7 +1,7 @@
1
1
  import { Machine, Service } from '@zag-js/core';
2
2
  import { CommonProperties, DirectionProperty, PropTypes } from '@zag-js/types';
3
3
 
4
- type LoadStatus = "error" | "loaded";
4
+ type LoadStatus = "loading" | "error" | "loaded";
5
5
  interface StatusChangeDetails {
6
6
  status: LoadStatus;
7
7
  }
@@ -42,6 +42,10 @@ interface AvatarSchema {
42
42
  type AvatarService = Service<AvatarSchema>;
43
43
  type AvatarMachine = Machine<AvatarSchema>;
44
44
  interface AvatarApi<T extends PropTypes = PropTypes> {
45
+ /**
46
+ * The current image loading status.
47
+ */
48
+ status: LoadStatus;
45
49
  /**
46
50
  * Whether the image is loaded.
47
51
  */
@@ -1,7 +1,7 @@
1
1
  import { Machine, Service } from '@zag-js/core';
2
2
  import { CommonProperties, DirectionProperty, PropTypes } from '@zag-js/types';
3
3
 
4
- type LoadStatus = "error" | "loaded";
4
+ type LoadStatus = "loading" | "error" | "loaded";
5
5
  interface StatusChangeDetails {
6
6
  status: LoadStatus;
7
7
  }
@@ -42,6 +42,10 @@ interface AvatarSchema {
42
42
  type AvatarService = Service<AvatarSchema>;
43
43
  type AvatarMachine = Machine<AvatarSchema>;
44
44
  interface AvatarApi<T extends PropTypes = PropTypes> {
45
+ /**
46
+ * The current image loading status.
47
+ */
48
+ status: LoadStatus;
45
49
  /**
46
50
  * Whether the image is loaded.
47
51
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/avatar",
3
- "version": "1.43.0",
3
+ "version": "2.0.0-next.1",
4
4
  "description": "Core logic for the avatar widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -30,11 +30,11 @@
30
30
  "url": "https://github.com/chakra-ui/zag/issues"
31
31
  },
32
32
  "dependencies": {
33
- "@zag-js/anatomy": "1.43.0",
34
- "@zag-js/core": "1.43.0",
35
- "@zag-js/dom-query": "1.43.0",
36
- "@zag-js/utils": "1.43.0",
37
- "@zag-js/types": "1.43.0"
33
+ "@zag-js/anatomy": "2.0.0-next.1",
34
+ "@zag-js/core": "2.0.0-next.1",
35
+ "@zag-js/dom-query": "2.0.0-next.1",
36
+ "@zag-js/utils": "2.0.0-next.1",
37
+ "@zag-js/types": "2.0.0-next.1"
38
38
  },
39
39
  "devDependencies": {
40
40
  "clean-package": "2.2.0"