Skip to main content

useDimensions

Screen-aware dimension utilities. Pass a screenPadding value to get a contentWidth that already accounts for horizontal padding, and use getItemWidth to calculate exact column widths including gaps, no manual maths needed.

Preview

width: 0
contentWidth: -32
3-col item width: -16
import { useDimensions } from '@devraj-labs/vajra-ui-core';

const { width, contentWidth, getItemWidth } = useDimensions(16);

// Width of one column in a 3-column grid with 8px gaps
const colWidth = getItemWidth(3, 8);

Usage

import { useDimensions } from '@devraj-labs/vajra-ui-core';

const { width, contentWidth, getItemWidth } = useDimensions(16);

// Width of one column in a 3-column grid with 8px gaps
const colWidth = getItemWidth(3, 8);

API

const dims = useDimensions(screenPadding?: number)

Parameters

ParameterTypeDefaultDescription
screenPaddingnumber0Horizontal padding from screen edges (applied to both sides)

Return values

ValueTypeDescription
widthnumberScreen width in pixels
heightnumberScreen height in pixels
scalenumberDevice pixel ratio
fontScalenumberDevice font scale factor
contentWidthnumberwidth − screenPadding × 2
getItemWidth(numColumns, gap?)(n: number, gap?: number) => numberWidth of one column in an n-column grid
getCustomWidth(numCols, marginHorizontal?, gap?)(n: number, m?: number, gap?: number) => numberAlternative calculation with custom margin

getItemWidth formula

totalGap = gap × (numColumns − 1)
itemWidth = (contentWidth − totalGap) / numColumns

Example

useDimensions example