react-justified-layout-ts 1.0.0

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.
@@ -0,0 +1,5 @@
1
+ import { TSJustifiedLayoutProps } from "./TSJustifiedLayoutProps";
2
+ import React from "react";
3
+ declare function TSJustifiedLayout({ children, images, itemSpacing, rowSpacing, showWidows, targetRowHeight, targetRowHeightTolerance, width }: TSJustifiedLayoutProps): React.JSX.Element;
4
+ export { TSJustifiedLayout };
5
+ //# sourceMappingURL=TSJustifiedLayout.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,126 @@
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
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ 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);
43
+ /**
44
+ *
45
+ * @param value The new aspect ratio to be checked
46
+ * @return If the buffer can accept the new value
47
+ * */
48
+ 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;
53
+ // Row still has space
54
+ if (newAspectRatio < minAspectRatio) {
55
+ rowBuffer.push(value);
56
+ return true;
57
+ }
58
+ // Row ran out of space, and the new item is larger than the max aspect ratio for the row
59
+ else if (newAspectRatio > maxAspectRatio) {
60
+ // Always accept if it's just 1 item
61
+ if (rowBuffer.length === 0) {
62
+ rowBuffer.push(value);
63
+ rows.push({ items: rowBuffer, height: rowWidthWithoutSpacing / newAspectRatio });
64
+ rowBuffer = [];
65
+ return true;
66
+ }
67
+ else {
68
+ // 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;
72
+ // 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
+ if (Math.abs(newAspectRatio - targetAspectRatio) > Math.abs(previousAspectRatio - previousTargetAspectRatio)) {
74
+ rows.push({ items: rowBuffer, height: previousRowWidthWithoutSpacing / previousAspectRatio });
75
+ rowBuffer = [];
76
+ return false;
77
+ }
78
+ // If the new aspect ratio is closer to the target aspect ratio, then insert item and push row buffer
79
+ else {
80
+ rowBuffer.push(value);
81
+ rows.push({ items: rowBuffer, height: rowWidthWithoutSpacing / newAspectRatio });
82
+ rowBuffer = [];
83
+ return true;
84
+ }
85
+ }
86
+ }
87
+ else {
88
+ // New aspect ratio is within aspect ratio tolerance, so we finish off the row
89
+ rowBuffer.push(value);
90
+ rows.push({ items: rowBuffer, height: rowWidthWithoutSpacing / newAspectRatio });
91
+ rowBuffer = [];
92
+ return true;
93
+ }
94
+ }
95
+ var rows = [];
96
+ var rowBuffer = [];
97
+ images.forEach(function (value) {
98
+ var isItemSuccessfullyAdded = addItem(value);
99
+ if (!isItemSuccessfullyAdded) {
100
+ addItem(value);
101
+ }
102
+ });
103
+ // Handle leftover content
104
+ if (showWidows) {
105
+ rows.push({ items: rowBuffer, height: rows.length === 0 ? targetRowHeight : rows[rows.length - 1].height });
106
+ }
107
+ var childNodeCounter = -1;
108
+ /**
109
+ * Clone the children element, and inject the height of the element as a prop
110
+ * @param height The height that the element should be
111
+ */
112
+ function renderChildren(height) {
113
+ childNodeCounter++;
114
+ return (0, react_1.cloneElement)(children[childNodeCounter], __assign(__assign({}, children[childNodeCounter].props), { style: __assign(__assign({}, children[childNodeCounter].style), { height: height }) }));
115
+ }
116
+ return (react_1.default.createElement(react_1.default.Fragment, null,
117
+ react_1.default.createElement("div", { style: { width: "100%" } }, rows.map(function (value) {
118
+ return react_1.default.createElement("div", { style: {
119
+ display: "flex",
120
+ flexDirection: "row",
121
+ gap: itemSpacing,
122
+ marginBottom: rowSpacing
123
+ } }, value.items.map(function () { return react_1.default.createElement("div", { style: { height: value.height } }, renderChildren(value.height)); }));
124
+ }))));
125
+ }
126
+ exports.TSJustifiedLayout = TSJustifiedLayout;
@@ -0,0 +1,17 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "react-justified-layout-ts",
3
+ "version": "1.0.0",
4
+ "description": "A component based off Flickr's justified layout that is compatible with Typescript",
5
+ "main": "dist/TSJustifiedLayout.d.ts",
6
+ "files": ["dist"],
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1",
9
+ "typescript": "tsc"
10
+ },
11
+ "keywords": [],
12
+ "author": "Alan19",
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "@types/node": "^20.10.7"
16
+ },
17
+ "devDependencies": {
18
+ "@types/react": "^18.2.47",
19
+ "react": "^18.2.0",
20
+ "typescript": "^5.3.3"
21
+ }
22
+ }