From 6d54964e0dd3edaea42b82a3c3f67b64b0feccdf Mon Sep 17 00:00:00 2001 From: Alex Walter Date: Tue, 26 Jun 2018 20:20:26 -0700 Subject: [PATCH 1/2] add TypeScript typings --- index.d.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e07cc09 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,87 @@ +declare namespace justifiedLayout { + type Items = (number | { width: number; height: number; })[]; + + interface Options { + /** + * The width that boxes will be contained within irrelevant of padding. + * @default 1060 + */ + containerWidth?: number; + /** + * Provide a single integer to apply padding to all sides or provide an object to apply individual values to each side. + * @default 10 + */ + containerPadding?: number | { + top: number; + right: number; + left: number; + bottom: number; + }; + /** + * Provide a single integer to apply spacing both horizontally and vertically or provide an object to apply individual values to each axis. + * @default 10 + */ + boxSpacing?: number | { + horizontal: number; + vertical: number; + }; + /** + * It'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. + * @default 320 + */ + targetRowHeight?: number; + /** + * How 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. + * @default 0.25 + */ + targetRowHeightTolerance?: number; + /** + * Will stop adding rows at this number regardless of how many items still need to be laid out. + * @default Number.POSITIVE_INFINITY + */ + maxNumRows?: number; + /** + * Provide 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. + * @default false + */ + forceAspectRatio?: boolean | number; + /** + * If 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. + * @default false + */ + fullWidthBreakoutRowCadence?: boolean | number; + /** + * By 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. + * @default true + */ + showWidows?: boolean; + /** + * If widows are visible, how should they be laid out? + * @default "left" + */ + widowLayoutStyle?: "left" | "justify" | "center"; + } + + interface Output { + containerHeight: number; + widowCount: number; + boxes: { + aspectRatio: number; + top: number; + width: number; + height: number; + left: number; + }[]; + } +} + +declare function justifiedLayout(items: justifiedLayout.Items, options?: justifiedLayout.Options): justifiedLayout.Output; + +export = justifiedLayout; From d168998b0632ebbc6a456965b6edf430fbe9f24f Mon Sep 17 00:00:00 2001 From: Alex Walter Date: Wed, 27 Jun 2018 13:59:36 -0700 Subject: [PATCH 2/2] Separate TypeScript interface for an output box --- index.d.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index e07cc09..2459ac0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -69,16 +69,18 @@ declare namespace justifiedLayout { widowLayoutStyle?: "left" | "justify" | "center"; } + interface Box { + aspectRatio: number; + top: number; + width: number; + height: number; + left: number; + } + interface Output { containerHeight: number; widowCount: number; - boxes: { - aspectRatio: number; - top: number; - width: number; - height: number; - left: number; - }[]; + boxes: Box[]; } }