This is a naive masonry implementation in Svelte without column balancing.
yarn add -D svelte-bricks
The kitchen sink for this component looks something like this:
<script>
import Masonry from 'svelte-bricks'
let nItems = 30
$: items = [...Array(nItems).keys()]
let [minColWidth, maxColWidth, gap] = [200, 800, 20]
let width, height
</script>
Masonry size: <span>{width}px</span> × <span>{height}px</span> (w × h)
<Masonry {items} {minColWidth} {maxColWidth} {gap} let:item bind:width bind:height>
<Some {item} />
</Masonry>
Note: On non-primitive types, i.e. if items
is an array of objects, this component requires that each object have a key named 'id'
that contains a unique primitive value. This value is used to identify each item in the keyed {#each}
block that renders the masonry layout. Without this, Svelte could not avoid duplicates when new items are added nor maintain order when existing ones are rearranged. Read the Svelte docs for details.
Masonry.svelte
expects an array of items
as well as a <slot />
component used to render each of the items
. The array can contain whatever data (objects, strings, numbers) as long as the slot component knows how to handle it.
Additional optional props are:
minColWidth: number = 330
(inpx
)maxColWidth: number = 500
(inpx
)gap: number = 20
(inpx
)masonryWidth: number = 0
: Bound to the masonrydiv
s width (inpx
).masonryHeight: number = 0
: Bound to the masonrydiv
s height (inpx
).animate: boolean = true
: Whether to FLIP-animate masonry items when viewport resizing or other events causeitems
to rearrange.