Unistyles 2.0 brings a C++ core, variants and media queries
Unistyles 2.0, the React Native styling library, is a complete rewrite that addresses problems frequently raised by users. Its announcement, written for the release candidate, describes the changes below.
C++ core and JSI bindings
The core of the library is now written in C++, with JSI bindings communicating with React Native. The library is expected to behave the same on iOS and Android. Unistyles now has its own events instead of relying on React Native, so re-renders happen only when necessary. The author says the new core also lays the groundwork for more fine-tuned features in the future.
No React Context
Because of JSI, React Context is no longer needed, and apps no longer have to be wrapped in the UnistylesTheme component.
TypeScript and DX
TypeScript types have been improved, with Intellisense hints across interactions with the library. The createStyleSheet and useStyles functions from version 1.0 remain available.
UnistylesRuntime
A new UnistylesRuntime API exposes information such as phone orientation, color scheme and screen dimensions. It can also change the theme from anywhere in the app, including outside the React component tree:
import { UnistylesRuntime } from 'react-native-unistyles';
// change the theme in any component
export const ChangeTheme = () => <Button title="Change theme" onPress={() => UnistylesRuntime.setTheme('dark')} />;
Variants
Variants, the most requested feature from the previous version, now have first-class support and work together with media queries and breakpoints.
New media queries
Media queries no longer rely on strings. They are built with the mq util, which resolves to a JavaScript symbol accepted as an object key, for example [mq.only.width(240, 380)].
Plugins
Plugin support allows adding or modifying functionality. Plugins are functions that take a style object and return a modified one:
import type { UnistylesPlugin } from 'react-native-unistyles';
export const myPlugin: UnistylesPlugin = {
name: 'myPlugin',
onParsedStyle: (key, styles, runtime) => {
// parse styles here
},
};
Docs and what's next
A new documentation website, with hand-drawn illustrations by graphic designer Magda, includes a guide for every feature. A roadmap for version 3.0 has already been shared and will evolve based on feedback, and ideas gathered from X (Twitter) are to be shared soon. The project has a community chat server, is looking for contributors, and invites users to submit showcases of apps, UI Kits and other projects using Unistyles.
Links
Source: www.reactnativecrossroads.com