-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPressable.tsx
More file actions
183 lines (170 loc) · 4.06 KB
/
Copy pathPressable.tsx
File metadata and controls
183 lines (170 loc) · 4.06 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React from "react"
import { useTheme } from "tamagui"
import { usePointerEvents } from "../../hooks/usePointerEvents"
import { useShadow as getShadow } from "../../hooks/useShadow"
import { MotionView, ViewContext, type ViewProps } from "../View"
import type { ColorValue } from "../../utils/color"
import type { OnPressWithRef } from "../../utils/types"
export type PressableState = {
isHovered: boolean
isActive: boolean
}
export type ParadigmPressableProps = ViewProps & {
/**
* what happens when the element is pressed?
*
* This is `OnPressWithRef` so its possible to find the element when needed (popups, etc.)
*/
onPress?: OnPressWithRef
/**
* Called whenever hover or active state changes.
*/
onStateChange?: (state: PressableState) => void
/**
* is this element important? Should only be 1 per page.
*/
isPrimary?: boolean
/**
* Does this element have a negative effect (delete, etc.)
*/
isNegative?: boolean
/**
* Is this element floating?
*/
isRaised?: boolean
/**
* is this element disabled?
* This will make `onPress` not work even if defined
*/
isDisabled?: boolean
/**
* Is this element currently in a loading state.
* This will disable `onPress` even if defined
*/
isLoading?: boolean
}
const { all: e1 } = getShadow({
shadowName: "elevation1",
forceBoxShadow: true,
})
const { all: e2 } = getShadow({
shadowName: "elevation2",
forceBoxShadow: true,
})
export const Pressable = ({
children,
onPress,
onStateChange,
isPrimary,
isNegative,
isRaised,
isDisabled: isMarkedDisabled = false,
isLoading,
...otherProps
}: ParadigmPressableProps) => {
const theme = useTheme()
const { color: parentColor } = React.useContext(ViewContext)
const { isHovered, isActive, pointerProps } = usePointerEvents()
React.useEffect(() => {
onStateChange?.({ isHovered, isActive })
}, [isHovered, isActive, onStateChange])
const pressableRef = React.useRef(null)
let backgroundColor: ColorValue
// the button is disabled both when loading and when actively marked disabled
const isDisabled = isMarkedDisabled || isLoading
const buttonState = isDisabled
? isLoading
? "loading"
: "disabled"
: isHovered
? isActive
? "hoverActive"
: "hover"
: isActive
? "active"
: "normal"
// Background Color
const raisedColor = parentColor
? parentColor.toString() !== "$background"
? theme.background
: theme.cardStock
: theme.cardStock
switch (buttonState) {
case "loading":
case "disabled": {
backgroundColor = theme.cardStock
break
}
case "hover": {
backgroundColor = isPrimary
? isNegative
? theme.negativeHover
: theme.primaryHover
: isRaised
? raisedColor
: theme.normalHover
break
}
case "active":
case "hoverActive": {
backgroundColor = isPrimary
? isNegative
? theme.negativeActive
: theme.primaryActive
: isRaised
? raisedColor
: theme.normalActive
break
}
default: {
// normal
if (isPrimary) {
if (isNegative) {
backgroundColor = theme.destructive
} else {
backgroundColor = theme.primary
}
} else {
if (isRaised) {
backgroundColor = raisedColor
} else {
backgroundColor = theme.cardStock
}
}
}
}
return (
<MotionView
ref={pressableRef}
center
noShrink
color={backgroundColor}
_tamaguiProps={{ flexDirection: "row" }}
{...otherProps}
{...pointerProps}
disabled={isDisabled ?? false}
cursor={isDisabled ? "not-allowed" : "pointer"}
variants={{
normal: { filter: "none" },
raised: { y: -1, filter: e1.filter ?? "none", boxShadow: e1.boxShadow },
hover: { y: -3, filter: e2.filter ?? "none", boxShadow: e2.boxShadow },
active: { y: 0, filter: e1.filter ?? "none", boxShadow: e1.boxShadow },
}}
animate={
isRaised && !isDisabled
? isHovered
? isActive
? "active"
: "hover"
: "raised"
: "normal"
}
transition={{ type: "spring", visualDuration: 0.35, bounce: 0.35 }}
onPress={(event) => {
onPress?.({ ref: pressableRef, event })
}}
>
{children}
</MotionView>
)
}