Skip to content

Send files directly between devices over the internet - no cloud storage, no size limits.

Add native switch component to mobile

Denis Lupookov601ac4ed32e8
diff --git a/knip.json b/knip.json
index 7964054..eec5511 100644
--- a/knip.json
+++ b/knip.json
@@ -25,7 +25,7 @@
"packages/components": {
"entry": ["scripts/*.mjs", ".storybook/{main,preview}.{ts,tsx}", "src/**/*.stories.{ts,tsx}"],
"project": ["src/**/*.{ts,tsx}", "scripts/**/*.mjs"],
- "ignoreDependencies": ["lucide-react", "lucide-react-native"],
+ "ignoreDependencies": ["lucide-react", "lucide-react-native", "@expo/ui"],
"ignoreUnresolved": ["^\\./DragSurface$", "^\\./useIconColor$", "^\\./lucide$"]
},
"apps/desktop": {
diff --git a/packages/components/package.json b/packages/components/package.json
index 16cbbb1..6542483 100644
--- a/packages/components/package.json
+++ b/packages/components/package.json
@@ -55,12 +55,16 @@
"react": "^19.0.0",
"react-dom": "^19.0.0",
"lucide-react": "*",
- "lucide-react-native": "*"
+ "lucide-react-native": "*",
+ "@expo/ui": "*"
},
"peerDependenciesMeta": {
"react-dom": {
"optional": true
},
+ "@expo/ui": {
+ "optional": true
+ },
"lucide-react": {
"optional": true
},
diff --git a/packages/components/src/components/ToggleSwitch/ToggleSwitch.android.tsx b/packages/components/src/components/ToggleSwitch/ToggleSwitch.android.tsx
new file mode 100644
index 0000000..78af850
--- /dev/null
+++ b/packages/components/src/components/ToggleSwitch/ToggleSwitch.android.tsx
@@ -0,0 +1,53 @@
+import { Host, Switch as ComposeSwitch } from '@expo/ui/jetpack-compose'
+import { StyleSheet } from 'react-native'
+import { useTheme } from '../../theme'
+import { ToggleSwitchRow } from './ToggleSwitchRow'
+import type { ToggleSwitchProps } from './types'
+
+export function ToggleSwitch({
+ checked = false,
+ onChange,
+ label,
+ description,
+ disabled = false,
+ 'aria-label': ariaLabel
+}: ToggleSwitchProps) {
+ const { theme } = useTheme()
+ const c = theme.colors
+
+ const handleToggle = () => {
+ if (!disabled && onChange) onChange(!checked)
+ }
+
+ return (
+ <ToggleSwitchRow
+ label={label}
+ description={description}
+ disabled={disabled}
+ accessibilityLabel={ariaLabel ?? label}
+ onToggle={handleToggle}
+ >
+ <Host style={styles.host}>
+ <ComposeSwitch
+ value={checked}
+ enabled={!disabled}
+ onCheckedChange={onChange}
+ colors={{
+ checkedTrackColor: c.colorSuccess,
+ checkedThumbColor: c.colorBackground,
+ checkedBorderColor: c.colorSuccess,
+ checkedIconColor: c.colorSuccess,
+ uncheckedTrackColor: c.colorBackgroundSubtle,
+ uncheckedThumbColor: c.colorBorderStrong,
+ uncheckedBorderColor: c.colorBorderStrong,
+ uncheckedIconColor: c.colorBackgroundSubtle
+ }}
+ />
+ </Host>
+ </ToggleSwitchRow>
+ )
+}
+
+const styles = StyleSheet.create({
+ host: { width: 52, height: 32 }
+})
diff --git a/packages/components/src/components/ToggleSwitch/ToggleSwitch.ios.tsx b/packages/components/src/components/ToggleSwitch/ToggleSwitch.ios.tsx
new file mode 100644
index 0000000..0797333
--- /dev/null
+++ b/packages/components/src/components/ToggleSwitch/ToggleSwitch.ios.tsx
@@ -0,0 +1,43 @@
+import { Host, Toggle } from '@expo/ui/swift-ui'
+import { disabled as disabledModifier, labelsHidden, tint } from '@expo/ui/swift-ui/modifiers'
+import { StyleSheet } from 'react-native'
+import { useTheme } from '../../theme'
+import { ToggleSwitchRow } from './ToggleSwitchRow'
+import type { ToggleSwitchProps } from './types'
+
+export function ToggleSwitch({
+ checked = false,
+ onChange,
+ label,
+ description,
+ disabled = false,
+ 'aria-label': ariaLabel
+}: ToggleSwitchProps) {
+ const { theme } = useTheme()
+
+ const handleToggle = () => {
+ if (!disabled && onChange) onChange(!checked)
+ }
+
+ return (
+ <ToggleSwitchRow
+ label={label}
+ description={description}
+ disabled={disabled}
+ accessibilityLabel={ariaLabel ?? label}
+ onToggle={handleToggle}
+ >
+ <Host style={styles.host}>
+ <Toggle
+ isOn={checked}
+ onIsOnChange={onChange}
+ modifiers={[labelsHidden(), tint(theme.colors.colorSuccess), disabledModifier(disabled)]}
+ />
+ </Host>
+ </ToggleSwitchRow>
+ )
+}
+
+const styles = StyleSheet.create({
+ host: { width: 51, height: 31 }
+})
diff --git a/packages/components/src/components/ToggleSwitch/ToggleSwitch.tsx b/packages/components/src/components/ToggleSwitch/ToggleSwitch.tsx
index 138e916..640d06f 100644
--- a/packages/components/src/components/ToggleSwitch/ToggleSwitch.tsx
+++ b/packages/components/src/components/ToggleSwitch/ToggleSwitch.tsx
@@ -1,17 +1,11 @@
import type { ComponentProps } from 'react'
import { html } from 'react-strict-dom'
import { styles } from './styles'
+import type { ToggleSwitchProps } from './types'
type HtmlDivProps = ComponentProps<typeof html.div>
-export type ToggleSwitchProps = Omit<HtmlDivProps, 'children'> & {
- checked?: boolean
- onChange?: (checked: boolean) => void
- label?: string
- description?: string
- disabled?: boolean
- 'aria-label'?: string
-}
+type Props = ToggleSwitchProps & Omit<HtmlDivProps, 'children'>
export function ToggleSwitch({
checked = false,
@@ -21,7 +15,7 @@ export function ToggleSwitch({
disabled = false,
'aria-label': ariaLabel,
...rest
-}: ToggleSwitchProps) {
+}: Props) {
const handleToggle = () => {
if (!disabled && onChange) onChange(!checked)
}
diff --git a/packages/components/src/components/ToggleSwitch/ToggleSwitchRow.tsx b/packages/components/src/components/ToggleSwitch/ToggleSwitchRow.tsx
new file mode 100644
index 0000000..6385010
--- /dev/null
+++ b/packages/components/src/components/ToggleSwitch/ToggleSwitchRow.tsx
@@ -0,0 +1,65 @@
+import type { ReactNode } from 'react'
+import { Pressable, StyleSheet, Text, View } from 'react-native'
+import { useTheme } from '../../theme'
+
+export interface ToggleSwitchRowProps {
+ label?: string
+ description?: string
+ disabled: boolean
+ accessibilityLabel?: string
+ onToggle: () => void
+ children: ReactNode
+}
+
+export function ToggleSwitchRow({
+ label,
+ description,
+ disabled,
+ accessibilityLabel,
+ onToggle,
+ children
+}: ToggleSwitchRowProps) {
+ const { theme } = useTheme()
+ const c = theme.colors
+ const hasDetails = Boolean(label) || Boolean(description)
+
+ return (
+ <View
+ style={[styles.root, disabled ? styles.disabled : null]}
+ accessibilityLabel={accessibilityLabel}
+ >
+ {hasDetails ? (
+ <Pressable style={styles.details} onPress={onToggle} disabled={disabled}>
+ {label ? (
+ <Text style={[styles.label, { color: c.colorTextPrimary }]}>{label}</Text>
+ ) : null}
+ {description ? (
+ <Text style={[styles.description, { color: c.colorTextMuted }]}>{description}</Text>
+ ) : null}
+ </Pressable>
+ ) : null}
+ {children}
+ </View>
+ )
+}
+
+const styles = StyleSheet.create({
+ root: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ justifyContent: 'space-between',
+ alignSelf: 'stretch',
+ gap: 12
+ },
+ disabled: { opacity: 0.5 },
+ details: {
+ flexGrow: 1,
+ flexShrink: 1,
+ flexBasis: 0,
+ justifyContent: 'center',
+ alignItems: 'flex-start',
+ gap: 4
+ },
+ label: { fontSize: 14, fontWeight: '500', lineHeight: 20 },
+ description: { fontSize: 12, lineHeight: 17 }
+})
diff --git a/packages/components/src/components/ToggleSwitch/index.ts b/packages/components/src/components/ToggleSwitch/index.ts
index 931d6e7..12dc6b8 100644
--- a/packages/components/src/components/ToggleSwitch/index.ts
+++ b/packages/components/src/components/ToggleSwitch/index.ts
@@ -1,2 +1,2 @@
export { ToggleSwitch } from './ToggleSwitch'
-export type { ToggleSwitchProps } from './ToggleSwitch'
+export type { ToggleSwitchProps } from './types'
diff --git a/packages/components/src/components/ToggleSwitch/types.ts b/packages/components/src/components/ToggleSwitch/types.ts
new file mode 100644
index 0000000..ccef65d
--- /dev/null
+++ b/packages/components/src/components/ToggleSwitch/types.ts
@@ -0,0 +1,8 @@
+export interface ToggleSwitchProps {
+ checked?: boolean
+ onChange?: (checked: boolean) => void
+ label?: string
+ description?: string
+ disabled?: boolean
+ 'aria-label'?: string
+}