1.0.0 • Published 3 months ago

better-justified-layout v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Justified Layout by Flickr... but better 🤩

A fork of flickr/justified-layout containing different improvements. We maintain it because it is used in https://github.com/nk-o/flickr-justified-gallery and https://visualportfolio.co/

Pass in box sizes and get back sizes and coordinates for a nice justified layout like that seen all over Flickr. The Visual Portfolio site is a great example.

Table of Contents

Quick Start

Usage

import justifiedLayout from 'better-justified-layout';

const layoutGeometry = justifiedLayout([0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2, 2.1]);

Example

It converts this:

[0.5, 1.5, 1, 1.8, 0.4, 0.7, 0.9, 1.1, 1.7, 2, 2.1]

Into this:

{
  "containerHeight": 1269,
  "widowCount": 0,
  "boxes": [
    {
      "aspectRatio": 0.5,
      "top": 10,
      "width": 170,
      "height": 340,
      "left": 10,
      "row": 0
    },
    {
      "aspectRatio": 1.5,
      "top": 10,
      "width": 510,
      "height": 340,
      "left": 190,
      "row": 0
    },
    ...
  ]
}

Install

npm install better-justified-layout

Import

Use one of the following examples to import script.

ESM

We provide a version of better-justified-layout built as ESM (better-justified-layout.esm.js and better-justified-layout.esm.min.js) which allows you to use it as a module in your browser, if your targeted browsers support it.

<script type="module">
  import justifiedLayout from "better-justified-layout.esm.min.js";
</script>

ESM CDN

<script type="module">
  import justifiedLayout from "better-justified-layout.esm.min.js@4.2/+esm";
</script>

CJS

Import better-justified-layout by adding this line to your app's entry point (usually index.js or app.js):

import justifiedLayout from 'better-justified-layout';

Options

No configuration is required but chances are you'd like to change some things. Here are your options:

NameTypeDefaultDescription
containerWidthint1060The width that boxes will be contained within irrelevant of padding.
containerPaddingint, object10Provide a single integer to apply padding to all sides or provide an object to apply individual values to each side.
boxSpacingint, object10Provide a single integer to apply spacing both horizontally and vertically or provide an object to apply individual values to each axis.
targetRowHeightint320It's called a target because row height is the lever we use in order to fit everything in nicely. The algorithm will get as close to the target row height as it can.
targetRowHeightTolerancefloat0.25How far row heights can stray from targetRowHeight. 0 would force rows to be the targetRowHeight exactly and would likely make it impossible to justify. The value must be between 0 and 1.
maxNumRowsintNumber.POSITIVE_INFINITYWill stop adding rows at this number regardless of how many items still need to be laid out.
edgeCaseMinRowHeightfloat0.5Sets the minimum height for each row in a layout, based on the targetRowHeight
edgeCaseMaxRowHeightfloat2.5Sets the maximum height for each row in a layout, based on the targetRowHeight
forceAspectRatiobool, floattrueProvide an aspect ratio here to return everything in that aspect ratio. Makes the values in your input array irrelevant. The length of the array remains relevant.
showWidowsbooltrueBy default we'll return items at the end of a justified layout even if they don't make a full row. If false they'll be omitted from the output.
fullWidthBreakoutRowCadencebool, intfalseIf you'd like to insert a full width box every n rows you can specify it with this parameter. The box on that row will ignore the targetRowHeight, make itself as wide as containerWidth - containerPadding and be as tall as its aspect ratio defines. It'll only happen if that item has an aspect ratio >= 1. Best to have a look at the examples to see what this does.

License

Open Source Licensed under the MIT license.

1.0.0

3 months ago