Skip to content

Nanoviews Spanned Grid

SpannedMasonryGrid and SpannedFrame from @masonry-grid/nanoviews make an experimental masonry layout with CSS alone: each frame spans as many grid rows as its aspect ratio needs, with grid-row: span …, so no JavaScript computes the layout and it works with server-side rendering. A frame spans whole rows, so it keeps its aspect ratio only approximately. The precision prop, 10 by default, makes the rows finer: higher values keep the ratios closer, but with many frames they can cause bugs in some browsers. How It Works explains the trick.

Use the controls above the grid to change the number of frames, the frame width, the gap, the width of the container and the precision.

The controls and the random frames come from controls.ts, see the whole example on GitHub.

main.ts
import {
div,
h1,
for_,
component$,
mount
} from 'nanoviews'
import {
SpannedMasonryGrid,
SpannedFrame
} from '@masonry-grid/nanoviews'
import {
Controls,
createControls,
createItems
} from './controls'
import './index.css'
const App = component$(() => {
const controls = createControls()
const $items = createItems(() => Number(controls.$itemsCount()))
return (
div({
class: 'container'
})(
h1()(
'Spanned Masonry Grid - Nanoviews'
),
Controls(controls),
SpannedMasonryGrid({
class: 'container',
gap: () => Number(controls.$gap()),
frameWidth: () => Number(controls.$frameWidth()),
precision: () => Number(controls.$precision()),
style: () => ({
width: `${controls.$containerWidth()}%`
})
})(
for_($items)(
($item, $index) => SpannedFrame({
class: 'frame',
width: () => $item().width,
height: () => $item().height,
innerStyle: () => ({
backgroundColor: $item().backgroundColor
})
})(
() => $index() + 1
)
)
)
)
)
})
mount(App, document.getElementById('root')!)