svelte-bricks
v0.4.1
Published
Svelte masonry component with SSR support and column balancing
Downloads
14,024
Maintainers
Readme
Svelte masonry component with SSR support (via CSS container queries) and automatic column balancing. Live demo
Installation
pnpm add -D svelte-bricksUsage
The kitchen sink for this component looks something like this:
<script>
import Masonry from 'svelte-bricks'
let nItems = $state(30);
let items = $derived([...Array(nItems).keys()])
let [minColWidth, maxColWidth, gap] = [200, 800, 20]
let width = $state(0), height = $state(0)
</script>
Masonry size: <span>{width}px</span> × <span>{height}px</span> (w × h)
<Masonry
{items}
{minColWidth}
{maxColWidth}
{gap}
style="padding: 20px;"
columnStyle="background-color: rgba(0, 0, 0, 0.1);"
bind:masonryWidth={width}
bind:masonryHeight={height}
>
{#snippet children({ item })}
<Some {item} />
{/snippet}
</Masonry>Note: If items is an array of objects, this component tries to access an id property on each item. This value is used to tell items apart in the keyed {#each} block that creates the masonry layout. Without it, Svelte could not avoid duplicates when new items are added or existing ones rearranged. Read the Svelte docs for details. To change the name of the identifier key, pass idKey="some-uniq-key. Or pass a function getId = (item: Item) => string | number that maps items to unique IDs.
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:
animate: boolean = trueWhether to FLIP-animate masonry items when viewport resizing or other events cause
itemsto rearrange.balance: boolean = trueEnable height-based column balancing. Items are distributed to the shortest column for a more even layout. Set to
falsefor simple round-robin distribution.calcCols = ( masonryWidth: number, minColWidth: number, gap: number, ): number => { return Math.min( items.length, Math.floor((masonryWidth + gap) / (minColWidth + gap)) || 1, ) }Function used to compute the number of columns based on the masonry width, minimum column width and gap.
class: string = ``Applies to the outer
divwrapping all masonry columns. For use with CSS frameworks like Tailwind.columnClass: string = ``Applies to each column
div.duration: number = 200Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.
gap: number = 20Gap between columns and items within each column in
px.getId = (item: Item): string | number => { if (typeof item === `number`) return item if (typeof item === `string`) return item return item[idKey] }Custom function that maps masonry items to unique IDs of type
stringornumber.idKey: string = `id`Name of the attribute to use as identifier if items are objects.
items: Item[]The only required prop is the list of items to render where
Itemis a generic type (viagenerics="Item") which usually will beobjectbut can also be simple typesstringornumber.masonryHeight: number = 0The masonry
divs height inpx.masonryWidth: number = 0The masonry
divs width inpx.maxColWidth: number = 500Maximum column width in
px.minColWidth: number = 330Minimum column width in
px.style: string = ``Inline styles that will be applied to the top-level
div.masonry.
Virtual Scrolling
For large lists (1000+ items), enable virtual scrolling to render only visible items:
<Masonry
{items}
virtualize={true}
height={600}
getEstimatedHeight={(item) => item.height ?? 150}
overscan={5}
>
{#snippet children({ item })}
<Card {item} />
{/snippet}
</Masonry>Virtualization Props
virtualize: boolean = falseEnable virtual scrolling. When
true, only visible items are rendered. Requires theheightprop.height: number | stringRequired when
virtualize=true. Sets the scroll container height (e.g.,500for pixels or"80vh").getEstimatedHeight?: (item: Item) => numberOptional function that returns an estimated height for items before they're measured. Defaults to 150px if not provided. Better estimates = less layout shift.
overscan: number = 5Number of items to render above and below the visible area. Higher values reduce flicker during fast scrolling but render more items.
Notes:
- FLIP animations are automatically disabled when virtualizing
- Balance mode works with estimated heights until items are measured
- The masonry div becomes a scroll container (
overflow-y: auto)
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) {
/* top-level wrapper div */
}
:global(div.masonry div.col) {
/* each column in the masonry layout */
}