@destyler/toggle 0.0.3 → 0.0.4

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.
@@ -1,6 +1,250 @@
1
- export { DestylerToggle, DestylerToggleGroupItem, DestylerToggleGroupRoot } from './index.mjs';
2
- import 'vue';
3
- import '@destyler/primitive';
4
- import '@destyler/composition';
5
- import '@destyler/shared';
6
- import '@destyler/roving-focus';
1
+ import { defineComponent, computed, h, toRefs, mergeProps } from 'vue';
2
+ import { primitiveProps, Primitive } from '@destyler/primitive';
3
+ import { useForwardExpose, useVModel, useDirection, useSingleOrMultipleValue } from '@destyler/composition';
4
+ import { createContext } from '@destyler/shared';
5
+ import { RovingFocusGroup, RovingFocusItem } from '@destyler/roving-focus';
6
+
7
+ const toggleProps = {
8
+ ...primitiveProps,
9
+ /**
10
+ * @default button
11
+ */
12
+ as: {
13
+ ...primitiveProps.as,
14
+ default: "button"
15
+ },
16
+ /**
17
+ * The pressed state of the toggle when it is initially rendered. Use when you do not need to control its open state.
18
+ */
19
+ defaultValue: {
20
+ type: Boolean,
21
+ required: false
22
+ },
23
+ /**
24
+ * The controlled pressed state of the toggle. Can be bind as `v-model`.
25
+ *
26
+ * @default undefined
27
+ */
28
+ pressed: {
29
+ type: Boolean,
30
+ required: false,
31
+ default: void 0
32
+ },
33
+ /**
34
+ * When `true`, prevents the user from interacting with the toggle.
35
+ */
36
+ disabled: {
37
+ type: Boolean,
38
+ required: false,
39
+ default: false
40
+ }
41
+ };
42
+ const toggleEmits = {
43
+ /**
44
+ * Event handler called when the pressed state of the toggle changes.
45
+ */
46
+ "update:pressed": (_value) => true
47
+ };
48
+ const Toggle = defineComponent({
49
+ name: "DestylerToggle",
50
+ props: toggleProps,
51
+ emits: toggleEmits,
52
+ setup(props, { emit }) {
53
+ useForwardExpose();
54
+ const pressed = useVModel(props, "pressed", emit, {
55
+ defaultValue: props.defaultValue,
56
+ passive: props.pressed === void 0
57
+ });
58
+ function togglePressed() {
59
+ pressed.value = !pressed.value;
60
+ }
61
+ const dataState = computed(() => {
62
+ return pressed.value ? "on" : "off";
63
+ });
64
+ return {
65
+ pressed,
66
+ togglePressed,
67
+ dataState
68
+ };
69
+ },
70
+ render() {
71
+ return h(Primitive, {
72
+ "as": this.$props.as,
73
+ "asChild": this.$props.asChild,
74
+ "type": this.$props.as === "button" ? "button" : void 0,
75
+ "aria-pressed": this.pressed,
76
+ "data-state": this.dataState,
77
+ "data-disabled": this.$props.disabled ? "" : void 0,
78
+ "disabled": this.$props.disabled,
79
+ "onClick": () => {
80
+ this.togglePressed();
81
+ }
82
+ }, () => this.$slots.default?.());
83
+ }
84
+ });
85
+
86
+ const toggleGroupRootProps = {
87
+ ...primitiveProps,
88
+ /**
89
+ * Determines whether a "single" or "multiple" items can be pressed at a time.
90
+ *
91
+ * This prop will be ignored if any of `v-model` or `defaultValue` is an defined, as the type will be inferred from the value.
92
+ *
93
+ * @default single
94
+ */
95
+ type: {
96
+ type: String,
97
+ required: false,
98
+ default: "single"
99
+ },
100
+ /**
101
+ * The default active value of the item(s).
102
+ *
103
+ * Use when you do not need to control the state of the item(s).
104
+ */
105
+ defaultValue: {
106
+ type: [String, Array],
107
+ required: false
108
+ },
109
+ /**
110
+ * The controlled value of the active item(s).
111
+ *
112
+ * Use this when you need to control the state of the items. Can be binded with `v-model`
113
+ */
114
+ modelValue: {
115
+ type: [String, Array],
116
+ required: false
117
+ },
118
+ /**
119
+ * When `false`, navigating through the items using arrow keys will be disabled.
120
+ *
121
+ * @default true
122
+ */
123
+ rovingFocus: {
124
+ type: Boolean,
125
+ required: false,
126
+ default: true
127
+ },
128
+ /**
129
+ * When `true`, prevents the user from interacting with the toggle group and all its items.
130
+ *
131
+ * @default false
132
+ */
133
+ disabled: {
134
+ type: Boolean,
135
+ required: false,
136
+ default: false
137
+ },
138
+ /**
139
+ * The orientation of the component, which determines how focus moves: `horizontal` for left/right arrows and `vertical` for up/down arrows.
140
+ */
141
+ orientation: {
142
+ type: String,
143
+ required: false
144
+ },
145
+ /**
146
+ * The reading direction of the combobox when applicable.
147
+ */
148
+ dir: {
149
+ type: String,
150
+ required: false
151
+ },
152
+ /**
153
+ * When `loop` and `rovingFocus` is `true`, keyboard navigation will loop from last item to first, and vice versa.
154
+ */
155
+ loop: {
156
+ type: Boolean,
157
+ required: false,
158
+ default: true
159
+ }
160
+ };
161
+ const toggleGroupRootEmits = {
162
+ /**
163
+ * Event handler called when the value changes.
164
+ */
165
+ "update:modelValue": (_value) => true
166
+ };
167
+ const [injectToggleGroupRootContext, provideToggleGroupRootContext] = createContext("DestylerToggleGroupRoot");
168
+ const ToggleGroupRoot = defineComponent({
169
+ name: "DestylerToggleGroupRoot",
170
+ props: toggleGroupRootProps,
171
+ emits: toggleGroupRootEmits,
172
+ setup(props, { emit }) {
173
+ const { loop, rovingFocus, disabled, dir: propDir } = toRefs(props);
174
+ const dir = useDirection(propDir);
175
+ const { forwardRef } = useForwardExpose();
176
+ const { modelValue, changeModelValue } = useSingleOrMultipleValue(props, emit);
177
+ provideToggleGroupRootContext({
178
+ type: props.type,
179
+ modelValue,
180
+ changeModelValue,
181
+ dir,
182
+ orientation: props.orientation,
183
+ loop,
184
+ rovingFocus,
185
+ disabled
186
+ });
187
+ return {
188
+ rovingFocus,
189
+ dir,
190
+ loop,
191
+ forwardRef
192
+ };
193
+ },
194
+ render() {
195
+ return h(this.rovingFocus ? RovingFocusGroup : Primitive, {
196
+ asChild: true,
197
+ orientation: this.rovingFocus ? this.$props.orientation : void 0,
198
+ dir: this.dir,
199
+ loop: this.rovingFocus ? this.loop : void 0
200
+ }, () => h(Primitive, {
201
+ ref: (el) => this.forwardRef(el),
202
+ role: "group",
203
+ as: this.$props.as,
204
+ asChild: this.$props.asChild
205
+ }, () => this.$slots.default?.()));
206
+ }
207
+ });
208
+
209
+ const toggleGroupItemProps = {
210
+ ...toggleProps,
211
+ /**
212
+ * A string value for the toggle group item. All items within a toggle group should use a unique value.
213
+ */
214
+ value: {
215
+ type: String,
216
+ required: true
217
+ }
218
+ };
219
+ const ToggleGroupItem = defineComponent({
220
+ name: "ToggleGroupItem",
221
+ props: toggleGroupItemProps,
222
+ setup(props) {
223
+ const rootContext = injectToggleGroupRootContext();
224
+ const disabled = computed(() => rootContext.disabled?.value || props.disabled);
225
+ const pressed = computed(() => rootContext.modelValue.value?.includes(props.value));
226
+ const { forwardRef } = useForwardExpose();
227
+ return {
228
+ rootContext,
229
+ disabled,
230
+ pressed,
231
+ forwardRef
232
+ };
233
+ },
234
+ render() {
235
+ return h(this.rootContext.rovingFocus.value ? RovingFocusItem : Primitive, {
236
+ asChild: true,
237
+ focusable: !this.disabled,
238
+ active: this.pressed
239
+ }, () => h(Toggle, mergeProps(this.$props, {
240
+ "ref": (el) => this.forwardRef(el),
241
+ "disabled": this.disabled,
242
+ "pressed": this.rootContext.type === "single" ? this.rootContext.modelValue.value === this.$props.value : this.rootContext.modelValue.value?.includes(this.$props.value),
243
+ "onUpdate:pressed": () => {
244
+ this.rootContext.changeModelValue(this.$props.value);
245
+ }
246
+ }), () => this.$slots.default?.()));
247
+ }
248
+ });
249
+
250
+ export { Toggle, ToggleGroupItem, ToggleGroupRoot, toggleEmits, toggleGroupItemProps, toggleGroupRootEmits, toggleGroupRootProps, toggleProps };
package/dist/index.cjs CHANGED
@@ -1,203 +1,17 @@
1
1
  'use strict';
2
2
 
3
- const vue = require('vue');
4
- const primitive = require('@destyler/primitive');
5
- const composition = require('@destyler/composition');
6
- const shared = require('@destyler/shared');
7
- const rovingFocus = require('@destyler/roving-focus');
3
+ const index = require('./component.cjs');
4
+ require('vue');
5
+ require('@destyler/primitive');
6
+ require('@destyler/composition');
7
+ require('@destyler/shared');
8
+ require('@destyler/roving-focus');
8
9
 
9
- const destylerToggleProps = {
10
- as: {
11
- type: [String, Object],
12
- required: false,
13
- default: "button"
14
- },
15
- asChild: {
16
- type: Boolean,
17
- required: false,
18
- default: false
19
- },
20
- defaultValue: {
21
- type: Boolean,
22
- required: false
23
- },
24
- pressed: {
25
- type: Boolean,
26
- required: false,
27
- default: void 0
28
- },
29
- disabled: {
30
- type: Boolean,
31
- required: false,
32
- default: false
33
- }
34
- };
35
- const DestylerToggle = vue.defineComponent({
36
- name: "DestylerToggle",
37
- props: destylerToggleProps,
38
- emits: ["update:pressed"],
39
- setup(props, { emit }) {
40
- composition.useForwardExpose();
41
- const pressed = composition.useVModel(props, "pressed", emit, {
42
- defaultValue: props.defaultValue,
43
- passive: props.pressed === void 0
44
- });
45
- function togglePressed() {
46
- pressed.value = !pressed.value;
47
- }
48
- const dataState = vue.computed(() => {
49
- return pressed.value ? "on" : "off";
50
- });
51
- return {
52
- pressed,
53
- togglePressed,
54
- dataState
55
- };
56
- },
57
- render() {
58
- return vue.h(primitive.DestylerPrimitive, {
59
- "as": this.$props.as,
60
- "asChild": this.$props.asChild,
61
- "type": this.$props.as === "button" ? "button" : void 0,
62
- "aria-pressed": this.pressed,
63
- "data-state": this.dataState,
64
- "data-disabled": this.$props.disabled ? "" : void 0,
65
- "disabled": this.$props.disabled,
66
- "onClick": () => {
67
- this.togglePressed();
68
- }
69
- }, () => this.$slots.default?.());
70
- }
71
- });
72
10
 
73
- const destylerToggleGroupRootProps = {
74
- as: {
75
- type: [String, Object],
76
- required: false,
77
- default: "div"
78
- },
79
- asChild: {
80
- type: Boolean,
81
- required: false,
82
- default: false
83
- },
84
- type: {
85
- type: String,
86
- required: false,
87
- default: "single"
88
- },
89
- defaultValue: {
90
- type: [String, Array],
91
- required: false
92
- },
93
- modelValue: {
94
- type: [String, Array],
95
- required: false
96
- },
97
- rovingFocus: {
98
- type: Boolean,
99
- required: false,
100
- default: true
101
- },
102
- disabled: {
103
- type: Boolean,
104
- required: false,
105
- default: false
106
- },
107
- orientation: {
108
- type: String,
109
- required: false
110
- },
111
- dir: {
112
- type: String,
113
- required: false
114
- },
115
- loop: {
116
- type: Boolean,
117
- required: false,
118
- default: true
119
- }
120
- };
121
- const [injectToggleGroupRootContext, provideToggleGroupRootContext] = shared.createContext("DestylerToggleGroupRoot");
122
- const DestylerToggleGroupRoot = vue.defineComponent({
123
- name: "DestylerToggleGroupRoot",
124
- props: destylerToggleGroupRootProps,
125
- emits: ["update:modelValue"],
126
- setup(props, { emit }) {
127
- const { loop, rovingFocus, disabled, dir: propDir } = vue.toRefs(props);
128
- const dir = composition.useDirection(propDir);
129
- const { forwardRef } = composition.useForwardExpose();
130
- const { modelValue, changeModelValue } = composition.useSingleOrMultipleValue(props, emit);
131
- provideToggleGroupRootContext({
132
- type: props.type,
133
- modelValue,
134
- changeModelValue,
135
- dir,
136
- orientation: props.orientation,
137
- loop,
138
- rovingFocus,
139
- disabled
140
- });
141
- return {
142
- rovingFocus,
143
- dir,
144
- loop,
145
- forwardRef
146
- };
147
- },
148
- render() {
149
- return vue.h(this.rovingFocus ? rovingFocus.DestylerRovingFocusGroup : primitive.DestylerPrimitive, {
150
- asChild: true,
151
- orientation: this.rovingFocus ? this.$props.orientation : void 0,
152
- dir: this.dir,
153
- loop: this.rovingFocus ? this.loop : void 0
154
- }, () => vue.h(primitive.DestylerPrimitive, {
155
- ref: (el) => this.forwardRef(el),
156
- role: "group",
157
- as: this.$props.as,
158
- asChild: this.$props.asChild
159
- }, () => this.$slots.default?.()));
160
- }
161
- });
162
11
 
163
- const destylerToggleGroupItemProps = {
164
- ...destylerToggleProps,
165
- value: {
166
- type: String,
167
- required: true
168
- }
169
- };
170
- const DestylerToggleGroupItem = vue.defineComponent({
171
- name: "DestylerToggleGroupItem",
172
- props: destylerToggleGroupItemProps,
173
- setup(props) {
174
- const rootContext = injectToggleGroupRootContext();
175
- const disabled = vue.computed(() => rootContext.disabled?.value || props.disabled);
176
- const pressed = vue.computed(() => rootContext.modelValue.value?.includes(props.value));
177
- const { forwardRef } = composition.useForwardExpose();
178
- return {
179
- rootContext,
180
- disabled,
181
- pressed,
182
- forwardRef
183
- };
184
- },
185
- render() {
186
- return vue.h(this.rootContext.rovingFocus.value ? rovingFocus.DestylerRovingFocusItem : primitive.DestylerPrimitive, {
187
- asChild: true,
188
- focusable: !this.disabled,
189
- active: this.pressed
190
- }, () => vue.h(DestylerToggle, vue.mergeProps(this.$props, {
191
- "ref": (el) => this.forwardRef(el),
192
- "disabled": this.disabled,
193
- "pressed": this.rootContext.type === "single" ? this.rootContext.modelValue.value === this.$props.value : this.rootContext.modelValue.value?.includes(this.$props.value),
194
- "onUpdate:pressed": () => {
195
- this.rootContext.changeModelValue(this.$props.value);
196
- }
197
- }), () => this.$slots.default?.()));
198
- }
199
- });
200
-
201
- exports.DestylerToggle = DestylerToggle;
202
- exports.DestylerToggleGroupItem = DestylerToggleGroupItem;
203
- exports.DestylerToggleGroupRoot = DestylerToggleGroupRoot;
12
+ exports.Toggle = index.Toggle;
13
+ exports.ToggleGroupItem = index.ToggleGroupItem;
14
+ exports.ToggleGroupRoot = index.ToggleGroupRoot;
15
+ exports.toggleGroupItemProps = index.toggleGroupItemProps;
16
+ exports.toggleGroupRootProps = index.toggleGroupRootProps;
17
+ exports.toggleProps = index.toggleProps;