Skip to content

Unistyles Runtime

UnistylesRuntime

Unistyles Runtime is a powerful feature that allows you to access platform specific values directly from JavaScript. It allows you to skip many dependencies and keep a lot of functionality under one object.

Usage

You can import UnistylesRuntime from react-native-unistyles:

import { UnistylesRuntime } from 'react-native-unistyles'

and use it anywhere in your code, even outside a React component.

Available getters

NameTypeDescription
colorSchemestringGet your device’s color scheme. Available options dark, light or unspecified
hasAdaptiveThemesbooleanIndicates if you have enabled adaptive themes
themeNamestring?Name of the selected theme or undefined if you haven’t register any theme
breakpointstring?Current breakpoint or undefined if you haven’t registered any
breakpointsObjectYour registered breakpoints
screen{width: number, height: number}Screen dimensions
isPortraitbooleanIndicates if your device is in portrait mode
isLandscapebooleanIndicates if your device is in landscape mode
contentSizeCategoryIOSContentSizeCategory or AndroidContentSizeCategoryYour device’s content size category
insets{ top: number, bottom: number, left: number, right: number, ime: number }Device insets which are safe to put content into
statusBar{width: number, height: number}Status bar dimensions
navigationBar{width: number, height: number}Navigation bar dimensions (Android)
pixelRationumberPixel density of the device
fontScalenumberFont scale of the device
rtlbooleanIndicates if the device is in RTL mode
getTheme(themeName?: string) => ThemeGet theme by name or current theme if not specified

Setters

NameTypeDescription
setTheme(themeName: string) => voidChange the current theme
setAdaptiveThemes(enabled: boolean) => voidToggle adaptive themes
updateTheme(themeName: string, updater: (currentTheme: Theme) => Theme) => voidUpdate the theme at runtime
statusBar.setHidden(hidden: boolean) => voidShow/hide status bar at runtime
navigationBar.setHidden(hidden: boolean) => voidShow/hide navigation bar at runtime
setImmersiveMode(enabled: boolean) => voidEnable/disable immersive mode (hiding both status and navigation bars)
setRootViewBackgroundColor(color: string) => voidset root view background color

Why UnistylesRuntime doesn’t re-render my component?

You should think of UnistylesRuntime as a JavaScript object. It’s not a React hook, so it doesn’t re-render your component when eg. screen size or breakpoint changes. Instead it will return up to date value whenever you access it.

How to re-render my stylesheets based on UnistylesRuntime?

You can do that while accessing miniRuntime in your StyleSheet:

One example could be reading device width and height:

import { StyleSheet } from 'react-native-unistyles'
// your component
const style = StyleSheet.create((theme, rt) => ({
container: {
backgroundColor: theme.colors.background,
width: rt.screen.width,
height: rt.screen.height
}
}))

Learn more on how Unistyles re-calculates your styles.