-
Notifications
You must be signed in to change notification settings - Fork 274
UI4: Card & Row Components #4593
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import * as React from 'react' | ||
| import { TouchableHighlight, View } from 'react-native' | ||
|
|
||
| import { useHandler } from '../../hooks/useHandler' | ||
| import { triggerHaptic } from '../../util/haptic' | ||
| import { showError } from '../services/AirshipInstance' | ||
| import { cacheStyles, Theme, useTheme } from '../services/ThemeContext' | ||
| import { SectionView } from './SectionView' | ||
|
|
||
| export type CardType = 'default' | 'warning' | 'error' | ||
|
|
||
| interface Props { | ||
| children: React.ReactNode | React.ReactNode[] | ||
| icon?: React.ReactNode | ||
| onLongPress?: () => Promise<void> | void | ||
| onPress?: () => Promise<void> | void | ||
| // cardType?: CardType // TODO | ||
| } | ||
|
|
||
| /** | ||
| * Rounded card that automatically adds horizontal dividers between each child, | ||
| * aligned in a column layout. Adds no dividers if only one child is given. | ||
| */ | ||
| export const CardUi4 = (props: Props) => { | ||
| const { children, icon, onLongPress, onPress } = props | ||
| const theme = useTheme() | ||
| const styles = getStyles(theme) | ||
|
|
||
| const handlePress = useHandler(async () => { | ||
| if (onPress != null) { | ||
| triggerHaptic('impactLight') | ||
| try { | ||
| await onPress() | ||
| } catch (err) { | ||
| showError(err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| const handleLongPress = useHandler(async () => { | ||
| if (onLongPress != null) { | ||
| triggerHaptic('impactLight') | ||
| try { | ||
| await onLongPress() | ||
| } catch (err) { | ||
| showError(err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| return ( | ||
| <TouchableHighlight | ||
| accessible={false} | ||
| onPress={handlePress} | ||
| onLongPress={handleLongPress} | ||
| disabled={handlePress == null && handleLongPress == null} | ||
| underlayColor={theme.touchHighlightUi4} | ||
| style={styles.cardContainer} | ||
| > | ||
| <> | ||
| {icon == null ? null : <View style={styles.iconContainer}>{icon}</View>} | ||
| <SectionView>{children}</SectionView> | ||
| </> | ||
| </TouchableHighlight> | ||
| ) | ||
| } | ||
|
|
||
| // TODO: Adjust margin/padding so everything combines with correct layout no | ||
| // matter the combination of UI4 components. | ||
| const getStyles = cacheStyles((theme: Theme) => ({ | ||
| cardContainer: { | ||
| backgroundColor: theme.cardBackgroundUi4, | ||
| borderRadius: theme.rem(theme.cardRadiusRemUi4), | ||
| margin: theme.rem(0.5), | ||
| padding: theme.rem(0.5), | ||
| flexDirection: 'row' | ||
| }, | ||
| iconContainer: { | ||
| margin: theme.rem(0.25), | ||
| justifyContent: 'center', | ||
| alignContent: 'center' | ||
| }, | ||
| warning: { | ||
| borderColor: theme.warningIcon | ||
| } | ||
| })) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import Clipboard from '@react-native-clipboard/clipboard' | ||
| import * as React from 'react' | ||
| import { ActivityIndicator, TouchableHighlight, View } from 'react-native' | ||
| import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome' | ||
| import FontAwesome5 from 'react-native-vector-icons/FontAwesome5' | ||
| import SimpleLineIcons from 'react-native-vector-icons/SimpleLineIcons' | ||
|
|
||
| import { useHandler } from '../../hooks/useHandler' | ||
| import { lstrings } from '../../locales/strings' | ||
| import { triggerHaptic } from '../../util/haptic' | ||
| import { showError, showToast } from '../services/AirshipInstance' | ||
| import { cacheStyles, Theme, useTheme } from '../services/ThemeContext' | ||
| import { EdgeText } from '../themed/EdgeText' | ||
|
|
||
| const textHeights = { | ||
| small: 2, | ||
| medium: 3, | ||
| large: 0 | ||
| } | ||
|
|
||
| export type RowType = 'copy' | 'editable' | 'questionable' | 'loading' | 'default' | 'touchable' | 'delete' | ||
|
|
||
| interface Props { | ||
| body?: string | ||
| children?: React.ReactNode | ||
| error?: boolean | ||
| onLongPress?: () => Promise<void> | void | ||
| onPress?: () => Promise<void> | void | ||
| title?: string | ||
| type?: RowType | ||
| maximumHeight?: 'small' | 'medium' | 'large' | ||
| icon?: React.ReactNode | ||
| } | ||
|
|
||
| export const RowUi4 = (props: Props) => { | ||
| const theme = useTheme() | ||
| const styles = getStyles(theme) | ||
|
|
||
| const { body, title, children, maximumHeight = 'medium', error, icon, onLongPress, onPress } = props | ||
| const { type = onLongPress == null && onPress == null ? 'default' : 'touchable' } = props | ||
|
|
||
| const numberOfLines = textHeights[maximumHeight] | ||
|
|
||
| const handlePress = useHandler(async () => { | ||
| if (type === 'copy' && body != null) { | ||
| triggerHaptic('impactLight') | ||
| Clipboard.setString(body) | ||
| showToast(lstrings.fragment_copied) | ||
| } else if (onPress != null) { | ||
| triggerHaptic('impactLight') | ||
| try { | ||
| await onPress() | ||
| } catch (err) { | ||
| showError(err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| const handleLongPress = useHandler(async () => { | ||
| if (onLongPress != null) { | ||
| triggerHaptic('impactLight') | ||
| try { | ||
| await onLongPress() | ||
| } catch (err) { | ||
| showError(err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| return type === 'loading' ? ( | ||
| <View style={styles.container}> | ||
| <View style={styles.content}> | ||
| <EdgeText style={styles.textHeader}>{title}</EdgeText> | ||
| <ActivityIndicator style={styles.loader} color={theme.primaryText} size="large" /> | ||
| </View> | ||
| </View> | ||
| ) : ( | ||
| <TouchableHighlight | ||
| accessible={false} | ||
| onPress={handlePress} | ||
| onLongPress={handleLongPress} | ||
| disabled={handlePress == null && handleLongPress == null} | ||
| underlayColor={theme.touchHighlightUi4} | ||
| > | ||
| <View style={styles.container}> | ||
| {icon == null ? null : <View style={styles.iconContainer}>{icon}</View>} | ||
| <View style={styles.content}> | ||
| {title == null ? null : ( | ||
| <EdgeText disableFontScaling ellipsizeMode="tail" style={error ? styles.textHeaderError : styles.textHeader}> | ||
| {title} | ||
| </EdgeText> | ||
| )} | ||
| {children == null ? ( | ||
| body == null ? null : ( | ||
| <EdgeText style={styles.textBody} numberOfLines={numberOfLines} ellipsizeMode="tail"> | ||
| {body} | ||
| </EdgeText> | ||
| ) | ||
| ) : ( | ||
| children | ||
| )} | ||
| </View> | ||
| <View style={styles.rightButtonContainer}> | ||
| {type === 'touchable' && <FontAwesome5 name="chevron-right" style={styles.tappableIcon} size={theme.rem(1)} />} | ||
| {type === 'editable' && <FontAwesomeIcon name="edit" style={styles.tappableIcon} size={theme.rem(1)} />} | ||
| {type === 'copy' && <FontAwesomeIcon name="copy" style={styles.tappableIcon} size={theme.rem(1)} />} | ||
| {type === 'delete' && <FontAwesomeIcon name="times" style={styles.tappableIcon} size={theme.rem(1)} />} | ||
| {type === 'questionable' && <SimpleLineIcons name="question" style={styles.tappableIcon} size={theme.rem(1)} />} | ||
| </View> | ||
| </View> | ||
| </TouchableHighlight> | ||
| ) | ||
| } | ||
|
|
||
| // TODO: Adjust margin/padding so everything combines with correct layout no | ||
| // matter the combination of UI4 components. | ||
|
Comment on lines
+115
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ambitious. I assume this will happen later as more stuff gets built out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I tried to start doing it and then found I had to make more changes upon tackling subsequent tasks and thinking of new rules for margin handling for sub components. Figured it would be better to just handle it at the end to make sure everything works and avoid all the rebasing, it's already almost there at this point. |
||
| const getStyles = cacheStyles((theme: Theme) => ({ | ||
| container: { | ||
| backgroundColor: theme.tileBackground, | ||
| paddingHorizontal: theme.rem(0.5), | ||
| paddingVertical: theme.rem(0.25), | ||
| flexDirection: 'row', | ||
| alignItems: 'center' | ||
| }, | ||
| content: { | ||
| flex: 1 | ||
| }, | ||
| iconContainer: { | ||
| marginRight: theme.rem(0.5) | ||
| }, | ||
| rightButtonContainer: { | ||
| justifyContent: 'center', | ||
| alignItems: 'center' | ||
| }, | ||
| tappableIcon: { | ||
| color: theme.iconTappable, | ||
| marginLeft: theme.rem(0.5), | ||
| textAlign: 'center' | ||
| }, | ||
| textHeader: { | ||
| color: theme.secondaryText, | ||
| fontSize: theme.rem(0.75), | ||
| paddingBottom: theme.rem(0.25), | ||
| paddingRight: theme.rem(1) | ||
| }, | ||
| textHeaderError: { | ||
| color: theme.dangerText, | ||
| fontSize: theme.rem(0.75) | ||
| }, | ||
| textBody: { | ||
| color: theme.primaryText, | ||
| fontSize: theme.rem(1) | ||
| }, | ||
| loader: { | ||
| marginTop: theme.rem(0.25) | ||
| } | ||
| })) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import * as React from 'react' | ||
| import { View } from 'react-native' | ||
|
|
||
| import { cacheStyles, Theme, useTheme } from '../services/ThemeContext' | ||
|
|
||
| interface Props { | ||
| children: React.ReactNode | React.ReactNode[] | ||
| } | ||
|
|
||
| /** | ||
| * View that automatically adds horizontal dividers between each child, aligned | ||
| * in a column layout. Adds no dividers if only one child is given. | ||
| */ | ||
| export const SectionView = (props: Props): JSX.Element | null => { | ||
| const { children } = props | ||
| const theme = useTheme() | ||
| const styles = getStyles(theme) | ||
|
|
||
| const nonNullChildren = React.Children.map(children, child => { | ||
| if (child != null) { | ||
| return child | ||
| } | ||
| }) | ||
| const numChildren = React.Children.count(nonNullChildren) | ||
|
|
||
| if (children == null || numChildren === 0) return null | ||
|
|
||
| // Add a line divider between each child if there's more than one: | ||
| return ( | ||
| <View style={styles.container}> | ||
| {numChildren === 1 | ||
| ? nonNullChildren | ||
| : React.Children.map(nonNullChildren, (child, index) => { | ||
| if (index < numChildren - 1) { | ||
| return ( | ||
| <> | ||
| {child} | ||
| <View style={styles.divider} /> | ||
| </> | ||
| ) | ||
| } | ||
| return child | ||
| })} | ||
| </View> | ||
| ) | ||
| } | ||
|
|
||
| const getStyles = cacheStyles((theme: Theme) => ({ | ||
| container: { | ||
| flexDirection: 'column', | ||
| flex: 1 | ||
| }, | ||
| divider: { | ||
| height: theme.thinLineWidth, | ||
| marginHorizontal: theme.rem(0.5), | ||
| borderBottomWidth: theme.thinLineWidth, | ||
| borderBottomColor: theme.lineDivider | ||
| } | ||
| })) |