Skip to main content

Box

A theme-aware replacement for View with shorthand props for padding, margin, gap, alignment, and background color. Spacing values can be theme tokens (xs, sm, md, lg, xl) or raw numbers.

Usage

import { Box, Typography } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider>
<View style={{ flex: 1, justifyContent: 'center', padding: 16 }}>
<Box p="md" bg="#f5f5f5">
<Typography variant="bodyMedium">Padded content</Typography>
</Box>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Spacing Tokens

TokenValue
xs4
sm8
md16
lg24
xl32

You can also pass a raw number:

import { Box, Typography } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider>
<View style={{ flex: 1, justifyContent: 'center' }}>
<Box p={20} mx={12} bg="#e0e0e0">
<Typography>20dp padding, 12dp horizontal margin</Typography>
</Box>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Padding & Margin

import { Box, Typography } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider>
<View style={{ flex: 1, padding: 16, gap: 12 }}>
<Box p="md" m="sm" bg="#fce4ec">
<Typography>p="md" m="sm"</Typography>
</Box>
<Box px="lg" py="sm" bg="#e8f5e9">
<Typography>px="lg" py="sm"</Typography>
</Box>
<Box pt="sm" pb="md" ps="lg" pe="xl" bg="#e3f2fd">
<Typography>pt sm, pb md, ps lg, pe xl (RTL-aware)</Typography>
</Box>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

ps and pe map to paddingStart / paddingEnd, so they flip automatically in RTL layouts. The same applies to ms / me.

Gap

import { Box, Button } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider>
<View style={{ flex: 1, justifyContent: 'center', padding: 16 }}>
<Box gap="sm">
<Button variant="filled" onPress={() => {}}>First</Button>
<Button variant="outlined" onPress={() => {}}>Second</Button>
</Box>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

rowGap and columnGap are also available for finer control.

Alignment

import { Box, Typography } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider>
<View style={{ flex: 1, padding: 16 }}>
<Box align="center" justify="space-between" style={{ height: 200, backgroundColor: '#f5f5f5' }}>
<Typography>Top</Typography>
<Typography>Middle</Typography>
<Typography>Bottom</Typography>
</Box>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Props

PropTypeDefaultRequiredDescription
pSpacingValue-NoPadding on all sides
pxSpacingValue-NoHorizontal padding (paddingStart + paddingEnd)
pySpacingValue-NoVertical padding (paddingTop + paddingBottom)
ptSpacingValue-NoPadding top
pbSpacingValue-NoPadding bottom
psSpacingValue-NoPadding start (left in LTR, right in RTL)
peSpacingValue-NoPadding end (right in LTR, left in RTL)
mSpacingValue-NoMargin on all sides
mxSpacingValue-NoHorizontal margin (marginStart + marginEnd)
mySpacingValue-NoVertical margin (marginTop + marginBottom)
mtSpacingValue-NoMargin top
mbSpacingValue-NoMargin bottom
msSpacingValue-NoMargin start
meSpacingValue-NoMargin end
gapSpacingValue-NoGap between children
rowGapSpacingValue-NoRow gap between children
columnGapSpacingValue-NoColumn gap between children
flexnumber-NoFlex value
alignenum-NoAlign items along the cross axis
justifyenum-NoJustify content along the main axis
bgstring-NoBackground color