Nanoviews Balanced Grid
BalancedMasonryGrid from @masonry-grid/nanoviews reorders the frames inside each row to make the grid shorter and the columns more even. Only the visual order changes, through the CSS order property: the DOM order stays the same. Balanced vs Regular shows both layouts side by side.
Use the controls above the grid to change the number of frames, the minimum frame width (frameWidth), the gap and the width of the container.
Source code
Section titled “Source code”The controls and the random frames come from controls.ts, see the whole example on GitHub.
import { div, h1, for_, component$, mount} from 'nanoviews'import { BalancedMasonryGrid, Frame} 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()( 'Balanced Masonry Grid - Nanoviews' ), Controls(controls), BalancedMasonryGrid({ class: 'container', gap: () => Number(controls.$gap()), frameWidth: () => Number(controls.$frameWidth()), style: () => ({ width: `${controls.$containerWidth()}%` }) })( for_($items)( ($item, $index) => Frame({ class: 'frame', width: () => $item().width, height: () => $item().height, style: () => ({ backgroundColor: $item().backgroundColor }) })( () => $index() + 1 ) ) ) ) )})
mount(App, document.getElementById('root')!)