
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
svelte-bricks
Advanced tools
npm install --dev svelte-bricks
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}
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.
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 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 = true
Whether to FLIP-animate masonry items when viewport resizing or other events cause items
to rearrange.
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 div
wrapping 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 (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 string
or number
.
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 = $$Generic
is a generic type which usually will be object
but can also be simple types string
or number
.
masonryHeight: number = 0
The masonry div
s height in px
.
masonryWidth: number = 0
The masonry div
s width in px
.
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 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 */
}
FAQs
Simple masonry implementation without column balancing
The npm package svelte-bricks receives a total of 1,854 weekly downloads. As such, svelte-bricks popularity was classified as popular.
We found that svelte-bricks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.