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

154 lines
3.2 KiB
TypeScript

import {
ActivityIndicator,
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>
}
export function Button({
title,
variant = "primary",
loading = false,
disabled,
icon,
style,
...props
}: ButtonProps) {
const colors = useThemeColors()
const isDisabled = disabled || loading
const backgroundColor =
variant === "primary"
? colors.primary
: variant === "secondary"
? colors.muted
: variant === "destructive"
? colors.destructive
: "transparent"
const textColor =
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 ? <Ionicons name={icon} size={18} color={textColor} /> : null}
<Text style={[styles.buttonText, { color: textColor }]}>{title}</Text>
</View>
)}
</Pressable>
)
}
export function Input({ style, ...props }: TextInputProps) {
const colors = useThemeColors()
return (
<TextInput
{...props}
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,
},
})