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
zio/stable
dan 2024-04-05 14:57:53 +01:00 committed by GitHub
parent 9b087b721d
commit 49266c355e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 106 additions and 99 deletions

View File

@ -32,9 +32,6 @@ module.exports = {
'H5', 'H5',
'H6', 'H6',
'P', 'P',
'Prompt.Cancel', // TODO: Not always safe.
'Prompt.Action', // TODO: Not always safe.
'ToggleButton.Button', // TODO: Not always safe.
], ],
impliedTextProps: [], impliedTextProps: [],
}, },

View File

@ -91,15 +91,13 @@ export function Actions({children}: React.PropsWithChildren<{}>) {
} }
export function Cancel({ export function Cancel({
children,
cta, cta,
}: React.PropsWithChildren<{ }: {
/** /**
* Optional i18n string, used in lieu of `children` for simple buttons. If * Optional i18n string. If undefined, it will default to "Cancel".
* undefined (and `children` is undefined), it will default to "Cancel".
*/ */
cta?: string cta?: string
}>) { }) {
const {_} = useLingui() const {_} = useLingui()
const {gtMobile} = useBreakpoints() const {gtMobile} = useBreakpoints()
const {close} = Dialog.useDialogContext() const {close} = Dialog.useDialogContext()
@ -114,27 +112,25 @@ export function Cancel({
size={gtMobile ? 'small' : 'medium'} size={gtMobile ? 'small' : 'medium'}
label={cta || _(msg`Cancel`)} label={cta || _(msg`Cancel`)}
onPress={onPress}> onPress={onPress}>
{children ? children : <ButtonText>{cta || _(msg`Cancel`)}</ButtonText>} <ButtonText>{cta || _(msg`Cancel`)}</ButtonText>
</Button> </Button>
) )
} }
export function Action({ export function Action({
children,
onPress, onPress,
color = 'primary', color = 'primary',
cta, cta,
testID, testID,
}: React.PropsWithChildren<{ }: {
onPress: () => void onPress: () => void
color?: ButtonColor color?: ButtonColor
/** /**
* Optional i18n string, used in lieu of `children` for simple buttons. If * Optional i18n string. If undefined, it will default to "Confirm".
* undefined (and `children` is undefined), it will default to "Confirm".
*/ */
cta?: string cta?: string
testID?: string testID?: string
}>) { }) {
const {_} = useLingui() const {_} = useLingui()
const {gtMobile} = useBreakpoints() const {gtMobile} = useBreakpoints()
const {close} = Dialog.useDialogContext() const {close} = Dialog.useDialogContext()
@ -151,7 +147,7 @@ export function Action({
label={cta || _(msg`Confirm`)} label={cta || _(msg`Confirm`)}
onPress={handleOnPress} onPress={handleOnPress}
testID={testID}> testID={testID}>
{children ? children : <ButtonText>{cta || _(msg`Confirm`)}</ButtonText>} <ButtonText>{cta || _(msg`Confirm`)}</ButtonText>
</Button> </Button>
) )
} }

View File

@ -1,16 +1,15 @@
import React from 'react' import React from 'react'
import {View, AccessibilityProps, TextStyle, ViewStyle} from 'react-native' import {AccessibilityProps, TextStyle, View, ViewStyle} from 'react-native'
import {atoms as a, useTheme, native} from '#/alf' import {atoms as a, native, useTheme} from '#/alf'
import * as Toggle from '#/components/forms/Toggle'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
import * as Toggle from '#/components/forms/Toggle' type ItemProps = Omit<Toggle.ItemProps, 'style' | 'role' | 'children'> &
AccessibilityProps & {
export type ItemProps = Omit<Toggle.ItemProps, 'style' | 'role' | 'children'> & children: React.ReactElement
AccessibilityProps &
React.PropsWithChildren<{
testID?: string testID?: string
}> }
export type GroupProps = Omit<Toggle.GroupProps, 'style' | 'type'> & { export type GroupProps = Omit<Toggle.GroupProps, 'style' | 'type'> & {
multiple?: boolean multiple?: boolean
@ -47,12 +46,10 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) {
const t = useTheme() const t = useTheme()
const state = Toggle.useItemContext() const state = Toggle.useItemContext()
const {baseStyles, hoverStyles, activeStyles, textStyles} = const {baseStyles, hoverStyles, activeStyles} = React.useMemo(() => {
React.useMemo(() => {
const base: ViewStyle[] = [] const base: ViewStyle[] = []
const hover: ViewStyle[] = [] const hover: ViewStyle[] = []
const active: ViewStyle[] = [] const active: ViewStyle[] = []
const text: TextStyle[] = []
hover.push( hover.push(
t.name === 'light' ? t.atoms.bg_contrast_100 : t.atoms.bg_contrast_25, t.name === 'light' ? t.atoms.bg_contrast_100 : t.atoms.bg_contrast_25,
@ -62,7 +59,6 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) {
active.push({ active.push({
backgroundColor: t.palette.contrast_800, backgroundColor: t.palette.contrast_800,
}) })
text.push(t.atoms.text_inverted)
hover.push({ hover.push({
backgroundColor: t.palette.contrast_800, backgroundColor: t.palette.contrast_800,
}) })
@ -78,16 +74,12 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) {
base.push({ base.push({
backgroundColor: t.palette.contrast_100, backgroundColor: t.palette.contrast_100,
}) })
text.push({
opacity: 0.5,
})
} }
return { return {
baseStyles: base, baseStyles: base,
hoverStyles: hover, hoverStyles: hover,
activeStyles: active, activeStyles: active,
textStyles: text,
} }
}, [t, state]) }, [t, state])
@ -110,7 +102,29 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) {
activeStyles, activeStyles,
(state.hovered || state.pressed) && hoverStyles, (state.hovered || state.pressed) && hoverStyles,
]}> ]}>
{typeof children === 'string' ? ( {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 <Text
style={[ style={[
a.text_center, a.text_center,
@ -120,9 +134,5 @@ function ButtonInner({children}: React.PropsWithChildren<{}>) {
]}> ]}>
{children} {children}
</Text> </Text>
) : (
children
)}
</View>
) )
} }

View File

@ -84,17 +84,17 @@ export function Buttons({
onChange={onChange}> onChange={onChange}>
{ignoreLabel && ( {ignoreLabel && (
<ToggleButton.Button name="ignore" label={ignoreLabel}> <ToggleButton.Button name="ignore" label={ignoreLabel}>
{ignoreLabel} <ToggleButton.ButtonText>{ignoreLabel}</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
)} )}
{warnLabel && ( {warnLabel && (
<ToggleButton.Button name="warn" label={warnLabel}> <ToggleButton.Button name="warn" label={warnLabel}>
{warnLabel} <ToggleButton.ButtonText>{warnLabel}</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
)} )}
{hideLabel && ( {hideLabel && (
<ToggleButton.Button name="hide" label={hideLabel}> <ToggleButton.Button name="hide" label={hideLabel}>
{hideLabel} <ToggleButton.ButtonText>{hideLabel}</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
)} )}
</ToggleButton.Group> </ToggleButton.Group>

View File

@ -1,17 +1,17 @@
import React from 'react' import React from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {LabelPreference, InterpretedLabelValueDefinition} from '@atproto/api' import {InterpretedLabelValueDefinition, LabelPreference} from '@atproto/api'
import {useLingui} from '@lingui/react'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useGlobalLabelStrings} from '#/lib/moderation/useGlobalLabelStrings'
import { import {
usePreferencesQuery, usePreferencesQuery,
usePreferencesSetContentLabelMutation, usePreferencesSetContentLabelMutation,
} from '#/state/queries/preferences' } from '#/state/queries/preferences'
import {atoms as a, useTheme} from '#/alf' import {atoms as a, useTheme} from '#/alf'
import {Text} from '#/components/Typography'
import * as ToggleButton from '#/components/forms/ToggleButton' import * as ToggleButton from '#/components/forms/ToggleButton'
import {useGlobalLabelStrings} from '#/lib/moderation/useGlobalLabelStrings' import {Text} from '#/components/Typography'
export function ModerationOption({ export function ModerationOption({
labelValueDefinition, labelValueDefinition,
@ -83,13 +83,13 @@ export function ModerationOption({
values={[visibility ?? 'hide']} values={[visibility ?? 'hide']}
onChange={onChange}> onChange={onChange}>
<ToggleButton.Button name="ignore" label={labels.show}> <ToggleButton.Button name="ignore" label={labels.show}>
{labels.show} <ToggleButton.ButtonText>{labels.show}</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="warn" label={labels.warn}> <ToggleButton.Button name="warn" label={labels.warn}>
{labels.warn} <ToggleButton.ButtonText>{labels.warn}</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="hide" label={labels.hide}> <ToggleButton.Button name="hide" label={labels.hide}>
{labels.hide} <ToggleButton.ButtonText>{labels.hide}</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
</ToggleButton.Group> </ToggleButton.Group>
)} )}

View File

@ -87,13 +87,17 @@ export function ServerInputDialog({
values={fixedOption} values={fixedOption}
onChange={setFixedOption}> onChange={setFixedOption}>
<ToggleButton.Button name={BSKY_SERVICE} label={_(msg`Bluesky`)}> <ToggleButton.Button name={BSKY_SERVICE} label={_(msg`Bluesky`)}>
<ToggleButton.ButtonText>
{_(msg`Bluesky`)} {_(msg`Bluesky`)}
</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button <ToggleButton.Button
testID="customSelectBtn" testID="customSelectBtn"
name="custom" name="custom"
label={_(msg`Custom`)}> label={_(msg`Custom`)}>
<ToggleButton.ButtonText>
{_(msg`Custom`)} {_(msg`Custom`)}
</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
</ToggleButton.Group> </ToggleButton.Group>

View File

@ -274,13 +274,13 @@ export const DebugModScreen = ({}: NativeStackScreenProps<
values={scenario} values={scenario}
onChange={setScenario}> onChange={setScenario}>
<ToggleButton.Button name="label" label="Label"> <ToggleButton.Button name="label" label="Label">
Label <ToggleButton.ButtonText>Label</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="block" label="Block"> <ToggleButton.Button name="block" label="Block">
Block <ToggleButton.ButtonText>Block</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="mute" label="Mute"> <ToggleButton.Button name="mute" label="Mute">
Mute <ToggleButton.ButtonText>Mute</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
</ToggleButton.Group> </ToggleButton.Group>
@ -474,16 +474,16 @@ export const DebugModScreen = ({}: NativeStackScreenProps<
<ToggleButton.Group label="Results" values={view} onChange={setView}> <ToggleButton.Group label="Results" values={view} onChange={setView}>
<ToggleButton.Button name="post" label="Post"> <ToggleButton.Button name="post" label="Post">
Post <ToggleButton.ButtonText>Post</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="notifications" label="Notifications"> <ToggleButton.Button name="notifications" label="Notifications">
Notifications <ToggleButton.ButtonText>Notifications</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="account" label="Account"> <ToggleButton.Button name="account" label="Account">
Account <ToggleButton.ButtonText>Account</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="data" label="Data"> <ToggleButton.Button name="data" label="Data">
Data <ToggleButton.ButtonText>Data</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
</ToggleButton.Group> </ToggleButton.Group>

View File

@ -67,8 +67,8 @@ export function Dialogs() {
description, as well as two actions. description, as well as two actions.
</Prompt.DescriptionText> </Prompt.DescriptionText>
<Prompt.Actions> <Prompt.Actions>
<Prompt.Cancel>Cancel</Prompt.Cancel> <Prompt.Cancel />
<Prompt.Action onPress={() => {}}>Confirm</Prompt.Action> <Prompt.Action cta="Confirm" onPress={() => {}} />
</Prompt.Actions> </Prompt.Actions>
</Prompt.Outer> </Prompt.Outer>

View File

@ -202,13 +202,13 @@ export function Forms() {
values={toggleGroupDValues} values={toggleGroupDValues}
onChange={setToggleGroupDValues}> onChange={setToggleGroupDValues}>
<ToggleButton.Button name="hide" label="Hide"> <ToggleButton.Button name="hide" label="Hide">
Hide <ToggleButton.ButtonText>Hide</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="warn" label="Warn"> <ToggleButton.Button name="warn" label="Warn">
Warn <ToggleButton.ButtonText>Warn</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="show" label="Show"> <ToggleButton.Button name="show" label="Show">
Show <ToggleButton.ButtonText>Show</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
</ToggleButton.Group> </ToggleButton.Group>
@ -218,13 +218,13 @@ export function Forms() {
values={toggleGroupDValues} values={toggleGroupDValues}
onChange={setToggleGroupDValues}> onChange={setToggleGroupDValues}>
<ToggleButton.Button name="hide" label="Hide"> <ToggleButton.Button name="hide" label="Hide">
Hide <ToggleButton.ButtonText>Hide</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="warn" label="Warn"> <ToggleButton.Button name="warn" label="Warn">
Warn <ToggleButton.ButtonText>Warn</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
<ToggleButton.Button name="show" label="Show"> <ToggleButton.Button name="show" label="Show">
Show <ToggleButton.ButtonText>Show</ToggleButton.ButtonText>
</ToggleButton.Button> </ToggleButton.Button>
</ToggleButton.Group> </ToggleButton.Group>
</View> </View>