npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-bricks

v0.2.1

Published

Simple masonry implementation without column balancing

Downloads

2,095

Readme

Tests NPM version GitHub Pages pre-commit.ci status Open in StackBlitz

Naive implementation in Svelte without column balancing. Live demo

Installation

npm install --dev 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> &times; <span>{height}px</span> (w &times;
h)

<Masonry
  {items}
  {minColWidth}
  {maxColWidth}
  {gap}
  let:item
  bind:masonryWidth={width}
  bind:masonryHeight={height}
>
  <Some {item} />
</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.

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:

  1. animate: boolean = true

    Whether to FLIP-animate masonry items when viewport resizing or other events cause items to rearrange.

  2. class: string = ``

    Applies to the outer div wrapping all masonry columns. For use with CSS frameworks like Tailwind.

  3. columnClass: string = ``

    Applies to each column div.

  4. duration: number = 200

    Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.

  5. gap: number = 20

    Gap between columns and items within each column in px.

  6. 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.

  7. idKey: string = `id`

    Name of the attribute to use as identifier if items are objects.

  8. 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.

  9. masonryHeight: number = 0

    The masonry divs height in px.

  10. masonryWidth: number = 0

    The masonry divs width in px.

  11. maxColWidth: number = 500

    Maximum column width in px.

  12. minColWidth: number = 330

    Minimum column width in px.

  13. style: string = ``

    Inline styles that will be applied to the top-level div.masonry.

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 */
}