@v-c/slider 1.0.2 → 1.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.
Files changed (76) hide show
  1. package/dist/Handles/Handle.cjs +195 -1
  2. package/dist/Handles/Handle.d.ts +17 -5
  3. package/dist/Handles/Handle.js +191 -202
  4. package/dist/Handles/index.cjs +108 -1
  5. package/dist/Handles/index.d.ts +20 -4
  6. package/dist/Handles/index.js +105 -116
  7. package/dist/Marks/Mark.cjs +51 -1
  8. package/dist/Marks/Mark.d.ts +2 -2
  9. package/dist/Marks/Mark.js +48 -39
  10. package/dist/Marks/index.cjs +39 -1
  11. package/dist/Marks/index.d.ts +2 -2
  12. package/dist/Marks/index.js +36 -32
  13. package/dist/Slider.cjs +569 -1
  14. package/dist/Slider.d.ts +60 -263
  15. package/dist/Slider.js +563 -352
  16. package/dist/Steps/Dot.cjs +52 -1
  17. package/dist/Steps/Dot.d.ts +2 -2
  18. package/dist/Steps/Dot.js +49 -38
  19. package/dist/Steps/index.cjs +64 -1
  20. package/dist/Steps/index.d.ts +2 -2
  21. package/dist/Steps/index.js +61 -41
  22. package/dist/Tracks/Track.cjs +74 -1
  23. package/dist/Tracks/Track.js +70 -81
  24. package/dist/Tracks/index.cjs +70 -1
  25. package/dist/Tracks/index.js +66 -82
  26. package/dist/_virtual/rolldown_runtime.cjs +21 -0
  27. package/dist/context.cjs +39 -1
  28. package/dist/context.d.ts +13 -7
  29. package/dist/context.js +29 -24
  30. package/dist/hooks/useDrag.cjs +160 -1
  31. package/dist/hooks/useDrag.d.ts +2 -2
  32. package/dist/hooks/useDrag.js +155 -86
  33. package/dist/hooks/useOffset.cjs +124 -1
  34. package/dist/hooks/useOffset.d.ts +2 -1
  35. package/dist/hooks/useOffset.js +122 -97
  36. package/dist/hooks/useRange.cjs +21 -1
  37. package/dist/hooks/useRange.js +18 -9
  38. package/dist/index.cjs +6 -1
  39. package/dist/index.d.ts +1 -0
  40. package/dist/index.js +4 -4
  41. package/dist/interface.cjs +0 -1
  42. package/dist/interface.js +0 -1
  43. package/dist/util.cjs +32 -1
  44. package/dist/util.js +27 -26
  45. package/package.json +18 -10
  46. package/docs/TooltipSlider.tsx +0 -94
  47. package/docs/assets/anim.less +0 -63
  48. package/docs/assets/bootstrap.less +0 -163
  49. package/docs/assets/index.less +0 -337
  50. package/docs/debug.vue +0 -60
  51. package/docs/editable.vue +0 -59
  52. package/docs/handle.vue +0 -45
  53. package/docs/marks.vue +0 -94
  54. package/docs/multiple.vue +0 -54
  55. package/docs/range.vue +0 -211
  56. package/docs/slider.stories.vue +0 -45
  57. package/docs/sliderDemo.vue +0 -267
  58. package/docs/vertical.vue +0 -122
  59. package/src/Handles/Handle.tsx +0 -223
  60. package/src/Handles/index.tsx +0 -124
  61. package/src/Marks/Mark.tsx +0 -40
  62. package/src/Marks/index.tsx +0 -40
  63. package/src/Slider.tsx +0 -593
  64. package/src/Steps/Dot.tsx +0 -40
  65. package/src/Steps/index.tsx +0 -54
  66. package/src/Tracks/Track.tsx +0 -89
  67. package/src/Tracks/index.tsx +0 -92
  68. package/src/context.ts +0 -65
  69. package/src/hooks/useDrag.ts +0 -243
  70. package/src/hooks/useOffset.ts +0 -264
  71. package/src/hooks/useRange.ts +0 -24
  72. package/src/index.ts +0 -8
  73. package/src/interface.ts +0 -17
  74. package/src/util.ts +0 -41
  75. package/vite.config.ts +0 -18
  76. package/vitest.config.ts +0 -11
@@ -1,264 +0,0 @@
1
- import type { InternalMarkObj } from '../Marks'
2
- import { computed } from 'vue'
3
-
4
- /** Format the value in the range of [min, max] */
5
- type FormatRangeValue = (value: number) => number
6
-
7
- /** Format value align with step */
8
- type FormatStepValue = (value: number) => number
9
-
10
- /** Format value align with step & marks */
11
- type FormatValue = (value: number) => number
12
-
13
- type OffsetMode = 'unit' | 'dist'
14
-
15
- type OffsetValue = (
16
- values: number[],
17
- offset: number | 'min' | 'max',
18
- valueIndex: number,
19
- mode?: OffsetMode,
20
- ) => number
21
-
22
- export type OffsetValues = (
23
- values: number[],
24
- offset: number | 'min' | 'max',
25
- valueIndex: number,
26
- mode?: OffsetMode,
27
- ) => {
28
- value: number
29
- values: number[]
30
- }
31
-
32
- export default function useOffset(
33
- min: number,
34
- max: number,
35
- step: number,
36
- markList: InternalMarkObj[],
37
- allowCross: boolean,
38
- pushable: false | number,
39
- ): [FormatValue, OffsetValues] {
40
- const formatRangeValue = computed<FormatRangeValue>(() => {
41
- return (val: number) => Math.max(min, Math.min(max, val))
42
- }).value
43
-
44
- const formatStepValue = computed<FormatStepValue>(() => {
45
- return (val: number) => {
46
- if (step !== null) {
47
- const stepValue = min + Math.round((formatRangeValue(val) - min) / step) * step
48
-
49
- // Cut number in case to be like 0.30000000000000004
50
- const getDecimal = (num: number) => (String(num).split('.')[1] || '').length
51
- const maxDecimal = Math.max(getDecimal(step), getDecimal(max), getDecimal(min))
52
- const fixedValue = Number(stepValue.toFixed(maxDecimal))
53
-
54
- return min <= fixedValue && fixedValue <= max ? fixedValue : null
55
- }
56
- return null
57
- }
58
- }).value
59
-
60
- const formatValue = computed<FormatValue>(() => {
61
- return (val: number) => {
62
- const formatNextValue = formatRangeValue(val)
63
-
64
- // List align values
65
- const alignValues = markList.map<number>(mark => mark.value)
66
- if (step !== null) {
67
- alignValues.push(formatStepValue(val))
68
- }
69
-
70
- // min & max
71
- alignValues.push(min, max)
72
-
73
- // Align with marks
74
- let closeValue = alignValues[0]
75
- let closeDist = max - min
76
-
77
- alignValues.forEach((alignValue) => {
78
- const dist = Math.abs(formatNextValue - alignValue)
79
- if (dist <= closeDist) {
80
- closeValue = alignValue
81
- closeDist = dist
82
- }
83
- })
84
-
85
- return closeValue
86
- }
87
- }).value
88
-
89
- // ========================== Offset ==========================
90
- // Single Value
91
- const offsetValue: OffsetValue = (values, offset, valueIndex, mode = 'unit') => {
92
- if (typeof offset === 'number') {
93
- let nextValue: number
94
- const originValue = values[valueIndex]
95
-
96
- // Only used for `dist` mode
97
- const targetDistValue = originValue + offset
98
-
99
- // Compare next step value & mark value which is best match
100
- let potentialValues: number[] = []
101
- markList.forEach((mark) => {
102
- potentialValues.push(mark.value)
103
- })
104
-
105
- // Min & Max
106
- potentialValues.push(min, max)
107
-
108
- // In case origin value is align with mark but not with step
109
- potentialValues.push(formatStepValue(originValue))
110
-
111
- // Put offset step value also
112
- const sign = offset > 0 ? 1 : -1
113
-
114
- if (mode === 'unit') {
115
- potentialValues.push(formatStepValue(originValue + sign * step))
116
- }
117
- else {
118
- potentialValues.push(formatStepValue(targetDistValue))
119
- }
120
-
121
- // Find close one
122
- potentialValues = potentialValues
123
- .filter(val => val !== null)
124
- // Remove reverse value
125
- .filter(val => (offset < 0 ? val <= originValue : val >= originValue))
126
-
127
- if (mode === 'unit') {
128
- // `unit` mode can not contain itself
129
- potentialValues = potentialValues.filter(val => val !== originValue)
130
- }
131
-
132
- const compareValue = mode === 'unit' ? originValue : targetDistValue
133
-
134
- nextValue = potentialValues[0]
135
- let valueDist = Math.abs(nextValue - compareValue)
136
-
137
- potentialValues.forEach((potentialValue) => {
138
- const dist = Math.abs(potentialValue - compareValue)
139
- if (dist < valueDist) {
140
- nextValue = potentialValue
141
- valueDist = dist
142
- }
143
- })
144
-
145
- // Out of range will back to range
146
- if (nextValue === undefined) {
147
- return offset < 0 ? min : max
148
- }
149
-
150
- // `dist` mode
151
- if (mode === 'dist') {
152
- return nextValue
153
- }
154
-
155
- // `unit` mode may need another round
156
- if (Math.abs(offset) > 1) {
157
- const cloneValues = [...values]
158
- cloneValues[valueIndex] = nextValue
159
-
160
- return offsetValue(cloneValues, offset - sign, valueIndex, mode)
161
- }
162
-
163
- return nextValue
164
- }
165
- else if (offset === 'min') {
166
- return min
167
- }
168
- else if (offset === 'max') {
169
- return max
170
- }
171
- }
172
-
173
- /** Same as `offsetValue` but return `changed` mark to tell value changed */
174
- const offsetChangedValue = (
175
- values: number[],
176
- offset: number,
177
- valueIndex: number,
178
- mode: OffsetMode = 'unit',
179
- ) => {
180
- const originValue = values[valueIndex]
181
- const nextValue = offsetValue(values, offset, valueIndex, mode)
182
- return {
183
- value: nextValue,
184
- changed: nextValue !== originValue,
185
- }
186
- }
187
-
188
- const needPush = (dist: number) => {
189
- return (pushable === null && dist === 0) || (typeof pushable === 'number' && dist < pushable)
190
- }
191
-
192
- // Values
193
- const offsetValues: OffsetValues = (values, offset, valueIndex, mode = 'unit') => {
194
- const nextValues = values.map<number>(formatValue)
195
- const originValue = nextValues[valueIndex]
196
- const nextValue = offsetValue(nextValues, offset, valueIndex, mode)
197
- nextValues[valueIndex] = nextValue
198
-
199
- if (!allowCross) {
200
- // >>>>> Allow Cross
201
- const pushNum = pushable || 0
202
-
203
- // ============ AllowCross ===============
204
- if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) {
205
- nextValues[valueIndex] = Math.max(
206
- nextValues[valueIndex],
207
- nextValues[valueIndex - 1] + pushNum,
208
- )
209
- }
210
-
211
- if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) {
212
- nextValues[valueIndex] = Math.min(
213
- nextValues[valueIndex],
214
- nextValues[valueIndex + 1] - pushNum,
215
- )
216
- }
217
- }
218
- else if (typeof pushable === 'number' || pushable === null) {
219
- // >>>>> Pushable
220
- // =============== Push ==================
221
-
222
- // >>>>>> Basic push
223
- // End values
224
- for (let i = valueIndex + 1; i < nextValues.length; i += 1) {
225
- let changed = true
226
- while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
227
- ({ value: nextValues[i], changed } = offsetChangedValue(nextValues, 1, i))
228
- }
229
- }
230
-
231
- // Start values
232
- for (let i = valueIndex; i > 0; i -= 1) {
233
- let changed = true
234
- while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
235
- ({ value: nextValues[i - 1], changed } = offsetChangedValue(nextValues, -1, i - 1))
236
- }
237
- }
238
-
239
- // >>>>> Revert back to safe push range
240
- // End to Start
241
- for (let i = nextValues.length - 1; i > 0; i -= 1) {
242
- let changed = true
243
- while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
244
- ({ value: nextValues[i - 1], changed } = offsetChangedValue(nextValues, -1, i - 1))
245
- }
246
- }
247
-
248
- // Start to End
249
- for (let i = 0; i < nextValues.length - 1; i += 1) {
250
- let changed = true
251
- while (needPush(nextValues[i + 1] - nextValues[i]) && changed) {
252
- ({ value: nextValues[i + 1], changed } = offsetChangedValue(nextValues, 1, i + 1))
253
- }
254
- }
255
- }
256
-
257
- return {
258
- value: nextValues[valueIndex],
259
- values: nextValues,
260
- }
261
- }
262
-
263
- return [formatValue, offsetValues]
264
- }
@@ -1,24 +0,0 @@
1
- import type { SliderProps } from '../Slider'
2
- import { warning } from '@v-c/util/dist/warning'
3
-
4
- export default function useRange(
5
- range?: SliderProps['range'],
6
- ): [
7
- range: boolean,
8
- rangeEditable: boolean,
9
- rangeDraggableTrack: boolean,
10
- minCount: number,
11
- maxCount?: number,
12
- ] {
13
- if (range === true || !range) {
14
- return [!!range, false, false, 0]
15
- }
16
-
17
- const { editable = false, draggableTrack = false, minCount, maxCount } = range
18
-
19
- if (process.env.NODE_ENV !== 'production') {
20
- warning(!editable || !draggableTrack, '`editable` can not work with `draggableTrack`.')
21
- }
22
-
23
- return [true, editable, !editable && draggableTrack, minCount || 0, maxCount]
24
- }
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- import type { SliderProps, SliderRef } from './Slider'
2
- import Slider from './Slider'
3
-
4
- // export { UnstableContext } from './context'
5
-
6
- export type { SliderProps, SliderRef }
7
-
8
- export default Slider
package/src/interface.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { CSSProperties } from 'vue'
2
-
3
- export type Direction = 'rtl' | 'ltr' | 'ttb' | 'btt'
4
-
5
- export type OnStartMove = (
6
- e: MouseEvent | TouchEvent,
7
- valueIndex: number,
8
- startValues?: number[],
9
- ) => void
10
-
11
- export type AriaValueFormat = (value: number) => string
12
-
13
- export type SemanticName = 'tracks' | 'track' | 'rail' | 'handle'
14
-
15
- export type SliderClassNames = Partial<Record<SemanticName, string>>
16
-
17
- export type SliderStyles = Partial<Record<SemanticName, CSSProperties>>
package/src/util.ts DELETED
@@ -1,41 +0,0 @@
1
- import type { CSSProperties } from 'vue'
2
- import type { Direction } from './interface'
3
-
4
- export function getOffset(value: number, min: number, max: number) {
5
- return (value - min) / (max - min)
6
- }
7
-
8
- export function getDirectionStyle(direction: Direction, value: number, min: number, max: number) {
9
- const offset = getOffset(value, min, max)
10
-
11
- const positionStyle: CSSProperties = {}
12
-
13
- switch (direction) {
14
- case 'rtl':
15
- positionStyle.right = `${offset * 100}%`
16
- positionStyle.transform = 'translateX(50%)'
17
- break
18
-
19
- case 'btt':
20
- positionStyle.bottom = `${offset * 100}%`
21
- positionStyle.transform = 'translateY(50%)'
22
- break
23
-
24
- case 'ttb':
25
- positionStyle.top = `${offset * 100}%`
26
- positionStyle.transform = 'translateY(-50%)'
27
- break
28
-
29
- default:
30
- positionStyle.left = `${offset * 100}%`
31
- positionStyle.transform = 'translateX(-50%)'
32
- break
33
- }
34
-
35
- return positionStyle
36
- }
37
-
38
- /** Return index value if is list or return value directly */
39
- export function getIndex<T>(value: T | T[], index: number) {
40
- return Array.isArray(value) ? value[index] : value
41
- }
package/vite.config.ts DELETED
@@ -1,18 +0,0 @@
1
- import type { UserConfig } from 'vite'
2
- import fg from 'fast-glob'
3
- import { defineConfig, mergeConfig } from 'vite'
4
- import { buildCommon } from '../../scripts/build.common'
5
-
6
- const entry = fg.sync(['src/**/*.ts', 'src/**/*.tsx', '!src/**/*.test.ts', '!src/**/tests'])
7
-
8
- export default defineConfig({
9
- ...mergeConfig(buildCommon({
10
- external: ['vue', 'classnames', /^@v-c\/util/],
11
- }), {
12
- build: {
13
- lib: {
14
- entry,
15
- },
16
- },
17
- } as UserConfig),
18
- })
package/vitest.config.ts DELETED
@@ -1,11 +0,0 @@
1
- import { defineProject, mergeConfig } from 'vitest/config'
2
- import configShared from '../../vitest.config.ts'
3
-
4
- export default mergeConfig(
5
- configShared,
6
- defineProject({
7
- test: {
8
- environment: 'jsdom',
9
- },
10
- }),
11
- )