Convert button to use forwardRef (#4576)

zio/stable
Eric Bailey 2024-06-19 18:47:43 -05:00 committed by GitHub
parent 89d99a8701
commit 7d8fca56dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 343 additions and 324 deletions

View File

@ -88,7 +88,9 @@ export function useButtonContext() {
return React.useContext(Context) return React.useContext(Context)
} }
export function Button({ export const Button = React.forwardRef<View, ButtonProps>(
(
{
children, children,
variant, variant,
color, color,
@ -99,7 +101,9 @@ export function Button({
style, style,
hoverStyle: hoverStyleProp, hoverStyle: hoverStyleProp,
...rest ...rest
}: ButtonProps) { },
ref,
) => {
const t = useTheme() const t = useTheme()
const [state, setState] = React.useState({ const [state, setState] = React.useState({
pressed: false, pressed: false,
@ -179,7 +183,9 @@ export function Button({
}) })
} else { } else {
baseStyles.push(a.border, { baseStyles.push(a.border, {
borderColor: light ? t.palette.primary_200 : t.palette.primary_900, borderColor: light
? t.palette.primary_200
: t.palette.primary_900,
}) })
} }
} else if (variant === 'ghost') { } else if (variant === 'ghost') {
@ -278,9 +284,19 @@ export function Button({
if (shape === 'default') { if (shape === 'default') {
if (size === 'large') { if (size === 'large') {
baseStyles.push({paddingVertical: 15}, a.px_2xl, a.rounded_sm, a.gap_md) baseStyles.push(
{paddingVertical: 15},
a.px_2xl,
a.rounded_sm,
a.gap_md,
)
} else if (size === 'medium') { } else if (size === 'medium') {
baseStyles.push({paddingVertical: 12}, a.px_2xl, a.rounded_sm, a.gap_md) baseStyles.push(
{paddingVertical: 12},
a.px_2xl,
a.rounded_sm,
a.gap_md,
)
} else if (size === 'small') { } else if (size === 'small') {
baseStyles.push({paddingVertical: 9}, a.px_lg, a.rounded_sm, a.gap_sm) baseStyles.push({paddingVertical: 9}, a.px_lg, a.rounded_sm, a.gap_sm)
} else if (size === 'xsmall') { } else if (size === 'xsmall') {
@ -368,6 +384,7 @@ export function Button({
role="button" role="button"
accessibilityHint={undefined} // optional accessibilityHint={undefined} // optional
{...rest} {...rest}
ref={ref}
aria-label={label} aria-label={label}
aria-pressed={state.pressed} aria-pressed={state.pressed}
accessibilityLabel={label} accessibilityLabel={label}
@ -417,7 +434,9 @@ export function Button({
</Context.Provider> </Context.Provider>
</Pressable> </Pressable>
) )
} },
)
Button.displayName = 'Button'
export function useSharedButtonTextStyles() { export function useSharedButtonTextStyles() {
const t = useTheme() const t = useTheme()