diff options
author | dan <dan.abramov@gmail.com> | 2024-04-05 14:57:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-05 14:57:53 +0100 |
commit | 49266c355ea781cbd7a0b373e64143da7740c91e (patch) | |
tree | 5f0a39882c30d2b8a8c5d8f952923c4a6fc3c607 /src/components/forms/ToggleButton.tsx | |
parent | 9b087b721dee0a6a33bb85ac5098989ab965d658 (diff) | |
download | voidsky-49266c355ea781cbd7a0b373e64143da7740c91e.tar.zst |
Remove special cases for some buttons with text (#3412)
* Toggle.Button -> Toggle.ButtonWithText * Simplify Prompt.Cancel/Action * Move lines down for better diff * Remove ButtonWithText * Simplify types
Diffstat (limited to 'src/components/forms/ToggleButton.tsx')
-rw-r--r-- | src/components/forms/ToggleButton.tsx | 124 |
1 files changed, 67 insertions, 57 deletions
diff --git a/src/components/forms/ToggleButton.tsx b/src/components/forms/ToggleButton.tsx index 9cdaaaa9d..752842638 100644 --- a/src/components/forms/ToggleButton.tsx +++ b/src/components/forms/ToggleButton.tsx @@ -1,16 +1,15 @@ import React from 'react' -import {View, AccessibilityProps, TextStyle, ViewStyle} from 'react-native' - -import {atoms as a, useTheme, native} from '#/alf' -import {Text} from '#/components/Typography' +import {AccessibilityProps, TextStyle, View, ViewStyle} from 'react-native' +import {atoms as a, native, useTheme} from '#/alf' import * as Toggle from '#/components/forms/Toggle' +import {Text} from '#/components/Typography' -export type ItemProps = Omit<Toggle.ItemProps, 'style' | 'role' | 'children'> & - AccessibilityProps & - React.PropsWithChildren<{ +type ItemProps = Omit<Toggle.ItemProps, 'style' | 'role' | 'children'> & + AccessibilityProps & { + children: React.ReactElement testID?: string - }> + } export type GroupProps = Omit<Toggle.GroupProps, 'style' | 'type'> & { multiple?: boolean @@ -47,49 +46,42 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) { const t = useTheme() const state = Toggle.useItemContext() - const {baseStyles, hoverStyles, activeStyles, textStyles} = - React.useMemo(() => { - const base: ViewStyle[] = [] - const hover: ViewStyle[] = [] - const active: ViewStyle[] = [] - const text: TextStyle[] = [] + const {baseStyles, hoverStyles, activeStyles} = React.useMemo(() => { + const base: ViewStyle[] = [] + const hover: ViewStyle[] = [] + const active: ViewStyle[] = [] - hover.push( - t.name === 'light' ? t.atoms.bg_contrast_100 : t.atoms.bg_contrast_25, - ) + hover.push( + t.name === 'light' ? t.atoms.bg_contrast_100 : t.atoms.bg_contrast_25, + ) - if (state.selected) { - active.push({ - backgroundColor: t.palette.contrast_800, - }) - text.push(t.atoms.text_inverted) - hover.push({ - backgroundColor: t.palette.contrast_800, - }) - - if (state.disabled) { - active.push({ - backgroundColor: t.palette.contrast_500, - }) - } - } + if (state.selected) { + active.push({ + backgroundColor: t.palette.contrast_800, + }) + hover.push({ + backgroundColor: t.palette.contrast_800, + }) if (state.disabled) { - base.push({ - backgroundColor: t.palette.contrast_100, - }) - text.push({ - opacity: 0.5, + active.push({ + backgroundColor: t.palette.contrast_500, }) } + } - return { - baseStyles: base, - hoverStyles: hover, - activeStyles: active, - textStyles: text, - } - }, [t, state]) + if (state.disabled) { + base.push({ + backgroundColor: t.palette.contrast_100, + }) + } + + return { + baseStyles: base, + hoverStyles: hover, + activeStyles: active, + } + }, [t, state]) return ( <View @@ -110,19 +102,37 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) { activeStyles, (state.hovered || state.pressed) && hoverStyles, ]}> - {typeof children === 'string' ? ( - <Text - style={[ - a.text_center, - a.font_bold, - t.atoms.text_contrast_medium, - textStyles, - ]}> - {children} - </Text> - ) : ( - children - )} + {children} </View> ) } + +export function ButtonText({children}: {children: React.ReactNode}) { + const t = useTheme() + const state = Toggle.useItemContext() + + const textStyles = React.useMemo(() => { + const text: TextStyle[] = [] + if (state.selected) { + text.push(t.atoms.text_inverted) + } + if (state.disabled) { + text.push({ + opacity: 0.5, + }) + } + return text + }, [t, state]) + + return ( + <Text + style={[ + a.text_center, + a.font_bold, + t.atoms.text_contrast_medium, + textStyles, + ]}> + {children} + </Text> + ) +} |