Control · Inputs
Slider
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).
import { createElement, useRef, useState, type ReactNode } from 'react';
import { I18nManager, Platform, StyleSheet, Text, View, type GestureResponderEvent } from 'react-native';
import { colors } from './theme';
export type SliderProps = {
/** Visible name and accessible label. */
label: string;
/** Current numeric value, controlled by the parent. */
value: number;
/** Called with a bounded, stepped value after a tap, drag or keyboard adjustment. */
onValueChange: (value: number) => void;
/** Called once after finishing a drag or keyboard adjustment that changed the value. */
onSlidingComplete?: (value: number) => void;
/** Smallest selectable value. */
minimumValue?: number;
/** Largest selectable value. Both endpoints can always be reached. */
maximumValue?: number;
/** Distance between stops, measured from minimumValue. Must be positive. */
step?: number;
/** Keep the current value visible and prevent edits. */
disabled?: boolean;
/** Convert a value to a label with units, for example 65%. */
formatValue?: (value: number) => string;
};
function getRange(value: number, min: number, max: number, step: number) {
const start = Number.isFinite(min) ? min : 0;
const end = Number.isFinite(max) ? Math.max(start, max) : start;
const stride = Number.isFinite(step) && step > 0 ? step : 1;
const last = Math.ceil(Number(((end - start) / stride).toPrecision(14)));
const at = (index: number) => index >= last ? end : Number((start + Math.max(0, index) * stride).toPrecision(15));
const bounded = Number.isFinite(value) ? Math.min(end, Math.max(start, value)) : start;
const nearest = Math.min(last, Math.round((bounded - start) / stride));
// The last interval can be shorter when the span is not divisible by the step.
const index = end - bounded < Math.abs(bounded - at(nearest)) ? last : nearest;
return { start, end, last, index, at, current: at(index) };
}
type CopyInkProps = { copyTestIDs?: { label?: string } };
type FaceProps = Omit<SliderProps, 'onValueChange' | 'onSlidingComplete'> & CopyInkProps & { focused?: boolean; children?: ReactNode };
const thumbSize = 22;
export function SliderFace({ label, value, minimumValue = 0, maximumValue = 100, step = 1, disabled, formatValue, focused, children, copyTestIDs }: FaceProps) {
const range = getRange(value, minimumValue, maximumValue, step);
const ratio = range.last ? range.index / range.last : 0;
const locked = disabled || !range.last;
return (
<View style={styles.root}>
<View aria-hidden={!!children} style={styles.caption}>
<Text testID={copyTestIDs?.label} style={[styles.label, locked && styles.muted]}>{label}</Text>
<Text style={[styles.value, locked && styles.muted]}>{formatValue ? formatValue(range.current) : String(range.current)}</Text>
</View>
<View style={styles.touchArea}>
<View pointerEvents="none" aria-hidden={true} style={styles.rail}>
<View style={[styles.line, styles.progress, locked && styles.inactive, { flexGrow: ratio }]} />
<View style={[styles.thumb, focused && !locked && styles.focused, locked && styles.disabledThumb]} />
<View style={[styles.line, styles.remaining, { flexGrow: 1 - ratio }]} />
</View>
{children}
</View>
</View>
);
}
export function Slider({
label, value, onValueChange, onSlidingComplete, minimumValue = 0, maximumValue = 100, step = 1, disabled = false, formatValue, copyTestIDs,
}: SliderProps & CopyInkProps) {
const [width, setWidth] = useState(0);
const [focused, setFocused] = useState(false);
const range = getRange(value, minimumValue, maximumValue, step);
const locked = disabled || range.last === 0;
const adjusted = useRef<number | undefined>(undefined);
const webGesture = useRef(false);
function complete() {
const next = adjusted.current;
adjusted.current = undefined;
if (!locked && next !== undefined) onSlidingComplete?.(next);
}
function endWebGesture() {
webGesture.current = false;
complete();
}
function select(index: number) {
const next = range.at(Math.max(0, Math.min(range.last, Math.round(index))));
if (!locked && next !== range.current) {
adjusted.current = next;
onValueChange(next);
}
}
function touch(event: GestureResponderEvent) {
if (width <= thumbSize) return;
const fraction = (event.nativeEvent.locationX - thumbSize / 2) / (width - thumbSize);
select((I18nManager.isRTL ? 1 - fraction : fraction) * range.last);
}
const description = formatValue ? formatValue(range.current) : String(range.current);
const input = Platform.OS === 'web'
// The browser's range maps the pointer through CSS transforms and handles keyboard, touch and drag capture.
? createElement('input', {
type: 'range', min: 0, max: range.last, step: 1, value: range.index, disabled: locked,
'aria-label': label, 'aria-valuemin': range.start, 'aria-valuemax': range.end, 'aria-valuenow': range.current, 'aria-valuetext': description,
onPointerDown: () => { webGesture.current = true; }, onPointerUp: endWebGesture,
onPointerCancel: () => { webGesture.current = false; adjusted.current = undefined; },
onKeyDown: () => { webGesture.current = true; }, onKeyUp: endWebGesture,
onChange: (event: { currentTarget: { value: string } }) => {
select(Number(event.currentTarget.value));
if (!webGesture.current) complete(); // Some assistive technology changes a range without pressing a key.
},
onFocus: () => setFocused(true), onBlur: () => { setFocused(false); endWebGesture(); },
style: { position: 'absolute', inset: 0, width: '100%', height: '100%', margin: 0, opacity: 0, cursor: locked ? 'default' : 'pointer' },
})
: (
<View
accessible accessibilityRole="adjustable" accessibilityLabel={label}
accessibilityValue={{ min: range.start, max: range.end, now: range.current, text: description }}
accessibilityState={{ disabled: locked }}
accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
onAccessibilityAction={({ nativeEvent }) => {
if (nativeEvent.actionName === 'increment') select(range.index + 1);
if (nativeEvent.actionName === 'decrement') select(range.index - 1);
complete();
}}
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
onStartShouldSetResponder={() => !locked}
onResponderGrant={(event) => { setFocused(true); touch(event); }}
onResponderMove={touch}
onResponderRelease={() => { setFocused(false); complete(); }}
onResponderTerminate={() => { setFocused(false); adjusted.current = undefined; }}
style={StyleSheet.absoluteFill}
/>
);
return (
<SliderFace label={label} value={value} minimumValue={minimumValue} maximumValue={maximumValue}
step={step} disabled={locked} focused={focused} formatValue={formatValue} copyTestIDs={copyTestIDs}>
{input}
</SliderFace>
);
}
const styles = StyleSheet.create({
root: { width: '100%' },
caption: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'baseline', gap: 12 },
label: { flexShrink: 1, color: colors.onSurface, fontSize: 16, fontWeight: '600' },
value: { color: colors.primary, fontSize: 16, fontWeight: '600', fontVariant: ['tabular-nums'] },
muted: { color: colors.onSurfaceVariant },
touchArea: { height: 48, justifyContent: 'center' },
rail: { flexDirection: 'row', alignItems: 'center' },
line: { height: 6, flexBasis: 0, borderRadius: 3 },
progress: { backgroundColor: colors.primary },
remaining: { backgroundColor: colors.outlineVariant },
inactive: { backgroundColor: colors.outline },
thumb: { width: thumbSize, height: thumbSize, borderRadius: thumbSize / 2, borderWidth: 3, borderColor: colors.primary, backgroundColor: colors.surface },
focused: { borderWidth: 5 },
disabledThumb: { borderColor: colors.outline, backgroundColor: colors.surfaceVariant },
});
import { createElement, useRef, useState, type ReactNode } from 'react';
import { I18nManager, Platform, StyleSheet, Text, View, type GestureResponderEvent } from 'react-native';
import { colors } from './theme';
export type SliderProps = {
/** Visible name and accessible label. */
label: string;
/** Current numeric value, controlled by the parent. */
value: number;
/** Called with a bounded, stepped value after a tap, drag or keyboard adjustment. */
onValueChange: (value: number) => void;
/** Called once after finishing a drag or keyboard adjustment that changed the value. */
onSlidingComplete?: (value: number) => void;
/** Smallest selectable value. */
minimumValue?: number;
/** Largest selectable value. Both endpoints can always be reached. */
maximumValue?: number;
/** Distance between stops, measured from minimumValue. Must be positive. */
step?: number;
/** Keep the current value visible and prevent edits. */
disabled?: boolean;
/** Convert a value to a label with units, for example 65%. */
formatValue?: (value: number) => string;
};
function getRange(value: number, min: number, max: number, step: number) {
const start = Number.isFinite(min) ? min : 0;
const end = Number.isFinite(max) ? Math.max(start, max) : start;
const stride = Number.isFinite(step) && step > 0 ? step : 1;
const last = Math.ceil(Number(((end - start) / stride).toPrecision(14)));
const at = (index: number) => index >= last ? end : Number((start + Math.max(0, index) * stride).toPrecision(15));
const bounded = Number.isFinite(value) ? Math.min(end, Math.max(start, value)) : start;
const nearest = Math.min(last, Math.round((bounded - start) / stride));
// The last interval can be shorter when the span is not divisible by the step.
const index = end - bounded < Math.abs(bounded - at(nearest)) ? last : nearest;
return { start, end, last, index, at, current: at(index) };
}
type CopyInkProps = { copyTestIDs?: { label?: string } };
type FaceProps = Omit<SliderProps, 'onValueChange' | 'onSlidingComplete'> & CopyInkProps & { focused?: boolean; children?: ReactNode };
const thumbSize = 22;
export function SliderFace({ label, value, minimumValue = 0, maximumValue = 100, step = 1, disabled, formatValue, focused, children, copyTestIDs }: FaceProps) {
const range = getRange(value, minimumValue, maximumValue, step);
const ratio = range.last ? range.index / range.last : 0;
const locked = disabled || !range.last;
return (
<View style={styles.root}>
<View aria-hidden={!!children} style={styles.caption}>
<Text testID={copyTestIDs?.label} style={[styles.label, locked && styles.muted]}>{label}</Text>
<Text style={[styles.value, locked && styles.muted]}>{formatValue ? formatValue(range.current) : String(range.current)}</Text>
</View>
<View style={styles.touchArea}>
<View pointerEvents="none" aria-hidden={true} style={styles.rail}>
<View style={[styles.line, styles.progress, locked && styles.inactive, { flexGrow: ratio }]} />
<View style={[styles.thumb, focused && !locked && styles.focused, locked && styles.disabledThumb]} />
<View style={[styles.line, styles.remaining, { flexGrow: 1 - ratio }]} />
</View>
{children}
</View>
</View>
);
}
export function Slider({
label, value, onValueChange, onSlidingComplete, minimumValue = 0, maximumValue = 100, step = 1, disabled = false, formatValue, copyTestIDs,
}: SliderProps & CopyInkProps) {
const [width, setWidth] = useState(0);
const [focused, setFocused] = useState(false);
const range = getRange(value, minimumValue, maximumValue, step);
const locked = disabled || range.last === 0;
const adjusted = useRef<number | undefined>(undefined);
const webGesture = useRef(false);
function complete() {
const next = adjusted.current;
adjusted.current = undefined;
if (!locked && next !== undefined) onSlidingComplete?.(next);
}
function endWebGesture() {
webGesture.current = false;
complete();
}
function select(index: number) {
const next = range.at(Math.max(0, Math.min(range.last, Math.round(index))));
if (!locked && next !== range.current) {
adjusted.current = next;
onValueChange(next);
}
}
function touch(event: GestureResponderEvent) {
if (width <= thumbSize) return;
const fraction = (event.nativeEvent.locationX - thumbSize / 2) / (width - thumbSize);
select((I18nManager.isRTL ? 1 - fraction : fraction) * range.last);
}
const description = formatValue ? formatValue(range.current) : String(range.current);
const input = Platform.OS === 'web'
// The browser's range maps the pointer through CSS transforms and handles keyboard, touch and drag capture.
? createElement('input', {
type: 'range', min: 0, max: range.last, step: 1, value: range.index, disabled: locked,
'aria-label': label, 'aria-valuemin': range.start, 'aria-valuemax': range.end, 'aria-valuenow': range.current, 'aria-valuetext': description,
onPointerDown: () => { webGesture.current = true; }, onPointerUp: endWebGesture,
onPointerCancel: () => { webGesture.current = false; adjusted.current = undefined; },
onKeyDown: () => { webGesture.current = true; }, onKeyUp: endWebGesture,
onChange: (event: { currentTarget: { value: string } }) => {
select(Number(event.currentTarget.value));
if (!webGesture.current) complete(); // Some assistive technology changes a range without pressing a key.
},
onFocus: () => setFocused(true), onBlur: () => { setFocused(false); endWebGesture(); },
style: { position: 'absolute', inset: 0, width: '100%', height: '100%', margin: 0, opacity: 0, cursor: locked ? 'default' : 'pointer' },
})
: (
<View
accessible accessibilityRole="adjustable" accessibilityLabel={label}
accessibilityValue={{ min: range.start, max: range.end, now: range.current, text: description }}
accessibilityState={{ disabled: locked }}
accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
onAccessibilityAction={({ nativeEvent }) => {
if (nativeEvent.actionName === 'increment') select(range.index + 1);
if (nativeEvent.actionName === 'decrement') select(range.index - 1);
complete();
}}
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
onStartShouldSetResponder={() => !locked}
onResponderGrant={(event) => { setFocused(true); touch(event); }}
onResponderMove={touch}
onResponderRelease={() => { setFocused(false); complete(); }}
onResponderTerminate={() => { setFocused(false); adjusted.current = undefined; }}
style={StyleSheet.absoluteFill}
/>
);
return (
<SliderFace label={label} value={value} minimumValue={minimumValue} maximumValue={maximumValue}
step={step} disabled={locked} focused={focused} formatValue={formatValue} copyTestIDs={copyTestIDs}>
{input}
</SliderFace>
);
}
const styles = StyleSheet.create({
root: { width: '100%' },
caption: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'baseline', gap: 12 },
label: { flexShrink: 1, color: colors.onSurface, fontSize: 16, fontWeight: '600' },
value: { color: colors.primary, fontSize: 16, fontWeight: '600', fontVariant: ['tabular-nums'] },
muted: { color: colors.onSurfaceVariant },
touchArea: { height: 48, justifyContent: 'center' },
rail: { flexDirection: 'row', alignItems: 'center' },
line: { height: 6, flexBasis: 0, borderRadius: 3 },
progress: { backgroundColor: colors.primary },
remaining: { backgroundColor: colors.outlineVariant },
inactive: { backgroundColor: colors.outline },
thumb: { width: thumbSize, height: thumbSize, borderRadius: thumbSize / 2, borderWidth: 3, borderColor: colors.primary, backgroundColor: colors.surface },
focused: { borderWidth: 5 },
disabledThumb: { borderColor: colors.outline, backgroundColor: colors.surfaceVariant },
});
// 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 { Slider } from '@/components/mobilens/Slider';
export function VoiceVolume() {
const [volume, setVolume] = useState(70);
return (
<Slider label="Voice volume" value={volume} minimumValue={0} maximumValue={100}
step={5} formatValue={(level) => `${level}%`} onValueChange={setVolume} />
);
}
import { useState } from 'react';
import { Slider } from '@/components/mobilens/Slider';
export function VoiceVolume() {
const [volume, setVolume] = useState(70);
return (
<Slider label="Voice volume" value={volume} minimumValue={0} maximumValue={100}
step={5} formatValue={(level) => `${level}%`} onValueChange={setVolume} />
);
}
API
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | – | Visible name and accessible label. |
| value | number | – | Current numeric value, controlled by the parent. |
| onValueChange | (value: number) => void | – | Called with a bounded, stepped value after a tap, drag or keyboard adjustment. |
| onSlidingComplete? | (value: number) => void | – | Called once after finishing a drag or keyboard adjustment that changed the value. |
| minimumValue? | number | 0 | Smallest selectable value. |
| maximumValue? | number | 100 | Largest selectable value. Both endpoints can always be reached. |
| step? | number | 1 | Distance between stops, measured from minimumValue. Must be positive. |
| disabled? | boolean | false | Keep the current value visible and prevent edits. |
| formatValue? | (value: number) => string | – | Convert a value to a label with units, for example 65%. |
Add the "Slider" 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/Slider.tsx ---
import { createElement, useRef, useState, type ReactNode } from 'react';
import { I18nManager, Platform, StyleSheet, Text, View, type GestureResponderEvent } from 'react-native';
import { colors } from './theme';
export type SliderProps = {
/** Visible name and accessible label. */
label: string;
/** Current numeric value, controlled by the parent. */
value: number;
/** Called with a bounded, stepped value after a tap, drag or keyboard adjustment. */
onValueChange: (value: number) => void;
/** Called once after finishing a drag or keyboard adjustment that changed the value. */
onSlidingComplete?: (value: number) => void;
/** Smallest selectable value. */
minimumValue?: number;
/** Largest selectable value. Both endpoints can always be reached. */
maximumValue?: number;
/** Distance between stops, measured from minimumValue. Must be positive. */
step?: number;
/** Keep the current value visible and prevent edits. */
disabled?: boolean;
/** Convert a value to a label with units, for example 65%. */
formatValue?: (value: number) => string;
};
function getRange(value: number, min: number, max: number, step: number) {
const start = Number.isFinite(min) ? min : 0;
const end = Number.isFinite(max) ? Math.max(start, max) : start;
const stride = Number.isFinite(step) && step > 0 ? step : 1;
const last = Math.ceil(Number(((end - start) / stride).toPrecision(14)));
const at = (index: number) => index >= last ? end : Number((start + Math.max(0, index) * stride).toPrecision(15));
const bounded = Number.isFinite(value) ? Math.min(end, Math.max(start, value)) : start;
const nearest = Math.min(last, Math.round((bounded - start) / stride));
// The last interval can be shorter when the span is not divisible by the step.
const index = end - bounded < Math.abs(bounded - at(nearest)) ? last : nearest;
return { start, end, last, index, at, current: at(index) };
}
type CopyInkProps = { copyTestIDs?: { label?: string } };
type FaceProps = Omit<SliderProps, 'onValueChange' | 'onSlidingComplete'> & CopyInkProps & { focused?: boolean; children?: ReactNode };
const thumbSize = 22;
export function SliderFace({ label, value, minimumValue = 0, maximumValue = 100, step = 1, disabled, formatValue, focused, children, copyTestIDs }: FaceProps) {
const range = getRange(value, minimumValue, maximumValue, step);
const ratio = range.last ? range.index / range.last : 0;
const locked = disabled || !range.last;
return (
<View style={styles.root}>
<View aria-hidden={!!children} style={styles.caption}>
<Text testID={copyTestIDs?.label} style={[styles.label, locked && styles.muted]}>{label}</Text>
<Text style={[styles.value, locked && styles.muted]}>{formatValue ? formatValue(range.current) : String(range.current)}</Text>
</View>
<View style={styles.touchArea}>
<View pointerEvents="none" aria-hidden={true} style={styles.rail}>
<View style={[styles.line, styles.progress, locked && styles.inactive, { flexGrow: ratio }]} />
<View style={[styles.thumb, focused && !locked && styles.focused, locked && styles.disabledThumb]} />
<View style={[styles.line, styles.remaining, { flexGrow: 1 - ratio }]} />
</View>
{children}
</View>
</View>
);
}
export function Slider({
label, value, onValueChange, onSlidingComplete, minimumValue = 0, maximumValue = 100, step = 1, disabled = false, formatValue, copyTestIDs,
}: SliderProps & CopyInkProps) {
const [width, setWidth] = useState(0);
const [focused, setFocused] = useState(false);
const range = getRange(value, minimumValue, maximumValue, step);
const locked = disabled || range.last === 0;
const adjusted = useRef<number | undefined>(undefined);
const webGesture = useRef(false);
function complete() {
const next = adjusted.current;
adjusted.current = undefined;
if (!locked && next !== undefined) onSlidingComplete?.(next);
}
function endWebGesture() {
webGesture.current = false;
complete();
}
function select(index: number) {
const next = range.at(Math.max(0, Math.min(range.last, Math.round(index))));
if (!locked && next !== range.current) {
adjusted.current = next;
onValueChange(next);
}
}
function touch(event: GestureResponderEvent) {
if (width <= thumbSize) return;
const fraction = (event.nativeEvent.locationX - thumbSize / 2) / (width - thumbSize);
select((I18nManager.isRTL ? 1 - fraction : fraction) * range.last);
}
const description = formatValue ? formatValue(range.current) : String(range.current);
const input = Platform.OS === 'web'
// The browser's range maps the pointer through CSS transforms and handles keyboard, touch and drag capture.
? createElement('input', {
type: 'range', min: 0, max: range.last, step: 1, value: range.index, disabled: locked,
'aria-label': label, 'aria-valuemin': range.start, 'aria-valuemax': range.end, 'aria-valuenow': range.current, 'aria-valuetext': description,
onPointerDown: () => { webGesture.current = true; }, onPointerUp: endWebGesture,
onPointerCancel: () => { webGesture.current = false; adjusted.current = undefined; },
onKeyDown: () => { webGesture.current = true; }, onKeyUp: endWebGesture,
onChange: (event: { currentTarget: { value: string } }) => {
select(Number(event.currentTarget.value));
if (!webGesture.current) complete(); // Some assistive technology changes a range without pressing a key.
},
onFocus: () => setFocused(true), onBlur: () => { setFocused(false); endWebGesture(); },
style: { position: 'absolute', inset: 0, width: '100%', height: '100%', margin: 0, opacity: 0, cursor: locked ? 'default' : 'pointer' },
})
: (
<View
accessible accessibilityRole="adjustable" accessibilityLabel={label}
accessibilityValue={{ min: range.start, max: range.end, now: range.current, text: description }}
accessibilityState={{ disabled: locked }}
accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
onAccessibilityAction={({ nativeEvent }) => {
if (nativeEvent.actionName === 'increment') select(range.index + 1);
if (nativeEvent.actionName === 'decrement') select(range.index - 1);
complete();
}}
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
onStartShouldSetResponder={() => !locked}
onResponderGrant={(event) => { setFocused(true); touch(event); }}
onResponderMove={touch}
onResponderRelease={() => { setFocused(false); complete(); }}
onResponderTerminate={() => { setFocused(false); adjusted.current = undefined; }}
style={StyleSheet.absoluteFill}
/>
);
return (
<SliderFace label={label} value={value} minimumValue={minimumValue} maximumValue={maximumValue}
step={step} disabled={locked} focused={focused} formatValue={formatValue} copyTestIDs={copyTestIDs}>
{input}
</SliderFace>
);
}
const styles = StyleSheet.create({
root: { width: '100%' },
caption: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'baseline', gap: 12 },
label: { flexShrink: 1, color: colors.onSurface, fontSize: 16, fontWeight: '600' },
value: { color: colors.primary, fontSize: 16, fontWeight: '600', fontVariant: ['tabular-nums'] },
muted: { color: colors.onSurfaceVariant },
touchArea: { height: 48, justifyContent: 'center' },
rail: { flexDirection: 'row', alignItems: 'center' },
line: { height: 6, flexBasis: 0, borderRadius: 3 },
progress: { backgroundColor: colors.primary },
remaining: { backgroundColor: colors.outlineVariant },
inactive: { backgroundColor: colors.outline },
thumb: { width: thumbSize, height: thumbSize, borderRadius: thumbSize / 2, borderWidth: 3, borderColor: colors.primary, backgroundColor: colors.surface },
focused: { borderWidth: 5 },
disabledThumb: { borderColor: colors.outline, backgroundColor: colors.surfaceVariant },
});
--- end of Slider.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { Slider } from '@/components/mobilens/Slider';
export function VoiceVolume() {
const [volume, setVolume] = useState(70);
return (
<Slider label="Voice volume" value={volume} minimumValue={0} maximumValue={100}
step={5} formatValue={(level) => `${level}%`} onValueChange={setVolume} />
);
}
STATES: all 5 must work in the screen where you use it
- Set: when the value falls between the endpoints.. The thumb position and displayed value correspond to the same step.
- Focused: when the track receives focus.. Keyboard and assistive input can adjust a focused slider.
- Minimum: when the value reaches the lower bound.. Further decreases keep the value at the minimum.
- Maximum: when the value reaches the upper bound.. Further increases keep the value at the maximum.
- Disabled: when the mix is locked.. The current value remains visible and adjustment is unavailable.
ACCESSIBILITY
- The slider exposes its accessible name, range, current value and disabled state.
- Screen readers can adjust the value by one step without dragging.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.