@v-c/slider 1.0.3 → 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 +4 -4
  3. package/dist/Handles/Handle.js +191 -204
  4. package/dist/Handles/index.cjs +108 -1
  5. package/dist/Handles/index.d.ts +1 -1
  6. package/dist/Handles/index.js +105 -120
  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 +53 -267
  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 -110
  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 -59
  51. package/docs/editable.vue +0 -58
  52. package/docs/handle.vue +0 -51
  53. package/docs/marks.vue +0 -104
  54. package/docs/multiple.vue +0 -50
  55. package/docs/range.vue +0 -224
  56. package/docs/slider.stories.vue +0 -45
  57. package/docs/sliderDemo.vue +0 -283
  58. package/docs/vertical.vue +0 -135
  59. package/src/Handles/Handle.tsx +0 -232
  60. package/src/Handles/index.tsx +0 -132
  61. package/src/Marks/Mark.tsx +0 -40
  62. package/src/Marks/index.tsx +0 -40
  63. package/src/Slider.tsx +0 -602
  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 -272
  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,272 +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 | null
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 | null,
36
- markList: InternalMarkObj[],
37
- allowCross: boolean,
38
- pushable: false | number | null,
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: FormatStepValue = (val: number) => {
45
- if (step !== null) {
46
- const stepValue = min + Math.round((formatRangeValue(val) - min) / step) * step
47
-
48
- // Cut number in case to be like 0.30000000000000004
49
- const getDecimal = (num: number) => (String(num).split('.')[1] || '').length
50
- const maxDecimal = Math.max(getDecimal(step), getDecimal(max), getDecimal(min))
51
- const fixedValue = Number(stepValue.toFixed(maxDecimal))
52
-
53
- return min <= fixedValue && fixedValue <= max ? fixedValue : null
54
- }
55
- return null
56
- }
57
-
58
- const formatValue = (val: number) => {
59
- const formatNextValue = formatRangeValue(val)
60
-
61
- // List align values
62
- const alignValues = markList.map<number>(mark => mark && mark.value)
63
- if (step !== null) {
64
- const stepValue = formatStepValue(val)
65
- if (stepValue !== null) {
66
- alignValues.push(stepValue)
67
- }
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
-
88
- // ========================== Offset ==========================
89
- // Single Value
90
- const offsetValue: OffsetValue = (values, offset, valueIndex, mode = 'unit') => {
91
- if (typeof offset === 'number') {
92
- let nextValue: number
93
- const originValue = values[valueIndex]
94
-
95
- // Only used for `dist` mode
96
- const targetDistValue = originValue + offset
97
-
98
- // Compare next step value & mark value which is best match
99
- let potentialValues: number[] = []
100
- markList.forEach((mark) => {
101
- potentialValues.push(mark.value)
102
- })
103
-
104
- // Min & Max
105
- potentialValues.push(min, max)
106
-
107
- // In case origin value is align with mark but not with step
108
- const originStepValue = formatStepValue(originValue)
109
- if (originStepValue !== null) {
110
- potentialValues.push(originStepValue)
111
- }
112
-
113
- // Put offset step value also
114
- const sign = offset > 0 ? 1 : -1
115
-
116
- if (mode === 'unit') {
117
- const allStepValues = formatStepValue(originValue + sign * step)
118
- if (allStepValues !== null) {
119
- potentialValues.push(allStepValues)
120
- }
121
- }
122
- else {
123
- const targetStepValue = formatStepValue(targetDistValue)
124
- if (targetStepValue !== null) {
125
- potentialValues.push(targetStepValue)
126
- }
127
- }
128
-
129
- // Find close one
130
- potentialValues = potentialValues
131
- .filter(val => val !== null)
132
- // Remove reverse value
133
- .filter(val => (offset < 0 ? val <= originValue : val >= originValue))
134
-
135
- if (mode === 'unit') {
136
- // `unit` mode can not contain itself
137
- potentialValues = potentialValues.filter(val => val !== originValue)
138
- }
139
-
140
- const compareValue = mode === 'unit' ? originValue : targetDistValue
141
-
142
- nextValue = potentialValues[0]
143
- let valueDist = Math.abs(nextValue - compareValue)
144
-
145
- potentialValues.forEach((potentialValue) => {
146
- const dist = Math.abs(potentialValue - compareValue)
147
- if (dist < valueDist) {
148
- nextValue = potentialValue
149
- valueDist = dist
150
- }
151
- })
152
-
153
- // Out of range will back to range
154
- if (nextValue === undefined) {
155
- return offset < 0 ? min : max
156
- }
157
-
158
- // `dist` mode
159
- if (mode === 'dist') {
160
- return nextValue
161
- }
162
-
163
- // `unit` mode may need another round
164
- if (Math.abs(offset) > 1) {
165
- const cloneValues = [...values]
166
- cloneValues[valueIndex] = nextValue
167
-
168
- return offsetValue(cloneValues, offset - sign, valueIndex, mode)
169
- }
170
-
171
- return nextValue
172
- }
173
- else if (offset === 'min') {
174
- return min
175
- }
176
- else if (offset === 'max') {
177
- return max
178
- }
179
- }
180
-
181
- /** Same as `offsetValue` but return `changed` mark to tell value changed */
182
- const offsetChangedValue = (
183
- values: number[],
184
- offset: number,
185
- valueIndex: number,
186
- mode: OffsetMode = 'unit',
187
- ) => {
188
- const originValue = values[valueIndex]
189
- const nextValue = offsetValue(values, offset, valueIndex, mode)
190
- return {
191
- value: nextValue,
192
- changed: nextValue !== originValue,
193
- }
194
- }
195
-
196
- const needPush = (dist: number) => {
197
- return (pushable === null && dist === 0) || (typeof pushable === 'number' && dist < pushable)
198
- }
199
-
200
- // Values
201
- const offsetValues: OffsetValues = (values, offset, valueIndex, mode = 'unit') => {
202
- const nextValues = values.map<number>(formatValue)
203
- const originValue = nextValues[valueIndex]
204
- const nextValue = offsetValue(nextValues, offset, valueIndex, mode)
205
- nextValues[valueIndex] = nextValue
206
-
207
- if (!allowCross) {
208
- // >>>>> Allow Cross
209
- const pushNum = pushable || 0
210
-
211
- // ============ AllowCross ===============
212
- if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) {
213
- nextValues[valueIndex] = Math.max(
214
- nextValues[valueIndex],
215
- nextValues[valueIndex - 1] + pushNum,
216
- )
217
- }
218
-
219
- if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) {
220
- nextValues[valueIndex] = Math.min(
221
- nextValues[valueIndex],
222
- nextValues[valueIndex + 1] - pushNum,
223
- )
224
- }
225
- }
226
- else if (typeof pushable === 'number' || pushable === null) {
227
- // >>>>> Pushable
228
- // =============== Push ==================
229
-
230
- // >>>>>> Basic push
231
- // End values
232
- for (let i = valueIndex + 1; i < nextValues.length; i += 1) {
233
- let changed = true
234
- while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
235
- ({ value: nextValues[i], changed } = offsetChangedValue(nextValues, 1, i))
236
- }
237
- }
238
-
239
- // Start values
240
- for (let i = valueIndex; i > 0; i -= 1) {
241
- let changed = true
242
- while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
243
- ({ value: nextValues[i - 1], changed } = offsetChangedValue(nextValues, -1, i - 1))
244
- }
245
- }
246
-
247
- // >>>>> Revert back to safe push range
248
- // End to Start
249
- for (let i = nextValues.length - 1; i > 0; i -= 1) {
250
- let changed = true
251
- while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
252
- ({ value: nextValues[i - 1], changed } = offsetChangedValue(nextValues, -1, i - 1))
253
- }
254
- }
255
-
256
- // Start to End
257
- for (let i = 0; i < nextValues.length - 1; i += 1) {
258
- let changed = true
259
- while (needPush(nextValues[i + 1] - nextValues[i]) && changed) {
260
- ({ value: nextValues[i + 1], changed } = offsetChangedValue(nextValues, 1, i + 1))
261
- }
262
- }
263
- }
264
-
265
- return {
266
- value: nextValues[valueIndex],
267
- values: nextValues,
268
- }
269
- }
270
-
271
- return [formatValue, offsetValues]
272
- }
@@ -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
- )