react-native-varia 0.3.0 → 0.4.1

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 (45) hide show
  1. package/lib/components/Accordion.tsx +61 -21
  2. package/lib/components/Badge.tsx +9 -0
  3. package/lib/components/Button.tsx +19 -8
  4. package/lib/components/Checkbox.tsx +21 -12
  5. package/lib/components/CircleProgress.tsx +8 -0
  6. package/lib/components/Divider.tsx +6 -0
  7. package/lib/components/Drawer.tsx +16 -4
  8. package/lib/components/Field.tsx +4 -0
  9. package/lib/components/FloatingAction.tsx +215 -0
  10. package/lib/components/GradientBackground.tsx +3 -0
  11. package/lib/components/GradientText.tsx +27 -14
  12. package/lib/components/IconWrapper.tsx +4 -3
  13. package/lib/components/Input.tsx +47 -21
  14. package/lib/components/Link.tsx +4 -0
  15. package/lib/components/Modal.tsx +18 -5
  16. package/lib/components/NumberInput.tsx +27 -5
  17. package/lib/components/RadioGroup.tsx +16 -5
  18. package/lib/components/ReText.tsx +4 -1
  19. package/lib/components/Select.tsx +20 -0
  20. package/lib/components/Slider.tsx +59 -23
  21. package/lib/components/Slideshow.tsx +19 -3
  22. package/lib/components/Spinner.tsx +9 -3
  23. package/lib/components/Switch.tsx +57 -26
  24. package/lib/components/Text.tsx +3 -0
  25. package/lib/components/Toast.tsx +110 -36
  26. package/lib/patterns/index.tsx +299 -90
  27. package/lib/theme/Accordion.tsx +184 -0
  28. package/lib/theme/Button.recipe.tsx +24 -7
  29. package/lib/theme/Drawer.recipe.tsx +2 -4
  30. package/lib/theme/Field.recipe.tsx +45 -15
  31. package/lib/theme/FloatingAction.tsx +112 -0
  32. package/lib/theme/GradientText.recipe.tsx +103 -34
  33. package/lib/theme/Input.recipe.tsx +14 -6
  34. package/lib/theme/Select.recipe.tsx +3 -0
  35. package/lib/theme/Slider.recipe.tsx +86 -150
  36. package/lib/theme/Spinner.recipe.tsx +4 -0
  37. package/lib/theme/Switch.recipe.tsx +19 -0
  38. package/lib/theme/Text.recipe.tsx +63 -12
  39. package/lib/theme/Toast.recipe.tsx +40 -7
  40. package/lib/varia/types.ts +3 -0
  41. package/lib/varia/utils.ts +110 -18
  42. package/package.json +1 -1
  43. package/lib/components/OldSlider.tsx +0 -327
  44. package/lib/components/SlidingDrawer.tsx +0 -301
  45. package/lib/patterns/newPatterns.tsx +0 -285
@@ -1,301 +0,0 @@
1
- import React, {useImperativeHandle} from 'react'
2
- import {StyleSheet, UnistylesVariants} from 'react-native-unistyles'
3
- import {Gesture, GestureDetector} from 'react-native-gesture-handler'
4
- import Animated, {
5
- useAnimatedStyle,
6
- useSharedValue,
7
- withSpring,
8
- withTiming,
9
- type SharedValue,
10
- } from 'react-native-reanimated'
11
- import {scheduleOnRN} from 'react-native-worklets'
12
- import {
13
- SlidingDrawerTokens,
14
- SlidingDrawerStyles,
15
- } from '../theme/SlidingDrawer.recipe'
16
- import {PalettesWithNestedKeys} from '../style/varia/types'
17
- import {TouchableWithoutFeedback, ViewStyle} from 'react-native'
18
-
19
- export type SlidingDrawerRef = {
20
- snapTo: (point: number) => void | null
21
- expand: () => void | null
22
- collapse: () => void | null
23
- }
24
-
25
- type SlidingDrawerVariants = UnistylesVariants<typeof SlidingDrawerStyles>
26
-
27
- type SlidingDrawerProps = SlidingDrawerVariants & {
28
- colorPalette?: PalettesWithNestedKeys
29
- animation?: keyof typeof SlidingDrawerTokens.variants.animation
30
- width?: string
31
- flex?: number
32
- direction?: 1 | -1
33
- onCollapse?: () => void
34
- onExpand?: () => void
35
- onSnap?: (point: number) => void
36
- snapPoints: number[]
37
- axis: 'y' | 'x'
38
- allowGestures?: boolean
39
- overlay?: boolean
40
- closeOnOverlayPress?: boolean
41
- children?: React.ReactNode
42
- ref?: React.RefObject<SlidingDrawerRef | null>
43
- }
44
-
45
- const AnimatedTouchableOpacity = Animated.createAnimatedComponent(
46
- TouchableWithoutFeedback,
47
- )
48
-
49
- const SlidingDrawer = ({
50
- colorPalette = 'accent',
51
- variant = SlidingDrawerTokens.defaultProps.variant,
52
- animation = SlidingDrawerTokens.defaultProps.animation,
53
- width = '100%',
54
- flex = 0,
55
- direction = 1,
56
- onCollapse,
57
- onExpand,
58
- onSnap,
59
- snapPoints,
60
- axis,
61
- allowGestures = true,
62
- overlay = false,
63
- closeOnOverlayPress = true,
64
- children,
65
- ref,
66
- }: SlidingDrawerProps) => {
67
- SlidingDrawerStyles.useVariants({
68
- variant,
69
- })
70
-
71
- const points = snapPoints.map(point => point * direction)
72
-
73
- const translate = useSharedValue(points[0])
74
-
75
- const context: SharedValue<{
76
- position: number
77
- snapPoint: number
78
- }> = useSharedValue({
79
- position: points[0],
80
- snapPoint: 0,
81
- })
82
-
83
- const changeDisplay = useSharedValue('none')
84
- const opacity = useSharedValue(0)
85
-
86
- const VELOCITY_THRESHOLD = 2000
87
-
88
- const updateCurrentSnapPoint = (snapPoint: number) => {
89
- 'worklet'
90
- context.value = {position: context.value.position, snapPoint}
91
- }
92
-
93
- const animations = {
94
- withSpring,
95
- withTiming,
96
- }
97
-
98
- const animationVariant = SlidingDrawerTokens.variants.animation[animation]
99
-
100
- const snapTo = (destination: number) => {
101
- 'worklet'
102
-
103
- if (animationVariant && animationVariant.type) {
104
- translate.value = animations[animationVariant.type](
105
- points[destination],
106
- animationVariant.props,
107
- )
108
- updateCurrentSnapPoint(destination)
109
- onSnap && scheduleOnRN(onSnap, destination)
110
- }
111
- }
112
-
113
- const expand = () => {
114
- snapTo(points.length - 1)
115
- if (overlay) {
116
- showOverlay()
117
- }
118
- onExpand && scheduleOnRN(onExpand)
119
- }
120
-
121
- const isCollapsed = () => {
122
- 'worklet'
123
- return context.value.snapPoint === 0
124
- }
125
-
126
- const collapse = () => {
127
- snapTo(0)
128
- if (overlay) {
129
- hideOverlay()
130
- }
131
- onCollapse && scheduleOnRN(onCollapse)
132
- }
133
-
134
- const hideOverlay = () => {
135
- 'worklet'
136
- changeDisplay.value = 'none'
137
- opacity.value = withTiming(0, {duration: 200})
138
- }
139
- const showOverlay = () => {
140
- 'worklet'
141
- changeDisplay.value = 'flex'
142
- opacity.value = withTiming(1, {duration: 200})
143
- }
144
-
145
- useImperativeHandle(ref, () => ({
146
- expand,
147
- collapse,
148
- snapTo,
149
- isCollapsed,
150
- }))
151
-
152
- const slideGesture = Gesture.Pan()
153
- .enabled(allowGestures)
154
- .onBegin(() => {
155
- context.value.position = translate.value
156
- })
157
- .onUpdate(event => {
158
- const delta = axis === 'y' ? event.translationY : event.translationX
159
-
160
- const proposed = delta + (context.value.position ?? 0)
161
-
162
- const minPoint = Math.min(...points)
163
- const maxPoint = Math.max(...points)
164
-
165
- let clamped = proposed
166
-
167
- if (proposed < minPoint) {
168
- const overdrag = minPoint - proposed
169
- clamped = minPoint - overdrag / (1 + overdrag / 60)
170
- } else if (proposed > maxPoint) {
171
- const overdrag = proposed - maxPoint
172
- clamped = maxPoint + overdrag / (1 + overdrag / 60)
173
- }
174
-
175
- translate.value = clamped
176
- })
177
- .onEnd(({velocityX, velocityY, translationX, translationY}) => {
178
- const velocity = axis === 'y' ? velocityY : velocityX
179
- const translation = axis === 'y' ? translationY : translationX
180
-
181
- const minPoint = Math.min(...points)
182
- const maxPoint = Math.max(...points)
183
-
184
- if (translate.value < minPoint) {
185
- translate.value = withSpring(minPoint, {velocity})
186
- return
187
- } else if (translate.value > maxPoint) {
188
- translate.value = withSpring(maxPoint, {velocity})
189
- return
190
- }
191
-
192
- const forwardsThreshold =
193
- (points[context.value.snapPoint] +
194
- points[context.value.snapPoint + 1]) /
195
- 2
196
- const backwardsThreshold =
197
- (points[context.value.snapPoint] +
198
- points[context.value.snapPoint - 1]) /
199
- 2
200
-
201
- if (translation * direction < 0) {
202
- if (
203
- ((direction === 1 && translate.value < forwardsThreshold) ||
204
- (direction === -1 && translate.value > forwardsThreshold) ||
205
- velocity * direction < -VELOCITY_THRESHOLD) &&
206
- context.value.snapPoint < points.length - 1
207
- ) {
208
- snapTo(context.value.snapPoint + 1)
209
- } else {
210
- snapTo(context.value.snapPoint)
211
- }
212
- } else {
213
- if (
214
- ((direction === 1 && translate.value > backwardsThreshold) ||
215
- (direction === -1 && translate.value < backwardsThreshold) ||
216
- velocity * direction > VELOCITY_THRESHOLD) &&
217
- context.value.snapPoint > 0
218
- ) {
219
- snapTo(context.value.snapPoint - 1)
220
- } else {
221
- snapTo(context.value.snapPoint)
222
- }
223
- }
224
- })
225
- .onFinalize(() => {
226
- if (overlay) {
227
- if (isCollapsed()) {
228
- hideOverlay()
229
- } else {
230
- showOverlay()
231
- }
232
- }
233
- })
234
-
235
- const blockAnimatedStyle = useAnimatedStyle(() => {
236
- if (axis === 'y') {
237
- return {
238
- transform: [{translateY: translate.value}],
239
- }
240
- } else {
241
- return {
242
- transform: [{translateX: translate.value}],
243
- }
244
- }
245
- })
246
-
247
- const displayOverlayStyle = useAnimatedStyle(() => {
248
- return {
249
- display: changeDisplay.value as ViewStyle['display'],
250
- opacity: opacity.value,
251
- }
252
- })
253
-
254
- return (
255
- <>
256
- {overlay && (
257
- <AnimatedTouchableOpacity
258
- onPress={() => closeOnOverlayPress && collapse()}>
259
- <Animated.View
260
- style={[
261
- StyleSheet.absoluteFillObject,
262
- displayOverlayStyle,
263
- SlidingDrawerStyles.overlay(colorPalette),
264
- ]}
265
- />
266
- </AnimatedTouchableOpacity>
267
- )}
268
- <GestureDetector gesture={slideGesture}>
269
- <Animated.View
270
- style={[
271
- styles.container(flex, direction, axis, width),
272
- blockAnimatedStyle,
273
- SlidingDrawerStyles.slider(colorPalette),
274
- ]}>
275
- {children}
276
- </Animated.View>
277
- </GestureDetector>
278
- </>
279
- )
280
- }
281
- export default SlidingDrawer
282
-
283
- const styles = StyleSheet.create({
284
- container: (flex, direction, axis, width) => ({
285
- position: flex === 0 ? 'absolute' : 'relative',
286
- bottom: axis === 'y' && direction === -1 ? 0 : 'auto',
287
- top: axis === 'y' && direction === 1 ? 0 : 'auto',
288
- left: axis === 'x' ? 0 : 'auto',
289
- flex: flex === 0 ? 1 : flex,
290
- height: '100%',
291
- width,
292
- flexDirection: 'column',
293
- alignSelf: 'stretch',
294
- alignItems: 'center',
295
- justifyContent: 'flex-start',
296
- zIndex: 100,
297
- }),
298
- customStyle: style => ({
299
- ...style,
300
- }),
301
- })
@@ -1,285 +0,0 @@
1
- import {useMemo} from 'react'
2
- import {StyleProp, View, ViewStyle} from 'react-native'
3
- import {StyleSheet} from 'react-native-unistyles'
4
-
5
- interface StackProps {
6
- backgroundColor?: ViewStyle['backgroundColor']
7
- bg?: ViewStyle['backgroundColor']
8
- width?: ViewStyle['width']
9
- w?: ViewStyle['width']
10
- height?: ViewStyle['height']
11
- h?: ViewStyle['height']
12
- borderColor?: ViewStyle['borderColor']
13
- borderWidth?: ViewStyle['borderWidth']
14
- borderStyle?: ViewStyle['borderStyle']
15
- borderRadius?: ViewStyle['borderRadius']
16
- br?: ViewStyle['borderRadius']
17
- alignSelf?: ViewStyle['alignSelf']
18
- alignItems?: ViewStyle['alignItems']
19
- justifyContent?: ViewStyle['justifyContent']
20
- flex?: ViewStyle['flex']
21
- flexShrink?: ViewStyle['flexShrink']
22
- flexGrow?: ViewStyle['flexGrow']
23
- flexBasis?: ViewStyle['flexBasis']
24
- flexWrap?: ViewStyle['flexWrap']
25
- gap?: ViewStyle['gap']
26
- m?: ViewStyle['margin']
27
- mt?: ViewStyle['marginTop']
28
- mb?: ViewStyle['marginBottom']
29
- ml?: ViewStyle['marginLeft']
30
- mr?: ViewStyle['marginRight']
31
- mx?: ViewStyle['marginHorizontal']
32
- my?: ViewStyle['marginVertical']
33
- margin?: ViewStyle['margin']
34
- marginVertical?: ViewStyle['marginVertical']
35
- marginHorizontal?: ViewStyle['marginHorizontal']
36
- marginTop?: ViewStyle['marginTop']
37
- marginBottom?: ViewStyle['marginBottom']
38
- marginLeft?: ViewStyle['marginLeft']
39
- marginRight?: ViewStyle['marginRight']
40
- p?: ViewStyle['padding']
41
- pt?: ViewStyle['paddingVertical']
42
- pb?: ViewStyle['paddingVertical']
43
- pl?: ViewStyle['paddingLeft']
44
- pr?: ViewStyle['paddingRight']
45
- px?: ViewStyle['paddingHorizontal']
46
- py?: ViewStyle['paddingVertical']
47
- padding?: ViewStyle['padding']
48
- paddingVertical?: ViewStyle['paddingVertical']
49
- paddingHorizontal?: ViewStyle['paddingHorizontal']
50
- paddingTop?: ViewStyle['paddingTop']
51
- paddingBottom?: ViewStyle['paddingBottom']
52
- paddingLeft?: ViewStyle['paddingLeft']
53
- paddingRight?: ViewStyle['paddingRight']
54
- children?: React.ReactNode
55
- // style?: ViewStyle
56
- style?: StyleProp<ViewStyle>
57
- align?: 'center' | 'vCenter' | 'hCenter'
58
- }
59
-
60
- function mapProps(props: StackProps) {
61
- const stylesArr: any[] = []
62
-
63
- Object.entries(props).forEach(([key, value]) => {
64
- if (value == null) return
65
-
66
- const fn = (stackStyles as any)[key]
67
- if (typeof fn === 'function') {
68
- stylesArr.push(fn(value))
69
- }
70
- })
71
-
72
- return stylesArr
73
- }
74
-
75
- const VStack = ({children, style, align, ...props}: StackProps) => {
76
- stackStyles.useVariants({align})
77
- const styleArr = useMemo(() => mapProps(props), [props])
78
-
79
- return (
80
- <View style={[stackStyles.stack, stackStyles.vstack, styleArr, style]}>
81
- {children}
82
- </View>
83
- )
84
- }
85
-
86
- const HStack = ({children, style, align, ...props}: StackProps) => {
87
- stackStyles.useVariants({align})
88
- const styleArr = useMemo(() => mapProps(props), [props])
89
-
90
- return (
91
- <View style={[stackStyles.stack, stackStyles.hstack, styleArr, style]}>
92
- {children}
93
- </View>
94
- )
95
- }
96
-
97
- const stackStyles = StyleSheet.create({
98
- stack: {},
99
- vstack: {
100
- flexDirection: 'column',
101
- variants: {
102
- align: {
103
- center: {
104
- alignItems: 'center',
105
- justifyContent: 'center',
106
- },
107
- vCenter: {
108
- justifyContent: 'center',
109
- },
110
- hCenter: {
111
- alignItems: 'center',
112
- },
113
- },
114
- },
115
- },
116
- hstack: {
117
- flexDirection: 'row',
118
- variants: {
119
- align: {
120
- center: {
121
- alignItems: 'center',
122
- justifyContent: 'center',
123
- },
124
- vCenter: {
125
- alignItems: 'center',
126
- },
127
- hCenter: {
128
- justifyContent: 'center',
129
- },
130
- },
131
- },
132
- },
133
-
134
- backgroundColor: value => ({
135
- backgroundColor: value,
136
- }),
137
- bg: value => ({
138
- backgroundColor: value,
139
- }),
140
-
141
- width: value => ({
142
- width: value,
143
- }),
144
- w: value => ({
145
- width: value,
146
- }),
147
- height: value => ({
148
- height: value,
149
- }),
150
- h: value => ({
151
- height: value,
152
- }),
153
-
154
- borderColor: value => ({
155
- borderColor: value,
156
- }),
157
- borderWidth: value => ({
158
- borderWidth: value,
159
- }),
160
- borderStyle: value => ({
161
- borderStyle: value,
162
- }),
163
- borderRadius: value => ({
164
- borderRadius: value,
165
- }),
166
- br: value => ({
167
- borderRadius: value,
168
- }),
169
-
170
- flex: value => ({
171
- flex: value,
172
- }),
173
- flexShrink: value => ({
174
- flexShrink: value,
175
- }),
176
- flexGrow: value => ({
177
- flexGrow: value,
178
- }),
179
- flexBasis: value => ({
180
- flexBasis: value,
181
- }),
182
- flexWrap: value => ({
183
- flexWrap: value,
184
- }),
185
- alignSelf: value => ({
186
- alignSelf: value,
187
- }),
188
- alignItems: value => ({
189
- alignItems: value,
190
- }),
191
- justifyContent: value => ({
192
- justifyContent: value,
193
- }),
194
- gap: value => ({
195
- gap: value,
196
- }),
197
-
198
- m: value => ({
199
- margin: value,
200
- }),
201
- mt: value => ({
202
- marginTop: value,
203
- }),
204
- mb: value => ({
205
- marginBottom: value,
206
- }),
207
- ml: value => ({
208
- marginLeft: value,
209
- }),
210
- mr: value => ({
211
- marginRight: value,
212
- }),
213
- mx: value => ({
214
- marginHorizontal: value,
215
- }),
216
- my: value => ({
217
- marginVertical: value,
218
- }),
219
- margin: value => ({
220
- margin: value,
221
- }),
222
- marginVertical: value => ({
223
- marginVertical: value,
224
- }),
225
- marginHorizontal: value => ({
226
- marginHorizontal: value,
227
- }),
228
- marginLeft: value => ({
229
- marginLeft: value,
230
- }),
231
- marginRight: value => ({
232
- marginRight: value,
233
- }),
234
- marginTop: value => ({
235
- marginTop: value,
236
- }),
237
- marginBottom: value => ({
238
- marginBottom: value,
239
- }),
240
-
241
- p: value => ({
242
- padding: value,
243
- }),
244
- pt: value => ({
245
- paddingTop: value,
246
- }),
247
- pb: value => ({
248
- paddingBottom: value,
249
- }),
250
- pl: value => ({
251
- paddingLeft: value,
252
- }),
253
- pr: value => ({
254
- paddingRight: value,
255
- }),
256
- px: value => ({
257
- paddingHorizontal: value,
258
- }),
259
- py: value => ({
260
- paddingVertical: value,
261
- }),
262
- padding: value => ({
263
- padding: value,
264
- }),
265
- paddingHorizontal: value => ({
266
- paddingHorizontal: value,
267
- }),
268
- paddingVertical: value => ({
269
- paddingVertical: value,
270
- }),
271
- paddingLeft: value => ({
272
- paddingLeft: value,
273
- }),
274
- paddingRight: value => ({
275
- paddingRight: value,
276
- }),
277
- paddingTop: value => ({
278
- paddingTop: value,
279
- }),
280
- paddingBottom: value => ({
281
- paddingBottom: value,
282
- }),
283
- })
284
-
285
- export {VStack, HStack}