Control · Selection
Toggle row
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).
// Toggle row: a setting with a label, an optional description and a switch. The whole row is the tap target.
// While a change is being saved the switch shows the new value and a spinner, and ignores more taps.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import { ActivityIndicator, Pressable, StyleSheet, Switch, Text, View } from 'react-native';
import { colors } from './theme';
export type ToggleRowProps = {
/** What the setting turns on, such as "Push notifications". */
label: string;
/** One line under the label about what changes. */
description?: string;
/** On or off. */
value: boolean;
/** Called with the new value when the row or the switch is tapped. */
onValueChange: (value: boolean) => void;
/** Grey and cannot be changed. Say why in the description. */
disabled?: boolean;
/** The change is being saved: shows a spinner and ignores taps until it is cleared. */
pending?: boolean;
};
type CopyInkProps = { copyTestIDs?: { label?: string; description?: string } };
export function ToggleRow({ label, description, value, onValueChange, disabled = false, pending = false, copyTestIDs }: ToggleRowProps & CopyInkProps) {
const locked = disabled || pending;
return (
<Pressable
onPress={locked ? undefined : () => onValueChange(!value)}
disabled={locked}
accessibilityRole="switch"
accessibilityLabel={label}
accessibilityHint={description}
aria-checked={value}
aria-disabled={locked}
aria-busy={pending}
style={({ pressed }) => [styles.row, pressed && styles.pressed]}
>
<View style={styles.text}>
<Text testID={copyTestIDs?.label} style={[styles.label, disabled && styles.muted]}>{label}</Text>
{description ? <Text testID={copyTestIDs?.description} style={styles.description}>{description}</Text> : null}
</View>
{pending ? <ActivityIndicator size="small" color={colors.onSurfaceVariant} /> : null}
<Switch
value={value}
onValueChange={locked ? undefined : onValueChange}
disabled={locked}
trackColor={{ false: colors.outlineVariant, true: colors.primary }}
thumbColor={colors.surface}
ios_backgroundColor={colors.outlineVariant}
// The row carries the label and state for screen readers.
importantForAccessibility="no-hide-descendants"
accessibilityElementsHidden
/>
</Pressable>
);
}
const styles = StyleSheet.create({
row: { minHeight: 56, paddingVertical: 12, paddingHorizontal: 16, flexDirection: 'row', alignItems: 'center', gap: 12 },
pressed: { backgroundColor: colors.surfaceVariant },
text: { flex: 1, gap: 2 },
label: { fontSize: 17, color: colors.onSurface },
muted: { color: colors.onSurfaceVariant },
description: { fontSize: 13, color: colors.onSurfaceVariant },
});
// Toggle row: a setting with a label, an optional description and a switch. The whole row is the tap target.
// While a change is being saved the switch shows the new value and a spinner, and ignores more taps.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import { ActivityIndicator, Pressable, StyleSheet, Switch, Text, View } from 'react-native';
import { colors } from './theme';
export type ToggleRowProps = {
/** What the setting turns on, such as "Push notifications". */
label: string;
/** One line under the label about what changes. */
description?: string;
/** On or off. */
value: boolean;
/** Called with the new value when the row or the switch is tapped. */
onValueChange: (value: boolean) => void;
/** Grey and cannot be changed. Say why in the description. */
disabled?: boolean;
/** The change is being saved: shows a spinner and ignores taps until it is cleared. */
pending?: boolean;
};
type CopyInkProps = { copyTestIDs?: { label?: string; description?: string } };
export function ToggleRow({ label, description, value, onValueChange, disabled = false, pending = false, copyTestIDs }: ToggleRowProps & CopyInkProps) {
const locked = disabled || pending;
return (
<Pressable
onPress={locked ? undefined : () => onValueChange(!value)}
disabled={locked}
accessibilityRole="switch"
accessibilityLabel={label}
accessibilityHint={description}
aria-checked={value}
aria-disabled={locked}
aria-busy={pending}
style={({ pressed }) => [styles.row, pressed && styles.pressed]}
>
<View style={styles.text}>
<Text testID={copyTestIDs?.label} style={[styles.label, disabled && styles.muted]}>{label}</Text>
{description ? <Text testID={copyTestIDs?.description} style={styles.description}>{description}</Text> : null}
</View>
{pending ? <ActivityIndicator size="small" color={colors.onSurfaceVariant} /> : null}
<Switch
value={value}
onValueChange={locked ? undefined : onValueChange}
disabled={locked}
trackColor={{ false: colors.outlineVariant, true: colors.primary }}
thumbColor={colors.surface}
ios_backgroundColor={colors.outlineVariant}
// The row carries the label and state for screen readers.
importantForAccessibility="no-hide-descendants"
accessibilityElementsHidden
/>
</Pressable>
);
}
const styles = StyleSheet.create({
row: { minHeight: 56, paddingVertical: 12, paddingHorizontal: 16, flexDirection: 'row', alignItems: 'center', gap: 12 },
pressed: { backgroundColor: colors.surfaceVariant },
text: { flex: 1, gap: 2 },
label: { fontSize: 17, color: colors.onSurface },
muted: { color: colors.onSurfaceVariant },
description: { fontSize: 13, color: colors.onSurfaceVariant },
});
// 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 { ToggleRow } from '@/components/mobilens/ToggleRow';
export function PushSetting({ initial, save }: { initial: boolean; save: (on: boolean) => Promise<void> }) {
const [on, setOn] = useState(initial);
const [saving, setSaving] = useState(false);
return (
<ToggleRow
label="Push notifications"
description="Messages and reminders"
value={on}
pending={saving}
onValueChange={async (next) => {
setOn(next); // show the change at once
setSaving(true);
try {
await save(next);
} catch {
setOn(!next); // put it back if the save failed
} finally {
setSaving(false);
}
}}
/>
);
}
import { useState } from 'react';
import { ToggleRow } from '@/components/mobilens/ToggleRow';
export function PushSetting({ initial, save }: { initial: boolean; save: (on: boolean) => Promise<void> }) {
const [on, setOn] = useState(initial);
const [saving, setSaving] = useState(false);
return (
<ToggleRow
label="Push notifications"
description="Messages and reminders"
value={on}
pending={saving}
onValueChange={async (next) => {
setOn(next); // show the change at once
setSaving(true);
try {
await save(next);
} catch {
setOn(!next); // put it back if the save failed
} finally {
setSaving(false);
}
}}
/>
);
}
API
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | – | What the setting turns on, such as "Push notifications". |
| description? | string | – | One line under the label about what changes. |
| value | boolean | – | On or off. |
| onValueChange | (value: boolean) => void | – | Called with the new value when the row or the switch is tapped. |
| disabled? | boolean | false | Grey and cannot be changed. Say why in the description. |
| pending? | boolean | false | The change is being saved: shows a spinner and ignores taps until it is cleared. |
Used in
Add the "Toggle row" 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/ToggleRow.tsx ---
// Toggle row: a setting with a label, an optional description and a switch. The whole row is the tap target.
// While a change is being saved the switch shows the new value and a spinner, and ignores more taps.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import { ActivityIndicator, Pressable, StyleSheet, Switch, Text, View } from 'react-native';
import { colors } from './theme';
export type ToggleRowProps = {
/** What the setting turns on, such as "Push notifications". */
label: string;
/** One line under the label about what changes. */
description?: string;
/** On or off. */
value: boolean;
/** Called with the new value when the row or the switch is tapped. */
onValueChange: (value: boolean) => void;
/** Grey and cannot be changed. Say why in the description. */
disabled?: boolean;
/** The change is being saved: shows a spinner and ignores taps until it is cleared. */
pending?: boolean;
};
type CopyInkProps = { copyTestIDs?: { label?: string; description?: string } };
export function ToggleRow({ label, description, value, onValueChange, disabled = false, pending = false, copyTestIDs }: ToggleRowProps & CopyInkProps) {
const locked = disabled || pending;
return (
<Pressable
onPress={locked ? undefined : () => onValueChange(!value)}
disabled={locked}
accessibilityRole="switch"
accessibilityLabel={label}
accessibilityHint={description}
aria-checked={value}
aria-disabled={locked}
aria-busy={pending}
style={({ pressed }) => [styles.row, pressed && styles.pressed]}
>
<View style={styles.text}>
<Text testID={copyTestIDs?.label} style={[styles.label, disabled && styles.muted]}>{label}</Text>
{description ? <Text testID={copyTestIDs?.description} style={styles.description}>{description}</Text> : null}
</View>
{pending ? <ActivityIndicator size="small" color={colors.onSurfaceVariant} /> : null}
<Switch
value={value}
onValueChange={locked ? undefined : onValueChange}
disabled={locked}
trackColor={{ false: colors.outlineVariant, true: colors.primary }}
thumbColor={colors.surface}
ios_backgroundColor={colors.outlineVariant}
// The row carries the label and state for screen readers.
importantForAccessibility="no-hide-descendants"
accessibilityElementsHidden
/>
</Pressable>
);
}
const styles = StyleSheet.create({
row: { minHeight: 56, paddingVertical: 12, paddingHorizontal: 16, flexDirection: 'row', alignItems: 'center', gap: 12 },
pressed: { backgroundColor: colors.surfaceVariant },
text: { flex: 1, gap: 2 },
label: { fontSize: 17, color: colors.onSurface },
muted: { color: colors.onSurfaceVariant },
description: { fontSize: 13, color: colors.onSurfaceVariant },
});
--- end of ToggleRow.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { ToggleRow } from '@/components/mobilens/ToggleRow';
export function PushSetting({ initial, save }: { initial: boolean; save: (on: boolean) => Promise<void> }) {
const [on, setOn] = useState(initial);
const [saving, setSaving] = useState(false);
return (
<ToggleRow
label="Push notifications"
description="Messages and reminders"
value={on}
pending={saving}
onValueChange={async (next) => {
setOn(next); // show the change at once
setSaving(true);
try {
await save(next);
} catch {
setOn(!next); // put it back if the save failed
} finally {
setSaving(false);
}
}}
/>
);
}
STATES: all 4 must work in the screen where you use it
- Off: when the setting is off. Tapping anywhere on the row turns it on, not only the switch.
- On: when the setting is on. The track uses the primary colour.
- Saving: when the change was made and is being saved. The switch shows the new value at once, with a spinner beside it. Taps are ignored until the save finishes. If the save fails, the switch goes back and the screen says why.
- Disabled: when the setting cannot be changed here. Grey label; the description says why, for example that it is off in system Settings.
ACCESSIBILITY
- The row is one switch for screen readers: its label, on or off, and the description as a hint.
- While saving, the row is announced as busy.
- The row is at least 56 points tall.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.