An open API service indexing awesome lists of open source software.

https://github.com/blaiti/react-native-mixins

A utility-first style mixin helper for clean and responsive UI design.
https://github.com/blaiti/react-native-mixins

react-native typescript

Last synced: 9 months ago
JSON representation

A utility-first style mixin helper for clean and responsive UI design.

Awesome Lists containing this project

README

          

# React Native Mixins

A lightweight utility library for React Native that provides essential mixins and utilities for responsive design, platform-specific styling, and device detection.

## ๐Ÿงช Features

- ๐Ÿ“ฑ **Responsive Scaling**: Scale sizes, fonts, and layouts across different screen sizes
- ๐ŸŽฏ **Platform-Specific Styling**: Easy platform-specific style management
- ๐Ÿ“ **Device Detection**: Detect device types and orientations
- ๐Ÿ“ฆ **Zero Dependencies**: No external dependencies
- โšก **Lightweight**: Minimal bundle size impact

## ๐Ÿ“ฆ Installation

```bash
npm install react-native-mixins
```

or

```bash
yarn add react-native-mixins
```

## ๐Ÿš€ Quick Start

```tsx
import {
sizeScale,
verticalScale,
fontScale,
platformMixin,
} from 'react-native-mixins';

const styles = StyleSheet.create({
container: {
padding: sizeScale(16),
gap: verticalScale(8),
},
text: {
fontSize: fontScale(18),
...platformMixin({
ios: { fontFamily: 'Arial' },
android: { fontFamily: 'Roboto' },
}),
},
});
```

## โš™๏ธ API Reference

### Scaling Utilities

#### `sizeScale(size: number): number`

Scales a size value based on screen width. Uses a guideline base width of 375px.

```tsx
const padding = sizeScale(16); // Responsive padding
const margin = sizeScale(8); // Responsive margin
```

#### `verticalScale(size: number): number`

Scales a size value based on screen height. Uses a guideline base height of 812px.

```tsx
const gap = verticalScale(16); // Responsive vertical spacing
const height = verticalScale(100); // Responsive height
```

#### `fontScale(size: number): number`

Scales font sizes with pixel-perfect rounding for crisp text rendering.

```tsx
const fontSize = fontScale(18); // Responsive font size
```

### Platform Mixin

#### `platformMixin(styles: PlatformStyles): object`

Applies platform-specific styles with fallback support.

```tsx
const styles = StyleSheet.create({
button: {
...platformMixin({
ios: {
backgroundColor: '#007AFF',
borderRadius: 8,
},
android: {
backgroundColor: '#6200EE',
elevation: 2,
},
default: {
backgroundColor: '#000000',
},
}),
},
});
```

### Device Detection

#### `isSmallDevice(): boolean`

Returns `true` if the device width is less than 360px.

```tsx
if (isSmallDevice()) {
// Apply compact layout
}
```

#### `isTablet(): boolean`

Returns `true` if the device width is 768px or greater.

```tsx
if (isTablet()) {
// Apply tablet-specific layout
}
```

## ๐Ÿ“– Guidelines

### Scaling Guidelines

- **Base Width**: 375px (iPhone X/11/12/13/14 width)
- **Base Height**: 812px (iPhone X/11/12/13/14 height)
- **Small Device**: < 360px width
- **Tablet**: โ‰ฅ 768px width

### Best Practices

1. **Use `sizeScale` for horizontal spacing**: padding, margin, width
2. **Use `verticalScale` for vertical spacing**: gap, height, line-height
3. **Use `fontScale` for text sizes**: Ensures crisp rendering
4. **Combine with device detection**: Create adaptive layouts
5. **Platform-specific styling**: Use `platformMixin` for native feel

## โœ๏ธ Contributing

Contributions are welcome! ๐Ÿš€

If you'd like to:

- Fix a bug
- Suggest a new animated component
- Improve performance or API design

Please open an [issue](https://github.com/blaiti/react-native-mixins/issues) or submit a [pull request](https://github.com/blaiti/react-native-mixins/pulls).

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

## ๐Ÿ“„ License

MIT ยฉ [Skander Blaiti](https://github.com/blaiti)

## ๐Ÿ’ฌ Let's Connect!

- [Portfolio](https://www.blaiti.com)
- [Twitter](https://twitter.com/SkanderBlaiti)
- [LinkedIn](https://www.linkedin.com/in/skanderblaiti)