Command palette

@yasinsensoy/native

React Native · APIs and utilities

Responsive layout

Layout info based on the window: breakpoint, orientation, whether it is a tablet.

20 exports

Import

import {
  BREAKPOINTS,
  breakpointOf,
  pickResponsive,
  useScale,
  useResponsive,
  createStyles,
  scaleStyle,
  DEFAULT_SCALING,
  createScaler,
  getScaler,
  scaleSize,
  scaleFont,
} from "@yasinsensoy/native";

Layout info based on the window: breakpoint, orientation, whether it is a tablet. If the window changes while the app is open (a foldable opening, split screen, rotation) the component re-renders.

useResponsive(): ResponsiveInfo

Examples

const { select, isTablet } = useResponsive();
<Grid columns={select({ base: 1, md: 2, xl: 4 })} />

BREAKPOINTS

constant

Breakpoints: the same as the Tailwind scale on web (window width, dp/pt). base covers every width.

BREAKPOINTS: { readonly base: 0; readonly sm: 640; readonly md: 768; readonly lg: 1024; readonly xl: 1280; readonly "2xl": 1536; }

breakpointOf

function

The largest breakpoint the width falls into.

breakpointOf(width: number): Breakpoint

Picks the value from a BreakpointValue based on the width.

pickResponsive(value: BreakpointValue<T>, width: number): T

useScale

function

The active scaler: s(20) for sizes, fs(16) for font sizes. When the window changes (foldable, split screen, rotation) the component re-renders with new values. The setting comes from the UIProvider scaling prop.

useScale(): Scaler

Examples

const { s, fs } = useScale();
<View style={{ padding: s(20), borderRadius: s(12) }}>
  <RNText style={{ fontSize: fs(18) }}>Hello</RNText>
</View>

createStyles

function

Device-scaled, theme-bound styles: the counterpart of StyleSheet.create. The pixel values you give (padding: 20, fontSize: 16) are scaled to the window (scaleStyle rules); for colors the function form receives the theme. The returned hook gives new styles when the window or theme changes; for the same scale and theme the result comes from a cache (each component instance does not recompute).

createStyles(styles: T | ((context: StyleContext) => T), options?: CreateStylesOptions): () => T

Examples

const useStyles = createStyles(({ colors, radius }) => ({
  card: { padding: 20, borderRadius: radius.lg, backgroundColor: colors.card },
  title: { fontSize: 18, lineHeight: 26, color: colors.foreground },
}));

function Card() {
  const styles = useStyles();
  return <View style={styles.card}><RNText style={styles.title}>Title</RNText></View>;
}

scaleStyle

function

Scales the pixel values in a style object. Only numbers change: percentages ("50%"), "auto", colors, flex, opacity, zIndex, aspectRatio, border widths (borderWidth, so thin lines stay sharp) and values of ±1 or less stay as they are. shadowOffset and translateX/Y inside transform are scaled too.

scaleStyle(style: T, scaler: Scaler): T

Examples

`scaleStyle({ padding: 20, fontSize: 16, flex: 1 }, scaler)`

The default settings.

DEFAULT_SCALING: Required<Omit<ScalingOptions, "maxFontScale">>

createScaler

function

Creates a scaler from a window size. If options is false the factor is 1 (scaling off). If the window size is unknown (0) the factor is 1.

createScaler(window: { width: number; height: number; }, options?: ScalingOptions | false): Scaler

Examples

const scaler = createScaler({ width: 360, height: 780 });
scaler.s(20); // 19.5

getScaler

function

The active scaler (the one of the most recently rendered UIProvider). For code outside components (navigation header settings, animation calculations). Inside components use useScale: it re-renders when the window changes.

getScaler(): Scaler

scaleSize

function

Scales a size with the active scaler (Scaler.s). For use outside components; the value does not refresh by itself when the window changes.

scaleSize(size: number): number

Examples

`const HEADER = scaleSize(56);`

scaleFont

function

Scales a font size with the active scaler (Scaler.fs). See scaleSize.

scaleFont(size: number): number

Breakpoint name. See BREAKPOINTS.

type Breakpoint = keyof typeof BREAKPOINTS

A value per breakpoint: breakpoints not given inherit from the previous one (the md:grid-cols-2 logic on web).

type BreakpointValue = { /** Value for the smallest widths (and the fallback). */ base: T; } & Partial<Record<Exclude<Breakpoint, "base">, T>>

Extends: Partial<Record<Exclude<Breakpoint, "base">, T>>

NameTypeDescription
baseTValue for the smallest widths (and the fallback).

Examples

`{ base: 1, md: 2, lg: 3 }` → 1 column on a phone, 2 on a tablet, 3 on a wide screen

Value of useResponsive.

NameTypeDescription
widthnumberWindow width (dp/pt).
heightnumberWindow height.
orientation"portrait" | "landscape"Landscape or portrait.
breakpointBreakpointThe active breakpoint (by width). See BREAKPOINTS.
isTabletbooleanA tablet or an open foldable: the window's short side is 600 or more (Android's sw600dp rule).
up(breakpoint: Breakpoint) => booleanWhether the width is at or above this breakpoint. @example up("md")
down(breakpoint: Breakpoint) => booleanWhether the width is below this breakpoint. @example down("md") → phone
select<T>(value: BreakpointValue<T>) => TPicks a value by width; breakpoints not given inherit from the previous one.

Named styles (the same shape as StyleSheet.create).

type NamedStyles = { [P in keyof T]: ViewStyle | TextStyle | ImageStyle; }

Values received by the function form of createStyles: the theme and the scaler.

type StyleContext = UIContextValue

Options of createStyles.

NameTypeDefaultDescription
scale?booleantrueWhether numbers are scaled automatically. When off, only values you give with scale.s() in the function form are scaled.

Scaling settings (the UIProvider scaling prop). Pixel values you give in the design (padding: 20, fontSize: 16) are scaled up or down to the device's window size; so text and spacing do not overflow on small screens or look scrawny on large ones.

The factor is computed from the window's short side (sizes do not jump on rotation) and updates live as the window changes (a foldable opening, split screen, Stage Manager on iPad, the "Display size" setting on Android):

ratio  = shortSide / baseWidth
factor = clamp(1 + (ratio − 1) × strength, min, max)

strength 1 means fully linear scaling, 0 no scaling at all; the default 0.5 applies half the difference (text grows proportionally across phones, without becoming huge on tablets).

NameTypeDefaultDescription
baseWidth?number390` (iPhone 12–16)Short side of the screen the design was made for (dp/pt).
strength?number0.5Scaling strength of sizes (0–1).
fontStrength?number0.4Scaling strength of font sizes (fontSize, lineHeight, letterSpacing) (0–1). Text grows a little less than spacing; readability is kept and nothing overflows.
min?number0.85Minimum factor.
max?number1.2Maximum factor (tablets and open foldables).
maxFontScale?numberUpper bound of the system font size (accessibility) multiplier; the library's Text component applies it as maxFontSizeMultiplier. If omitted it is unlimited (recommended; respects the accessibility setting).

Examples

<UIProvider scaling={{ baseWidth: 375, strength: 0.6, max: 1.3 }}>…</UIProvider>
<UIProvider scaling={false}>…</UIProvider> // scaling off

Scaler

interface

Scaler: factors computed for the current window and conversion functions. Obtained with useScale; a new one arrives when the window changes.

NameTypeDescription
factornumberSize factor (1 means no scaling).
fontFactornumberFont factor.
widthnumberWindow width used in the computation.
heightnumberWindow height used in the computation.
maxFontScalenumber | undefinedLimit of the system font size multiplier (ScalingOptions.maxFontScale).
s(size: number) => numberScales a size and rounds it to the nearest physical pixel: spacing, width, height, corner radius, icon size.
fs(size: number) => numberScales a font size (fontSize, lineHeight, letterSpacing).
ms(size: number, strength: number) => numberScales with your own strength: strength 0 none, 1 fully linear.