@zag-js/toolbar 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.
- package/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +39 -0
- package/dist/index.mjs +10 -0
- package/dist/toolbar.anatomy.d.mts +6 -0
- package/dist/toolbar.anatomy.d.ts +6 -0
- package/dist/toolbar.anatomy.js +34 -0
- package/dist/toolbar.anatomy.mjs +8 -0
- package/dist/toolbar.connect.d.mts +7 -0
- package/dist/toolbar.connect.d.ts +7 -0
- package/dist/toolbar.connect.js +229 -0
- package/dist/toolbar.connect.mjs +202 -0
- package/dist/toolbar.dom.d.mts +14 -0
- package/dist/toolbar.dom.d.ts +14 -0
- package/dist/toolbar.dom.js +76 -0
- package/dist/toolbar.dom.mjs +42 -0
- package/dist/toolbar.machine.d.mts +7 -0
- package/dist/toolbar.machine.d.ts +7 -0
- package/dist/toolbar.machine.js +190 -0
- package/dist/toolbar.machine.mjs +155 -0
- package/dist/toolbar.props.d.mts +10 -0
- package/dist/toolbar.props.d.ts +10 -0
- package/dist/toolbar.props.js +49 -0
- package/dist/toolbar.props.mjs +21 -0
- package/dist/toolbar.types.d.mts +148 -0
- package/dist/toolbar.types.d.ts +148 -0
- package/dist/toolbar.types.js +18 -0
- package/dist/toolbar.types.mjs +0 -0
- package/package.json +73 -0
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/toolbar
|
|
2
|
+
|
|
3
|
+
Core logic for the toolbar widget implemented as a state machine
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn add @zag-js/toolbar
|
|
9
|
+
# or
|
|
10
|
+
npm i @zag-js/toolbar
|
|
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).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { anatomy } from './toolbar.anatomy.mjs';
|
|
2
|
+
export { connect } from './toolbar.connect.mjs';
|
|
3
|
+
export { machine } from './toolbar.machine.mjs';
|
|
4
|
+
export { itemProps, props, splitItemProps, splitProps } from './toolbar.props.mjs';
|
|
5
|
+
export { ToolbarApi as Api, ElementIds, GroupProps, ItemProps, ItemState, LinkProps, ToolbarMachine as Machine, ToolbarProps as Props, ToolbarService as Service } from './toolbar.types.mjs';
|
|
6
|
+
export { Orientation } from '@zag-js/types';
|
|
7
|
+
import '@zag-js/anatomy';
|
|
8
|
+
import '@zag-js/core';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { anatomy } from './toolbar.anatomy.js';
|
|
2
|
+
export { connect } from './toolbar.connect.js';
|
|
3
|
+
export { machine } from './toolbar.machine.js';
|
|
4
|
+
export { itemProps, props, splitItemProps, splitProps } from './toolbar.props.js';
|
|
5
|
+
export { ToolbarApi as Api, ElementIds, GroupProps, ItemProps, ItemState, LinkProps, ToolbarMachine as Machine, ToolbarProps as Props, ToolbarService as Service } from './toolbar.types.js';
|
|
6
|
+
export { Orientation } from '@zag-js/types';
|
|
7
|
+
import '@zag-js/anatomy';
|
|
8
|
+
import '@zag-js/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
anatomy: () => import_toolbar.anatomy,
|
|
25
|
+
connect: () => import_toolbar2.connect,
|
|
26
|
+
machine: () => import_toolbar3.machine
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
var import_toolbar = require("./toolbar.anatomy.js");
|
|
30
|
+
var import_toolbar2 = require("./toolbar.connect.js");
|
|
31
|
+
var import_toolbar3 = require("./toolbar.machine.js");
|
|
32
|
+
__reExport(index_exports, require("./toolbar.props.js"), module.exports);
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
anatomy,
|
|
36
|
+
connect,
|
|
37
|
+
machine,
|
|
38
|
+
...require("./toolbar.props.js")
|
|
39
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
+
|
|
3
|
+
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "group" | "separator" | "item">;
|
|
4
|
+
declare const parts: Record<"root" | "group" | "separator" | "item", _zag_js_anatomy.AnatomyPart>;
|
|
5
|
+
|
|
6
|
+
export { anatomy, parts };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
+
|
|
3
|
+
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "group" | "separator" | "item">;
|
|
4
|
+
declare const parts: Record<"root" | "group" | "separator" | "item", _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/toolbar.anatomy.ts
|
|
21
|
+
var toolbar_anatomy_exports = {};
|
|
22
|
+
__export(toolbar_anatomy_exports, {
|
|
23
|
+
anatomy: () => anatomy,
|
|
24
|
+
parts: () => parts
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(toolbar_anatomy_exports);
|
|
27
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
28
|
+
var anatomy = (0, import_anatomy.createAnatomy)("toolbar").parts("root", "group", "separator", "item");
|
|
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 { ToolbarService, ToolbarApi } from './toolbar.types.mjs';
|
|
3
|
+
import '@zag-js/core';
|
|
4
|
+
|
|
5
|
+
declare function connect<T extends PropTypes>(service: ToolbarService, normalize: NormalizeProps<T>): ToolbarApi<T>;
|
|
6
|
+
|
|
7
|
+
export { connect };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PropTypes, NormalizeProps } from '@zag-js/types';
|
|
2
|
+
import { ToolbarService, ToolbarApi } from './toolbar.types.js';
|
|
3
|
+
import '@zag-js/core';
|
|
4
|
+
|
|
5
|
+
declare function connect<T extends PropTypes>(service: ToolbarService, normalize: NormalizeProps<T>): ToolbarApi<T>;
|
|
6
|
+
|
|
7
|
+
export { connect };
|
|
@@ -0,0 +1,229 @@
|
|
|
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/toolbar.connect.ts
|
|
31
|
+
var toolbar_connect_exports = {};
|
|
32
|
+
__export(toolbar_connect_exports, {
|
|
33
|
+
connect: () => connect
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(toolbar_connect_exports);
|
|
36
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
37
|
+
var import_toolbar = require("./toolbar.anatomy.js");
|
|
38
|
+
var dom = __toESM(require("./toolbar.dom.js"));
|
|
39
|
+
function connect(service, normalize) {
|
|
40
|
+
const { context, send, prop, scope } = service;
|
|
41
|
+
const orientation = prop("orientation");
|
|
42
|
+
const isHorizontal = orientation === "horizontal";
|
|
43
|
+
const rootDisabled = !!prop("disabled");
|
|
44
|
+
function getItemState(props) {
|
|
45
|
+
return {
|
|
46
|
+
id: dom.getItemId(scope, props.value),
|
|
47
|
+
disabled: Boolean(props.disabled || rootDisabled),
|
|
48
|
+
focusableWhenDisabled: props.focusableWhenDisabled ?? true,
|
|
49
|
+
focused: context.get("focusedValue") === props.value,
|
|
50
|
+
orientation
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function getNavKeyMap() {
|
|
54
|
+
const map = {
|
|
55
|
+
Home() {
|
|
56
|
+
send({ type: "ITEM.FOCUS_FIRST" });
|
|
57
|
+
},
|
|
58
|
+
End() {
|
|
59
|
+
send({ type: "ITEM.FOCUS_LAST" });
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
if (isHorizontal) {
|
|
63
|
+
map.ArrowRight = () => send({ type: "ITEM.FOCUS_NEXT" });
|
|
64
|
+
map.ArrowLeft = () => send({ type: "ITEM.FOCUS_PREV" });
|
|
65
|
+
} else {
|
|
66
|
+
map.ArrowDown = () => send({ type: "ITEM.FOCUS_NEXT" });
|
|
67
|
+
map.ArrowUp = () => send({ type: "ITEM.FOCUS_PREV" });
|
|
68
|
+
}
|
|
69
|
+
return map;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
disabled: rootDisabled,
|
|
73
|
+
focusedValue: context.get("focusedValue"),
|
|
74
|
+
orientation,
|
|
75
|
+
getItemId(value) {
|
|
76
|
+
return dom.getItemId(scope, value);
|
|
77
|
+
},
|
|
78
|
+
getItemState,
|
|
79
|
+
getRootProps() {
|
|
80
|
+
return normalize.element({
|
|
81
|
+
...import_toolbar.parts.root.attrs(scope.id),
|
|
82
|
+
id: dom.getRootId(scope),
|
|
83
|
+
role: "toolbar",
|
|
84
|
+
dir: prop("dir"),
|
|
85
|
+
"aria-orientation": orientation,
|
|
86
|
+
"data-orientation": orientation,
|
|
87
|
+
"aria-disabled": (0, import_dom_query.ariaAttr)(rootDisabled),
|
|
88
|
+
"data-disabled": (0, import_dom_query.dataAttr)(rootDisabled),
|
|
89
|
+
tabIndex: context.get("hasInteracted") ? -1 : 0,
|
|
90
|
+
style: { outline: "none" },
|
|
91
|
+
onMouseDown() {
|
|
92
|
+
if (rootDisabled) return;
|
|
93
|
+
send({ type: "ROOT.MOUSE_DOWN" });
|
|
94
|
+
},
|
|
95
|
+
onFocus(event) {
|
|
96
|
+
if (rootDisabled) return;
|
|
97
|
+
const target = (0, import_dom_query.getEventTarget)(event);
|
|
98
|
+
if (event.currentTarget !== target) {
|
|
99
|
+
if ((0, import_dom_query.contains)(event.currentTarget, target)) send({ type: "DESCENDANT.FOCUS" });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (context.get("isClickFocus")) return;
|
|
103
|
+
send({ type: "ROOT.FOCUS" });
|
|
104
|
+
},
|
|
105
|
+
onBlur(event) {
|
|
106
|
+
const relatedTarget = event.relatedTarget;
|
|
107
|
+
if ((0, import_dom_query.contains)(event.currentTarget, relatedTarget)) return;
|
|
108
|
+
if (rootDisabled) return;
|
|
109
|
+
send({ type: "ROOT.BLUR" });
|
|
110
|
+
},
|
|
111
|
+
onKeyDown(event) {
|
|
112
|
+
if (rootDisabled) return;
|
|
113
|
+
if (event.defaultPrevented) return;
|
|
114
|
+
const target = (0, import_dom_query.getEventTarget)(event);
|
|
115
|
+
if (!(0, import_dom_query.contains)(event.currentTarget, target)) return;
|
|
116
|
+
const keyMap = getNavKeyMap();
|
|
117
|
+
const exec = keyMap[(0, import_dom_query.getEventKey)(event, { dir: prop("dir"), orientation })];
|
|
118
|
+
if (exec) {
|
|
119
|
+
exec(event);
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
getGroupProps(props) {
|
|
126
|
+
return normalize.element({
|
|
127
|
+
...import_toolbar.parts.group.attrs(scope.id),
|
|
128
|
+
id: dom.getGroupId(scope, props.value),
|
|
129
|
+
role: "group",
|
|
130
|
+
dir: prop("dir"),
|
|
131
|
+
"aria-disabled": (0, import_dom_query.ariaAttr)(!!props.disabled),
|
|
132
|
+
"data-disabled": (0, import_dom_query.dataAttr)(!!props.disabled),
|
|
133
|
+
"data-orientation": orientation
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
getSeparatorProps() {
|
|
137
|
+
const separatorOrientation = isHorizontal ? "vertical" : "horizontal";
|
|
138
|
+
return normalize.element({
|
|
139
|
+
...import_toolbar.parts.separator.attrs(scope.id),
|
|
140
|
+
role: "separator",
|
|
141
|
+
dir: prop("dir"),
|
|
142
|
+
"aria-orientation": separatorOrientation,
|
|
143
|
+
"data-orientation": separatorOrientation
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
getItemProps(props) {
|
|
147
|
+
const itemState = getItemState(props);
|
|
148
|
+
const isNativelyDisabled = itemState.disabled && !itemState.focusableWhenDisabled;
|
|
149
|
+
return normalize.button({
|
|
150
|
+
...import_toolbar.parts.item.attrs(scope.id),
|
|
151
|
+
id: itemState.id,
|
|
152
|
+
type: "button",
|
|
153
|
+
dir: prop("dir"),
|
|
154
|
+
disabled: isNativelyDisabled || void 0,
|
|
155
|
+
"aria-disabled": (0, import_dom_query.ariaAttr)(itemState.disabled),
|
|
156
|
+
"data-disabled": (0, import_dom_query.dataAttr)(itemState.disabled),
|
|
157
|
+
"data-focusable-when-disabled": (0, import_dom_query.dataAttr)(itemState.disabled && itemState.focusableWhenDisabled),
|
|
158
|
+
"data-orientation": orientation,
|
|
159
|
+
tabIndex: isNativelyDisabled ? void 0 : itemState.focused ? 0 : -1,
|
|
160
|
+
onFocus() {
|
|
161
|
+
send({ type: "ITEM.FOCUS", value: props.value });
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
getLinkProps(props) {
|
|
166
|
+
const itemState = getItemState(props);
|
|
167
|
+
return normalize.element({
|
|
168
|
+
...import_toolbar.parts.item.attrs(scope.id),
|
|
169
|
+
id: itemState.id,
|
|
170
|
+
dir: prop("dir"),
|
|
171
|
+
"data-orientation": itemState.orientation,
|
|
172
|
+
tabIndex: itemState.focused ? 0 : -1,
|
|
173
|
+
onFocus() {
|
|
174
|
+
send({ type: "ITEM.FOCUS", value: props.value });
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
getInputProps(props) {
|
|
179
|
+
const itemState = getItemState(props);
|
|
180
|
+
const isNativelyDisabled = itemState.disabled && !itemState.focusableWhenDisabled;
|
|
181
|
+
return normalize.input({
|
|
182
|
+
...import_toolbar.parts.item.attrs(scope.id),
|
|
183
|
+
id: itemState.id,
|
|
184
|
+
dir: prop("dir"),
|
|
185
|
+
disabled: isNativelyDisabled || void 0,
|
|
186
|
+
readOnly: itemState.disabled && itemState.focusableWhenDisabled ? true : void 0,
|
|
187
|
+
"aria-disabled": (0, import_dom_query.ariaAttr)(itemState.disabled),
|
|
188
|
+
"data-disabled": (0, import_dom_query.dataAttr)(itemState.disabled),
|
|
189
|
+
"data-focusable-when-disabled": (0, import_dom_query.dataAttr)(itemState.disabled && itemState.focusableWhenDisabled),
|
|
190
|
+
"data-orientation": orientation,
|
|
191
|
+
tabIndex: isNativelyDisabled ? void 0 : itemState.focused ? 0 : -1,
|
|
192
|
+
onClick(event) {
|
|
193
|
+
if (itemState.disabled) event.preventDefault();
|
|
194
|
+
},
|
|
195
|
+
onPointerDown(event) {
|
|
196
|
+
if (itemState.disabled) event.preventDefault();
|
|
197
|
+
},
|
|
198
|
+
onFocus(event) {
|
|
199
|
+
send({ type: "ITEM.FOCUS", value: props.value });
|
|
200
|
+
event.currentTarget.select();
|
|
201
|
+
},
|
|
202
|
+
onKeyDown(event) {
|
|
203
|
+
const key = (0, import_dom_query.getEventKey)(event, { dir: prop("dir"), orientation });
|
|
204
|
+
if (key === "Home" || key === "End") {
|
|
205
|
+
event.stopPropagation();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const isForward = isHorizontal ? key === "ArrowRight" : key === "ArrowDown";
|
|
209
|
+
const isBackward = isHorizontal ? key === "ArrowLeft" : key === "ArrowUp";
|
|
210
|
+
if (!isForward && !isBackward) return;
|
|
211
|
+
if (event.defaultPrevented) {
|
|
212
|
+
event.stopPropagation();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const input = event.currentTarget;
|
|
216
|
+
const collapsed = input.selectionStart === input.selectionEnd;
|
|
217
|
+
const atBoundary = isForward ? (0, import_dom_query.isCaretAtEnd)(input) : (0, import_dom_query.isCaretAtStart)(input);
|
|
218
|
+
if (!collapsed || !atBoundary) {
|
|
219
|
+
event.stopPropagation();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
227
|
+
0 && (module.exports = {
|
|
228
|
+
connect
|
|
229
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// src/toolbar.connect.ts
|
|
2
|
+
import {
|
|
3
|
+
ariaAttr,
|
|
4
|
+
contains,
|
|
5
|
+
dataAttr,
|
|
6
|
+
getEventKey,
|
|
7
|
+
getEventTarget,
|
|
8
|
+
isCaretAtEnd,
|
|
9
|
+
isCaretAtStart
|
|
10
|
+
} from "@zag-js/dom-query";
|
|
11
|
+
import { parts } from "./toolbar.anatomy.mjs";
|
|
12
|
+
import * as dom from "./toolbar.dom.mjs";
|
|
13
|
+
function connect(service, normalize) {
|
|
14
|
+
const { context, send, prop, scope } = service;
|
|
15
|
+
const orientation = prop("orientation");
|
|
16
|
+
const isHorizontal = orientation === "horizontal";
|
|
17
|
+
const rootDisabled = !!prop("disabled");
|
|
18
|
+
function getItemState(props) {
|
|
19
|
+
return {
|
|
20
|
+
id: dom.getItemId(scope, props.value),
|
|
21
|
+
disabled: Boolean(props.disabled || rootDisabled),
|
|
22
|
+
focusableWhenDisabled: props.focusableWhenDisabled ?? true,
|
|
23
|
+
focused: context.get("focusedValue") === props.value,
|
|
24
|
+
orientation
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function getNavKeyMap() {
|
|
28
|
+
const map = {
|
|
29
|
+
Home() {
|
|
30
|
+
send({ type: "ITEM.FOCUS_FIRST" });
|
|
31
|
+
},
|
|
32
|
+
End() {
|
|
33
|
+
send({ type: "ITEM.FOCUS_LAST" });
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
if (isHorizontal) {
|
|
37
|
+
map.ArrowRight = () => send({ type: "ITEM.FOCUS_NEXT" });
|
|
38
|
+
map.ArrowLeft = () => send({ type: "ITEM.FOCUS_PREV" });
|
|
39
|
+
} else {
|
|
40
|
+
map.ArrowDown = () => send({ type: "ITEM.FOCUS_NEXT" });
|
|
41
|
+
map.ArrowUp = () => send({ type: "ITEM.FOCUS_PREV" });
|
|
42
|
+
}
|
|
43
|
+
return map;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
disabled: rootDisabled,
|
|
47
|
+
focusedValue: context.get("focusedValue"),
|
|
48
|
+
orientation,
|
|
49
|
+
getItemId(value) {
|
|
50
|
+
return dom.getItemId(scope, value);
|
|
51
|
+
},
|
|
52
|
+
getItemState,
|
|
53
|
+
getRootProps() {
|
|
54
|
+
return normalize.element({
|
|
55
|
+
...parts.root.attrs(scope.id),
|
|
56
|
+
id: dom.getRootId(scope),
|
|
57
|
+
role: "toolbar",
|
|
58
|
+
dir: prop("dir"),
|
|
59
|
+
"aria-orientation": orientation,
|
|
60
|
+
"data-orientation": orientation,
|
|
61
|
+
"aria-disabled": ariaAttr(rootDisabled),
|
|
62
|
+
"data-disabled": dataAttr(rootDisabled),
|
|
63
|
+
tabIndex: context.get("hasInteracted") ? -1 : 0,
|
|
64
|
+
style: { outline: "none" },
|
|
65
|
+
onMouseDown() {
|
|
66
|
+
if (rootDisabled) return;
|
|
67
|
+
send({ type: "ROOT.MOUSE_DOWN" });
|
|
68
|
+
},
|
|
69
|
+
onFocus(event) {
|
|
70
|
+
if (rootDisabled) return;
|
|
71
|
+
const target = getEventTarget(event);
|
|
72
|
+
if (event.currentTarget !== target) {
|
|
73
|
+
if (contains(event.currentTarget, target)) send({ type: "DESCENDANT.FOCUS" });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (context.get("isClickFocus")) return;
|
|
77
|
+
send({ type: "ROOT.FOCUS" });
|
|
78
|
+
},
|
|
79
|
+
onBlur(event) {
|
|
80
|
+
const relatedTarget = event.relatedTarget;
|
|
81
|
+
if (contains(event.currentTarget, relatedTarget)) return;
|
|
82
|
+
if (rootDisabled) return;
|
|
83
|
+
send({ type: "ROOT.BLUR" });
|
|
84
|
+
},
|
|
85
|
+
onKeyDown(event) {
|
|
86
|
+
if (rootDisabled) return;
|
|
87
|
+
if (event.defaultPrevented) return;
|
|
88
|
+
const target = getEventTarget(event);
|
|
89
|
+
if (!contains(event.currentTarget, target)) return;
|
|
90
|
+
const keyMap = getNavKeyMap();
|
|
91
|
+
const exec = keyMap[getEventKey(event, { dir: prop("dir"), orientation })];
|
|
92
|
+
if (exec) {
|
|
93
|
+
exec(event);
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
getGroupProps(props) {
|
|
100
|
+
return normalize.element({
|
|
101
|
+
...parts.group.attrs(scope.id),
|
|
102
|
+
id: dom.getGroupId(scope, props.value),
|
|
103
|
+
role: "group",
|
|
104
|
+
dir: prop("dir"),
|
|
105
|
+
"aria-disabled": ariaAttr(!!props.disabled),
|
|
106
|
+
"data-disabled": dataAttr(!!props.disabled),
|
|
107
|
+
"data-orientation": orientation
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
getSeparatorProps() {
|
|
111
|
+
const separatorOrientation = isHorizontal ? "vertical" : "horizontal";
|
|
112
|
+
return normalize.element({
|
|
113
|
+
...parts.separator.attrs(scope.id),
|
|
114
|
+
role: "separator",
|
|
115
|
+
dir: prop("dir"),
|
|
116
|
+
"aria-orientation": separatorOrientation,
|
|
117
|
+
"data-orientation": separatorOrientation
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
getItemProps(props) {
|
|
121
|
+
const itemState = getItemState(props);
|
|
122
|
+
const isNativelyDisabled = itemState.disabled && !itemState.focusableWhenDisabled;
|
|
123
|
+
return normalize.button({
|
|
124
|
+
...parts.item.attrs(scope.id),
|
|
125
|
+
id: itemState.id,
|
|
126
|
+
type: "button",
|
|
127
|
+
dir: prop("dir"),
|
|
128
|
+
disabled: isNativelyDisabled || void 0,
|
|
129
|
+
"aria-disabled": ariaAttr(itemState.disabled),
|
|
130
|
+
"data-disabled": dataAttr(itemState.disabled),
|
|
131
|
+
"data-focusable-when-disabled": dataAttr(itemState.disabled && itemState.focusableWhenDisabled),
|
|
132
|
+
"data-orientation": orientation,
|
|
133
|
+
tabIndex: isNativelyDisabled ? void 0 : itemState.focused ? 0 : -1,
|
|
134
|
+
onFocus() {
|
|
135
|
+
send({ type: "ITEM.FOCUS", value: props.value });
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
getLinkProps(props) {
|
|
140
|
+
const itemState = getItemState(props);
|
|
141
|
+
return normalize.element({
|
|
142
|
+
...parts.item.attrs(scope.id),
|
|
143
|
+
id: itemState.id,
|
|
144
|
+
dir: prop("dir"),
|
|
145
|
+
"data-orientation": itemState.orientation,
|
|
146
|
+
tabIndex: itemState.focused ? 0 : -1,
|
|
147
|
+
onFocus() {
|
|
148
|
+
send({ type: "ITEM.FOCUS", value: props.value });
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
getInputProps(props) {
|
|
153
|
+
const itemState = getItemState(props);
|
|
154
|
+
const isNativelyDisabled = itemState.disabled && !itemState.focusableWhenDisabled;
|
|
155
|
+
return normalize.input({
|
|
156
|
+
...parts.item.attrs(scope.id),
|
|
157
|
+
id: itemState.id,
|
|
158
|
+
dir: prop("dir"),
|
|
159
|
+
disabled: isNativelyDisabled || void 0,
|
|
160
|
+
readOnly: itemState.disabled && itemState.focusableWhenDisabled ? true : void 0,
|
|
161
|
+
"aria-disabled": ariaAttr(itemState.disabled),
|
|
162
|
+
"data-disabled": dataAttr(itemState.disabled),
|
|
163
|
+
"data-focusable-when-disabled": dataAttr(itemState.disabled && itemState.focusableWhenDisabled),
|
|
164
|
+
"data-orientation": orientation,
|
|
165
|
+
tabIndex: isNativelyDisabled ? void 0 : itemState.focused ? 0 : -1,
|
|
166
|
+
onClick(event) {
|
|
167
|
+
if (itemState.disabled) event.preventDefault();
|
|
168
|
+
},
|
|
169
|
+
onPointerDown(event) {
|
|
170
|
+
if (itemState.disabled) event.preventDefault();
|
|
171
|
+
},
|
|
172
|
+
onFocus(event) {
|
|
173
|
+
send({ type: "ITEM.FOCUS", value: props.value });
|
|
174
|
+
event.currentTarget.select();
|
|
175
|
+
},
|
|
176
|
+
onKeyDown(event) {
|
|
177
|
+
const key = getEventKey(event, { dir: prop("dir"), orientation });
|
|
178
|
+
if (key === "Home" || key === "End") {
|
|
179
|
+
event.stopPropagation();
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const isForward = isHorizontal ? key === "ArrowRight" : key === "ArrowDown";
|
|
183
|
+
const isBackward = isHorizontal ? key === "ArrowLeft" : key === "ArrowUp";
|
|
184
|
+
if (!isForward && !isBackward) return;
|
|
185
|
+
if (event.defaultPrevented) {
|
|
186
|
+
event.stopPropagation();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const input = event.currentTarget;
|
|
190
|
+
const collapsed = input.selectionStart === input.selectionEnd;
|
|
191
|
+
const atBoundary = isForward ? isCaretAtEnd(input) : isCaretAtStart(input);
|
|
192
|
+
if (!collapsed || !atBoundary) {
|
|
193
|
+
event.stopPropagation();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
export {
|
|
201
|
+
connect
|
|
202
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Scope } from '@zag-js/core';
|
|
2
|
+
|
|
3
|
+
declare const getRootId: (ctx: Scope) => any;
|
|
4
|
+
declare const getItemId: (ctx: Scope, value: string) => any;
|
|
5
|
+
declare const getGroupId: (ctx: Scope, value: string) => any;
|
|
6
|
+
declare const getRootEl: (ctx: Scope) => HTMLElement | null;
|
|
7
|
+
declare const getElements: (ctx: Scope) => HTMLElement[];
|
|
8
|
+
declare const getFirstEl: (ctx: Scope) => HTMLElement | undefined;
|
|
9
|
+
declare const getLastEl: (ctx: Scope) => HTMLElement | undefined;
|
|
10
|
+
declare const hasFocusableItem: (ctx: Scope) => boolean;
|
|
11
|
+
declare const getNextEl: (ctx: Scope, loopFocus: boolean) => HTMLElement | undefined;
|
|
12
|
+
declare const getPrevEl: (ctx: Scope, loopFocus: boolean) => HTMLElement | null | undefined;
|
|
13
|
+
|
|
14
|
+
export { getElements, getFirstEl, getGroupId, getItemId, getLastEl, getNextEl, getPrevEl, getRootEl, getRootId, hasFocusableItem };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Scope } from '@zag-js/core';
|
|
2
|
+
|
|
3
|
+
declare const getRootId: (ctx: Scope) => any;
|
|
4
|
+
declare const getItemId: (ctx: Scope, value: string) => any;
|
|
5
|
+
declare const getGroupId: (ctx: Scope, value: string) => any;
|
|
6
|
+
declare const getRootEl: (ctx: Scope) => HTMLElement | null;
|
|
7
|
+
declare const getElements: (ctx: Scope) => HTMLElement[];
|
|
8
|
+
declare const getFirstEl: (ctx: Scope) => HTMLElement | undefined;
|
|
9
|
+
declare const getLastEl: (ctx: Scope) => HTMLElement | undefined;
|
|
10
|
+
declare const hasFocusableItem: (ctx: Scope) => boolean;
|
|
11
|
+
declare const getNextEl: (ctx: Scope, loopFocus: boolean) => HTMLElement | undefined;
|
|
12
|
+
declare const getPrevEl: (ctx: Scope, loopFocus: boolean) => HTMLElement | null | undefined;
|
|
13
|
+
|
|
14
|
+
export { getElements, getFirstEl, getGroupId, getItemId, getLastEl, getNextEl, getPrevEl, getRootEl, getRootId, hasFocusableItem };
|