@zag-js/avatar 0.0.0-dev-20230518155940

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Chakra UI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @zag-js/avatar
2
+
3
+ Core logic for the avatar widget implemented as a state machine
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @zag-js/avatar
9
+ # or
10
+ npm i @zag-js/avatar
11
+ ```
12
+
13
+ ## Contribution
14
+
15
+ Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
16
+
17
+ ## Licence
18
+
19
+ This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
@@ -0,0 +1,6 @@
1
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
2
+
3
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "image" | "fallback">;
4
+ declare const parts: Record<"root" | "image" | "fallback", _zag_js_anatomy.AnatomyPart>;
5
+
6
+ export { anatomy, parts };
@@ -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,8 @@
1
+ import {
2
+ anatomy,
3
+ parts
4
+ } from "./chunk-LM2Q4YBK.mjs";
5
+ export {
6
+ anatomy,
7
+ parts
8
+ };
@@ -0,0 +1,31 @@
1
+ import { PropTypes, NormalizeProps } from '@zag-js/types';
2
+ import { State, Send } from './avatar.types.js';
3
+ import '@zag-js/core';
4
+
5
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
6
+ /**
7
+ * Whether the image is loaded.
8
+ */
9
+ isLoaded: boolean;
10
+ /**
11
+ * Whether the fallback is shown.
12
+ */
13
+ showFallback: boolean;
14
+ /**
15
+ * Function to set new src.
16
+ */
17
+ setSrc(src: string): void;
18
+ /**
19
+ * Function to set loaded state.
20
+ */
21
+ setLoaded(): void;
22
+ /**
23
+ * Function to set error state.
24
+ */
25
+ setError(): void;
26
+ rootProps: T["element"];
27
+ imageProps: T["img"];
28
+ fallbackProps: T["element"];
29
+ };
30
+
31
+ export { connect };
@@ -0,0 +1,111 @@
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.connect.ts
21
+ var avatar_connect_exports = {};
22
+ __export(avatar_connect_exports, {
23
+ connect: () => connect
24
+ });
25
+ module.exports = __toCommonJS(avatar_connect_exports);
26
+ var import_dom_query2 = require("@zag-js/dom-query");
27
+
28
+ // src/avatar.anatomy.ts
29
+ var import_anatomy = require("@zag-js/anatomy");
30
+ var anatomy = (0, import_anatomy.createAnatomy)("avatar").parts("root", "image", "fallback");
31
+ var parts = anatomy.build();
32
+
33
+ // src/avatar.dom.ts
34
+ var import_dom_query = require("@zag-js/dom-query");
35
+ var dom = (0, import_dom_query.createScope)({
36
+ getRootId: (ctx) => `avatar:${ctx.id}`,
37
+ getImageId: (ctx) => `avatar:${ctx.id}:image`,
38
+ getFallbackId: (ctx) => `avatar:${ctx.id}:fallback`,
39
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
40
+ getImageEl: (ctx) => dom.getById(ctx, dom.getImageId(ctx))
41
+ });
42
+
43
+ // src/avatar.connect.ts
44
+ function connect(state, send, normalize) {
45
+ const isLoaded = state.matches("loaded");
46
+ const showFallback = !isLoaded;
47
+ return {
48
+ /**
49
+ * Whether the image is loaded.
50
+ */
51
+ isLoaded,
52
+ /**
53
+ * Whether the fallback is shown.
54
+ */
55
+ showFallback,
56
+ /**
57
+ * Function to set new src.
58
+ */
59
+ setSrc(src) {
60
+ send({ type: "SRC.SET", src });
61
+ },
62
+ /**
63
+ * Function to set loaded state.
64
+ */
65
+ setLoaded() {
66
+ send({ type: "IMG.LOADED", src: "api" });
67
+ },
68
+ /**
69
+ * Function to set error state.
70
+ */
71
+ setError() {
72
+ send({ type: "IMG.ERROR", src: "api" });
73
+ },
74
+ rootProps: normalize.element({
75
+ ...parts.root.attrs,
76
+ id: dom.getRootId(state.context),
77
+ style: {
78
+ display: "grid",
79
+ gridTemplateRows: "1fr 1fr",
80
+ overflow: "hidden"
81
+ }
82
+ }),
83
+ imageProps: normalize.img({
84
+ ...parts.image.attrs,
85
+ id: dom.getImageId(state.context),
86
+ "data-loaded": (0, import_dom_query2.dataAttr)(isLoaded),
87
+ onLoad() {
88
+ send({ type: "IMG.LOADED", src: "element" });
89
+ },
90
+ onError() {
91
+ send({ type: "IMG.ERROR", src: "element" });
92
+ },
93
+ style: {
94
+ gridArea: "1 / 1 / 2 / 2",
95
+ visibility: !isLoaded ? "hidden" : void 0
96
+ }
97
+ }),
98
+ fallbackProps: normalize.element({
99
+ ...parts.fallback.attrs,
100
+ id: dom.getFallbackId(state.context),
101
+ hidden: !showFallback,
102
+ style: {
103
+ gridArea: "1 / 1 / 2 / 2"
104
+ }
105
+ })
106
+ };
107
+ }
108
+ // Annotate the CommonJS export names for ESM import in node:
109
+ 0 && (module.exports = {
110
+ connect
111
+ });
@@ -0,0 +1,8 @@
1
+ import {
2
+ connect
3
+ } from "./chunk-KGWU4KVZ.mjs";
4
+ import "./chunk-LM2Q4YBK.mjs";
5
+ import "./chunk-ORAGSSGY.mjs";
6
+ export {
7
+ connect
8
+ };
@@ -0,0 +1,32 @@
1
+ import { MachineContext } from './avatar.types.js';
2
+ import '@zag-js/core';
3
+ import '@zag-js/types';
4
+
5
+ declare const dom: {
6
+ getRootNode: (ctx: {
7
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
8
+ }) => Document | ShadowRoot;
9
+ getDoc: (ctx: {
10
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
11
+ }) => Document;
12
+ getWin: (ctx: {
13
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
14
+ }) => Window & typeof globalThis;
15
+ getActiveElement: (ctx: {
16
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
17
+ }) => HTMLElement | null;
18
+ getById: <T extends HTMLElement = HTMLElement>(ctx: {
19
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
20
+ }, id: string) => T | null;
21
+ queryById: <T_1 extends HTMLElement = HTMLElement>(ctx: {
22
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
23
+ }, id: string) => T_1;
24
+ } & {
25
+ getRootId: (ctx: MachineContext) => string;
26
+ getImageId: (ctx: MachineContext) => string;
27
+ getFallbackId: (ctx: MachineContext) => string;
28
+ getRootEl: (ctx: MachineContext) => HTMLElement | null;
29
+ getImageEl: (ctx: MachineContext) => HTMLImageElement | null;
30
+ };
31
+
32
+ export { dom };
@@ -0,0 +1,37 @@
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
+ dom: () => dom
24
+ });
25
+ module.exports = __toCommonJS(avatar_dom_exports);
26
+ var import_dom_query = require("@zag-js/dom-query");
27
+ var dom = (0, import_dom_query.createScope)({
28
+ getRootId: (ctx) => `avatar:${ctx.id}`,
29
+ getImageId: (ctx) => `avatar:${ctx.id}:image`,
30
+ getFallbackId: (ctx) => `avatar:${ctx.id}:fallback`,
31
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
32
+ getImageEl: (ctx) => dom.getById(ctx, dom.getImageId(ctx))
33
+ });
34
+ // Annotate the CommonJS export names for ESM import in node:
35
+ 0 && (module.exports = {
36
+ dom
37
+ });
@@ -0,0 +1,6 @@
1
+ import {
2
+ dom
3
+ } from "./chunk-ORAGSSGY.mjs";
4
+ export {
5
+ dom
6
+ };
@@ -0,0 +1,7 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { UserDefinedContext, MachineContext, MachineState } from './avatar.types.js';
3
+ import '@zag-js/types';
4
+
5
+ declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
6
+
7
+ export { machine };
@@ -0,0 +1,125 @@
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.machine.ts
21
+ var avatar_machine_exports = {};
22
+ __export(avatar_machine_exports, {
23
+ machine: () => machine
24
+ });
25
+ module.exports = __toCommonJS(avatar_machine_exports);
26
+ var import_core = require("@zag-js/core");
27
+ var import_utils = require("@zag-js/utils");
28
+ var import_mutation_observer = require("@zag-js/mutation-observer");
29
+
30
+ // src/avatar.dom.ts
31
+ var import_dom_query = require("@zag-js/dom-query");
32
+ var dom = (0, import_dom_query.createScope)({
33
+ getRootId: (ctx) => `avatar:${ctx.id}`,
34
+ getImageId: (ctx) => `avatar:${ctx.id}:image`,
35
+ getFallbackId: (ctx) => `avatar:${ctx.id}:fallback`,
36
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
37
+ getImageEl: (ctx) => dom.getById(ctx, dom.getImageId(ctx))
38
+ });
39
+
40
+ // src/avatar.machine.ts
41
+ function machine(userContext) {
42
+ const ctx = (0, import_utils.compact)(userContext);
43
+ return (0, import_core.createMachine)(
44
+ {
45
+ id: "avatar",
46
+ initial: "loading",
47
+ activities: ["trackImageRemoval"],
48
+ context: ctx,
49
+ on: {
50
+ "SRC.CHANGE": {
51
+ target: "loading"
52
+ },
53
+ "IMG.UNMOUNT": {
54
+ target: "error"
55
+ }
56
+ },
57
+ states: {
58
+ loading: {
59
+ activities: ["trackSrcChange"],
60
+ entry: ["checkImgStatus"],
61
+ on: {
62
+ "IMG.LOADED": {
63
+ target: "loaded",
64
+ actions: ["invokeOnLoad"]
65
+ },
66
+ "IMG.ERROR": {
67
+ target: "error",
68
+ actions: ["invokeOnError"]
69
+ }
70
+ }
71
+ },
72
+ error: {
73
+ activities: ["trackSrcChange"],
74
+ on: {
75
+ "IMG.LOADED": {
76
+ target: "loaded",
77
+ actions: ["invokeOnLoad"]
78
+ }
79
+ }
80
+ },
81
+ loaded: {
82
+ activities: ["trackSrcChange"]
83
+ }
84
+ }
85
+ },
86
+ {
87
+ activities: {
88
+ trackSrcChange(ctx2, _evt, { send }) {
89
+ const img = dom.getImageEl(ctx2);
90
+ return (0, import_mutation_observer.observeAttributes)(img, ["src", "srcset"], () => {
91
+ send({ type: "SRC.CHANGE" });
92
+ });
93
+ },
94
+ trackImageRemoval(ctx2, _evt, { send }) {
95
+ const rootEl = dom.getRootEl(ctx2);
96
+ return (0, import_mutation_observer.observeChildren)(rootEl, (records) => {
97
+ const removedNodes = Array.from(records[0].removedNodes);
98
+ const removed = removedNodes.find((node) => node.matches("[data-scope=avatar][data-part=image]"));
99
+ if (removed) {
100
+ send({ type: "IMG.UNMOUNT" });
101
+ }
102
+ });
103
+ }
104
+ },
105
+ actions: {
106
+ invokeOnLoad(ctx2) {
107
+ ctx2.onLoad?.();
108
+ },
109
+ invokeOnError(ctx2) {
110
+ ctx2.onError?.();
111
+ },
112
+ checkImgStatus(ctx2, _evt, { send }) {
113
+ const img = dom.getImageEl(ctx2);
114
+ if (img?.complete) {
115
+ send({ type: "IMG.LOADED", src: "ssr" });
116
+ }
117
+ }
118
+ }
119
+ }
120
+ );
121
+ }
122
+ // Annotate the CommonJS export names for ESM import in node:
123
+ 0 && (module.exports = {
124
+ machine
125
+ });
@@ -0,0 +1,7 @@
1
+ import {
2
+ machine
3
+ } from "./chunk-SKBVPI7R.mjs";
4
+ import "./chunk-ORAGSSGY.mjs";
5
+ export {
6
+ machine
7
+ };
@@ -0,0 +1,18 @@
1
+ import { StateMachine } from '@zag-js/core';
2
+ import { RequiredBy, CommonProperties, Context } from '@zag-js/types';
3
+
4
+ type PublicContext = CommonProperties & {
5
+ onLoad?: () => void;
6
+ onError?: () => void;
7
+ };
8
+ type PrivateContext = Context<{}>;
9
+ type ComputedContext = Readonly<{}>;
10
+ type UserDefinedContext = RequiredBy<PublicContext, "id">;
11
+ type MachineContext = PublicContext & PrivateContext & ComputedContext;
12
+ type MachineState = {
13
+ value: "loading" | "error" | "loaded";
14
+ };
15
+ type State = StateMachine.State<MachineContext, MachineState>;
16
+ type Send = StateMachine.Send<StateMachine.AnyEventObject>;
17
+
18
+ export { MachineContext, MachineState, Send, State, UserDefinedContext };
@@ -0,0 +1,18 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/avatar.types.ts
17
+ var avatar_types_exports = {};
18
+ module.exports = __toCommonJS(avatar_types_exports);
File without changes
@@ -0,0 +1,77 @@
1
+ import {
2
+ parts
3
+ } from "./chunk-LM2Q4YBK.mjs";
4
+ import {
5
+ dom
6
+ } from "./chunk-ORAGSSGY.mjs";
7
+
8
+ // src/avatar.connect.ts
9
+ import { dataAttr } from "@zag-js/dom-query";
10
+ function connect(state, send, normalize) {
11
+ const isLoaded = state.matches("loaded");
12
+ const showFallback = !isLoaded;
13
+ return {
14
+ /**
15
+ * Whether the image is loaded.
16
+ */
17
+ isLoaded,
18
+ /**
19
+ * Whether the fallback is shown.
20
+ */
21
+ showFallback,
22
+ /**
23
+ * Function to set new src.
24
+ */
25
+ setSrc(src) {
26
+ send({ type: "SRC.SET", src });
27
+ },
28
+ /**
29
+ * Function to set loaded state.
30
+ */
31
+ setLoaded() {
32
+ send({ type: "IMG.LOADED", src: "api" });
33
+ },
34
+ /**
35
+ * Function to set error state.
36
+ */
37
+ setError() {
38
+ send({ type: "IMG.ERROR", src: "api" });
39
+ },
40
+ rootProps: normalize.element({
41
+ ...parts.root.attrs,
42
+ id: dom.getRootId(state.context),
43
+ style: {
44
+ display: "grid",
45
+ gridTemplateRows: "1fr 1fr",
46
+ overflow: "hidden"
47
+ }
48
+ }),
49
+ imageProps: normalize.img({
50
+ ...parts.image.attrs,
51
+ id: dom.getImageId(state.context),
52
+ "data-loaded": dataAttr(isLoaded),
53
+ onLoad() {
54
+ send({ type: "IMG.LOADED", src: "element" });
55
+ },
56
+ onError() {
57
+ send({ type: "IMG.ERROR", src: "element" });
58
+ },
59
+ style: {
60
+ gridArea: "1 / 1 / 2 / 2",
61
+ visibility: !isLoaded ? "hidden" : void 0
62
+ }
63
+ }),
64
+ fallbackProps: normalize.element({
65
+ ...parts.fallback.attrs,
66
+ id: dom.getFallbackId(state.context),
67
+ hidden: !showFallback,
68
+ style: {
69
+ gridArea: "1 / 1 / 2 / 2"
70
+ }
71
+ })
72
+ };
73
+ }
74
+
75
+ export {
76
+ connect
77
+ };
@@ -0,0 +1,9 @@
1
+ // src/avatar.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("avatar").parts("root", "image", "fallback");
4
+ var parts = anatomy.build();
5
+
6
+ export {
7
+ anatomy,
8
+ parts
9
+ };
@@ -0,0 +1,13 @@
1
+ // src/avatar.dom.ts
2
+ import { createScope } from "@zag-js/dom-query";
3
+ var dom = createScope({
4
+ getRootId: (ctx) => `avatar:${ctx.id}`,
5
+ getImageId: (ctx) => `avatar:${ctx.id}:image`,
6
+ getFallbackId: (ctx) => `avatar:${ctx.id}:fallback`,
7
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
8
+ getImageEl: (ctx) => dom.getById(ctx, dom.getImageId(ctx))
9
+ });
10
+
11
+ export {
12
+ dom
13
+ };
@@ -0,0 +1,93 @@
1
+ import {
2
+ dom
3
+ } from "./chunk-ORAGSSGY.mjs";
4
+
5
+ // src/avatar.machine.ts
6
+ import { createMachine } from "@zag-js/core";
7
+ import { compact } from "@zag-js/utils";
8
+ import { observeAttributes, observeChildren } from "@zag-js/mutation-observer";
9
+ function machine(userContext) {
10
+ const ctx = compact(userContext);
11
+ return createMachine(
12
+ {
13
+ id: "avatar",
14
+ initial: "loading",
15
+ activities: ["trackImageRemoval"],
16
+ context: ctx,
17
+ on: {
18
+ "SRC.CHANGE": {
19
+ target: "loading"
20
+ },
21
+ "IMG.UNMOUNT": {
22
+ target: "error"
23
+ }
24
+ },
25
+ states: {
26
+ loading: {
27
+ activities: ["trackSrcChange"],
28
+ entry: ["checkImgStatus"],
29
+ on: {
30
+ "IMG.LOADED": {
31
+ target: "loaded",
32
+ actions: ["invokeOnLoad"]
33
+ },
34
+ "IMG.ERROR": {
35
+ target: "error",
36
+ actions: ["invokeOnError"]
37
+ }
38
+ }
39
+ },
40
+ error: {
41
+ activities: ["trackSrcChange"],
42
+ on: {
43
+ "IMG.LOADED": {
44
+ target: "loaded",
45
+ actions: ["invokeOnLoad"]
46
+ }
47
+ }
48
+ },
49
+ loaded: {
50
+ activities: ["trackSrcChange"]
51
+ }
52
+ }
53
+ },
54
+ {
55
+ activities: {
56
+ trackSrcChange(ctx2, _evt, { send }) {
57
+ const img = dom.getImageEl(ctx2);
58
+ return observeAttributes(img, ["src", "srcset"], () => {
59
+ send({ type: "SRC.CHANGE" });
60
+ });
61
+ },
62
+ trackImageRemoval(ctx2, _evt, { send }) {
63
+ const rootEl = dom.getRootEl(ctx2);
64
+ return observeChildren(rootEl, (records) => {
65
+ const removedNodes = Array.from(records[0].removedNodes);
66
+ const removed = removedNodes.find((node) => node.matches("[data-scope=avatar][data-part=image]"));
67
+ if (removed) {
68
+ send({ type: "IMG.UNMOUNT" });
69
+ }
70
+ });
71
+ }
72
+ },
73
+ actions: {
74
+ invokeOnLoad(ctx2) {
75
+ ctx2.onLoad?.();
76
+ },
77
+ invokeOnError(ctx2) {
78
+ ctx2.onError?.();
79
+ },
80
+ checkImgStatus(ctx2, _evt, { send }) {
81
+ const img = dom.getImageEl(ctx2);
82
+ if (img?.complete) {
83
+ send({ type: "IMG.LOADED", src: "ssr" });
84
+ }
85
+ }
86
+ }
87
+ }
88
+ );
89
+ }
90
+
91
+ export {
92
+ machine
93
+ };
@@ -0,0 +1,5 @@
1
+ export { connect } from './avatar.connect.js';
2
+ export { machine } from './avatar.machine.js';
3
+ export { UserDefinedContext as Context } from './avatar.types.js';
4
+ import '@zag-js/types';
5
+ import '@zag-js/core';
package/dist/index.js ADDED
@@ -0,0 +1,201 @@
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/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ connect: () => connect,
24
+ machine: () => machine
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/avatar.connect.ts
29
+ var import_dom_query2 = require("@zag-js/dom-query");
30
+
31
+ // src/avatar.anatomy.ts
32
+ var import_anatomy = require("@zag-js/anatomy");
33
+ var anatomy = (0, import_anatomy.createAnatomy)("avatar").parts("root", "image", "fallback");
34
+ var parts = anatomy.build();
35
+
36
+ // src/avatar.dom.ts
37
+ var import_dom_query = require("@zag-js/dom-query");
38
+ var dom = (0, import_dom_query.createScope)({
39
+ getRootId: (ctx) => `avatar:${ctx.id}`,
40
+ getImageId: (ctx) => `avatar:${ctx.id}:image`,
41
+ getFallbackId: (ctx) => `avatar:${ctx.id}:fallback`,
42
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
43
+ getImageEl: (ctx) => dom.getById(ctx, dom.getImageId(ctx))
44
+ });
45
+
46
+ // src/avatar.connect.ts
47
+ function connect(state, send, normalize) {
48
+ const isLoaded = state.matches("loaded");
49
+ const showFallback = !isLoaded;
50
+ return {
51
+ /**
52
+ * Whether the image is loaded.
53
+ */
54
+ isLoaded,
55
+ /**
56
+ * Whether the fallback is shown.
57
+ */
58
+ showFallback,
59
+ /**
60
+ * Function to set new src.
61
+ */
62
+ setSrc(src) {
63
+ send({ type: "SRC.SET", src });
64
+ },
65
+ /**
66
+ * Function to set loaded state.
67
+ */
68
+ setLoaded() {
69
+ send({ type: "IMG.LOADED", src: "api" });
70
+ },
71
+ /**
72
+ * Function to set error state.
73
+ */
74
+ setError() {
75
+ send({ type: "IMG.ERROR", src: "api" });
76
+ },
77
+ rootProps: normalize.element({
78
+ ...parts.root.attrs,
79
+ id: dom.getRootId(state.context),
80
+ style: {
81
+ display: "grid",
82
+ gridTemplateRows: "1fr 1fr",
83
+ overflow: "hidden"
84
+ }
85
+ }),
86
+ imageProps: normalize.img({
87
+ ...parts.image.attrs,
88
+ id: dom.getImageId(state.context),
89
+ "data-loaded": (0, import_dom_query2.dataAttr)(isLoaded),
90
+ onLoad() {
91
+ send({ type: "IMG.LOADED", src: "element" });
92
+ },
93
+ onError() {
94
+ send({ type: "IMG.ERROR", src: "element" });
95
+ },
96
+ style: {
97
+ gridArea: "1 / 1 / 2 / 2",
98
+ visibility: !isLoaded ? "hidden" : void 0
99
+ }
100
+ }),
101
+ fallbackProps: normalize.element({
102
+ ...parts.fallback.attrs,
103
+ id: dom.getFallbackId(state.context),
104
+ hidden: !showFallback,
105
+ style: {
106
+ gridArea: "1 / 1 / 2 / 2"
107
+ }
108
+ })
109
+ };
110
+ }
111
+
112
+ // src/avatar.machine.ts
113
+ var import_core = require("@zag-js/core");
114
+ var import_utils = require("@zag-js/utils");
115
+ var import_mutation_observer = require("@zag-js/mutation-observer");
116
+ function machine(userContext) {
117
+ const ctx = (0, import_utils.compact)(userContext);
118
+ return (0, import_core.createMachine)(
119
+ {
120
+ id: "avatar",
121
+ initial: "loading",
122
+ activities: ["trackImageRemoval"],
123
+ context: ctx,
124
+ on: {
125
+ "SRC.CHANGE": {
126
+ target: "loading"
127
+ },
128
+ "IMG.UNMOUNT": {
129
+ target: "error"
130
+ }
131
+ },
132
+ states: {
133
+ loading: {
134
+ activities: ["trackSrcChange"],
135
+ entry: ["checkImgStatus"],
136
+ on: {
137
+ "IMG.LOADED": {
138
+ target: "loaded",
139
+ actions: ["invokeOnLoad"]
140
+ },
141
+ "IMG.ERROR": {
142
+ target: "error",
143
+ actions: ["invokeOnError"]
144
+ }
145
+ }
146
+ },
147
+ error: {
148
+ activities: ["trackSrcChange"],
149
+ on: {
150
+ "IMG.LOADED": {
151
+ target: "loaded",
152
+ actions: ["invokeOnLoad"]
153
+ }
154
+ }
155
+ },
156
+ loaded: {
157
+ activities: ["trackSrcChange"]
158
+ }
159
+ }
160
+ },
161
+ {
162
+ activities: {
163
+ trackSrcChange(ctx2, _evt, { send }) {
164
+ const img = dom.getImageEl(ctx2);
165
+ return (0, import_mutation_observer.observeAttributes)(img, ["src", "srcset"], () => {
166
+ send({ type: "SRC.CHANGE" });
167
+ });
168
+ },
169
+ trackImageRemoval(ctx2, _evt, { send }) {
170
+ const rootEl = dom.getRootEl(ctx2);
171
+ return (0, import_mutation_observer.observeChildren)(rootEl, (records) => {
172
+ const removedNodes = Array.from(records[0].removedNodes);
173
+ const removed = removedNodes.find((node) => node.matches("[data-scope=avatar][data-part=image]"));
174
+ if (removed) {
175
+ send({ type: "IMG.UNMOUNT" });
176
+ }
177
+ });
178
+ }
179
+ },
180
+ actions: {
181
+ invokeOnLoad(ctx2) {
182
+ ctx2.onLoad?.();
183
+ },
184
+ invokeOnError(ctx2) {
185
+ ctx2.onError?.();
186
+ },
187
+ checkImgStatus(ctx2, _evt, { send }) {
188
+ const img = dom.getImageEl(ctx2);
189
+ if (img?.complete) {
190
+ send({ type: "IMG.LOADED", src: "ssr" });
191
+ }
192
+ }
193
+ }
194
+ }
195
+ );
196
+ }
197
+ // Annotate the CommonJS export names for ESM import in node:
198
+ 0 && (module.exports = {
199
+ connect,
200
+ machine
201
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,12 @@
1
+ import {
2
+ connect
3
+ } from "./chunk-KGWU4KVZ.mjs";
4
+ import "./chunk-LM2Q4YBK.mjs";
5
+ import {
6
+ machine
7
+ } from "./chunk-SKBVPI7R.mjs";
8
+ import "./chunk-ORAGSSGY.mjs";
9
+ export {
10
+ connect,
11
+ machine
12
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@zag-js/avatar",
3
+ "version": "0.0.0-dev-20230518155940",
4
+ "description": "Core logic for the avatar widget implemented as a state machine",
5
+ "keywords": [
6
+ "js",
7
+ "machine",
8
+ "xstate",
9
+ "statechart",
10
+ "component",
11
+ "chakra-ui",
12
+ "avatar"
13
+ ],
14
+ "author": "Segun Adebayo <sage@adebayosegun.com>",
15
+ "homepage": "https://github.com/chakra-ui/zag#readme",
16
+ "license": "MIT",
17
+ "main": "dist/index.js",
18
+ "repository": "https://github.com/chakra-ui/zag/tree/main/packages/avatar",
19
+ "sideEffects": false,
20
+ "files": [
21
+ "dist/**/*"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/chakra-ui/zag/issues"
28
+ },
29
+ "dependencies": {
30
+ "@zag-js/anatomy": "0.0.0-dev-20230518155940",
31
+ "@zag-js/mutation-observer": "0.0.0-dev-20230518155940",
32
+ "@zag-js/core": "0.0.0-dev-20230518155940",
33
+ "@zag-js/dom-query": "0.0.0-dev-20230518155940",
34
+ "@zag-js/utils": "0.0.0-dev-20230518155940",
35
+ "@zag-js/types": "0.0.0-dev-20230518155940"
36
+ },
37
+ "devDependencies": {
38
+ "clean-package": "2.2.0"
39
+ },
40
+ "clean-package": "../../../clean-package.config.json",
41
+ "module": "dist/index.mjs",
42
+ "types": "dist/index.d.ts",
43
+ "exports": {
44
+ ".": {
45
+ "types": "./dist/index.d.ts",
46
+ "import": "./dist/index.mjs",
47
+ "require": "./dist/index.js"
48
+ },
49
+ "./package.json": "./package.json"
50
+ },
51
+ "scripts": {
52
+ "build-fast": "tsup src",
53
+ "start": "pnpm build --watch",
54
+ "build": "tsup src --dts",
55
+ "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
56
+ "lint": "eslint src --ext .ts,.tsx",
57
+ "test-ci": "pnpm test --ci --runInBand",
58
+ "test-watch": "pnpm test --watch -u",
59
+ "typecheck": "tsc --noEmit"
60
+ }
61
+ }