RNUtils is an all-encompassing library designed to provide React Native developers with an extensive suite of tools for creating dynamic styles, animations, and utility functions for an improved developer experience and efficient, clean codebases.
- Features
- Installation
- Animated Components
- Animated Components Usage
- Hooks
- Utility Functions
- API Reference
- Types
- Static Styles Functions
- Platform-Specific Utilities
- Platform-Specific Utilities Usage
- Extensibility
- Contributing
- License
- Acknowledgments
- A range of Animated Components for improved user experiences with smooth animations
- Hooks that tap into Reanimated 3(3.3.0) for fluid animations and gestures
- Regular expressions to identify numeric and percentage-based values
- Utility functions to streamline common style pattern generations
- Type support for TypeScript users to leverage strong typing throughout their styles
To install the library, run the following command in your React Native project:
npm install @ilz5753/rnutils
yarn add @ilz5753/rnutils
The library wraps common React Native components, providing animated versions that can be used interchangeably to enhance your application.
import { ReView, ReText, ReTouchableOpacity } from '@ilz5753/rnutils';
These components act as drop-in replacements for their React Native equivalents, now with support for animated styles generated by Reanimated:
<ReView>
<ReText>This is animated text within an animated view.</ReText>
</ReView>
RNUtils
provides a rich set of hooks for working with shared and derived values, easing the animation process with tools for color, dimension, and number interpolations.
useCacheShareValue
This hook manages a shared value for animations with a caching mechanism that memorizes the previous and current states to facilitate reversed or repeated animations.
useColors
Provides a suite of animated styles based on a single color value for different properties including backgroundColor
, borderColor
, and more.
useNumberSizes
Generate numeric size-based styles like flex
, borderRadius
, or fontSize
that are animatable.
useDimensionSizes
Manipulate dimensions like width
, height
, margin
, and padding
with animated support for fluidly responsive designs.
The library includes numerous utility functions to aid in quick style generation, border radii control, color manipulations, shadow handling, and layout shortcuts, along with others that determine the current operating system for conditional styling logic.
getStyle
Retrieve predefined styles for common needs using design tokens:
const center = getStyle(['aic', 'jcc']); // Align items center, Justify content center
For in-depth documentation on the API, including all methods, hooks, and components, please refer to the included API documentation in the package.
TypeScript users can employ RNUtils’ comprehensive types to enforce strict typing throughout their styling code. The library exports types for every provided function and utility, ensuring that errors can be caught at compile-time.
import type {
CustomDimensionValue,
StrNum,
DerivedColor,
DerivedDimension,
} from '@ilz5753/rnutils';
CustomDimensionValue
Allows either a number or a string percentage value, supporting dynamic dimensions.
StrNum
A union type that embraces both string and number, facilitating flexible API design where both types are commonplace.
DerivedColor
, DerivedDimension
Wrap Reanimated’s DerivedValue for specific use-cases like colors and dimensions, associating strong typing with animated values.
With a focus on flexibility, static functions are provided to create scalable styles with minimal code, perfect for a range of scenarios from theming to dynamic user-driven styles.
Detect the current platform using booleans like isAndroid
or isIos
, or check for mobile environments in general with isMobile
.
Import and use functions directly within your style definitions:
import { backgroundColor, flex, Layout } from '@ilz5753/rnutils';
const styles = {
...backgroundColor('blue'),
...flex(1),
...Layout('50%', '25%'),
};
RNUtils is designed to be extensible, working alongside your existing solutions and third-party libraries, allowing for broad compatibility and comprehensive styling control within React Native.
We welcome contributions to make RNUtils better for everyone. If you’re looking to contribute, please follow the guidelines in CONTRIBUTION.md. Your proposals, bug reports, and pull requests are appreciated.
RNUtils is licensed under the MIT License. See the LICENSE file for more details.
Special thanks to all the contributors and the open-source community for their ongoing support and inspiration.
For more detailed usage and API information, please refer to the individual files pertaining to components, hooks, and utilities you wish to employ.