Installation
yarn add -D svelte-bricks
Usage
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.
Hint: Balanced columns can be achieved even with this simple implementation if masonry items are allowed to stretch to the column height.
Props
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:
items: any[]
: required
minColWidth: number = 330
(in px
)
maxColWidth: number = 500
(in px
)
gap: number = 20
(in px
)
masonryWidth: number = 0
: Bound to the masonry div
s width (in px
).
masonryHeight: number = 0
: Bound to the masonry div
s height (in px
).
animate: boolean = true
: Whether to FLIP-animate masonry items when viewport resizing or other events cause items
to rearrange.
style: string = ''
: Inline styles that will be applied to the top-level div.masonry
.
duration: number = 200
: Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.
class: string = ''
: Applies to the outer div
wrapping all masonry columns. For use with CSS frameworks like Tailwind.
columnClass: string = ''
: Applies to each column div
.
Styling
Besides inline CSS which you can apply through the style
prop, the following :global()
CSS selectors can be used for fine-grained control of wrapper and column styles:
:global(div.masonry) {
}
:global(div.masonry div.col) {
}