@tamagui/button 1.0.1-beta.57 → 1.0.1-beta.61

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/button",
3
- "version": "1.0.1-beta.57",
3
+ "version": "1.0.1-beta.61",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -8,6 +8,7 @@
8
8
  "module": "dist/esm",
9
9
  "module:jsx": "dist/jsx",
10
10
  "files": [
11
+ "src",
11
12
  "types",
12
13
  "dist"
13
14
  ],
@@ -16,16 +17,16 @@
16
17
  "watch": "tamagui-build --watch"
17
18
  },
18
19
  "dependencies": {
19
- "@tamagui/core": "^1.0.1-beta.57",
20
- "@tamagui/font-size": "^1.0.1-beta.57",
21
- "@tamagui/helpers-tamagui": "^1.0.1-beta.57"
20
+ "@tamagui/core": "^1.0.1-beta.61",
21
+ "@tamagui/font-size": "^1.0.1-beta.61",
22
+ "@tamagui/helpers-tamagui": "^1.0.1-beta.61"
22
23
  },
23
24
  "peerDependencies": {
24
25
  "react": "*",
25
26
  "react-dom": "*"
26
27
  },
27
28
  "devDependencies": {
28
- "@tamagui/build": "^1.0.1-beta.57",
29
+ "@tamagui/build": "^1.0.1-beta.61",
29
30
  "react": "*",
30
31
  "react-dom": "*"
31
32
  },
package/src/Button.tsx ADDED
@@ -0,0 +1,186 @@
1
+ import {
2
+ ButtonInsideButtonContext,
3
+ GetProps,
4
+ ReactComponentWithRef,
5
+ ThemeableProps,
6
+ getButtonSize,
7
+ getVariableValue,
8
+ spacedChildren,
9
+ styled,
10
+ themeable,
11
+ } from '@tamagui/core'
12
+ import { getFontSize } from '@tamagui/font-size'
13
+ import {
14
+ TextParentStyles,
15
+ useGetThemedIcon,
16
+ wrapStringChildrenInText,
17
+ } from '@tamagui/helpers-tamagui'
18
+ import { ThemeableStack } from '@tamagui/stacks'
19
+ import { SizableText } from '@tamagui/text'
20
+ import React, { FunctionComponent, forwardRef, useContext } from 'react'
21
+ import { View } from 'react-native'
22
+
23
+ // bugfix esbuild strips react jsx: 'preserve'
24
+ React['createElement']
25
+
26
+ type ButtonIconProps = { color?: string; size?: number }
27
+ type IconProp = JSX.Element | FunctionComponent<ButtonIconProps> | null
28
+
29
+ export type ButtonProps = Omit<TextParentStyles, 'TextComponent'> &
30
+ GetProps<typeof ButtonFrame> &
31
+ ThemeableProps & {
32
+ // add icon before, passes color and size automatically if Component
33
+ icon?: IconProp
34
+ // add icon after, passes color and size automatically if Component
35
+ iconAfter?: IconProp
36
+ // adjust icon relative to size
37
+ // default: -1
38
+ scaleIcon?: number
39
+ // dont wrap inner contents in a text element
40
+ noTextWrap?: boolean
41
+ // make the spacing elements flex
42
+ spaceFlex?: number | boolean
43
+ // adjust internal space relative to icon size
44
+ scaleSpace?: number
45
+ }
46
+
47
+ const ButtonFrame = styled(ThemeableStack, {
48
+ name: 'Button',
49
+ tag: 'button',
50
+ hoverable: true,
51
+ pressable: true,
52
+ backgrounded: true,
53
+ borderWidth: 1,
54
+ borderColor: 'transparent',
55
+ justifyContent: 'center',
56
+ alignItems: 'center',
57
+ flexWrap: 'nowrap',
58
+ flexDirection: 'row',
59
+
60
+ // if we wanted this only when pressable = true, we'd need to merge variants?
61
+ cursor: 'pointer',
62
+
63
+ pressStyle: {
64
+ borderColor: 'transparent',
65
+ },
66
+
67
+ hoverStyle: {
68
+ borderColor: 'transparent',
69
+ },
70
+
71
+ focusStyle: {
72
+ borderColor: '$borderColorFocus',
73
+ },
74
+
75
+ variants: {
76
+ size: {
77
+ '...size': getButtonSize,
78
+ },
79
+
80
+ active: {
81
+ true: {
82
+ hoverStyle: {
83
+ backgroundColor: '$background',
84
+ },
85
+ },
86
+ },
87
+
88
+ disabled: {
89
+ true: {
90
+ opacity: 0.5,
91
+ pointerEvents: 'none',
92
+ },
93
+ },
94
+
95
+ // TODO see core/styled.ts bug
96
+ } as const,
97
+
98
+ defaultVariants: {
99
+ size: '$4',
100
+ },
101
+ })
102
+
103
+ // see TODO breaking types
104
+ // type x = GetProps<typeof ButtonFrame>
105
+ // type y = x['size']
106
+
107
+ export const ButtonText = styled(SizableText, {
108
+ color: '$color',
109
+ selectable: false,
110
+ // flexGrow 1 leads to inconsistent native style where text pushes to start of view
111
+ flexGrow: 0,
112
+ flexShrink: 1,
113
+ ellipse: true,
114
+ })
115
+
116
+ const ButtonComponent = forwardRef((props: ButtonProps, ref) => {
117
+ // careful not to desctructure and re-order props, order is important
118
+ const {
119
+ children,
120
+ icon,
121
+ iconAfter,
122
+ noTextWrap,
123
+ theme: themeName,
124
+ space,
125
+ spaceFlex,
126
+ scaleIcon = 1,
127
+ scaleSpace = 0.66,
128
+ separator,
129
+
130
+ // text props
131
+ color,
132
+ fontWeight,
133
+ letterSpacing,
134
+ fontSize,
135
+ fontFamily,
136
+ textAlign,
137
+ textProps,
138
+ ...rest
139
+ } = props as ButtonProps
140
+
141
+ const isInsideButton = useContext(ButtonInsideButtonContext)
142
+ const size = props.size || '$4'
143
+ const iconSize = getFontSize(size) * scaleIcon
144
+ const getThemedIcon = useGetThemedIcon({ size: iconSize, color })
145
+ const [themedIcon, themedIconAfter] = [icon, iconAfter].map(getThemedIcon)
146
+ const spaceSize = getVariableValue(iconSize) * scaleSpace
147
+ const contents = wrapStringChildrenInText(ButtonText, props)
148
+
149
+ return (
150
+ <ButtonFrame
151
+ fontFamily={fontFamily}
152
+ // fixes SSR issue + DOM nesting issue of not allowing button in button
153
+ {...(isInsideButton && {
154
+ tag: 'span',
155
+ })}
156
+ ref={ref as any}
157
+ {...rest}
158
+ >
159
+ <ButtonInsideButtonContext.Provider value={true}>
160
+ {themedIcon || themedIconAfter
161
+ ? spacedChildren({
162
+ // a bit arbitrary but scaling to font size is necessary so long as button does
163
+ space: spaceSize,
164
+ spaceFlex,
165
+ separator,
166
+ direction: props.flexDirection || 'row',
167
+ children: [themedIcon, contents, themedIconAfter],
168
+ })
169
+ : contents}
170
+ </ButtonInsideButtonContext.Provider>
171
+ </ButtonFrame>
172
+ )
173
+ })
174
+
175
+ export const Button: ReactComponentWithRef<ButtonProps, HTMLButtonElement | View> =
176
+ ButtonFrame.extractable(themeable(ButtonComponent as any) as any, {
177
+ inlineProps: new Set([
178
+ // text props go here (can't really optimize them, but we never fully extract button anyway)
179
+ 'color',
180
+ 'fontWeight',
181
+ 'fontSize',
182
+ 'fontFamily',
183
+ 'letterSpacing',
184
+ 'textAlign',
185
+ ]),
186
+ })
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Button'