@zag-js/avatar 0.82.1 → 1.0.0
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/index.d.mts +28 -20
- package/dist/index.d.ts +28 -20
- package/dist/index.js +97 -104
- package/dist/index.mjs +98 -105
- package/package.json +14 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
-
import {
|
|
2
|
+
import { CommonProperties, DirectionProperty, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
3
|
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
-
import {
|
|
4
|
+
import { Service } from '@zag-js/core';
|
|
5
5
|
|
|
6
6
|
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "image" | "fallback">;
|
|
7
7
|
|
|
@@ -14,7 +14,7 @@ type ElementIds = Partial<{
|
|
|
14
14
|
image: string;
|
|
15
15
|
fallback: string;
|
|
16
16
|
}>;
|
|
17
|
-
interface
|
|
17
|
+
interface AvatarProps extends CommonProperties, DirectionProperty {
|
|
18
18
|
/**
|
|
19
19
|
* Functional called when the image loading status changes.
|
|
20
20
|
*/
|
|
@@ -24,19 +24,27 @@ interface PublicContext extends CommonProperties, DirectionProperty {
|
|
|
24
24
|
*/
|
|
25
25
|
ids?: ElementIds | undefined;
|
|
26
26
|
}
|
|
27
|
-
interface
|
|
27
|
+
interface AvatarSchema {
|
|
28
|
+
props: AvatarProps;
|
|
29
|
+
context: any;
|
|
30
|
+
initial: "loading";
|
|
31
|
+
effect: "trackImageRemoval" | "trackSrcChange";
|
|
32
|
+
action: "invokeOnLoad" | "invokeOnError" | "checkImageStatus";
|
|
33
|
+
event: {
|
|
34
|
+
type: "img.loaded";
|
|
35
|
+
src?: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "img.error";
|
|
38
|
+
src?: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: "img.unmount";
|
|
41
|
+
} | {
|
|
42
|
+
type: "src.change";
|
|
43
|
+
};
|
|
44
|
+
state: "loading" | "error" | "loaded";
|
|
28
45
|
}
|
|
29
|
-
type
|
|
30
|
-
|
|
31
|
-
interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
|
|
32
|
-
}
|
|
33
|
-
interface MachineState {
|
|
34
|
-
value: "loading" | "error" | "loaded";
|
|
35
|
-
}
|
|
36
|
-
type State = StateMachine.State<MachineContext, MachineState>;
|
|
37
|
-
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
38
|
-
type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
|
|
39
|
-
interface MachineApi<T extends PropTypes = PropTypes> {
|
|
46
|
+
type AvatarService = Service<AvatarSchema>;
|
|
47
|
+
interface AvatarApi<T extends PropTypes = PropTypes> {
|
|
40
48
|
/**
|
|
41
49
|
* Whether the image is loaded.
|
|
42
50
|
*/
|
|
@@ -58,11 +66,11 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
58
66
|
getFallbackProps(): T["element"];
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
declare function connect<T extends PropTypes>(
|
|
69
|
+
declare function connect<T extends PropTypes>(service: Service<AvatarSchema>, normalize: NormalizeProps<T>): AvatarApi<T>;
|
|
62
70
|
|
|
63
|
-
declare
|
|
71
|
+
declare const machine: _zag_js_core.MachineConfig<AvatarSchema>;
|
|
64
72
|
|
|
65
|
-
declare const props: (
|
|
66
|
-
declare const splitProps: <Props extends Partial<
|
|
73
|
+
declare const props: (keyof AvatarProps)[];
|
|
74
|
+
declare const splitProps: <Props extends Partial<AvatarProps>>(props: Props) => [Partial<AvatarProps>, Omit<Props, keyof AvatarProps>];
|
|
67
75
|
|
|
68
|
-
export { type
|
|
76
|
+
export { type AvatarApi as Api, type ElementIds, type LoadStatus, type AvatarProps as Props, type AvatarService as Service, type StatusChangeDetails, anatomy, connect, machine, props, splitProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
-
import {
|
|
2
|
+
import { CommonProperties, DirectionProperty, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
3
|
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
-
import {
|
|
4
|
+
import { Service } from '@zag-js/core';
|
|
5
5
|
|
|
6
6
|
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "image" | "fallback">;
|
|
7
7
|
|
|
@@ -14,7 +14,7 @@ type ElementIds = Partial<{
|
|
|
14
14
|
image: string;
|
|
15
15
|
fallback: string;
|
|
16
16
|
}>;
|
|
17
|
-
interface
|
|
17
|
+
interface AvatarProps extends CommonProperties, DirectionProperty {
|
|
18
18
|
/**
|
|
19
19
|
* Functional called when the image loading status changes.
|
|
20
20
|
*/
|
|
@@ -24,19 +24,27 @@ interface PublicContext extends CommonProperties, DirectionProperty {
|
|
|
24
24
|
*/
|
|
25
25
|
ids?: ElementIds | undefined;
|
|
26
26
|
}
|
|
27
|
-
interface
|
|
27
|
+
interface AvatarSchema {
|
|
28
|
+
props: AvatarProps;
|
|
29
|
+
context: any;
|
|
30
|
+
initial: "loading";
|
|
31
|
+
effect: "trackImageRemoval" | "trackSrcChange";
|
|
32
|
+
action: "invokeOnLoad" | "invokeOnError" | "checkImageStatus";
|
|
33
|
+
event: {
|
|
34
|
+
type: "img.loaded";
|
|
35
|
+
src?: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "img.error";
|
|
38
|
+
src?: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: "img.unmount";
|
|
41
|
+
} | {
|
|
42
|
+
type: "src.change";
|
|
43
|
+
};
|
|
44
|
+
state: "loading" | "error" | "loaded";
|
|
28
45
|
}
|
|
29
|
-
type
|
|
30
|
-
|
|
31
|
-
interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
|
|
32
|
-
}
|
|
33
|
-
interface MachineState {
|
|
34
|
-
value: "loading" | "error" | "loaded";
|
|
35
|
-
}
|
|
36
|
-
type State = StateMachine.State<MachineContext, MachineState>;
|
|
37
|
-
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
38
|
-
type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
|
|
39
|
-
interface MachineApi<T extends PropTypes = PropTypes> {
|
|
46
|
+
type AvatarService = Service<AvatarSchema>;
|
|
47
|
+
interface AvatarApi<T extends PropTypes = PropTypes> {
|
|
40
48
|
/**
|
|
41
49
|
* Whether the image is loaded.
|
|
42
50
|
*/
|
|
@@ -58,11 +66,11 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
58
66
|
getFallbackProps(): T["element"];
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
declare function connect<T extends PropTypes>(
|
|
69
|
+
declare function connect<T extends PropTypes>(service: Service<AvatarSchema>, normalize: NormalizeProps<T>): AvatarApi<T>;
|
|
62
70
|
|
|
63
|
-
declare
|
|
71
|
+
declare const machine: _zag_js_core.MachineConfig<AvatarSchema>;
|
|
64
72
|
|
|
65
|
-
declare const props: (
|
|
66
|
-
declare const splitProps: <Props extends Partial<
|
|
73
|
+
declare const props: (keyof AvatarProps)[];
|
|
74
|
+
declare const splitProps: <Props extends Partial<AvatarProps>>(props: Props) => [Partial<AvatarProps>, Omit<Props, keyof AvatarProps>];
|
|
67
75
|
|
|
68
|
-
export { type
|
|
76
|
+
export { type AvatarApi as Api, type ElementIds, type LoadStatus, type AvatarProps as Props, type AvatarService as Service, type StatusChangeDetails, anatomy, connect, machine, props, splitProps };
|
package/dist/index.js
CHANGED
|
@@ -3,162 +3,155 @@
|
|
|
3
3
|
var anatomy$1 = require('@zag-js/anatomy');
|
|
4
4
|
var domQuery = require('@zag-js/dom-query');
|
|
5
5
|
var core = require('@zag-js/core');
|
|
6
|
-
var utils = require('@zag-js/utils');
|
|
7
6
|
var types = require('@zag-js/types');
|
|
7
|
+
var utils = require('@zag-js/utils');
|
|
8
8
|
|
|
9
9
|
// src/avatar.anatomy.ts
|
|
10
10
|
var anatomy = anatomy$1.createAnatomy("avatar").parts("root", "image", "fallback");
|
|
11
11
|
var parts = anatomy.build();
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
|
|
13
|
+
// src/avatar.dom.ts
|
|
14
|
+
var getRootId = (ctx) => ctx.ids?.root ?? `avatar:${ctx.id}`;
|
|
15
|
+
var getImageId = (ctx) => ctx.ids?.image ?? `avatar:${ctx.id}:image`;
|
|
16
|
+
var getFallbackId = (ctx) => ctx.ids?.fallback ?? `avatar:${ctx.id}:fallback`;
|
|
17
|
+
var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
|
|
18
|
+
var getImageEl = (ctx) => ctx.getById(getImageId(ctx));
|
|
19
19
|
|
|
20
20
|
// src/avatar.connect.ts
|
|
21
|
-
function connect(
|
|
21
|
+
function connect(service, normalize) {
|
|
22
|
+
const { state, send, prop, scope } = service;
|
|
22
23
|
const loaded = state.matches("loaded");
|
|
23
24
|
return {
|
|
24
25
|
loaded,
|
|
25
26
|
setSrc(src) {
|
|
26
|
-
|
|
27
|
+
const img = getImageEl(scope);
|
|
28
|
+
img?.setAttribute("src", src);
|
|
27
29
|
},
|
|
28
30
|
setLoaded() {
|
|
29
|
-
send({ type: "
|
|
31
|
+
send({ type: "img.loaded", src: "api" });
|
|
30
32
|
},
|
|
31
33
|
setError() {
|
|
32
|
-
send({ type: "
|
|
34
|
+
send({ type: "img.error", src: "api" });
|
|
33
35
|
},
|
|
34
36
|
getRootProps() {
|
|
35
37
|
return normalize.element({
|
|
36
38
|
...parts.root.attrs,
|
|
37
|
-
dir:
|
|
38
|
-
id:
|
|
39
|
+
dir: prop("dir"),
|
|
40
|
+
id: getRootId(scope)
|
|
39
41
|
});
|
|
40
42
|
},
|
|
41
43
|
getImageProps() {
|
|
42
44
|
return normalize.img({
|
|
43
45
|
...parts.image.attrs,
|
|
44
46
|
hidden: !loaded,
|
|
45
|
-
dir:
|
|
46
|
-
id:
|
|
47
|
+
dir: prop("dir"),
|
|
48
|
+
id: getImageId(scope),
|
|
47
49
|
"data-state": loaded ? "visible" : "hidden",
|
|
48
50
|
onLoad() {
|
|
49
|
-
send({ type: "
|
|
51
|
+
send({ type: "img.loaded", src: "element" });
|
|
50
52
|
},
|
|
51
53
|
onError() {
|
|
52
|
-
send({ type: "
|
|
54
|
+
send({ type: "img.error", src: "element" });
|
|
53
55
|
}
|
|
54
56
|
});
|
|
55
57
|
},
|
|
56
58
|
getFallbackProps() {
|
|
57
59
|
return normalize.element({
|
|
58
60
|
...parts.fallback.attrs,
|
|
59
|
-
dir:
|
|
60
|
-
id:
|
|
61
|
+
dir: prop("dir"),
|
|
62
|
+
id: getFallbackId(scope),
|
|
61
63
|
hidden: loaded,
|
|
62
64
|
"data-state": loaded ? "hidden" : "visible"
|
|
63
65
|
});
|
|
64
66
|
}
|
|
65
67
|
};
|
|
66
68
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
var machine = core.createMachine({
|
|
70
|
+
initialState() {
|
|
71
|
+
return "loading";
|
|
72
|
+
},
|
|
73
|
+
effects: ["trackImageRemoval", "trackSrcChange"],
|
|
74
|
+
on: {
|
|
75
|
+
"src.change": {
|
|
76
|
+
target: "loading"
|
|
77
|
+
},
|
|
78
|
+
"img.unmount": {
|
|
79
|
+
target: "error"
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
states: {
|
|
83
|
+
loading: {
|
|
84
|
+
entry: ["checkImageStatus"],
|
|
75
85
|
on: {
|
|
76
|
-
"
|
|
77
|
-
target: "
|
|
86
|
+
"img.loaded": {
|
|
87
|
+
target: "loaded",
|
|
88
|
+
actions: ["invokeOnLoad"]
|
|
78
89
|
},
|
|
79
|
-
"
|
|
80
|
-
target: "error"
|
|
90
|
+
"img.error": {
|
|
91
|
+
target: "error",
|
|
92
|
+
actions: ["invokeOnError"]
|
|
81
93
|
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
target: "loaded",
|
|
90
|
-
actions: ["invokeOnLoad"]
|
|
91
|
-
},
|
|
92
|
-
"IMG.ERROR": {
|
|
93
|
-
target: "error",
|
|
94
|
-
actions: ["invokeOnError"]
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
error: {
|
|
99
|
-
activities: ["trackSrcChange"],
|
|
100
|
-
on: {
|
|
101
|
-
"IMG.LOADED": {
|
|
102
|
-
target: "loaded",
|
|
103
|
-
actions: ["invokeOnLoad"]
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
loaded: {
|
|
108
|
-
activities: ["trackSrcChange"],
|
|
109
|
-
on: {
|
|
110
|
-
"IMG.ERROR": {
|
|
111
|
-
target: "error",
|
|
112
|
-
actions: ["invokeOnError"]
|
|
113
|
-
}
|
|
114
|
-
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
error: {
|
|
97
|
+
on: {
|
|
98
|
+
"img.loaded": {
|
|
99
|
+
target: "loaded",
|
|
100
|
+
actions: ["invokeOnLoad"]
|
|
115
101
|
}
|
|
116
102
|
}
|
|
117
103
|
},
|
|
118
|
-
{
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
attributes: ["src", "srcset"],
|
|
124
|
-
callback() {
|
|
125
|
-
send({ type: "SRC.CHANGE" });
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
trackImageRemoval(ctx2, _evt, { send }) {
|
|
130
|
-
const rootEl = dom.getRootEl(ctx2);
|
|
131
|
-
return domQuery.observeChildren(rootEl, {
|
|
132
|
-
callback(records) {
|
|
133
|
-
const removedNodes = Array.from(records[0].removedNodes);
|
|
134
|
-
const removed = removedNodes.find(
|
|
135
|
-
(node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
|
|
136
|
-
);
|
|
137
|
-
if (removed) {
|
|
138
|
-
send({ type: "IMG.UNMOUNT" });
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
});
|
|
104
|
+
loaded: {
|
|
105
|
+
on: {
|
|
106
|
+
"img.error": {
|
|
107
|
+
target: "error",
|
|
108
|
+
actions: ["invokeOnError"]
|
|
142
109
|
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
implementations: {
|
|
114
|
+
actions: {
|
|
115
|
+
invokeOnLoad({ prop }) {
|
|
116
|
+
prop("onStatusChange")?.({ status: "loaded" });
|
|
143
117
|
},
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
118
|
+
invokeOnError({ prop }) {
|
|
119
|
+
prop("onStatusChange")?.({ status: "error" });
|
|
120
|
+
},
|
|
121
|
+
checkImageStatus({ send, scope }) {
|
|
122
|
+
const imageEl = getImageEl(scope);
|
|
123
|
+
if (!imageEl?.complete) return;
|
|
124
|
+
const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
|
|
125
|
+
send({ type, src: "ssr" });
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
effects: {
|
|
129
|
+
trackImageRemoval({ send, scope }) {
|
|
130
|
+
const rootEl = getRootEl(scope);
|
|
131
|
+
return domQuery.observeChildren(rootEl, {
|
|
132
|
+
callback(records) {
|
|
133
|
+
const removedNodes = Array.from(records[0].removedNodes);
|
|
134
|
+
const removed = removedNodes.find(
|
|
135
|
+
(node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
|
|
136
|
+
);
|
|
137
|
+
if (removed) {
|
|
138
|
+
send({ type: "img.unmount" });
|
|
139
|
+
}
|
|
156
140
|
}
|
|
157
|
-
}
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
trackSrcChange({ send, scope }) {
|
|
144
|
+
const imageEl = getImageEl(scope);
|
|
145
|
+
return domQuery.observeAttributes(imageEl, {
|
|
146
|
+
attributes: ["src", "srcset"],
|
|
147
|
+
callback() {
|
|
148
|
+
send({ type: "src.change" });
|
|
149
|
+
}
|
|
150
|
+
});
|
|
158
151
|
}
|
|
159
152
|
}
|
|
160
|
-
|
|
161
|
-
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
162
155
|
function hasLoaded(image) {
|
|
163
156
|
return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
|
|
164
157
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,162 +1,155 @@
|
|
|
1
1
|
import { createAnatomy } from '@zag-js/anatomy';
|
|
2
|
-
import {
|
|
2
|
+
import { observeChildren, observeAttributes } from '@zag-js/dom-query';
|
|
3
3
|
import { createMachine } from '@zag-js/core';
|
|
4
|
-
import { createSplitProps, compact } from '@zag-js/utils';
|
|
5
4
|
import { createProps } from '@zag-js/types';
|
|
5
|
+
import { createSplitProps } from '@zag-js/utils';
|
|
6
6
|
|
|
7
7
|
// src/avatar.anatomy.ts
|
|
8
8
|
var anatomy = createAnatomy("avatar").parts("root", "image", "fallback");
|
|
9
9
|
var parts = anatomy.build();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
|
|
11
|
+
// src/avatar.dom.ts
|
|
12
|
+
var getRootId = (ctx) => ctx.ids?.root ?? `avatar:${ctx.id}`;
|
|
13
|
+
var getImageId = (ctx) => ctx.ids?.image ?? `avatar:${ctx.id}:image`;
|
|
14
|
+
var getFallbackId = (ctx) => ctx.ids?.fallback ?? `avatar:${ctx.id}:fallback`;
|
|
15
|
+
var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
|
|
16
|
+
var getImageEl = (ctx) => ctx.getById(getImageId(ctx));
|
|
17
17
|
|
|
18
18
|
// src/avatar.connect.ts
|
|
19
|
-
function connect(
|
|
19
|
+
function connect(service, normalize) {
|
|
20
|
+
const { state, send, prop, scope } = service;
|
|
20
21
|
const loaded = state.matches("loaded");
|
|
21
22
|
return {
|
|
22
23
|
loaded,
|
|
23
24
|
setSrc(src) {
|
|
24
|
-
|
|
25
|
+
const img = getImageEl(scope);
|
|
26
|
+
img?.setAttribute("src", src);
|
|
25
27
|
},
|
|
26
28
|
setLoaded() {
|
|
27
|
-
send({ type: "
|
|
29
|
+
send({ type: "img.loaded", src: "api" });
|
|
28
30
|
},
|
|
29
31
|
setError() {
|
|
30
|
-
send({ type: "
|
|
32
|
+
send({ type: "img.error", src: "api" });
|
|
31
33
|
},
|
|
32
34
|
getRootProps() {
|
|
33
35
|
return normalize.element({
|
|
34
36
|
...parts.root.attrs,
|
|
35
|
-
dir:
|
|
36
|
-
id:
|
|
37
|
+
dir: prop("dir"),
|
|
38
|
+
id: getRootId(scope)
|
|
37
39
|
});
|
|
38
40
|
},
|
|
39
41
|
getImageProps() {
|
|
40
42
|
return normalize.img({
|
|
41
43
|
...parts.image.attrs,
|
|
42
44
|
hidden: !loaded,
|
|
43
|
-
dir:
|
|
44
|
-
id:
|
|
45
|
+
dir: prop("dir"),
|
|
46
|
+
id: getImageId(scope),
|
|
45
47
|
"data-state": loaded ? "visible" : "hidden",
|
|
46
48
|
onLoad() {
|
|
47
|
-
send({ type: "
|
|
49
|
+
send({ type: "img.loaded", src: "element" });
|
|
48
50
|
},
|
|
49
51
|
onError() {
|
|
50
|
-
send({ type: "
|
|
52
|
+
send({ type: "img.error", src: "element" });
|
|
51
53
|
}
|
|
52
54
|
});
|
|
53
55
|
},
|
|
54
56
|
getFallbackProps() {
|
|
55
57
|
return normalize.element({
|
|
56
58
|
...parts.fallback.attrs,
|
|
57
|
-
dir:
|
|
58
|
-
id:
|
|
59
|
+
dir: prop("dir"),
|
|
60
|
+
id: getFallbackId(scope),
|
|
59
61
|
hidden: loaded,
|
|
60
62
|
"data-state": loaded ? "hidden" : "visible"
|
|
61
63
|
});
|
|
62
64
|
}
|
|
63
65
|
};
|
|
64
66
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
var machine = createMachine({
|
|
68
|
+
initialState() {
|
|
69
|
+
return "loading";
|
|
70
|
+
},
|
|
71
|
+
effects: ["trackImageRemoval", "trackSrcChange"],
|
|
72
|
+
on: {
|
|
73
|
+
"src.change": {
|
|
74
|
+
target: "loading"
|
|
75
|
+
},
|
|
76
|
+
"img.unmount": {
|
|
77
|
+
target: "error"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
states: {
|
|
81
|
+
loading: {
|
|
82
|
+
entry: ["checkImageStatus"],
|
|
73
83
|
on: {
|
|
74
|
-
"
|
|
75
|
-
target: "
|
|
84
|
+
"img.loaded": {
|
|
85
|
+
target: "loaded",
|
|
86
|
+
actions: ["invokeOnLoad"]
|
|
76
87
|
},
|
|
77
|
-
"
|
|
78
|
-
target: "error"
|
|
88
|
+
"img.error": {
|
|
89
|
+
target: "error",
|
|
90
|
+
actions: ["invokeOnError"]
|
|
79
91
|
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
target: "loaded",
|
|
88
|
-
actions: ["invokeOnLoad"]
|
|
89
|
-
},
|
|
90
|
-
"IMG.ERROR": {
|
|
91
|
-
target: "error",
|
|
92
|
-
actions: ["invokeOnError"]
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
error: {
|
|
97
|
-
activities: ["trackSrcChange"],
|
|
98
|
-
on: {
|
|
99
|
-
"IMG.LOADED": {
|
|
100
|
-
target: "loaded",
|
|
101
|
-
actions: ["invokeOnLoad"]
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
loaded: {
|
|
106
|
-
activities: ["trackSrcChange"],
|
|
107
|
-
on: {
|
|
108
|
-
"IMG.ERROR": {
|
|
109
|
-
target: "error",
|
|
110
|
-
actions: ["invokeOnError"]
|
|
111
|
-
}
|
|
112
|
-
}
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
error: {
|
|
95
|
+
on: {
|
|
96
|
+
"img.loaded": {
|
|
97
|
+
target: "loaded",
|
|
98
|
+
actions: ["invokeOnLoad"]
|
|
113
99
|
}
|
|
114
100
|
}
|
|
115
101
|
},
|
|
116
|
-
{
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
attributes: ["src", "srcset"],
|
|
122
|
-
callback() {
|
|
123
|
-
send({ type: "SRC.CHANGE" });
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
},
|
|
127
|
-
trackImageRemoval(ctx2, _evt, { send }) {
|
|
128
|
-
const rootEl = dom.getRootEl(ctx2);
|
|
129
|
-
return observeChildren(rootEl, {
|
|
130
|
-
callback(records) {
|
|
131
|
-
const removedNodes = Array.from(records[0].removedNodes);
|
|
132
|
-
const removed = removedNodes.find(
|
|
133
|
-
(node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
|
|
134
|
-
);
|
|
135
|
-
if (removed) {
|
|
136
|
-
send({ type: "IMG.UNMOUNT" });
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
});
|
|
102
|
+
loaded: {
|
|
103
|
+
on: {
|
|
104
|
+
"img.error": {
|
|
105
|
+
target: "error",
|
|
106
|
+
actions: ["invokeOnError"]
|
|
140
107
|
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
implementations: {
|
|
112
|
+
actions: {
|
|
113
|
+
invokeOnLoad({ prop }) {
|
|
114
|
+
prop("onStatusChange")?.({ status: "loaded" });
|
|
141
115
|
},
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
116
|
+
invokeOnError({ prop }) {
|
|
117
|
+
prop("onStatusChange")?.({ status: "error" });
|
|
118
|
+
},
|
|
119
|
+
checkImageStatus({ send, scope }) {
|
|
120
|
+
const imageEl = getImageEl(scope);
|
|
121
|
+
if (!imageEl?.complete) return;
|
|
122
|
+
const type = hasLoaded(imageEl) ? "img.loaded" : "img.error";
|
|
123
|
+
send({ type, src: "ssr" });
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
effects: {
|
|
127
|
+
trackImageRemoval({ send, scope }) {
|
|
128
|
+
const rootEl = getRootEl(scope);
|
|
129
|
+
return observeChildren(rootEl, {
|
|
130
|
+
callback(records) {
|
|
131
|
+
const removedNodes = Array.from(records[0].removedNodes);
|
|
132
|
+
const removed = removedNodes.find(
|
|
133
|
+
(node) => node.nodeType === Node.ELEMENT_NODE && node.matches("[data-scope=avatar][data-part=image]")
|
|
134
|
+
);
|
|
135
|
+
if (removed) {
|
|
136
|
+
send({ type: "img.unmount" });
|
|
137
|
+
}
|
|
154
138
|
}
|
|
155
|
-
}
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
trackSrcChange({ send, scope }) {
|
|
142
|
+
const imageEl = getImageEl(scope);
|
|
143
|
+
return observeAttributes(imageEl, {
|
|
144
|
+
attributes: ["src", "srcset"],
|
|
145
|
+
callback() {
|
|
146
|
+
send({ type: "src.change" });
|
|
147
|
+
}
|
|
148
|
+
});
|
|
156
149
|
}
|
|
157
150
|
}
|
|
158
|
-
|
|
159
|
-
}
|
|
151
|
+
}
|
|
152
|
+
});
|
|
160
153
|
function hasLoaded(image) {
|
|
161
154
|
return image.complete && image.naturalWidth !== 0 && image.naturalHeight !== 0;
|
|
162
155
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/avatar",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Core logic for the avatar widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@zag-js/anatomy": "0.
|
|
31
|
-
"@zag-js/core": "0.
|
|
32
|
-
"@zag-js/dom-query": "0.
|
|
33
|
-
"@zag-js/utils": "0.
|
|
34
|
-
"@zag-js/types": "0.
|
|
30
|
+
"@zag-js/anatomy": "1.0.0",
|
|
31
|
+
"@zag-js/core": "1.0.0",
|
|
32
|
+
"@zag-js/dom-query": "1.0.0",
|
|
33
|
+
"@zag-js/utils": "1.0.0",
|
|
34
|
+
"@zag-js/types": "1.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"clean-package": "2.2.0"
|
|
@@ -41,9 +41,14 @@
|
|
|
41
41
|
"types": "dist/index.d.ts",
|
|
42
42
|
"exports": {
|
|
43
43
|
".": {
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
"import": {
|
|
45
|
+
"types": "./dist/index.d.mts",
|
|
46
|
+
"default": "./dist/index.mjs"
|
|
47
|
+
},
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./dist/index.d.ts",
|
|
50
|
+
"default": "./dist/index.js"
|
|
51
|
+
}
|
|
47
52
|
},
|
|
48
53
|
"./package.json": "./package.json"
|
|
49
54
|
},
|