Files
otunes/apps/mobile/components/ui/primitives.tsx
T

185 lines
4.0 KiB
TypeScript

import { PaletteText, PaletteIcon } from "@/components/palette-text"
import {
ActivityIndicator,
Platform,
Pressable,
PressableProps,
StyleProp,
StyleSheet,
Text,
TextInput,
TextInputProps,
View,
ViewStyle,
} from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { radius, spacing, useThemeColors } from "@/lib/theme"
type ButtonProps = Omit<PressableProps, "style"> & {
title: string
variant?: "primary" | "secondary" | "ghost" | "destructive"
loading?: boolean
icon?: React.ComponentProps<typeof Ionicons>["name"]
style?: StyleProp<ViewStyle>
/** Overrides the variant's colors, e.g. on screens tinted by cover art. */
tint?: { background: string; foreground: string }
}
export function Button({
title,
variant = "primary",
loading = false,
disabled,
icon,
style,
tint,
...props
}: ButtonProps) {
const colors = useThemeColors()
const isDisabled = disabled || loading
const backgroundColor = tint
? tint.background
: variant === "primary"
? colors.primary
: variant === "secondary"
? colors.muted
: variant === "destructive"
? colors.destructive
: "transparent"
const textColor = tint
? tint.foreground
: variant === "primary"
? colors.primaryForeground
: variant === "destructive"
? "#fff"
: colors.foreground
return (
<Pressable
{...props}
disabled={isDisabled}
style={({ pressed }) => [
styles.button,
{
backgroundColor,
opacity: isDisabled ? 0.5 : pressed ? 0.85 : 1,
},
style,
]}
>
{loading ? (
<ActivityIndicator color={textColor} />
) : (
<View style={styles.buttonContent}>
{icon ? (
<PaletteIcon
tone={tint ? "accentForeground" : undefined}
name={icon}
size={18}
color={textColor}
/>
) : null}
<PaletteText
tone={tint ? "accentForeground" : undefined}
style={[styles.buttonText, { color: textColor }]}
>
{title}
</PaletteText>
</View>
)}
</Pressable>
)
}
export function Input({ style, ...props }: TextInputProps) {
const colors = useThemeColors()
return (
<TextInput
{...props}
{...(Platform.OS === "android"
? {
selectionColor: colors.primary,
cursorColor: colors.primary,
selectionHandleColor: colors.primary,
}
: {})}
placeholderTextColor={colors.mutedForeground}
style={[
styles.input,
{
color: colors.foreground,
backgroundColor: colors.muted,
borderColor: colors.border,
},
style,
]}
/>
)
}
export function Card({ children }: { children: React.ReactNode }) {
const colors = useThemeColors()
return (
<View
style={[
styles.card,
{ backgroundColor: colors.card, borderColor: colors.border },
]}
>
{children}
</View>
)
}
export function LoadingView() {
const colors = useThemeColors()
return (
<View style={styles.loading}>
<ActivityIndicator color={colors.foreground} size="large" />
</View>
)
}
const styles = StyleSheet.create({
button: {
minHeight: 44,
borderRadius: radius.md,
alignItems: "center",
justifyContent: "center",
paddingHorizontal: spacing.lg,
paddingVertical: spacing.sm,
},
buttonContent: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
gap: spacing.xs,
},
buttonText: {
fontSize: 16,
fontWeight: "600",
},
input: {
minHeight: 44,
borderRadius: radius.md,
borderWidth: 1,
paddingHorizontal: spacing.md,
fontSize: 16,
},
card: {
borderRadius: radius.lg,
borderWidth: 1,
padding: spacing.lg,
gap: spacing.md,
},
loading: {
flex: 1,
alignItems: "center",
justifyContent: "center",
padding: spacing.xl,
},
})