Skip to content

@masonry-grid/nanoviews

Every prop of the components takes a plain value or a signal, except as, which takes an element factory or a component.

Standard masonry layout that stacks items by pulling them up to fill gaps.

import { img } from 'nanoviews'
import { RegularMasonryGrid as MasonryGrid, Frame } from '@masonry-grid/nanoviews'
MasonryGrid({
frameWidth: 200,
gap: 10
})(
Frame({
width: 4,
height: 3
})(
img({
src: '...'
})
)
// more frames...
)
  • frameWidth?: Signalish<number | string> - Minimum width of each frame. Used to calculate grid-template-columns. Can be a number (px) or string with units.
  • gap?: Signalish<number | string> - Gap between items. Can be a number (px) or string with units.
  • disabled?: Signalish<boolean> - Disable the masonry layout (no transforms/reordering will be applied).
  • as?: PolymorphicTarget - Render as a different element or component (default: div).
  • All other attributes of the target are passed through, ref included.

Balanced masonry layout that reorders items inside rows to minimize overall grid height.

import { img } from 'nanoviews'
import { BalancedMasonryGrid as MasonryGrid, Frame } from '@masonry-grid/nanoviews'
MasonryGrid({
frameWidth: 200,
gap: 10
})(
Frame({
width: 4,
height: 3
})(
img({
src: '...'
})
)
// more frames...
)

Same as RegularMasonryGrid.

Component for defining masonry grid item with aspect ratio.

import { img, li } from 'nanoviews'
import { Frame } from '@masonry-grid/nanoviews'
Frame({
as: li,
width: 16,
height: 9
})(
img({
src: '...'
})
)
  • width: Signalish<number> - Width for aspect ratio calculation (not necessarily real pixel width).
  • height: Signalish<number> - Height for aspect ratio calculation (not necessarily real pixel height).
  • as?: PolymorphicTarget - Render as a different element or component (default: div).
  • All other attributes of the target are passed through.

Experemental CSS-only masonry layout that uses grid-row: span x to create masonry effect.

import { img } from 'nanoviews'
import { SpannedMasonryGrid as MasonryGrid, SpannedFrame as Frame } from '@masonry-grid/nanoviews'
MasonryGrid({
frameWidth: 200,
gap: 10
})(
Frame({
width: 4,
height: 3
})(
img({
src: '...'
})
)
// more frames...
)

Same as RegularMasonryGrid, except disabled, with an additional props:

  • precision?: Signalish<number> - Precision for span calculation (default: 10). Affects how accurately frames will maintain their aspect ratios. Higher precision results in more accurate sizes but may impact performance and cause bugs in some browsers.
  • innerStyle?: StyleValue - Additional styles applied to the inner container that holds the frames.

Component for defining spanned masonry grid item with aspect ratio.

import { img, li } from 'nanoviews'
import { SpannedFrame as Frame } from '@masonry-grid/nanoviews'
Frame({
as: li,
width: 16,
height: 9
})(
img({
src: '...'
})
)

Same as Frame, with an additional prop:

  • innerStyle?: StyleValue - Additional styles applied to the inner container that holds the content.

Hook for advanced use cases when you need direct access to the container element. Call it while a view is built, in the render of a component: it returns a signal to pass to the ref of the container element.

import { div, component$ } from 'nanoviews'
import { useMasonryGrid } from '@masonry-grid/nanoviews'
import { RegularMasonryGrid } from '@masonry-grid/vanilla'
const CustomGrid = component$((_props, children) => {
const $container = useMasonryGrid({
type: RegularMasonryGrid
})
return (
div({
ref: $container,
style: {
display: 'grid',
overflow: 'hidden',
gap: '10px'
}
})(
...children
)
)
})