Code Monkey home page Code Monkey logo

svelte-bricks's Introduction

Svelte Bricks
 Svelte Bricks

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

Naive implementation in Svelte without column balancing. Live demo

Installation

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

<Masonry
  {items}
  {minColWidth}
  {maxColWidth}
  {gap}
  let:item
  bind:width
  bind:height
>
  <Some {item} />
</Masonry>

Note: On non-primitive items, i.e. if items is an array of objects, this component by default tries to access a key named 'id' on each item. This value is used to tell items apart in the keyed {#each} block that renders 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, use the idKey prop. You can also pass in a custom function as getId that should map 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 */
}

svelte-bricks's People

Contributors

janosh avatar pre-commit-ci[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.