@zag-js/avatar 1.34.1 → 1.35.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.
- package/dist/avatar.anatomy.d.mts +6 -0
- package/dist/avatar.anatomy.d.ts +6 -0
- package/dist/avatar.anatomy.js +34 -0
- package/dist/avatar.anatomy.mjs +8 -0
- package/dist/avatar.connect.d.mts +7 -0
- package/dist/avatar.connect.d.ts +7 -0
- package/dist/avatar.connect.js +89 -0
- package/dist/avatar.connect.mjs +54 -0
- package/dist/avatar.dom.d.mts +9 -0
- package/dist/avatar.dom.d.ts +9 -0
- package/dist/avatar.dom.js +42 -0
- package/dist/avatar.dom.mjs +13 -0
- package/dist/avatar.machine.d.mts +7 -0
- package/dist/avatar.machine.d.ts +7 -0
- package/dist/avatar.machine.js +131 -0
- package/dist/avatar.machine.mjs +96 -0
- package/dist/avatar.props.d.mts +8 -0
- package/dist/avatar.props.d.ts +8 -0
- package/dist/avatar.props.js +35 -0
- package/dist/avatar.props.mjs +9 -0
- package/dist/avatar.types.d.mts +66 -0
- package/dist/avatar.types.d.ts +66 -0
- package/dist/avatar.types.js +18 -0
- package/dist/avatar.types.mjs +0 -0
- package/dist/index.d.mts +8 -77
- package/dist/index.d.ts +8 -77
- package/dist/index.js +37 -163
- package/dist/index.mjs +10 -159
- package/package.json +17 -7
|
@@ -0,0 +1,34 @@
|
|
|
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.anatomy.ts
|
|
21
|
+
var avatar_anatomy_exports = {};
|
|
22
|
+
__export(avatar_anatomy_exports, {
|
|
23
|
+
anatomy: () => anatomy,
|
|
24
|
+
parts: () => parts
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(avatar_anatomy_exports);
|
|
27
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
28
|
+
var anatomy = (0, import_anatomy.createAnatomy)("avatar").parts("root", "image", "fallback");
|
|
29
|
+
var parts = anatomy.build();
|
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
31
|
+
0 && (module.exports = {
|
|
32
|
+
anatomy,
|
|
33
|
+
parts
|
|
34
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PropTypes, NormalizeProps } from '@zag-js/types';
|
|
2
|
+
import { Service } from '@zag-js/core';
|
|
3
|
+
import { AvatarSchema, AvatarApi } from './avatar.types.mjs';
|
|
4
|
+
|
|
5
|
+
declare function connect<T extends PropTypes>(service: Service<AvatarSchema>, normalize: NormalizeProps<T>): AvatarApi<T>;
|
|
6
|
+
|
|
7
|
+
export { connect };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PropTypes, NormalizeProps } from '@zag-js/types';
|
|
2
|
+
import { Service } from '@zag-js/core';
|
|
3
|
+
import { AvatarSchema, AvatarApi } from './avatar.types.js';
|
|
4
|
+
|
|
5
|
+
declare function connect<T extends PropTypes>(service: Service<AvatarSchema>, normalize: NormalizeProps<T>): AvatarApi<T>;
|
|
6
|
+
|
|
7
|
+
export { connect };
|
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
|
|
30
|
+
// src/avatar.connect.ts
|
|
31
|
+
var avatar_connect_exports = {};
|
|
32
|
+
__export(avatar_connect_exports, {
|
|
33
|
+
connect: () => connect
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(avatar_connect_exports);
|
|
36
|
+
var import_avatar = require("./avatar.anatomy.cjs");
|
|
37
|
+
var dom = __toESM(require("./avatar.dom.cjs"));
|
|
38
|
+
function connect(service, normalize) {
|
|
39
|
+
const { state, send, prop, scope } = service;
|
|
40
|
+
const loaded = state.matches("loaded");
|
|
41
|
+
return {
|
|
42
|
+
loaded,
|
|
43
|
+
setSrc(src) {
|
|
44
|
+
const img = dom.getImageEl(scope);
|
|
45
|
+
img?.setAttribute("src", src);
|
|
46
|
+
},
|
|
47
|
+
setLoaded() {
|
|
48
|
+
send({ type: "img.loaded", src: "api" });
|
|
49
|
+
},
|
|
50
|
+
setError() {
|
|
51
|
+
send({ type: "img.error", src: "api" });
|
|
52
|
+
},
|
|
53
|
+
getRootProps() {
|
|
54
|
+
return normalize.element({
|
|
55
|
+
...import_avatar.parts.root.attrs,
|
|
56
|
+
dir: prop("dir"),
|
|
57
|
+
id: dom.getRootId(scope)
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
getImageProps() {
|
|
61
|
+
return normalize.img({
|
|
62
|
+
...import_avatar.parts.image.attrs,
|
|
63
|
+
hidden: !loaded,
|
|
64
|
+
dir: prop("dir"),
|
|
65
|
+
id: dom.getImageId(scope),
|
|
66
|
+
"data-state": loaded ? "visible" : "hidden",
|
|
67
|
+
onLoad() {
|
|
68
|
+
send({ type: "img.loaded", src: "element" });
|
|
69
|
+
},
|
|
70
|
+
onError() {
|
|
71
|
+
send({ type: "img.error", src: "element" });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
getFallbackProps() {
|
|
76
|
+
return normalize.element({
|
|
77
|
+
...import_avatar.parts.fallback.attrs,
|
|
78
|
+
dir: prop("dir"),
|
|
79
|
+
id: dom.getFallbackId(scope),
|
|
80
|
+
hidden: loaded,
|
|
81
|
+
"data-state": loaded ? "hidden" : "visible"
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
connect
|
|
89
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/avatar.connect.ts
|
|
2
|
+
import { parts } from "./avatar.anatomy.mjs";
|
|
3
|
+
import * as dom from "./avatar.dom.mjs";
|
|
4
|
+
function connect(service, normalize) {
|
|
5
|
+
const { state, send, prop, scope } = service;
|
|
6
|
+
const loaded = state.matches("loaded");
|
|
7
|
+
return {
|
|
8
|
+
loaded,
|
|
9
|
+
setSrc(src) {
|
|
10
|
+
const img = dom.getImageEl(scope);
|
|
11
|
+
img?.setAttribute("src", src);
|
|
12
|
+
},
|
|
13
|
+
setLoaded() {
|
|
14
|
+
send({ type: "img.loaded", src: "api" });
|
|
15
|
+
},
|
|
16
|
+
setError() {
|
|
17
|
+
send({ type: "img.error", src: "api" });
|
|
18
|
+
},
|
|
19
|
+
getRootProps() {
|
|
20
|
+
return normalize.element({
|
|
21
|
+
...parts.root.attrs,
|
|
22
|
+
dir: prop("dir"),
|
|
23
|
+
id: dom.getRootId(scope)
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
getImageProps() {
|
|
27
|
+
return normalize.img({
|
|
28
|
+
...parts.image.attrs,
|
|
29
|
+
hidden: !loaded,
|
|
30
|
+
dir: prop("dir"),
|
|
31
|
+
id: dom.getImageId(scope),
|
|
32
|
+
"data-state": loaded ? "visible" : "hidden",
|
|
33
|
+
onLoad() {
|
|
34
|
+
send({ type: "img.loaded", src: "element" });
|
|
35
|
+
},
|
|
36
|
+
onError() {
|
|
37
|
+
send({ type: "img.error", src: "element" });
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
getFallbackProps() {
|
|
42
|
+
return normalize.element({
|
|
43
|
+
...parts.fallback.attrs,
|
|
44
|
+
dir: prop("dir"),
|
|
45
|
+
id: dom.getFallbackId(scope),
|
|
46
|
+
hidden: loaded,
|
|
47
|
+
"data-state": loaded ? "hidden" : "visible"
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export {
|
|
53
|
+
connect
|
|
54
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Scope } from '@zag-js/core';
|
|
2
|
+
|
|
3
|
+
declare const getRootId: (ctx: Scope) => any;
|
|
4
|
+
declare const getImageId: (ctx: Scope) => any;
|
|
5
|
+
declare const getFallbackId: (ctx: Scope) => any;
|
|
6
|
+
declare const getRootEl: (ctx: Scope) => HTMLElement | null;
|
|
7
|
+
declare const getImageEl: (ctx: Scope) => HTMLImageElement | null;
|
|
8
|
+
|
|
9
|
+
export { getFallbackId, getImageEl, getImageId, getRootEl, getRootId };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Scope } from '@zag-js/core';
|
|
2
|
+
|
|
3
|
+
declare const getRootId: (ctx: Scope) => any;
|
|
4
|
+
declare const getImageId: (ctx: Scope) => any;
|
|
5
|
+
declare const getFallbackId: (ctx: Scope) => any;
|
|
6
|
+
declare const getRootEl: (ctx: Scope) => HTMLElement | null;
|
|
7
|
+
declare const getImageEl: (ctx: Scope) => HTMLImageElement | null;
|
|
8
|
+
|
|
9
|
+
export { getFallbackId, getImageEl, getImageId, getRootEl, getRootId };
|
|
@@ -0,0 +1,42 @@
|
|
|
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.dom.ts
|
|
21
|
+
var avatar_dom_exports = {};
|
|
22
|
+
__export(avatar_dom_exports, {
|
|
23
|
+
getFallbackId: () => getFallbackId,
|
|
24
|
+
getImageEl: () => getImageEl,
|
|
25
|
+
getImageId: () => getImageId,
|
|
26
|
+
getRootEl: () => getRootEl,
|
|
27
|
+
getRootId: () => getRootId
|
|
28
|
+
});
|
|
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));
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
getFallbackId,
|
|
38
|
+
getImageEl,
|
|
39
|
+
getImageId,
|
|
40
|
+
getRootEl,
|
|
41
|
+
getRootId
|
|
42
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
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));
|
|
7
|
+
export {
|
|
8
|
+
getFallbackId,
|
|
9
|
+
getImageEl,
|
|
10
|
+
getImageId,
|
|
11
|
+
getRootEl,
|
|
12
|
+
getRootId
|
|
13
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
|
|
30
|
+
// src/avatar.machine.ts
|
|
31
|
+
var avatar_machine_exports = {};
|
|
32
|
+
__export(avatar_machine_exports, {
|
|
33
|
+
machine: () => machine
|
|
34
|
+
});
|
|
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.cjs"));
|
|
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 }) {
|
|
92
|
+
const imageEl = dom.getImageEl(scope);
|
|
93
|
+
if (!imageEl?.complete) return;
|
|
94
|
+
const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
|
|
95
|
+
send({ type, src: "ssr" });
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
effects: {
|
|
99
|
+
trackImageRemoval({ send, scope }) {
|
|
100
|
+
const rootEl = dom.getRootEl(scope);
|
|
101
|
+
return (0, import_dom_query.observeChildren)(rootEl, {
|
|
102
|
+
callback(records) {
|
|
103
|
+
const removedNodes = Array.from(records[0].removedNodes);
|
|
104
|
+
const removed = removedNodes.find(
|
|
105
|
+
(node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
|
|
106
|
+
);
|
|
107
|
+
if (removed) {
|
|
108
|
+
send({ type: "img.unmount" });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
trackSrcChange({ send, scope }) {
|
|
114
|
+
const imageEl = dom.getImageEl(scope);
|
|
115
|
+
return (0, import_dom_query.observeAttributes)(imageEl, {
|
|
116
|
+
attributes: ["src", "srcset"],
|
|
117
|
+
callback() {
|
|
118
|
+
send({ type: "src.change" });
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
function hasLoaded(image) {
|
|
126
|
+
return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
|
|
127
|
+
}
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {
|
|
130
|
+
machine
|
|
131
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// src/avatar.machine.ts
|
|
2
|
+
import { observeAttributes, observeChildren } from "@zag-js/dom-query";
|
|
3
|
+
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 }) {
|
|
58
|
+
const imageEl = dom.getImageEl(scope);
|
|
59
|
+
if (!imageEl?.complete) return;
|
|
60
|
+
const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
|
|
61
|
+
send({ type, src: "ssr" });
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
effects: {
|
|
65
|
+
trackImageRemoval({ send, scope }) {
|
|
66
|
+
const rootEl = dom.getRootEl(scope);
|
|
67
|
+
return observeChildren(rootEl, {
|
|
68
|
+
callback(records) {
|
|
69
|
+
const removedNodes = Array.from(records[0].removedNodes);
|
|
70
|
+
const removed = removedNodes.find(
|
|
71
|
+
(node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
|
|
72
|
+
);
|
|
73
|
+
if (removed) {
|
|
74
|
+
send({ type: "img.unmount" });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
trackSrcChange({ send, scope }) {
|
|
80
|
+
const imageEl = dom.getImageEl(scope);
|
|
81
|
+
return observeAttributes(imageEl, {
|
|
82
|
+
attributes: ["src", "srcset"],
|
|
83
|
+
callback() {
|
|
84
|
+
send({ type: "src.change" });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
function hasLoaded(image) {
|
|
92
|
+
return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
machine
|
|
96
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
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 };
|
|
@@ -0,0 +1,8 @@
|
|
|
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 };
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
|
35
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/avatar.props.ts
|
|
2
|
+
import { createProps } from "@zag-js/types";
|
|
3
|
+
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
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Machine, Service } from '@zag-js/core';
|
|
2
|
+
import { CommonProperties, DirectionProperty, PropTypes } from '@zag-js/types';
|
|
3
|
+
|
|
4
|
+
type LoadStatus = "error" | "loaded";
|
|
5
|
+
interface StatusChangeDetails {
|
|
6
|
+
status: LoadStatus;
|
|
7
|
+
}
|
|
8
|
+
type ElementIds = Partial<{
|
|
9
|
+
root: string;
|
|
10
|
+
image: string;
|
|
11
|
+
fallback: string;
|
|
12
|
+
}>;
|
|
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;
|
|
22
|
+
}
|
|
23
|
+
interface AvatarSchema {
|
|
24
|
+
props: AvatarProps;
|
|
25
|
+
context: any;
|
|
26
|
+
initial: "loading";
|
|
27
|
+
effect: "trackImageRemoval" | "trackSrcChange";
|
|
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";
|
|
41
|
+
}
|
|
42
|
+
type AvatarService = Service<AvatarSchema>;
|
|
43
|
+
type AvatarMachine = Machine<AvatarSchema>;
|
|
44
|
+
interface AvatarApi<T extends PropTypes = PropTypes> {
|
|
45
|
+
/**
|
|
46
|
+
* Whether the image is loaded.
|
|
47
|
+
*/
|
|
48
|
+
loaded: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Function to set new src.
|
|
51
|
+
*/
|
|
52
|
+
setSrc: (src: string) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Function to set loaded state.
|
|
55
|
+
*/
|
|
56
|
+
setLoaded: VoidFunction;
|
|
57
|
+
/**
|
|
58
|
+
* Function to set error state.
|
|
59
|
+
*/
|
|
60
|
+
setError: VoidFunction;
|
|
61
|
+
getRootProps: () => T["element"];
|
|
62
|
+
getImageProps: () => T["img"];
|
|
63
|
+
getFallbackProps: () => T["element"];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type { AvatarApi, AvatarMachine, AvatarProps, AvatarSchema, AvatarService, ElementIds, LoadStatus, StatusChangeDetails };
|