2.2.3 • Published 8 months ago

@appnest/masonry-layout v2.2.3

Weekly downloads
163
License
MIT
Repository
github
Last release
8 months ago
  • Simple: Works right out of the box (just add it to your markup)
  • Lightweight: Super small (1kb minified & gzipped)
  • Zero dependencies: Created using only vanilla js - no dependencies and framework agnostic!
  • Customizable: Can customize almost everything (eg. columns, transitions, gap).
  • User friendly: Automatically re-distribute items when the size of the grid changes or new elements are added
  • Performant: Efficient & fast - Build with performance in mind

-----------------------------------------------------

➤ Installation

npm i @appnest/masonry-layout

If you prefer to use umd bundle you can load https://unpkg.com/@appnest/masonry-layout/umd/masonry-layout.min.js instead.

-----------------------------------------------------

➤ Example

Import @appnest/masonry-layout somewhere in your code and you're ready to go! Simply add the masonry-layout element to your html and then add your elements in between the start and closing tags.

<masonry-layout>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</masonry-layout>

-----------------------------------------------------

➤ Customize

Customize the functionality by applying the attributes.

Amount of columns

The cols attribute specifies the amount of columns. The default value is auto which ensures to distribute the elements based on the available width.

<masonry-layout cols="5">
  ...
</masonry-layout>

Gap between columns and rows

The gap attribute specifies how many pixels the gap between the elements should be. The default value is 24px.

<masonry-layout gap="50">
  ...
</masonry-layout>

Max column width

The maxcolwidth specifies how many pixels a column can maximum have when the cols attribute is set to auto. The default value is 400px.

<masonry-layout maxcolwidth="200">
  ...
</masonry-layout>

Change debounce time

The debounce attribute specifies the amount of time in ms the layout reflow debounces each time the size of the masonry layout changes. This reflow is debounced to avoid the layout algorithm being invoked too many times in a row. The default value is 300ms.

<masonry-layout debounce="500">
  ...
</masonry-layout>

-----------------------------------------------------

➤ Trigger layout

If you want to force layout to can call the layout() function on the masonry layout.

document.querySelector("masonry-layout").layout();

If you wish to debounce the layout you can call the scheduleLayout() function instead.

document.querySelector("masonry-layout").scheduleLayout();

-----------------------------------------------------

➤ Overview

Here's a complete overview of the element.

masonry-layout

Masonry layout web component. It places the slotted elements in the optimal position based on the available vertical space, just like mason fitting stones in a wall.

Example

<masonry-layout><div class="item"></div><div class="item"></div></masonry-layout>

Properties

PropertyAttributeTypeDescription
colscolsnumber \| "auto"The amount of columns.
debouncedebouncenumberThe ms of debounce when the element resizes.
gapgapnumberThe gap in pixels between the columns.
layout
maxColWidthmaxcolwidthnumberThe maximum width of each column if cols are set to auto.
onResize
onSlotChange

Methods

MethodTypeDescription
layout(): voidLayouts the elements.
onResize(entries?: object \| undefined): voidEach time the element resizes we need to schedule a layoutif the amount available columns has has changed.
onSlotChange(): void
renderCols(colCount: number): voidRender X amount of columns.
scheduleLayout(ms?: number): voidSchedules a layout.

Slots

NameDescription
Items that should be distributed in the layout.

CSS Shadow Parts

PartDescription
columnEach column of the masonry layout.
column-indexThe specific column at the given index (eg. column-0 would target the first column and so on)

-----------------------------------------------------

➤ Support for old browsers

If you are going to support older browsers that doesn't support Custom Elements, Shadow Dom or ResizeObservers you should polyfill the features. You can do this very easily by using the brilliant polfiller service. This can be done in one line of code by adding the following to your index.html before you import the masonry-layout.

<script crossorigin src="https://polyfill.app/api/polyfill?features=es,template,shadow-dom,custom-elements,resizeobserver"></script>

-----------------------------------------------------

➤ License

Licensed under MIT.

2.2.3

8 months ago

2.2.2

8 months ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago