forked from rcbyr/keen-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-native.ts
124 lines (113 loc) · 3.04 KB
/
react-native.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { useEffect, useMemo, useRef } from 'react'
import Slider from './core/slider'
import {
SliderHooks,
SliderInstance,
SliderOptions,
SliderPlugin,
} from './core/types'
import { checkOptions } from './core/utils'
import Modes from './plugins/modes'
import Native from './plugins/native/native'
import { NativeInstance, NativeOptions } from './plugins/native/types'
import {
HOOK_DRAG_CHECKED,
HOOK_DRAG_ENDED,
HOOK_DRAG_STARTED,
HOOK_DRAGGED,
HOOK_UPDATED,
} from './plugins/types'
export type KeenSliderNativeHooks =
| SliderHooks
| HOOK_UPDATED
| HOOK_DRAGGED
| HOOK_DRAG_ENDED
| HOOK_DRAG_STARTED
| HOOK_DRAG_CHECKED
export type KeenSliderNativeOptions<
O = {},
P = {},
H extends string = KeenSliderNativeHooks
> = SliderOptions<NativeOptions> & {
[key in Exclude<
H | KeenSliderNativeHooks,
keyof SliderOptions<NativeOptions>
>]?: (slider: KeenSliderNativeInstance<O, P, H>) => void
} & Omit<O, keyof SliderOptions<NativeOptions>>
export type KeenSliderNativeInstance<
O = {},
P = {},
H extends string = KeenSliderNativeHooks
> = SliderInstance<
KeenSliderNativeOptions<O, P, H>,
NativeInstance<KeenSliderNativeOptions<O, P, H>> & P,
KeenSliderNativeHooks | H
>
export type KeenSliderNativePlugin<
O = {},
P = {},
H extends string = KeenSliderNativeHooks
> = SliderPlugin<
KeenSliderNativeOptions<O, P, H>,
KeenSliderNativeInstance<O, P, H>,
KeenSliderNativeHooks | H
>
export * from './plugins/types'
export * from './plugins/native/types'
export * from './core/types'
const KeenSliderNative = function <
O,
P,
H extends string = KeenSliderNativeHooks
>(
options?: KeenSliderNativeOptions<O, P, H>,
plugins?: KeenSliderNativePlugin[]
): KeenSliderNativeInstance<O, P, H> {
try {
const defOpts = {
drag: true,
mode: 'snap',
rubberband: true,
} as KeenSliderNativeOptions
return Slider<
KeenSliderNativeOptions<O, P, H>,
KeenSliderNativeInstance<O, P, H>,
KeenSliderNativeHooks
>(options, [
Native<KeenSliderNativeOptions>(defOpts),
Modes,
...(plugins || []),
])
} catch (e) {
console.error(e)
}
}
export default KeenSliderNative as unknown as {
new <O = {}, P = {}, H extends string = KeenSliderNativeHooks>(
options?: KeenSliderNativeOptions<O, P, H>,
plugins?: KeenSliderNativePlugin<O, P, H>[]
): KeenSliderNativeInstance<O, P, H>
}
export function useKeenSliderNative<
O = {},
P = {},
H extends string = KeenSliderNativeHooks
>(
options?: KeenSliderNativeOptions<O, P, H>,
plugins?: KeenSliderNativePlugin<O, P, H>[]
): KeenSliderNativeInstance<O, P, H> {
const optionsCheckedFirst = useRef(false)
const currentOptions = useRef(options)
const slider = useMemo<KeenSliderNativeInstance<O, P, H>>(
() => KeenSliderNative(options, plugins),
[]
)
useEffect(() => {
if (!optionsCheckedFirst.current) {
optionsCheckedFirst.current = true
return
}
if (slider) slider.update(currentOptions.current)
}, [checkOptions(currentOptions, options)])
return slider
}