Repository navigation
refactor(mobile): unify sheet header actions #21978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+346
−167
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { View } from "react-native"; | ||
|
|
||
| import { AndroidScreenHeader } from "./AndroidScreenHeader"; | ||
| import { Button } from "./Button"; | ||
| import { IconButton } from "./IconButton"; | ||
| import type { SheetCloseButtonProps, SheetHeaderProps } from "./SheetHeader.types"; | ||
|
|
||
| export function SheetCloseButton(props: SheetCloseButtonProps) { | ||
| return ( | ||
| <IconButton | ||
| icon="xmark" | ||
| accessibilityLabel={props.accessibilityLabel ?? (props.onCancel ? "Cancel" : "Close")} | ||
| disabled={props.disabled} | ||
| tintColorClassName="accent-header-foreground" | ||
| onPress={props.onCancel ?? props.onClose} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export function SheetHeader(props: SheetHeaderProps) { | ||
| const confirmation = props.confirm; | ||
| const trailing = | ||
| props.trailing || confirmation ? ( | ||
| <View className="flex-row items-center gap-2"> | ||
| {props.trailing} | ||
| {confirmation ? ( | ||
| <Button | ||
| label={confirmation.label} | ||
| tone="text" | ||
| loading={confirmation.loading} | ||
| disabled={confirmation.disabled} | ||
| onPress={confirmation.onPress} | ||
| /> | ||
| ) : null} | ||
| </View> | ||
| ) : undefined; | ||
|
|
||
| return ( | ||
| <AndroidScreenHeader | ||
| embedded={props.presentation === "inline"} | ||
| title={props.title} | ||
| subtitle={props.subtitle} | ||
| leading={ | ||
| <SheetCloseButton | ||
| {...(props.onCancel ? { onCancel: props.onCancel } : { onClose: props.onClose })} | ||
| accessibilityLabel={props.accessibilityLabel} | ||
| disabled={props.dismissDisabled} | ||
| /> | ||
| } | ||
| trailing={trailing} | ||
| hideBottomBorder | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { Pressable, View } from "react-native"; | ||
|
|
||
| import { NativeStackScreenOptions } from "../../native/StackHeader"; | ||
| import { AppActivityIndicator } from "./AppActivityIndicator"; | ||
| import { AppText } from "./AppText"; | ||
| import { SymbolView } from "./AppSymbol"; | ||
| import { IconButton } from "./IconButton"; | ||
| import { Toolbar } from "./Toolbar"; | ||
| import { useControlTheme } from "./useControlTheme"; | ||
| import type { SheetCloseButtonProps, SheetHeaderProps } from "./SheetHeader.types"; | ||
|
|
||
| export function SheetCloseButton(props: SheetCloseButtonProps) { | ||
| return ( | ||
| <IconButton | ||
| icon="xmark" | ||
| accessibilityLabel={props.accessibilityLabel ?? (props.onCancel ? "Cancel" : "Close")} | ||
| disabled={props.disabled} | ||
| onPress={props.onCancel ?? props.onClose} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export function SheetHeader(props: SheetHeaderProps) { | ||
| return props.presentation !== "inline" ? ( | ||
| <NativeSheetHeader {...props} /> | ||
| ) : ( | ||
| <InlineSheetHeader {...props} /> | ||
| ); | ||
| } | ||
|
|
||
| function NativeSheetHeader(props: SheetHeaderProps) { | ||
| const { colors } = useControlTheme(); | ||
| const onDismiss = props.onCancel ?? props.onClose; | ||
| const dismissLabel = props.accessibilityLabel ?? (props.onCancel ? "Cancel" : "Close"); | ||
| const confirmation = props.confirm; | ||
|
|
||
| // Toolbar children are parsed as native items, so keep the actual Button | ||
| // elements here instead of hiding them behind another component. | ||
| return ( | ||
| <> | ||
| <NativeStackScreenOptions | ||
| options={{ | ||
| title: props.title, | ||
| headerBackVisible: false, | ||
| unstable_headerSubtitle: props.subtitle, | ||
| }} | ||
| /> | ||
| <Toolbar placement="left"> | ||
| <Toolbar.Button | ||
| icon="xmark" | ||
| accessibilityLabel={dismissLabel} | ||
| onPress={onDismiss} | ||
| disabled={props.dismissDisabled} | ||
| /> | ||
| </Toolbar> | ||
| {confirmation || props.trailing ? ( | ||
| <Toolbar placement="right"> | ||
| {props.trailing ? ( | ||
| <Toolbar.Custom> | ||
| <View>{props.trailing}</View> | ||
| </Toolbar.Custom> | ||
| ) : null} | ||
| {confirmation?.loading ? ( | ||
| <Toolbar.Custom> | ||
| <View | ||
| className="size-11 items-center justify-center" | ||
| accessible | ||
| accessibilityLabel={confirmation.label} | ||
| accessibilityState={{ busy: true }} | ||
| > | ||
| <AppActivityIndicator colorClassName="accent-prominent-action" /> | ||
| </View> | ||
| </Toolbar.Custom> | ||
| ) : confirmation ? ( | ||
| <Toolbar.Button | ||
| icon="checkmark" | ||
| variant="prominent" | ||
| tintColor={colors["--color-prominent-action"]} | ||
| accessibilityLabel={confirmation.label} | ||
| disabled={confirmation.disabled} | ||
| onPress={confirmation.onPress} | ||
| /> | ||
| ) : null} | ||
| </Toolbar> | ||
| ) : null} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function InlineConfirmation({ | ||
| confirmation, | ||
| }: { | ||
| readonly confirmation: NonNullable<SheetHeaderProps["confirm"]>; | ||
| }) { | ||
| const confirmDisabled = confirmation.disabled || confirmation.loading; | ||
| return ( | ||
| <Pressable | ||
| accessibilityRole="button" | ||
| accessibilityLabel={confirmation.label} | ||
| accessibilityState={{ | ||
| disabled: Boolean(confirmDisabled), | ||
| busy: Boolean(confirmation.loading), | ||
| }} | ||
| disabled={confirmDisabled} | ||
| onPress={confirmation.onPress} | ||
| className={ | ||
| confirmDisabled | ||
| ? "size-11 items-center justify-center rounded-full bg-subtle-strong" | ||
| : "size-11 items-center justify-center rounded-full bg-prominent-action" | ||
| } | ||
| > | ||
| {confirmation.loading ? ( | ||
| <AppActivityIndicator colorClassName="accent-icon-subtle" /> | ||
| ) : ( | ||
| <SymbolView | ||
| name="checkmark" | ||
| size={20} | ||
| tintColorClassName={ | ||
| confirmDisabled ? "accent-icon-subtle" : "accent-prominent-action-foreground" | ||
| } | ||
| /> | ||
| )} | ||
| </Pressable> | ||
| ); | ||
| } | ||
|
|
||
| function InlineSheetHeader(props: SheetHeaderProps) { | ||
| const close = ( | ||
| <SheetCloseButton | ||
| {...(props.onCancel ? { onCancel: props.onCancel } : { onClose: props.onClose })} | ||
| accessibilityLabel={props.accessibilityLabel} | ||
| disabled={props.dismissDisabled} | ||
| /> | ||
| ); | ||
| const confirm = props.confirm ? <InlineConfirmation confirmation={props.confirm} /> : null; | ||
| const trailing = | ||
| props.trailing || confirm ? ( | ||
| <View className="flex-row items-center gap-2"> | ||
| {props.trailing} | ||
| {confirm} | ||
| </View> | ||
| ) : undefined; | ||
|
|
||
| return ( | ||
| <View className="flex-row items-center gap-3 px-4 pt-3 pb-3"> | ||
| {close} | ||
| <View className="min-w-0 flex-1 gap-1"> | ||
| <AppText numberOfLines={1} className="text-base font-t3-bold text-foreground"> | ||
| {props.title} | ||
| </AppText> | ||
| {props.subtitle ? ( | ||
| <AppText | ||
| numberOfLines={1} | ||
| ellipsizeMode="middle" | ||
| className="text-xs text-foreground-muted" | ||
| > | ||
| {props.subtitle} | ||
| </AppText> | ||
| ) : null} | ||
| </View> | ||
| {trailing} | ||
| </View> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| type SheetDismissAction = | ||
| | { readonly onCancel: () => void; readonly onClose?: never } | ||
| | { readonly onClose: () => void; readonly onCancel?: never }; | ||
|
|
||
| type SheetConfirmation = { | ||
| readonly label: string; | ||
| readonly onPress: () => void; | ||
| readonly disabled?: boolean; | ||
| readonly loading?: boolean; | ||
| }; | ||
|
|
||
| export type SheetHeaderProps = SheetDismissAction & { | ||
| readonly title: string; | ||
| readonly subtitle?: string; | ||
| readonly accessibilityLabel?: string; | ||
| readonly dismissDisabled?: boolean; | ||
| readonly confirm?: SheetConfirmation; | ||
| readonly presentation?: "native" | "inline"; | ||
| readonly trailing?: ReactNode; | ||
| }; | ||
|
|
||
| export type SheetCloseButtonProps = SheetDismissAction & { | ||
| readonly accessibilityLabel?: string; | ||
| readonly disabled?: boolean; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.