Control · Buttons
Button
Screen
Colour scheme
Mode
Corners
Colours
Every state
- When
- Behaviour
Save these 2 files in components/mobilens/. They need only react-native (checked with Expo SDK 58).
// Button: four variants, pressed feedback, a loading state that keeps its width, and a real disabled state.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import type { ReactNode } from 'react';
import { ActivityIndicator, Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle } from 'react-native';
import { colors, radius } from './theme';
export type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'destructive';
export type ButtonProps = {
/** The label. Keep it a verb: "Send code", not "OK". */
title: string;
/** Called on tap. Not called while loading or disabled. */
onPress?: () => void;
/** primary for the one main action on a screen, destructive for what cannot be undone. */
variant?: ButtonVariant;
/** Shows a spinner in place of the label, keeps the width, and ignores taps. */
loading?: boolean;
/** Greyed out and ignores taps. Screen readers say "dimmed". */
disabled?: boolean;
/** Shown before the label. */
icon?: ReactNode;
/** Override the label and spinner colour while enabled. */
color?: string;
/** Override the enabled button surface colour. */
backgroundColor?: string;
/** Minimum height of the visible button surface, in points. */
minHeight?: number;
/** Layout only, such as margins or alignSelf. Width fills the parent by default. */
style?: StyleProp<ViewStyle>;
};
type CopyInkProps = { copyTestIDs?: { label?: string } };
export function Button({ title, onPress, variant = 'primary', loading = false, disabled = false, icon, color, backgroundColor, minHeight, style, copyTestIDs }: ButtonProps & CopyInkProps) {
const inactive = disabled || loading;
return (
<Pressable
onPress={inactive ? undefined : onPress}
disabled={inactive}
accessibilityRole="button"
accessibilityLabel={title}
aria-disabled={inactive}
aria-busy={loading}
style={style}
>
{({ pressed }) => (
<ButtonFace title={title} variant={variant} loading={loading} disabled={disabled} pressed={pressed && !inactive} icon={icon}
color={color} backgroundColor={backgroundColor} minHeight={minHeight} copyTestIDs={copyTestIDs} />
)}
</Pressable>
);
}
export type ButtonFaceProps = Pick<ButtonProps, 'title' | 'variant' | 'loading' | 'disabled' | 'icon' | 'color' | 'backgroundColor' | 'minHeight'> & { pressed?: boolean };
/** What the button looks like, with no press handling. Button draws this; use it alone to show a state. */
export function ButtonFace({ title, variant = 'primary', loading = false, disabled = false, pressed = false, icon, color, backgroundColor, minHeight, copyTestIDs }: ButtonFaceProps & CopyInkProps) {
const look = LOOKS[variant];
const text = disabled ? colors.onSurfaceVariant : color ?? look.text;
const fill = disabled ? colors.surfaceVariant : backgroundColor ?? look.fill;
const variantBorder = look.fill === look.border ? fill : look.border;
const border = disabled ? colors.surfaceVariant : variantBorder;
return (
<View
style={[
styles.face,
{ backgroundColor: fill, borderColor: border },
minHeight !== undefined && { minHeight },
pressed && styles.pressed,
]}
>
{pressed ? <View style={[styles.fill, styles.shade, { backgroundColor: look.shade }]} /> : null}
{/* The label stays in the layout while loading, so the button does not change width. */}
<View style={[styles.content, loading && styles.hidden]}>
{icon}
<Text testID={loading ? undefined : copyTestIDs?.label} style={[styles.label, { color: text }]} numberOfLines={1}>{title}</Text>
</View>
{loading ? <ActivityIndicator style={styles.fill} color={text} /> : null}
</View>
);
}
// Pressed darkens a filled button and tints an outlined one, so it works with any theme colours.
const LOOKS: Record<ButtonVariant, { fill: string; border: string; text: string; shade: string }> = {
primary: { fill: colors.primary, border: colors.primary, text: colors.onPrimary, shade: colors.shadow },
secondary: { fill: colors.surface, border: colors.outlineVariant, text: colors.onSurface, shade: colors.onSurface },
ghost: { fill: 'transparent', border: 'transparent', text: colors.primary, shade: colors.primary },
destructive: { fill: colors.error, border: colors.error, text: colors.onError, shade: colors.shadow },
};
const styles = StyleSheet.create({
face: {
minHeight: 50,
paddingHorizontal: 20,
borderRadius: radius.md,
borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
},
pressed: { transform: [{ scale: 0.98 }] },
fill: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 },
shade: { opacity: 0.12 },
content: { flexDirection: 'row', alignItems: 'center', gap: 8 },
hidden: { opacity: 0 },
label: { fontSize: 17, fontWeight: '600' },
});
// Button: four variants, pressed feedback, a loading state that keeps its width, and a real disabled state.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import type { ReactNode } from 'react';
import { ActivityIndicator, Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle } from 'react-native';
import { colors, radius } from './theme';
export type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'destructive';
export type ButtonProps = {
/** The label. Keep it a verb: "Send code", not "OK". */
title: string;
/** Called on tap. Not called while loading or disabled. */
onPress?: () => void;
/** primary for the one main action on a screen, destructive for what cannot be undone. */
variant?: ButtonVariant;
/** Shows a spinner in place of the label, keeps the width, and ignores taps. */
loading?: boolean;
/** Greyed out and ignores taps. Screen readers say "dimmed". */
disabled?: boolean;
/** Shown before the label. */
icon?: ReactNode;
/** Override the label and spinner colour while enabled. */
color?: string;
/** Override the enabled button surface colour. */
backgroundColor?: string;
/** Minimum height of the visible button surface, in points. */
minHeight?: number;
/** Layout only, such as margins or alignSelf. Width fills the parent by default. */
style?: StyleProp<ViewStyle>;
};
type CopyInkProps = { copyTestIDs?: { label?: string } };
export function Button({ title, onPress, variant = 'primary', loading = false, disabled = false, icon, color, backgroundColor, minHeight, style, copyTestIDs }: ButtonProps & CopyInkProps) {
const inactive = disabled || loading;
return (
<Pressable
onPress={inactive ? undefined : onPress}
disabled={inactive}
accessibilityRole="button"
accessibilityLabel={title}
aria-disabled={inactive}
aria-busy={loading}
style={style}
>
{({ pressed }) => (
<ButtonFace title={title} variant={variant} loading={loading} disabled={disabled} pressed={pressed && !inactive} icon={icon}
color={color} backgroundColor={backgroundColor} minHeight={minHeight} copyTestIDs={copyTestIDs} />
)}
</Pressable>
);
}
export type ButtonFaceProps = Pick<ButtonProps, 'title' | 'variant' | 'loading' | 'disabled' | 'icon' | 'color' | 'backgroundColor' | 'minHeight'> & { pressed?: boolean };
/** What the button looks like, with no press handling. Button draws this; use it alone to show a state. */
export function ButtonFace({ title, variant = 'primary', loading = false, disabled = false, pressed = false, icon, color, backgroundColor, minHeight, copyTestIDs }: ButtonFaceProps & CopyInkProps) {
const look = LOOKS[variant];
const text = disabled ? colors.onSurfaceVariant : color ?? look.text;
const fill = disabled ? colors.surfaceVariant : backgroundColor ?? look.fill;
const variantBorder = look.fill === look.border ? fill : look.border;
const border = disabled ? colors.surfaceVariant : variantBorder;
return (
<View
style={[
styles.face,
{ backgroundColor: fill, borderColor: border },
minHeight !== undefined && { minHeight },
pressed && styles.pressed,
]}
>
{pressed ? <View style={[styles.fill, styles.shade, { backgroundColor: look.shade }]} /> : null}
{/* The label stays in the layout while loading, so the button does not change width. */}
<View style={[styles.content, loading && styles.hidden]}>
{icon}
<Text testID={loading ? undefined : copyTestIDs?.label} style={[styles.label, { color: text }]} numberOfLines={1}>{title}</Text>
</View>
{loading ? <ActivityIndicator style={styles.fill} color={text} /> : null}
</View>
);
}
// Pressed darkens a filled button and tints an outlined one, so it works with any theme colours.
const LOOKS: Record<ButtonVariant, { fill: string; border: string; text: string; shade: string }> = {
primary: { fill: colors.primary, border: colors.primary, text: colors.onPrimary, shade: colors.shadow },
secondary: { fill: colors.surface, border: colors.outlineVariant, text: colors.onSurface, shade: colors.onSurface },
ghost: { fill: 'transparent', border: 'transparent', text: colors.primary, shade: colors.primary },
destructive: { fill: colors.error, border: colors.error, text: colors.onError, shade: colors.shadow },
};
const styles = StyleSheet.create({
face: {
minHeight: 50,
paddingHorizontal: 20,
borderRadius: radius.md,
borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
},
pressed: { transform: [{ scale: 0.98 }] },
fill: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 },
shade: { opacity: 0.12 },
content: { flexDirection: 'row', alignItems: 'center', gap: 8 },
hidden: { opacity: 0 },
label: { fontSize: 17, fontWeight: '600' },
});
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
Usage
import { useState } from 'react';
import { View } from 'react-native';
import { Button } from '@/components/mobilens/Button';
export function SaveBar({ save, cancel }: { save: () => Promise<void>; cancel: () => void }) {
const [saving, setSaving] = useState(false);
return (
<View style={{ gap: 12 }}>
<Button
title="Save changes"
loading={saving}
onPress={async () => {
setSaving(true);
try {
await save();
} finally {
setSaving(false);
}
}}
/>
<Button title="Cancel" variant="secondary" disabled={saving} onPress={cancel} />
</View>
);
}
import { useState } from 'react';
import { View } from 'react-native';
import { Button } from '@/components/mobilens/Button';
export function SaveBar({ save, cancel }: { save: () => Promise<void>; cancel: () => void }) {
const [saving, setSaving] = useState(false);
return (
<View style={{ gap: 12 }}>
<Button
title="Save changes"
loading={saving}
onPress={async () => {
setSaving(true);
try {
await save();
} finally {
setSaving(false);
}
}}
/>
<Button title="Cancel" variant="secondary" disabled={saving} onPress={cancel} />
</View>
);
}
API
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | – | The label. Keep it a verb: "Send code", not "OK". |
| onPress? | () => void | – | Called on tap. Not called while loading or disabled. |
| variant? | ButtonVariant | 'primary' | primary for the one main action on a screen, destructive for what cannot be undone. |
| loading? | boolean | false | Shows a spinner in place of the label, keeps the width, and ignores taps. |
| disabled? | boolean | false | Greyed out and ignores taps. Screen readers say "dimmed". |
| icon? | ReactNode | – | Shown before the label. |
| color? | string | – | Override the label and spinner colour while enabled. |
| backgroundColor? | string | – | Override the enabled button surface colour. |
| minHeight? | number | – | Minimum height of the visible button surface, in points. |
| style? | StyleProp<ViewStyle> | – | Layout only, such as margins or alignSelf. Width fills the parent by default. |
Used in
Add the "Button" control to this Expo or React Native project. Create the files below with exactly this content. Do not restyle or rewrite them.
FILES
Put them in components/mobilens/. theme.ts is shared by every Mobilens component: if it already exists, keep one copy, and skip any other file that is already there with the same content. If the project has no "@/..." import alias, change those imports to relative paths.
--- components/mobilens/theme.ts ---
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
--- end of theme.ts ---
--- components/mobilens/Button.tsx ---
// Button: four variants, pressed feedback, a loading state that keeps its width, and a real disabled state.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import type { ReactNode } from 'react';
import { ActivityIndicator, Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle } from 'react-native';
import { colors, radius } from './theme';
export type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'destructive';
export type ButtonProps = {
/** The label. Keep it a verb: "Send code", not "OK". */
title: string;
/** Called on tap. Not called while loading or disabled. */
onPress?: () => void;
/** primary for the one main action on a screen, destructive for what cannot be undone. */
variant?: ButtonVariant;
/** Shows a spinner in place of the label, keeps the width, and ignores taps. */
loading?: boolean;
/** Greyed out and ignores taps. Screen readers say "dimmed". */
disabled?: boolean;
/** Shown before the label. */
icon?: ReactNode;
/** Override the label and spinner colour while enabled. */
color?: string;
/** Override the enabled button surface colour. */
backgroundColor?: string;
/** Minimum height of the visible button surface, in points. */
minHeight?: number;
/** Layout only, such as margins or alignSelf. Width fills the parent by default. */
style?: StyleProp<ViewStyle>;
};
type CopyInkProps = { copyTestIDs?: { label?: string } };
export function Button({ title, onPress, variant = 'primary', loading = false, disabled = false, icon, color, backgroundColor, minHeight, style, copyTestIDs }: ButtonProps & CopyInkProps) {
const inactive = disabled || loading;
return (
<Pressable
onPress={inactive ? undefined : onPress}
disabled={inactive}
accessibilityRole="button"
accessibilityLabel={title}
aria-disabled={inactive}
aria-busy={loading}
style={style}
>
{({ pressed }) => (
<ButtonFace title={title} variant={variant} loading={loading} disabled={disabled} pressed={pressed && !inactive} icon={icon}
color={color} backgroundColor={backgroundColor} minHeight={minHeight} copyTestIDs={copyTestIDs} />
)}
</Pressable>
);
}
export type ButtonFaceProps = Pick<ButtonProps, 'title' | 'variant' | 'loading' | 'disabled' | 'icon' | 'color' | 'backgroundColor' | 'minHeight'> & { pressed?: boolean };
/** What the button looks like, with no press handling. Button draws this; use it alone to show a state. */
export function ButtonFace({ title, variant = 'primary', loading = false, disabled = false, pressed = false, icon, color, backgroundColor, minHeight, copyTestIDs }: ButtonFaceProps & CopyInkProps) {
const look = LOOKS[variant];
const text = disabled ? colors.onSurfaceVariant : color ?? look.text;
const fill = disabled ? colors.surfaceVariant : backgroundColor ?? look.fill;
const variantBorder = look.fill === look.border ? fill : look.border;
const border = disabled ? colors.surfaceVariant : variantBorder;
return (
<View
style={[
styles.face,
{ backgroundColor: fill, borderColor: border },
minHeight !== undefined && { minHeight },
pressed && styles.pressed,
]}
>
{pressed ? <View style={[styles.fill, styles.shade, { backgroundColor: look.shade }]} /> : null}
{/* The label stays in the layout while loading, so the button does not change width. */}
<View style={[styles.content, loading && styles.hidden]}>
{icon}
<Text testID={loading ? undefined : copyTestIDs?.label} style={[styles.label, { color: text }]} numberOfLines={1}>{title}</Text>
</View>
{loading ? <ActivityIndicator style={styles.fill} color={text} /> : null}
</View>
);
}
// Pressed darkens a filled button and tints an outlined one, so it works with any theme colours.
const LOOKS: Record<ButtonVariant, { fill: string; border: string; text: string; shade: string }> = {
primary: { fill: colors.primary, border: colors.primary, text: colors.onPrimary, shade: colors.shadow },
secondary: { fill: colors.surface, border: colors.outlineVariant, text: colors.onSurface, shade: colors.onSurface },
ghost: { fill: 'transparent', border: 'transparent', text: colors.primary, shade: colors.primary },
destructive: { fill: colors.error, border: colors.error, text: colors.onError, shade: colors.shadow },
};
const styles = StyleSheet.create({
face: {
minHeight: 50,
paddingHorizontal: 20,
borderRadius: radius.md,
borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
},
pressed: { transform: [{ scale: 0.98 }] },
fill: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 },
shade: { opacity: 0.12 },
content: { flexDirection: 'row', alignItems: 'center', gap: 8 },
hidden: { opacity: 0 },
label: { fontSize: 17, fontWeight: '600' },
});
--- end of Button.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { View } from 'react-native';
import { Button } from '@/components/mobilens/Button';
export function SaveBar({ save, cancel }: { save: () => Promise<void>; cancel: () => void }) {
const [saving, setSaving] = useState(false);
return (
<View style={{ gap: 12 }}>
<Button
title="Save changes"
loading={saving}
onPress={async () => {
setSaving(true);
try {
await save();
} finally {
setSaving(false);
}
}}
/>
<Button title="Cancel" variant="secondary" disabled={saving} onPress={cancel} />
</View>
);
}
STATES: all 4 must work in the screen where you use it
- Default: when the button can be pressed. Use one primary button per screen. The tap area is at least 50 points tall.
- Pressed: when A finger is down on the button. The button darkens and shrinks slightly. Sliding the finger off cancels the tap.
- Loading: when the screen sets loading while its request runs. A spinner replaces the label; the width stays the same. Taps are ignored, so the request is not sent twice.
- Disabled: when the screen sets disabled, for example until a form is valid. Grey fill and text; taps do nothing.
ACCESSIBILITY
- Each button is announced as a button with its label; disabled ones as dimmed.
- While loading, screen readers are told the button is busy.
- The label is one line and the button is at least 50 points tall, above the 44-point minimum tap size.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.