- Notifications
You must be signed in to change notification settings - Fork6
Naive Svelte Masonry component without column balancing (ragged columns at the bottom)
License
janosh/svelte-bricks
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
npm install --dev svelte-bricks
The kitchen sink for this component looks something like this:
<script>importMasonryfrom'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}> {#snippetchildren({item })} <Some {item} /> {/snippet}</Masonry>
Note: Ifitems is an array of objects, this component tries to access anid 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 theSvelte docs for details. To change the name of the identifier key, passidKey="some-uniq-key. Or pass a functiongetId = (item: Item) => string | number that maps items to unique IDs.
Hint: Balanced columns can be achieved even with this simple implementation if masonry items are allowed to stretch to the column height.
Masonry.svelte expects an array ofitems as well as a<slot /> component used to render each of theitems. 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=true
Whether toFLIP-animate masonry items when viewport resizing or other events cause
itemsto rearrange.calcCols=(masonryWidth:number,minColWidth:number,gap:number,):number=>{returnMath.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=200
Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.
gap:number=20
Gap between columns and items within each column in
px.getId=(item:Item):string|number=>{if(typeofitem===`number`)returnitemif(typeofitem===`string`)returnitemreturnitem[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 are the list of items to render where
Item = $$Genericis a generic type which usually will beobjectbut can also be simple typesstringornumber.masonryHeight:number=0
The masonry
divs height inpx.masonryWidth:number=0
The masonry
divs width inpx.maxColWidth:number=500
Maximum column width in
px.minColWidth:number=330
Minimum column width in
px.style:string=``
Inline styles that will be applied to the top-level
div.masonry.
Besides inline CSS which you can apply through thestyle 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 */}
About
Naive Svelte Masonry component without column balancing (ragged columns at the bottom)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.