@destyler/toggle 0.0.2 → 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.
- package/README.md +7 -0
- package/dist/component.cjs +254 -9
- package/dist/component.d.cts +549 -4
- package/dist/component.d.mts +549 -4
- package/dist/component.d.ts +549 -4
- package/dist/component.mjs +250 -6
- package/dist/index.cjs +12 -212
- package/dist/index.d.cts +4 -369
- package/dist/index.d.mts +4 -369
- package/dist/index.d.ts +4 -369
- package/dist/index.mjs +6 -213
- package/dist/resolver.cjs +5 -3
- package/dist/resolver.d.cts +2 -2
- package/dist/resolver.d.mts +2 -2
- package/dist/resolver.d.ts +2 -2
- package/dist/resolver.mjs +5 -3
- package/package.json +16 -17
package/dist/component.mjs
CHANGED
|
@@ -1,6 +1,250 @@
|
|
|
1
|
-
|
|
2
|
-
import '
|
|
3
|
-
import '@destyler/
|
|
4
|
-
import '@destyler/
|
|
5
|
-
import '@destyler/
|
|
6
|
-
|
|
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,217 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
}, {
|
|
70
|
-
default: () => this.$slots.default?.()
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
10
|
|
|
75
|
-
const destylerToggleGroupRootProps = {
|
|
76
|
-
as: {
|
|
77
|
-
type: [String, Object],
|
|
78
|
-
required: false,
|
|
79
|
-
default: "div"
|
|
80
|
-
},
|
|
81
|
-
asChild: {
|
|
82
|
-
type: Boolean,
|
|
83
|
-
required: false,
|
|
84
|
-
default: false
|
|
85
|
-
},
|
|
86
|
-
type: {
|
|
87
|
-
type: String,
|
|
88
|
-
required: false,
|
|
89
|
-
default: "single"
|
|
90
|
-
},
|
|
91
|
-
defaultValue: {
|
|
92
|
-
type: [String, Array],
|
|
93
|
-
required: false
|
|
94
|
-
},
|
|
95
|
-
modelValue: {
|
|
96
|
-
type: [String, Array],
|
|
97
|
-
required: false
|
|
98
|
-
},
|
|
99
|
-
rovingFocus: {
|
|
100
|
-
type: Boolean,
|
|
101
|
-
required: false,
|
|
102
|
-
default: true
|
|
103
|
-
},
|
|
104
|
-
disabled: {
|
|
105
|
-
type: Boolean,
|
|
106
|
-
required: false,
|
|
107
|
-
default: false
|
|
108
|
-
},
|
|
109
|
-
orientation: {
|
|
110
|
-
type: String,
|
|
111
|
-
required: false
|
|
112
|
-
},
|
|
113
|
-
dir: {
|
|
114
|
-
type: String,
|
|
115
|
-
required: false
|
|
116
|
-
},
|
|
117
|
-
loop: {
|
|
118
|
-
type: Boolean,
|
|
119
|
-
required: false,
|
|
120
|
-
default: true
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
const [injectToggleGroupRootContext, provideToggleGroupRootContext] = shared.createContext("DestylerToggleGroupRoot");
|
|
124
|
-
const DestylerToggleGroupRoot = vue.defineComponent({
|
|
125
|
-
name: "DestylerToggleGroupRoot",
|
|
126
|
-
props: destylerToggleGroupRootProps,
|
|
127
|
-
emits: ["update:modelValue"],
|
|
128
|
-
setup(props, { emit }) {
|
|
129
|
-
const { loop, rovingFocus, disabled, dir: propDir } = vue.toRefs(props);
|
|
130
|
-
const dir = composition.useDirection(propDir);
|
|
131
|
-
const { forwardRef } = composition.useForwardExpose();
|
|
132
|
-
const { modelValue, changeModelValue } = composition.useSingleOrMultipleValue(props, emit);
|
|
133
|
-
provideToggleGroupRootContext({
|
|
134
|
-
type: props.type,
|
|
135
|
-
modelValue,
|
|
136
|
-
changeModelValue,
|
|
137
|
-
dir,
|
|
138
|
-
orientation: props.orientation,
|
|
139
|
-
loop,
|
|
140
|
-
rovingFocus,
|
|
141
|
-
disabled
|
|
142
|
-
});
|
|
143
|
-
return {
|
|
144
|
-
rovingFocus,
|
|
145
|
-
dir,
|
|
146
|
-
loop,
|
|
147
|
-
forwardRef
|
|
148
|
-
};
|
|
149
|
-
},
|
|
150
|
-
render() {
|
|
151
|
-
return vue.h(this.rovingFocus ? rovingFocus.DestylerRovingFocusGroup : primitive.DestylerPrimitive, {
|
|
152
|
-
asChild: true,
|
|
153
|
-
orientation: this.rovingFocus ? this.$props.orientation : void 0,
|
|
154
|
-
dir: this.dir,
|
|
155
|
-
loop: this.rovingFocus ? this.loop : void 0
|
|
156
|
-
}, {
|
|
157
|
-
default: () => {
|
|
158
|
-
return vue.h(primitive.DestylerPrimitive, {
|
|
159
|
-
ref: (el) => this.forwardRef(el),
|
|
160
|
-
role: "group",
|
|
161
|
-
as: this.$props.as,
|
|
162
|
-
asChild: this.$props.asChild
|
|
163
|
-
}, {
|
|
164
|
-
default: () => this.$slots.default?.()
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
11
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
};
|
|
178
|
-
const DestylerToggleGroupItem = vue.defineComponent({
|
|
179
|
-
name: "DestylerToggleGroupItem",
|
|
180
|
-
props: destylerToggleGroupItemProps,
|
|
181
|
-
setup(props) {
|
|
182
|
-
const rootContext = injectToggleGroupRootContext();
|
|
183
|
-
const disabled = vue.computed(() => rootContext.disabled?.value || props.disabled);
|
|
184
|
-
const pressed = vue.computed(() => rootContext.modelValue.value?.includes(props.value));
|
|
185
|
-
const { forwardRef } = composition.useForwardExpose();
|
|
186
|
-
return {
|
|
187
|
-
rootContext,
|
|
188
|
-
disabled,
|
|
189
|
-
pressed,
|
|
190
|
-
forwardRef
|
|
191
|
-
};
|
|
192
|
-
},
|
|
193
|
-
render() {
|
|
194
|
-
return vue.h(this.rootContext.rovingFocus.value ? rovingFocus.DestylerRovingFocusItem : primitive.DestylerPrimitive, {
|
|
195
|
-
asChild: true,
|
|
196
|
-
focusable: !this.disabled,
|
|
197
|
-
active: this.pressed
|
|
198
|
-
}, {
|
|
199
|
-
default: () => {
|
|
200
|
-
return vue.h(DestylerToggle, vue.mergeProps(this.$props, {
|
|
201
|
-
"ref": (el) => this.forwardRef(el),
|
|
202
|
-
"disabled": this.disabled,
|
|
203
|
-
"pressed": this.rootContext.type === "single" ? this.rootContext.modelValue.value === this.$props.value : this.rootContext.modelValue.value?.includes(this.$props.value),
|
|
204
|
-
"onUpdate:pressed": () => {
|
|
205
|
-
this.rootContext.changeModelValue(this.$props.value);
|
|
206
|
-
}
|
|
207
|
-
}), {
|
|
208
|
-
default: () => this.$slots.default?.()
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
exports.DestylerToggle = DestylerToggle;
|
|
216
|
-
exports.DestylerToggleGroupItem = DestylerToggleGroupItem;
|
|
217
|
-
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;
|