react-justified-layout-ts 1.0.0 → 1.0.1
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/dist/TSJustifiedLayout.d.ts +13 -2
- package/dist/TSJustifiedLayout.d.ts.map +1 -1
- package/dist/TSJustifiedLayout.js +19 -31
- package/package.json +6 -2
- package/dist/TSJustifiedLayoutProps.d.ts +0 -17
- package/dist/TSJustifiedLayoutProps.d.ts.map +0 -1
- package/dist/TSJustifiedLayoutProps.js +0 -2
|
@@ -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,
|
|
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
|
-
|
|
39
|
-
function TSJustifiedLayout(
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
images.forEach(
|
|
98
|
-
|
|
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
|
-
|
|
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],
|
|
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(
|
|
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(
|
|
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.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A component based off Flickr's justified layout that is compatible with Typescript",
|
|
5
|
-
"main": "dist/TSJustifiedLayout.
|
|
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,8 @@
|
|
|
18
19
|
"@types/react": "^18.2.47",
|
|
19
20
|
"react": "^18.2.0",
|
|
20
21
|
"typescript": "^5.3.3"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18.2.0"
|
|
21
25
|
}
|
|
22
26
|
}
|
|
@@ -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"}
|