A React Native library for retrieving dynamic themes from Android devices, ensuring seamless theme adaptation for a better user experience.
- π± System-Aware: Automatically adapts to the system's light or dark mode.
- π¨ Material You Support: Leverages Android's dynamic theming for a native feel.
- β‘ Optimized Performance: Uses efficient hooks and memoization to minimize re-renders.
- π Simple API: Easy integration with just a few lines of code.
Install the package using npm:
npm install react-native-dynamic-theme
Import the necessary dependencies and use the library to retrieve the dynamic color scheme based on the system theme:
import { useMemo } from 'react';
import { StyleSheet, Text, useColorScheme, View } from 'react-native';
import {
getDynamicColorScheme,
type DynamicColorScheme,
type ColorScheme,
} from 'react-native-dynamic-theme';
const SampleUsage = () => {
const dynamicColorScheme = getDynamicColorScheme() as DynamicColorScheme;
const systemColorScheme = useColorScheme();
const colors = useMemo<ColorScheme>(() => {
return systemColorScheme === 'dark'
? dynamicColorScheme.dark
: dynamicColorScheme.light;
}, [systemColorScheme]);
if (!dynamicColorScheme) {
return (
<View style={styles.container}>
<Text>iOS Device (not supported)</Text>
</View>
);
}
return (
<View style={[styles.container, { backgroundColor: colors.background }]}>
<Text style={[styles.title, { color: colors.onBackground }]}>
Welcome to Dynamic Theme
</Text>
<Text style={[styles.subtitle, { color: colors.onSurface }]}>
This app dynamically adapts to your system theme.
</Text>
<View style={[styles.button, { backgroundColor: colors.primary }]}>
<Text style={[styles.buttonText, { color: colors.onPrimary }]}>
Get Started
</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 16,
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 8,
},
subtitle: {
fontSize: 16,
opacity: 0.7,
textAlign: 'center',
marginBottom: 16,
},
button: {
paddingVertical: 12,
paddingHorizontal: 24,
borderRadius: 8,
elevation: 4,
},
buttonText: {
fontWeight: 'bold',
},
});
export default SampleUsage;
Retrieves the dynamic color scheme based on the system's theme.
- A
DynamicColorScheme
object containing light and dark mode colors. - Returns
null
on unsupported platforms (e.g., iOS).
import { getDynamicColorScheme } from 'react-native-dynamic-theme';
const dynamicColors = getDynamicColorScheme();
if (dynamicColors) {
console.log(dynamicColors.light.primary); // Light mode primary color
console.log(dynamicColors.dark.primary); // Dark mode primary color
} else {
console.log("Dynamic theming not supported on this device.");
}
- On Android 12+, it fetches colors from the systemβs Material You theme.
- On older Android versions, it falls back to predefined light and dark themes.
- On iOS, it returns
null
(iOS support planned in future updates).
Represents the color scheme used in both light and dark modes.
type ColorScheme = {
primary: string;
onPrimary: string;
primaryContainer: string;
onPrimaryContainer: string;
inversePrimary: string;
secondary: string;
onSecondary: string;
secondaryContainer: string;
onSecondaryContainer: string;
tertiary: string;
onTertiary: string;
tertiaryContainer: string;
onTertiaryContainer: string;
background: string;
onBackground: string;
surface: string;
onSurface: string;
surfaceVariant: string;
onSurfaceVariant: string;
inverseSurface: string;
inverseOnSurface: string;
outline: string;
outlineVariant: string;
surfaceBright: string;
surfaceDim: string;
surfaceContainer: string;
surfaceContainerHigh: string;
surfaceContainerHighest: string;
surfaceContainerLow: string;
surfaceContainerLowest: string;
surfaceTint: string;
};
Defines the dynamic color scheme containing both light and dark mode configurations.
type DynamicColorScheme = {
light: ColorScheme;
dark: ColorScheme;
};
- β Initial Release
- π Support for iOS (Planned)
- π Custom Theme Overrides
- π Expo Compatibility
We welcome contributions! To get started, check out our contributing guide.
This project is licensed under the MIT License.
Built with β€οΈ and create-react-native-library.
- πΉ Enhanced Methods section with a clear explanation, return type, and example.
- πΉ Better structure & readability for improved developer experience.
- πΉ Explicit platform behavior (Android 12+, older Android, iOS).
- πΉ Error handling for unsupported platforms (e.g., iOS).