react-justified-layout-ts 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Alan19
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ A TypeScript component similar to Flickr's justified layout.
2
+
3
+ # Usage
4
+
5
+ ---
6
+ Add the `TSJustifiedLayout` component to your code with the following props:
7
+ - `layoutItems: number[];` - An array of aspect ratios for the images you are adding to the grid
8
+ - `itemSpacing?: number;` - The amount of spacing between each image, in pixels. (Default: 10)
9
+ - `rowSpacing?: number;` - The amount of spacing between each row, in pixels. (Default: 10)
10
+ - `targetRowHeight?: number;` - The target row height for a row, in pixels. (Default: 320)
11
+ - `targetRowHeightTolerance?: number;` - The amount the row height could deviate from the target row height, as a percent. (Default: .25)
12
+ - `width: number;` - The width of the container. I would use something like `react-use-measure` if you're trying to make it dynamically take up the size of the parent container.
13
+ - `children: any[];` - The children elements that makes up the grid.
14
+ - `showWidows?: boolean;` - If the last row should be shown. (Default: true)
15
+
16
+ ## Example Usage
17
+ ```typescript jsx
18
+ <TSJustifiedLayout width={width}
19
+ layoutItems={imagesOnPage.map(value => ({
20
+ dimensions: value.aspectRatio || 1
21
+ }))}>
22
+ {imagesOnPage.map(value => <img
23
+ src={value.src}
24
+ alt={value.alt}
25
+ />)}
26
+ </TSJustifiedLayout>
27
+ ```
28
+
29
+ # Install
30
+
31
+ ---
32
+ `npm i react-justified-layout-ts`
33
+
34
+ # Credits
35
+
36
+ ---
37
+ The display logic for the layout and the math used to calculate the row height for each row is adapted from [Flickr's Justified Layout library](https://github.com/flickr/justified-layout).
38
+
39
+ # License
40
+
41
+ ---
42
+ Open Source Licensed under the MIT license.
@@ -1,5 +1,16 @@
1
- import { TSJustifiedLayoutProps } from "./TSJustifiedLayoutProps";
2
1
  import React from "react";
2
+ export interface TSJustifiedLayoutProps {
3
+ images: {
4
+ src: string;
5
+ dimensions: number;
6
+ }[];
7
+ itemSpacing?: number;
8
+ rowSpacing?: number;
9
+ targetRowHeight?: number;
10
+ targetRowHeightTolerance?: number;
11
+ width: number;
12
+ children: any[];
13
+ showWidows?: boolean;
14
+ }
3
15
  declare function TSJustifiedLayout({ children, images, itemSpacing, rowSpacing, showWidows, targetRowHeight, targetRowHeightTolerance, width }: TSJustifiedLayoutProps): React.JSX.Element;
4
16
  export { TSJustifiedLayout };
5
- //# sourceMappingURL=TSJustifiedLayout.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TSJustifiedLayout.d.ts","sourceRoot":"","sources":["../src/components/TSJustifiedLayout/TSJustifiedLayout.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,sBAAsB,EAAC,MAAM,0BAA0B,CAAC;AAChE,OAAO,KAAqB,MAAM,OAAO,CAAC;AAG1C,iBAAS,iBAAiB,CAAC,EACW,QAAQ,EACR,MAAM,EACN,WAAgB,EAChB,UAAe,EACf,UAAiB,EACjB,eAAqB,EACrB,wBAA8B,EAC9B,KAAK,EACR,EAAE,sBAAsB,qBA4G1D;AAED,OAAO,EAAC,iBAAiB,EAAC,CAAA"}
1
+ {"version":3,"file":"TSJustifiedLayout.d.ts","sourceRoot":"","sources":["../src/components/TSJustifiedLayout/TSJustifiedLayout.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAqB,MAAM,OAAO,CAAC;AAE1C,MAAM,WAAW,sBAAsB;IACnC,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,GAAG,EAAE,CAAC;IAChB,UAAU,CAAC,EAAE,OAAO,CAAC;CAIxB;AAED,iBAAS,iBAAiB,CAAC,EACW,QAAQ,EACR,MAAM,EACN,WAAgB,EAChB,UAAe,EACf,UAAiB,EACjB,eAAqB,EACrB,wBAA8B,EAC9B,KAAK,EACR,EAAE,sBAAsB,qBA4G1D;AAED,OAAO,EAAC,iBAAiB,EAAC,CAAA"}
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -35,21 +24,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
35
24
  };
36
25
  Object.defineProperty(exports, "__esModule", { value: true });
37
26
  exports.TSJustifiedLayout = void 0;
38
- var react_1 = __importStar(require("react"));
39
- function TSJustifiedLayout(_a) {
40
- var children = _a.children, images = _a.images, _b = _a.itemSpacing, itemSpacing = _b === void 0 ? 10 : _b, _c = _a.rowSpacing, rowSpacing = _c === void 0 ? 10 : _c, _d = _a.showWidows, showWidows = _d === void 0 ? true : _d, _e = _a.targetRowHeight, targetRowHeight = _e === void 0 ? 320 : _e, _f = _a.targetRowHeightTolerance, targetRowHeightTolerance = _f === void 0 ? .25 : _f, width = _a.width;
41
- var minAspectRatio = width / targetRowHeight * (1 - targetRowHeightTolerance);
42
- var maxAspectRatio = width / targetRowHeight * (1 + targetRowHeightTolerance);
27
+ const react_1 = __importStar(require("react"));
28
+ function TSJustifiedLayout({ children, images, itemSpacing = 10, rowSpacing = 10, showWidows = true, targetRowHeight = 320, targetRowHeightTolerance = .25, width }) {
29
+ const minAspectRatio = width / targetRowHeight * (1 - targetRowHeightTolerance);
30
+ const maxAspectRatio = width / targetRowHeight * (1 + targetRowHeightTolerance);
43
31
  /**
44
32
  *
45
33
  * @param value The new aspect ratio to be checked
46
34
  * @return If the buffer can accept the new value
47
35
  * */
48
36
  function addItem(value) {
49
- var newItems = rowBuffer.concat(value);
50
- var newAspectRatio = newItems.map(function (data) { return data.dimensions; }).reduce(function (previousValue, currentValue) { return previousValue + currentValue; }, 0);
51
- var rowWidthWithoutSpacing = width - (newItems.length - 1) * itemSpacing;
52
- var targetAspectRatio = rowWidthWithoutSpacing / targetRowHeight;
37
+ const newItems = rowBuffer.concat(value);
38
+ const newAspectRatio = newItems.map(data => data.dimensions).reduce((previousValue, currentValue) => previousValue + currentValue, 0);
39
+ const rowWidthWithoutSpacing = width - (newItems.length - 1) * itemSpacing;
40
+ const targetAspectRatio = rowWidthWithoutSpacing / targetRowHeight;
53
41
  // Row still has space
54
42
  if (newAspectRatio < minAspectRatio) {
55
43
  rowBuffer.push(value);
@@ -66,9 +54,9 @@ function TSJustifiedLayout(_a) {
66
54
  }
67
55
  else {
68
56
  // Calculate width/aspect ratio for row before adding new item
69
- var previousRowWidthWithoutSpacing = width - (rowBuffer.length - 1) * itemSpacing;
70
- var previousAspectRatio = rowBuffer.map(function (data) { return data.dimensions; }).reduce(function (previousValue, currentValue) { return previousValue + currentValue; }, 0);
71
- var previousTargetAspectRatio = previousRowWidthWithoutSpacing / targetRowHeight;
57
+ const previousRowWidthWithoutSpacing = width - (rowBuffer.length - 1) * itemSpacing;
58
+ const previousAspectRatio = rowBuffer.map(data => data.dimensions).reduce((previousValue, currentValue) => previousValue + currentValue, 0);
59
+ const previousTargetAspectRatio = previousRowWidthWithoutSpacing / targetRowHeight;
72
60
  // If the new aspect ratio is farther from the target after the insert, then push row buffer and insert new item into the next row
73
61
  if (Math.abs(newAspectRatio - targetAspectRatio) > Math.abs(previousAspectRatio - previousTargetAspectRatio)) {
74
62
  rows.push({ items: rowBuffer, height: previousRowWidthWithoutSpacing / previousAspectRatio });
@@ -92,10 +80,10 @@ function TSJustifiedLayout(_a) {
92
80
  return true;
93
81
  }
94
82
  }
95
- var rows = [];
96
- var rowBuffer = [];
97
- images.forEach(function (value) {
98
- var isItemSuccessfullyAdded = addItem(value);
83
+ const rows = [];
84
+ let rowBuffer = [];
85
+ images.forEach((value) => {
86
+ const isItemSuccessfullyAdded = addItem(value);
99
87
  if (!isItemSuccessfullyAdded) {
100
88
  addItem(value);
101
89
  }
@@ -104,23 +92,23 @@ function TSJustifiedLayout(_a) {
104
92
  if (showWidows) {
105
93
  rows.push({ items: rowBuffer, height: rows.length === 0 ? targetRowHeight : rows[rows.length - 1].height });
106
94
  }
107
- var childNodeCounter = -1;
95
+ let childNodeCounter = -1;
108
96
  /**
109
97
  * Clone the children element, and inject the height of the element as a prop
110
98
  * @param height The height that the element should be
111
99
  */
112
100
  function renderChildren(height) {
113
101
  childNodeCounter++;
114
- return (0, react_1.cloneElement)(children[childNodeCounter], __assign(__assign({}, children[childNodeCounter].props), { style: __assign(__assign({}, children[childNodeCounter].style), { height: height }) }));
102
+ return (0, react_1.cloneElement)(children[childNodeCounter], Object.assign(Object.assign({}, children[childNodeCounter].props), { style: Object.assign(Object.assign({}, children[childNodeCounter].style), { height: height }) }));
115
103
  }
116
104
  return (react_1.default.createElement(react_1.default.Fragment, null,
117
- react_1.default.createElement("div", { style: { width: "100%" } }, rows.map(function (value) {
105
+ react_1.default.createElement("div", { style: { width: "100%" } }, rows.map(value => {
118
106
  return react_1.default.createElement("div", { style: {
119
107
  display: "flex",
120
108
  flexDirection: "row",
121
109
  gap: itemSpacing,
122
110
  marginBottom: rowSpacing
123
- } }, value.items.map(function () { return react_1.default.createElement("div", { style: { height: value.height } }, renderChildren(value.height)); }));
111
+ } }, value.items.map(() => react_1.default.createElement("div", { style: { height: value.height } }, renderChildren(value.height))));
124
112
  }))));
125
113
  }
126
114
  exports.TSJustifiedLayout = TSJustifiedLayout;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "react-justified-layout-ts",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A component based off Flickr's justified layout that is compatible with Typescript",
5
- "main": "dist/TSJustifiedLayout.d.ts",
5
+ "main": "./dist/TSJustifiedLayout.js",
6
+ "types": "./dist/TSJustifiedLayout.d.ts",
6
7
  "files": ["dist"],
7
8
  "scripts": {
8
9
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -18,5 +19,9 @@
18
19
  "@types/react": "^18.2.47",
19
20
  "react": "^18.2.0",
20
21
  "typescript": "^5.3.3"
21
- }
22
+ },
23
+ "peerDependencies": {
24
+ "react": "^18.2.0"
25
+ },
26
+ "repository": "https://github.com/Alan19/react-justified-layout-ts"
22
27
  }
@@ -1,17 +0,0 @@
1
- export interface TSJustifiedLayoutProps {
2
- images: {
3
- src: string;
4
- dimensions: number;
5
- }[];
6
- itemSpacing?: number;
7
- rowSpacing?: number;
8
- targetRowHeight?: number;
9
- targetRowHeightTolerance?: number;
10
- width: number;
11
- children: any[];
12
- showWidows?: boolean;
13
- maxNumRows?: number;
14
- fullWidthBreakoutRowCadence?: number;
15
- widowLayoutStyle: "left" | "justify" | "center";
16
- }
17
- //# sourceMappingURL=TSJustifiedLayoutProps.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"TSJustifiedLayoutProps.d.ts","sourceRoot":"","sources":["../src/components/TSJustifiedLayout/TSJustifiedLayoutProps.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACnC,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,GAAG,EAAE,CAAC;IAChB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,2BAA2B,CAAC,EAAE,MAAM,CAAA;IACpC,gBAAgB,EAAE,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAA;CAClD"}
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });