etudes 0.56.0 → 0.59.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.
- package/lib/MasonryGrid.d.ts +21 -101
- package/lib/MasonryGrid.js +189 -261
- package/lib/MasonryGrid.js.map +1 -1
- package/lib/hooks/useSearchParamState.d.ts +12 -4
- package/lib/hooks/useSearchParamState.js +14 -13
- package/lib/hooks/useSearchParamState.js.map +1 -1
- package/package.json +12 -12
package/lib/MasonryGrid.d.ts
CHANGED
|
@@ -1,104 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CSSProperties, PureComponent, ReactNode } from 'react';
|
|
1
|
+
import { HTMLAttributes, PropsWithChildren } from 'react';
|
|
3
2
|
import { Orientation } from './types';
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
sections: number;
|
|
13
|
-
verticalSpacing: number;
|
|
14
|
-
height?: string;
|
|
15
|
-
width?: string;
|
|
16
|
-
}
|
|
3
|
+
export declare type Props = HTMLAttributes<HTMLDivElement> & PropsWithChildren<{
|
|
4
|
+
areSectionsAligned?: boolean;
|
|
5
|
+
horizontalSpacing?: number;
|
|
6
|
+
isReversed?: boolean;
|
|
7
|
+
orientation?: Orientation;
|
|
8
|
+
sections?: number;
|
|
9
|
+
verticalSpacing?: number;
|
|
10
|
+
}>;
|
|
17
11
|
/**
|
|
18
|
-
* This is a React component that
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* parallel to the orientation of the grid. This means that in a vertically
|
|
30
|
-
* oriented grid, *number of secitons* refers to the *number of rows*, whereas
|
|
31
|
-
* in a horizontally oriented grid, *number of sections* refers to the *number
|
|
32
|
-
* of columns*.
|
|
12
|
+
* This is a React component that arranges all of its immediate children in a masonry grid. Refrain
|
|
13
|
+
* from assigning CSS styles to it via `className` or `style` property, though they are still
|
|
14
|
+
* handled if absolutely necessary. Customize the grid via its supported properties. The grid can be
|
|
15
|
+
* in either vertical or horizontal orientation. The length of every child element *parallel to the
|
|
16
|
+
* direction of the orientation* is automatically set according to the number of sections specified
|
|
17
|
+
* for the grid. This means that in an horizontally oriented grid, the *width* of each child element
|
|
18
|
+
* is automatically set, whereas in a vertically oriented grid the *height* of each child element is
|
|
19
|
+
* automatically set. Additionally, the *number of sections* corresponds to the maximum the number
|
|
20
|
+
* of child elements present in the direction that is parallel to the orientation of the grid.
|
|
21
|
+
* Hence, in a vertically oriented grid, *number of secitons* refers to the *number of rows*,
|
|
22
|
+
* whereas in a horizontally oriented grid, *number of sections* refers to the *number of columns*.
|
|
33
23
|
*/
|
|
34
|
-
|
|
35
|
-
static defaultProps: Props;
|
|
36
|
-
static BASE_MODIFIER_CLASS_PREFIX: string;
|
|
37
|
-
private nodeRefs;
|
|
38
|
-
private minWidth;
|
|
39
|
-
private maxWidth;
|
|
40
|
-
private minHeight;
|
|
41
|
-
private maxHeight;
|
|
42
|
-
private updateDelegate?;
|
|
43
|
-
get width(): number;
|
|
44
|
-
get height(): number;
|
|
45
|
-
componentDidMount(): void;
|
|
46
|
-
componentDidUpdate(prevProps: Props): void;
|
|
47
|
-
componentWillUnmount(): void;
|
|
48
|
-
render(): JSX.Element;
|
|
49
|
-
update(info: DirtyInfo): void;
|
|
50
|
-
/**
|
|
51
|
-
* Repositions all the child elements of the grid.
|
|
52
|
-
*/
|
|
53
|
-
private repositionChildren;
|
|
54
|
-
/**
|
|
55
|
-
* Computes the index and current length of the next available section for a
|
|
56
|
-
* specific base value, based on a provided array of existing section lengths.
|
|
57
|
-
*
|
|
58
|
-
* @param currentSectionLengths - An array of the current section lengths.
|
|
59
|
-
* @param base - The base value of the item to be inserted into the grid, and
|
|
60
|
-
* to be used to evaluate the next available section.
|
|
61
|
-
*
|
|
62
|
-
* @returns An array consiting of the computed section index and its to-be
|
|
63
|
-
* length if a new item were to be placed in it.
|
|
64
|
-
*/
|
|
65
|
-
private computeNextAvailableSectionAndLengthByBase;
|
|
66
|
-
/**
|
|
67
|
-
* A helper function that computes the max section length of an array of
|
|
68
|
-
* section lengths. Only the first n = `base` sections are inspected.
|
|
69
|
-
*
|
|
70
|
-
* @param currentSectionLengths - An array of section lengths.
|
|
71
|
-
* @param base - The number representing the first n sections to inspect.
|
|
72
|
-
* Any non-numerical values will be ignored and return value
|
|
73
|
-
* will be based on all sections. A `base` value will be clamped
|
|
74
|
-
* between 1 and the maximum length of the array of section
|
|
75
|
-
* lengths.
|
|
76
|
-
*
|
|
77
|
-
* @returns The max section length.
|
|
78
|
-
*/
|
|
79
|
-
private computeMaxLength;
|
|
80
|
-
/**
|
|
81
|
-
* Computes the base value of an element from its classes.
|
|
82
|
-
*
|
|
83
|
-
* @param element - The HTML element.
|
|
84
|
-
*
|
|
85
|
-
* @returns The computed base value that is clamped between 1 and max number
|
|
86
|
-
* of sections.
|
|
87
|
-
*/
|
|
88
|
-
private computeBaseFromElement;
|
|
89
|
-
/**
|
|
90
|
-
* Reinitializes the update delegate. If there are images within the body
|
|
91
|
-
* of the masonry grid, the initialization will be deferred until all images
|
|
92
|
-
* are loaded.
|
|
93
|
-
*/
|
|
94
|
-
private reconfigureUpdateDelegate;
|
|
95
|
-
/**
|
|
96
|
-
* Scans an HTML string and returns all the image sources.
|
|
97
|
-
*
|
|
98
|
-
* @param htmlString The HTML string.
|
|
99
|
-
*
|
|
100
|
-
* @returns The image sources.
|
|
101
|
-
*/
|
|
102
|
-
private getAllImageSources;
|
|
103
|
-
}
|
|
104
|
-
export default MasonryGrid;
|
|
24
|
+
export default function MasonryGrid({ areSectionsAligned, children, horizontalSpacing, isReversed, orientation, sections, style, verticalSpacing, ...props }: Props): JSX.Element;
|
package/lib/MasonryGrid.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
|
|
18
3
|
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
|
|
19
4
|
return cooked;
|
|
@@ -52,6 +37,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
52
37
|
__setModuleDefault(result, mod);
|
|
53
38
|
return result;
|
|
54
39
|
};
|
|
40
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
41
|
+
var t = {};
|
|
42
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
43
|
+
t[p] = s[p];
|
|
44
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
45
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
46
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
47
|
+
t[p[i]] = s[p[i]];
|
|
48
|
+
}
|
|
49
|
+
return t;
|
|
50
|
+
};
|
|
55
51
|
var __read = (this && this.__read) || function (o, n) {
|
|
56
52
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
57
53
|
if (!m) return o;
|
|
@@ -81,128 +77,179 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
81
77
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
82
78
|
};
|
|
83
79
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
84
|
-
var dirty_dom_1 = require("dirty-dom");
|
|
85
80
|
var react_1 = __importStar(require("react"));
|
|
86
81
|
var spase_1 = require("spase");
|
|
87
82
|
var styled_components_1 = __importDefault(require("styled-components"));
|
|
83
|
+
var useResizeEffect_1 = __importDefault(require("./hooks/useResizeEffect"));
|
|
88
84
|
var debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:masonry-grid') : function () { };
|
|
85
|
+
var BASE_MODIFIER_CLASS_PREFIX = 'base-';
|
|
89
86
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* child element is automatically set. Additionally, the *number of sections*
|
|
100
|
-
* means maximum the number of child elements present in the direction that is
|
|
101
|
-
* parallel to the orientation of the grid. This means that in a vertically
|
|
102
|
-
* oriented grid, *number of secitons* refers to the *number of rows*, whereas
|
|
103
|
-
* in a horizontally oriented grid, *number of sections* refers to the *number
|
|
104
|
-
* of columns*.
|
|
87
|
+
* Computes the index and current length of the next available section for a specific base value,
|
|
88
|
+
* based on a provided array of existing section lengths.
|
|
89
|
+
*
|
|
90
|
+
* @param currentSectionLengths - An array of the current section lengths.
|
|
91
|
+
* @param base - The base value of the item to be inserted into the grid, and to be used to evaluate
|
|
92
|
+
* the next available section.
|
|
93
|
+
*
|
|
94
|
+
* @returns An array consiting of the computed section index and its to-be length if a new item were
|
|
95
|
+
* to be placed in it.
|
|
105
96
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return _this;
|
|
119
|
-
}
|
|
120
|
-
Object.defineProperty(MasonryGrid.prototype, "width", {
|
|
121
|
-
get: function () {
|
|
122
|
-
var _a, _b;
|
|
123
|
-
return (_b = (_a = spase_1.Rect.from(this.nodeRefs.root.current)) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
|
|
124
|
-
},
|
|
125
|
-
enumerable: false,
|
|
126
|
-
configurable: true
|
|
127
|
-
});
|
|
128
|
-
Object.defineProperty(MasonryGrid.prototype, "height", {
|
|
129
|
-
get: function () {
|
|
130
|
-
var _a, _b;
|
|
131
|
-
return (_b = (_a = spase_1.Rect.from(this.nodeRefs.root.current)) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0;
|
|
132
|
-
},
|
|
133
|
-
enumerable: false,
|
|
134
|
-
configurable: true
|
|
135
|
-
});
|
|
136
|
-
MasonryGrid.prototype.componentDidMount = function () {
|
|
137
|
-
this.reconfigureUpdateDelegate();
|
|
138
|
-
};
|
|
139
|
-
MasonryGrid.prototype.componentDidUpdate = function (prevProps) {
|
|
140
|
-
this.reconfigureUpdateDelegate();
|
|
141
|
-
};
|
|
142
|
-
MasonryGrid.prototype.componentWillUnmount = function () {
|
|
143
|
-
var _a;
|
|
144
|
-
(_a = this.updateDelegate) === null || _a === void 0 ? void 0 : _a.deinit();
|
|
145
|
-
};
|
|
146
|
-
MasonryGrid.prototype.render = function () {
|
|
147
|
-
var _a, _b;
|
|
148
|
-
return (react_1.default.createElement(StyledRoot, { ref: this.nodeRefs.root, className: this.props.className, orientation: this.props.orientation, style: __assign(__assign({}, this.props.style), { height: (_a = this.props.height) !== null && _a !== void 0 ? _a : ((this.props.orientation === 'vertical' && !isNaN(this.minHeight)) ? "".concat(this.minHeight, "px") : ''), width: (_b = this.props.width) !== null && _b !== void 0 ? _b : ((this.props.orientation === 'horizontal' && !isNaN(this.minWidth)) ? "".concat(this.minWidth, "px") : ''), flex: '0 0 auto', padding: '0' }) }, this.props.children));
|
|
149
|
-
};
|
|
150
|
-
MasonryGrid.prototype.update = function (info) {
|
|
151
|
-
var _a = info, _b = dirty_dom_1.DirtyType.SIZE, dirtySize = _a[_b];
|
|
152
|
-
if (dirtySize) {
|
|
153
|
-
if ((this.minWidth !== this.width) || (this.minHeight !== this.height) || (dirtySize.maxSize.width !== this.maxWidth) || dirtySize.maxSize.height !== this.maxHeight) {
|
|
154
|
-
this.repositionChildren();
|
|
155
|
-
this.maxWidth = dirtySize.maxSize.width;
|
|
156
|
-
this.maxHeight = dirtySize.maxSize.height;
|
|
97
|
+
function computeNextAvailableSectionAndLengthByBase(currentSectionLengths, base) {
|
|
98
|
+
var numSections = currentSectionLengths.length;
|
|
99
|
+
var sectionIdx = NaN;
|
|
100
|
+
var minLength = Infinity;
|
|
101
|
+
for (var i = 0; i < numSections; i++) {
|
|
102
|
+
var length_1 = currentSectionLengths[i];
|
|
103
|
+
var isShorter = length_1 < minLength;
|
|
104
|
+
var isEligibleSection = (i + base) <= numSections;
|
|
105
|
+
var hasRoomInSubsequentSections = true;
|
|
106
|
+
for (var j = 1; j < base; j++) {
|
|
107
|
+
if (currentSectionLengths[i + j] > length_1) {
|
|
108
|
+
hasRoomInSubsequentSections = false;
|
|
157
109
|
}
|
|
158
110
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
111
|
+
if (isShorter && isEligibleSection && hasRoomInSubsequentSections) {
|
|
112
|
+
sectionIdx = i;
|
|
113
|
+
minLength = length_1;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (isNaN(sectionIdx)) {
|
|
117
|
+
return [0, computeMaxLength(currentSectionLengths, base)];
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
return [sectionIdx, minLength];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A helper function that computes the max section length of an array of section lengths. Only the
|
|
125
|
+
* first n = `base` sections are inspected.
|
|
126
|
+
*
|
|
127
|
+
* @param currentSectionLengths - An array of section lengths.
|
|
128
|
+
* @param base - The number representing the first n sections to inspect. Any non-numerical values
|
|
129
|
+
* will be ignored and return value will be based on all sections. A `base` value will
|
|
130
|
+
* be clamped between 1 and the maximum length of the array of section lengths.
|
|
131
|
+
*
|
|
132
|
+
* @returns The max section length.
|
|
133
|
+
*/
|
|
134
|
+
function computeMaxLength(currentSectionLengths, base) {
|
|
135
|
+
var arr = currentSectionLengths;
|
|
136
|
+
if (base !== undefined && base !== null && !isNaN(base)) {
|
|
137
|
+
arr = arr.slice(0, Math.max(1, Math.min(base, currentSectionLengths.length)));
|
|
138
|
+
}
|
|
139
|
+
return arr.reduce(function (out, curr, i) { return (curr > out) ? curr : out; }, 0);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Computes the base value of an element from its classes.
|
|
143
|
+
*
|
|
144
|
+
* @param element - The HTML element.
|
|
145
|
+
* @param numSections - Total number of sections.
|
|
146
|
+
*
|
|
147
|
+
* @returns The computed base value that is clamped between 1 and max number of sections.
|
|
148
|
+
*/
|
|
149
|
+
function computeBaseFromElement(element, numSections) {
|
|
150
|
+
var classList = element.classList;
|
|
151
|
+
for (var i = 0; i < classList.length; i++) {
|
|
152
|
+
var c = classList[i];
|
|
153
|
+
if (c.startsWith(BASE_MODIFIER_CLASS_PREFIX)) {
|
|
154
|
+
var base = parseFloat(c.replace(BASE_MODIFIER_CLASS_PREFIX, ''));
|
|
155
|
+
if (!isNaN(base))
|
|
156
|
+
return Math.min(Math.max(base, 1), numSections);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Scans an HTML string and returns all the image sources.
|
|
163
|
+
*
|
|
164
|
+
* @param htmlString The HTML string.
|
|
165
|
+
*
|
|
166
|
+
* @returns The image sources.
|
|
167
|
+
*/
|
|
168
|
+
function getAllImageSources(htmlString) {
|
|
169
|
+
var _a, _b;
|
|
170
|
+
if (!htmlString)
|
|
171
|
+
return [];
|
|
172
|
+
var regexImg = /<img.*?src=("|')(.*?)("|')/g;
|
|
173
|
+
var regexSrc = /<img.*?src=("|')(.*?)("|')/;
|
|
174
|
+
var imageTags = (_a = htmlString.match(regexImg)) !== null && _a !== void 0 ? _a : [];
|
|
175
|
+
var out = [];
|
|
176
|
+
for (var i = 0; i < imageTags.length; i++) {
|
|
177
|
+
var tag = imageTags[i];
|
|
178
|
+
var src = (_b = tag.match(regexSrc)) === null || _b === void 0 ? void 0 : _b[2];
|
|
179
|
+
if (!src)
|
|
180
|
+
continue;
|
|
181
|
+
out.push(src);
|
|
182
|
+
}
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* This is a React component that arranges all of its immediate children in a masonry grid. Refrain
|
|
187
|
+
* from assigning CSS styles to it via `className` or `style` property, though they are still
|
|
188
|
+
* handled if absolutely necessary. Customize the grid via its supported properties. The grid can be
|
|
189
|
+
* in either vertical or horizontal orientation. The length of every child element *parallel to the
|
|
190
|
+
* direction of the orientation* is automatically set according to the number of sections specified
|
|
191
|
+
* for the grid. This means that in an horizontally oriented grid, the *width* of each child element
|
|
192
|
+
* is automatically set, whereas in a vertically oriented grid the *height* of each child element is
|
|
193
|
+
* automatically set. Additionally, the *number of sections* corresponds to the maximum the number
|
|
194
|
+
* of child elements present in the direction that is parallel to the orientation of the grid.
|
|
195
|
+
* Hence, in a vertically oriented grid, *number of secitons* refers to the *number of rows*,
|
|
196
|
+
* whereas in a horizontally oriented grid, *number of sections* refers to the *number of columns*.
|
|
197
|
+
*/
|
|
198
|
+
function MasonryGrid(_a) {
|
|
199
|
+
var _b = _a.areSectionsAligned, areSectionsAligned = _b === void 0 ? false : _b, children = _a.children, _c = _a.horizontalSpacing, horizontalSpacing = _c === void 0 ? 0 : _c, _d = _a.isReversed, isReversed = _d === void 0 ? false : _d, _e = _a.orientation, orientation = _e === void 0 ? 'vertical' : _e, _f = _a.sections, sections = _f === void 0 ? 3 : _f, _g = _a.style, style = _g === void 0 ? {} : _g, _h = _a.verticalSpacing, verticalSpacing = _h === void 0 ? 0 : _h, props = __rest(_a, ["areSectionsAligned", "children", "horizontalSpacing", "isReversed", "orientation", "sections", "style", "verticalSpacing"]);
|
|
200
|
+
var rootRef = (0, react_1.useRef)(null);
|
|
201
|
+
var _j = __read((0, react_1.useState)(NaN), 2), minWidth = _j[0], setMinWidth = _j[1];
|
|
202
|
+
var _k = __read((0, react_1.useState)(NaN), 2), minHeight = _k[0], setMinHeight = _k[1];
|
|
203
|
+
var _l = __read((0, react_1.useState)(NaN), 2), maxWidth = _l[0], setMaxWidth = _l[1];
|
|
204
|
+
var _m = __read((0, react_1.useState)(NaN), 2), maxHeight = _m[0], setMaxHeight = _m[1];
|
|
205
|
+
var getCurrentWidth = function () { var _a, _b; return (_b = (_a = spase_1.Rect.from(rootRef.current)) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0; };
|
|
206
|
+
var getCurrentHeight = function () { var _a, _b; return (_b = (_a = spase_1.Rect.from(rootRef.current)) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0; };
|
|
207
|
+
var repositionChildren = function () {
|
|
164
208
|
var _a, _b, _c, _d;
|
|
165
|
-
var rootNode =
|
|
209
|
+
var rootNode = rootRef.current;
|
|
166
210
|
if (!rootNode)
|
|
167
211
|
return;
|
|
168
212
|
debug('Repositioning children... OK');
|
|
169
213
|
var children = rootNode.children;
|
|
170
|
-
var numSections =
|
|
214
|
+
var numSections = sections;
|
|
171
215
|
if (numSections <= 0)
|
|
172
216
|
throw new Error('You must specifiy a minimum of 1 section(s) (a.k.a. row(s) for horizontal orientation, column(s) for vertical orientation) for a MasonryGrid instance');
|
|
173
|
-
if (
|
|
217
|
+
if (orientation === 'vertical') {
|
|
174
218
|
var sectionHeights = __spreadArray([], __read(new Array(numSections)), false).map(function () { return 0; });
|
|
175
219
|
for (var i = 0; i < children.length; i++) {
|
|
176
220
|
var child = children[i];
|
|
177
221
|
if (!(child instanceof HTMLElement))
|
|
178
222
|
continue;
|
|
179
|
-
var base =
|
|
180
|
-
var _e = __read(
|
|
223
|
+
var base = computeBaseFromElement(child, sections);
|
|
224
|
+
var _e = __read(computeNextAvailableSectionAndLengthByBase(sectionHeights, base), 2), colIdx = _e[0], y = _e[1];
|
|
181
225
|
child.style.position = 'absolute';
|
|
182
|
-
child.style.width = "calc(".concat(100 / numSections * base, "% - ").concat((
|
|
226
|
+
child.style.width = "calc(".concat(100 / numSections * base, "% - ").concat((horizontalSpacing * (numSections - 1) / numSections) * base, "px + ").concat(horizontalSpacing * (base - 1), "px)");
|
|
183
227
|
child.style.height = '';
|
|
184
|
-
child.style.left = "calc(".concat(100 / numSections * colIdx, "% - ").concat((
|
|
185
|
-
child.style.top = "".concat(y + (y === 0 ? 0 :
|
|
228
|
+
child.style.left = "calc(".concat(100 / numSections * colIdx, "% - ").concat((horizontalSpacing * (numSections - 1) / numSections) * colIdx, "px + ").concat(horizontalSpacing * colIdx, "px)");
|
|
229
|
+
child.style.top = "".concat(y + (y === 0 ? 0 : verticalSpacing), "px");
|
|
186
230
|
for (var j = 0; j < base; j++) {
|
|
187
|
-
sectionHeights[colIdx + j] = y + (y === 0 ? 0 :
|
|
231
|
+
sectionHeights[colIdx + j] = y + (y === 0 ? 0 : verticalSpacing) + ((_b = (_a = spase_1.Rect.from(child)) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0);
|
|
188
232
|
}
|
|
189
|
-
if (
|
|
190
|
-
var m =
|
|
233
|
+
if (areSectionsAligned && ((colIdx + base) === numSections)) {
|
|
234
|
+
var m = computeMaxLength(sectionHeights);
|
|
191
235
|
for (var j = 0; j < numSections; j++) {
|
|
192
236
|
sectionHeights[j] = m;
|
|
193
237
|
}
|
|
194
238
|
}
|
|
195
239
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
240
|
+
var w = getCurrentWidth();
|
|
241
|
+
var h = computeMaxLength(sectionHeights, numSections);
|
|
242
|
+
setMinWidth(w);
|
|
243
|
+
setMinHeight(h);
|
|
244
|
+
if (!isNaN(h))
|
|
245
|
+
rootNode.style.height = "".concat(h, "px");
|
|
246
|
+
if (isReversed) {
|
|
200
247
|
for (var i = 0; i < children.length; i++) {
|
|
201
248
|
var child = children[i];
|
|
202
249
|
if (!(child instanceof HTMLElement))
|
|
203
250
|
continue;
|
|
204
251
|
var x = parseFloat(child.style.left);
|
|
205
|
-
child.style.left = "".concat(
|
|
252
|
+
child.style.left = "".concat(w - x - parseFloat(child.style.width), "px");
|
|
206
253
|
}
|
|
207
254
|
}
|
|
208
255
|
}
|
|
@@ -212,185 +259,66 @@ var MasonryGrid = /** @class */ (function (_super) {
|
|
|
212
259
|
var child = children[i];
|
|
213
260
|
if (!(child instanceof HTMLElement))
|
|
214
261
|
continue;
|
|
215
|
-
var base =
|
|
216
|
-
var _f = __read(
|
|
262
|
+
var base = computeBaseFromElement(child, sections);
|
|
263
|
+
var _f = __read(computeNextAvailableSectionAndLengthByBase(sectionWidths, base), 2), rowIdx = _f[0], x = _f[1];
|
|
217
264
|
child.style.position = 'absolute';
|
|
218
265
|
child.style.width = '';
|
|
219
|
-
child.style.height = "calc(".concat(100 / numSections * base, "% - ").concat((
|
|
220
|
-
child.style.top = "calc(".concat(100 / numSections * rowIdx, "% - ").concat((
|
|
221
|
-
child.style.left = "".concat(x + (x === 0 ? 0 :
|
|
266
|
+
child.style.height = "calc(".concat(100 / numSections * base, "% - ").concat((verticalSpacing * (numSections - 1) / numSections) * base, "px + ").concat(verticalSpacing * (base - 1), "px)");
|
|
267
|
+
child.style.top = "calc(".concat(100 / numSections * rowIdx, "% - ").concat((verticalSpacing * (numSections - 1) / numSections) * rowIdx, "px + ").concat(verticalSpacing * rowIdx, "px)");
|
|
268
|
+
child.style.left = "".concat(x + (x === 0 ? 0 : horizontalSpacing), "px");
|
|
222
269
|
for (var j = 0; j < base; j++) {
|
|
223
|
-
sectionWidths[rowIdx + j] = x + (x === 0 ? 0 :
|
|
270
|
+
sectionWidths[rowIdx + j] = x + (x === 0 ? 0 : horizontalSpacing) + ((_d = (_c = spase_1.Rect.from(child)) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : 0);
|
|
224
271
|
}
|
|
225
|
-
if (
|
|
226
|
-
var m =
|
|
272
|
+
if (areSectionsAligned && ((rowIdx + base) === numSections)) {
|
|
273
|
+
var m = computeMaxLength(sectionWidths);
|
|
227
274
|
for (var j = 0; j < numSections; j++) {
|
|
228
275
|
sectionWidths[j] = m;
|
|
229
276
|
}
|
|
230
277
|
}
|
|
231
278
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (
|
|
279
|
+
var h = getCurrentHeight();
|
|
280
|
+
var w = computeMaxLength(sectionWidths, numSections);
|
|
281
|
+
setMinHeight(h);
|
|
282
|
+
setMinWidth(w);
|
|
283
|
+
if (!isNaN(w))
|
|
284
|
+
rootNode.style.width = "".concat(w, "px");
|
|
285
|
+
if (isReversed) {
|
|
237
286
|
for (var i = 0; i < children.length; i++) {
|
|
238
287
|
var child = children[i];
|
|
239
288
|
if (!(child instanceof HTMLElement))
|
|
240
289
|
continue;
|
|
241
290
|
var y = parseFloat(child.style.top);
|
|
242
|
-
child.style.top = "".concat(
|
|
291
|
+
child.style.top = "".concat(h - y - parseFloat(child.style.height), "px");
|
|
243
292
|
}
|
|
244
293
|
}
|
|
245
294
|
}
|
|
246
295
|
};
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
* @returns An array consiting of the computed section index and its to-be
|
|
256
|
-
* length if a new item were to be placed in it.
|
|
257
|
-
*/
|
|
258
|
-
MasonryGrid.prototype.computeNextAvailableSectionAndLengthByBase = function (currentSectionLengths, base) {
|
|
259
|
-
if (currentSectionLengths.length !== this.props.sections)
|
|
260
|
-
throw new Error('Unmatched number of provided section lengths');
|
|
261
|
-
var numSections = currentSectionLengths.length;
|
|
262
|
-
var sectionIdx = NaN;
|
|
263
|
-
var minLength = Infinity;
|
|
264
|
-
for (var i = 0; i < numSections; i++) {
|
|
265
|
-
var length_1 = currentSectionLengths[i];
|
|
266
|
-
var isShorter = length_1 < minLength;
|
|
267
|
-
var isEligibleSection = (i + base) <= numSections;
|
|
268
|
-
var hasRoomInSubsequentSections = true;
|
|
269
|
-
for (var j = 1; j < base; j++) {
|
|
270
|
-
if (currentSectionLengths[i + j] > length_1) {
|
|
271
|
-
hasRoomInSubsequentSections = false;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (isShorter && isEligibleSection && hasRoomInSubsequentSections) {
|
|
275
|
-
sectionIdx = i;
|
|
276
|
-
minLength = length_1;
|
|
296
|
+
(0, useResizeEffect_1.default)(rootRef, {
|
|
297
|
+
onResize: function (maxSize) {
|
|
298
|
+
var currWidth = getCurrentWidth();
|
|
299
|
+
var currHeight = getCurrentHeight();
|
|
300
|
+
if ((minWidth !== currWidth) || (minHeight !== currHeight) || (maxSize.width !== maxWidth) || maxSize.height !== maxHeight) {
|
|
301
|
+
repositionChildren();
|
|
302
|
+
setMaxWidth(maxSize.width);
|
|
303
|
+
setMaxHeight(maxSize.height);
|
|
277
304
|
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
return [sectionIdx, minLength];
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
/**
|
|
287
|
-
* A helper function that computes the max section length of an array of
|
|
288
|
-
* section lengths. Only the first n = `base` sections are inspected.
|
|
289
|
-
*
|
|
290
|
-
* @param currentSectionLengths - An array of section lengths.
|
|
291
|
-
* @param base - The number representing the first n sections to inspect.
|
|
292
|
-
* Any non-numerical values will be ignored and return value
|
|
293
|
-
* will be based on all sections. A `base` value will be clamped
|
|
294
|
-
* between 1 and the maximum length of the array of section
|
|
295
|
-
* lengths.
|
|
296
|
-
*
|
|
297
|
-
* @returns The max section length.
|
|
298
|
-
*/
|
|
299
|
-
MasonryGrid.prototype.computeMaxLength = function (currentSectionLengths, base) {
|
|
300
|
-
var arr = currentSectionLengths;
|
|
301
|
-
if (base !== undefined && base !== null && !isNaN(base)) {
|
|
302
|
-
arr = arr.slice(0, Math.max(1, Math.min(base, currentSectionLengths.length)));
|
|
303
|
-
}
|
|
304
|
-
return arr.reduce(function (out, curr, i) { return (curr > out) ? curr : out; }, 0);
|
|
305
|
-
};
|
|
306
|
-
/**
|
|
307
|
-
* Computes the base value of an element from its classes.
|
|
308
|
-
*
|
|
309
|
-
* @param element - The HTML element.
|
|
310
|
-
*
|
|
311
|
-
* @returns The computed base value that is clamped between 1 and max number
|
|
312
|
-
* of sections.
|
|
313
|
-
*/
|
|
314
|
-
MasonryGrid.prototype.computeBaseFromElement = function (element) {
|
|
315
|
-
var classList = element.classList;
|
|
316
|
-
for (var i = 0; i < classList.length; i++) {
|
|
317
|
-
var c = classList[i];
|
|
318
|
-
if (c.startsWith(MasonryGrid.BASE_MODIFIER_CLASS_PREFIX)) {
|
|
319
|
-
var base = parseFloat(c.replace(MasonryGrid.BASE_MODIFIER_CLASS_PREFIX, ''));
|
|
320
|
-
if (!isNaN(base))
|
|
321
|
-
return Math.min(Math.max(base, 1), this.props.sections);
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return 1;
|
|
325
|
-
};
|
|
326
|
-
/**
|
|
327
|
-
* Reinitializes the update delegate. If there are images within the body
|
|
328
|
-
* of the masonry grid, the initialization will be deferred until all images
|
|
329
|
-
* are loaded.
|
|
330
|
-
*/
|
|
331
|
-
MasonryGrid.prototype.reconfigureUpdateDelegate = function () {
|
|
305
|
+
},
|
|
306
|
+
}, [areSectionsAligned, horizontalSpacing, isReversed, sections, verticalSpacing]);
|
|
307
|
+
(0, react_1.useEffect)(function () {
|
|
332
308
|
var _a;
|
|
333
|
-
var
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
var numImages_1 = imageSources.length;
|
|
343
|
-
for (var i = 0; i < numImages_1; i++) {
|
|
344
|
-
var src = imageSources[i];
|
|
345
|
-
var image = new Image();
|
|
346
|
-
image.src = src;
|
|
347
|
-
image.onload = function () {
|
|
348
|
-
var _a;
|
|
349
|
-
if (++loaded_1 === numImages_1)
|
|
350
|
-
(_a = _this.updateDelegate) === null || _a === void 0 ? void 0 : _a.init();
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
(_d = this.updateDelegate) === null || _d === void 0 ? void 0 : _d.init();
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
/**
|
|
359
|
-
* Scans an HTML string and returns all the image sources.
|
|
360
|
-
*
|
|
361
|
-
* @param htmlString The HTML string.
|
|
362
|
-
*
|
|
363
|
-
* @returns The image sources.
|
|
364
|
-
*/
|
|
365
|
-
MasonryGrid.prototype.getAllImageSources = function (htmlString) {
|
|
366
|
-
var _a, _b;
|
|
367
|
-
if (!htmlString)
|
|
368
|
-
return [];
|
|
369
|
-
var regexImg = /<img.*?src=("|')(.*?)("|')/g;
|
|
370
|
-
var regexSrc = /<img.*?src=("|')(.*?)("|')/;
|
|
371
|
-
var imageTags = (_a = htmlString.match(regexImg)) !== null && _a !== void 0 ? _a : [];
|
|
372
|
-
var out = [];
|
|
373
|
-
for (var i = 0; i < imageTags.length; i++) {
|
|
374
|
-
var tag = imageTags[i];
|
|
375
|
-
var src = (_b = tag.match(regexSrc)) === null || _b === void 0 ? void 0 : _b[2];
|
|
376
|
-
if (!src)
|
|
377
|
-
continue;
|
|
378
|
-
out.push(src);
|
|
309
|
+
var imageSources = getAllImageSources((_a = rootRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML);
|
|
310
|
+
if (imageSources.length === 0)
|
|
311
|
+
return;
|
|
312
|
+
var numImages = imageSources.length;
|
|
313
|
+
for (var i = 0; i < numImages; i++) {
|
|
314
|
+
var src = imageSources[i];
|
|
315
|
+
var image = new Image();
|
|
316
|
+
image.src = src;
|
|
317
|
+
image.onload = function () { return repositionChildren(); };
|
|
379
318
|
}
|
|
380
|
-
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
areSectionsAligned: false,
|
|
384
|
-
horizontalSpacing: 0,
|
|
385
|
-
isReversed: false,
|
|
386
|
-
orientation: 'vertical',
|
|
387
|
-
sections: 3,
|
|
388
|
-
style: {},
|
|
389
|
-
verticalSpacing: 0,
|
|
390
|
-
};
|
|
391
|
-
MasonryGrid.BASE_MODIFIER_CLASS_PREFIX = 'base-';
|
|
392
|
-
return MasonryGrid;
|
|
393
|
-
}(react_1.PureComponent));
|
|
319
|
+
}, []);
|
|
320
|
+
return (react_1.default.createElement(StyledRoot, __assign({}, props, { ref: rootRef, orientation: orientation, style: __assign(__assign({}, style), { flex: '0 0 auto', minHeight: ((orientation === 'vertical' && !isNaN(minHeight)) ? "".concat(minHeight, "px") : ''), minWidth: ((orientation === 'horizontal' && !isNaN(minWidth)) ? "".concat(minWidth, "px") : ''), padding: '0' }) }), children));
|
|
321
|
+
}
|
|
394
322
|
exports.default = MasonryGrid;
|
|
395
323
|
var StyledRoot = styled_components_1.default.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n box-sizing: border-box;\n display: block;\n height: ", ";\n position: relative;\n width: ", ";\n"], ["\n box-sizing: border-box;\n display: block;\n height: ", ";\n position: relative;\n width: ", ";\n"])), function (props) { return props.orientation === 'vertical' ? 'auto' : '100%'; }, function (props) { return props.orientation === 'horizontal' ? 'auto' : '100%'; });
|
|
396
324
|
var templateObject_1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kD,EAE5D,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAApD,CAAoD,CACvE,CAAA","sourcesContent":["import { DirtyInfo, DirtyType, EventType, UpdateDelegate } from 'dirty-dom'\nimport React, { createRef, CSSProperties, PureComponent, ReactNode } from 'react'\nimport { Rect } from 'spase'\nimport styled from 'styled-components'\nimport { Orientation } from './types'\n\nconst debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:masonry-grid') : () => {}\n\nexport interface Props {\n className?: string\n style: CSSProperties\n children?: ReactNode | ReactNode[]\n areSectionsAligned: boolean\n isReversed: boolean\n horizontalSpacing: number\n orientation: Orientation\n sections: number\n verticalSpacing: number\n height?: string\n width?: string\n}\n\n/**\n * This is a React component that aligns all of its immediate children in a\n * masonry grid. Refrain from assigning CSS styles to it via `className` or\n * `style` property, although they are handled if absolutely necessary.\n * Customize the grid via its supported properties. The grid can be in either\n * vertical or horizontal orientation. The length of every child element\n * *parallel to the direction of the orientation* is automatically set according\n * to the number of sections specified for the grid. This means that in an\n * horizontally oriented grid, the *width* of each child element is\n * automatically set, whereas in a vertically oriented grid the *height* of each\n * child element is automatically set. Additionally, the *number of sections*\n * means maximum the number of child elements present in the direction that is\n * parallel to the orientation of the grid. This means that in a vertically\n * oriented grid, *number of secitons* refers to the *number of rows*, whereas\n * in a horizontally oriented grid, *number of sections* refers to the *number\n * of columns*.\n */\nclass MasonryGrid extends PureComponent<Props> {\n static defaultProps: Props = {\n areSectionsAligned: false,\n horizontalSpacing: 0,\n isReversed: false,\n orientation: 'vertical',\n sections: 3,\n style: {},\n verticalSpacing: 0,\n }\n\n static BASE_MODIFIER_CLASS_PREFIX = 'base-'\n\n private nodeRefs = {\n root: createRef<HTMLDivElement>(),\n }\n\n private minWidth = NaN\n private maxWidth = NaN\n private minHeight = NaN\n private maxHeight = NaN\n\n private updateDelegate?: UpdateDelegate = undefined\n\n get width(): number {\n return Rect.from(this.nodeRefs.root.current)?.width ?? 0\n }\n\n get height(): number {\n return Rect.from(this.nodeRefs.root.current)?.height ?? 0\n }\n\n componentDidMount() {\n this.reconfigureUpdateDelegate()\n }\n\n componentDidUpdate(prevProps: Props) {\n this.reconfigureUpdateDelegate()\n }\n\n componentWillUnmount() {\n this.updateDelegate?.deinit()\n }\n\n render() {\n return (\n <StyledRoot\n ref={this.nodeRefs.root}\n className={this.props.className}\n orientation={this.props.orientation}\n style={{\n ...this.props.style,\n height: this.props.height ?? ((this.props.orientation === 'vertical' && !isNaN(this.minHeight)) ? `${this.minHeight}px` : ''),\n width: this.props.width ?? ((this.props.orientation === 'horizontal' && !isNaN(this.minWidth)) ? `${this.minWidth}px` : ''),\n flex: '0 0 auto',\n padding: '0',\n }}\n >\n {this.props.children}\n </StyledRoot>\n )\n }\n\n update(info: DirtyInfo) {\n const { [DirtyType.SIZE]: dirtySize } = info\n\n if (dirtySize) {\n if ((this.minWidth !== this.width) || (this.minHeight !== this.height) || (dirtySize.maxSize.width !== this.maxWidth) || dirtySize.maxSize.height !== this.maxHeight) {\n this.repositionChildren()\n this.maxWidth = dirtySize.maxSize.width\n this.maxHeight = dirtySize.maxSize.height\n }\n }\n }\n\n /**\n * Repositions all the child elements of the grid.\n */\n private repositionChildren() {\n const rootNode = this.nodeRefs.root.current\n\n if (!rootNode) return\n\n debug('Repositioning children... OK')\n\n const children = rootNode.children\n const numSections = this.props.sections\n\n if (numSections <= 0) throw new Error('You must specifiy a minimum of 1 section(s) (a.k.a. row(s) for horizontal orientation, column(s) for vertical orientation) for a MasonryGrid instance')\n\n if (this.props.orientation === 'vertical') {\n const sectionHeights: number[] = [...new Array(numSections)].map(() => 0)\n\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const base = this.computeBaseFromElement(child)\n const [colIdx, y] = this.computeNextAvailableSectionAndLengthByBase(sectionHeights, base)\n\n child.style.position = 'absolute'\n child.style.width = `calc(${100 / numSections * base}% - ${(this.props.horizontalSpacing * (numSections - 1) / numSections) * base}px + ${this.props.horizontalSpacing * (base - 1)}px)`\n child.style.height = ''\n child.style.left = `calc(${100 / numSections * colIdx}% - ${(this.props.horizontalSpacing * (numSections - 1) / numSections) * colIdx}px + ${this.props.horizontalSpacing * colIdx}px)`\n child.style.top = `${y + (y === 0 ? 0 : this.props.verticalSpacing)}px`\n\n for (let j = 0; j < base; j++) {\n sectionHeights[colIdx + j] = y + (y === 0 ? 0 : this.props.verticalSpacing) + (Rect.from(child)?.height ?? 0)\n }\n\n if (this.props.areSectionsAligned && ((colIdx + base) === numSections)) {\n const m = this.computeMaxLength(sectionHeights)\n\n for (let j = 0; j < numSections; j++) {\n sectionHeights[j] = m\n }\n }\n }\n\n this.minWidth = this.width\n this.minHeight = this.computeMaxLength(sectionHeights, numSections)\n rootNode.style.height = `${this.minHeight}px`\n\n if (this.props.isReversed) {\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const x = parseFloat(child.style.left)\n\n child.style.left = `${this.width - x - parseFloat(child.style.width)}px`\n }\n }\n }\n else {\n const sectionWidths: number[] = [...new Array(numSections)].map(() => 0)\n\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const base = this.computeBaseFromElement(child)\n const [rowIdx, x] = this.computeNextAvailableSectionAndLengthByBase(sectionWidths, base)\n\n child.style.position = 'absolute'\n child.style.width = ''\n child.style.height = `calc(${100 / numSections * base}% - ${(this.props.verticalSpacing * (numSections - 1) / numSections) * base}px + ${this.props.verticalSpacing * (base - 1)}px)`\n child.style.top = `calc(${100 / numSections * rowIdx}% - ${(this.props.verticalSpacing * (numSections - 1) / numSections) * rowIdx}px + ${this.props.verticalSpacing * rowIdx}px)`\n child.style.left = `${x + (x === 0 ? 0 : this.props.horizontalSpacing)}px`\n\n for (let j = 0; j < base; j++) {\n sectionWidths[rowIdx + j] = x + (x === 0 ? 0 : this.props.horizontalSpacing) + (Rect.from(child)?.width ?? 0)\n }\n\n if (this.props.areSectionsAligned && ((rowIdx + base) === numSections)) {\n const m = this.computeMaxLength(sectionWidths)\n\n for (let j = 0; j < numSections; j++) {\n sectionWidths[j] = m\n }\n }\n }\n\n this.minHeight = this.height\n this.minWidth = this.computeMaxLength(sectionWidths, numSections)\n if (!isNaN(this.minWidth)) rootNode.style.width = `${this.minWidth}px`\n\n if (this.props.isReversed) {\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const y = parseFloat(child.style.top)\n\n child.style.top = `${this.height - y - parseFloat(child.style.height)}px`\n }\n }\n }\n }\n\n /**\n * Computes the index and current length of the next available section for a\n * specific base value, based on a provided array of existing section lengths.\n *\n * @param currentSectionLengths - An array of the current section lengths.\n * @param base - The base value of the item to be inserted into the grid, and\n * to be used to evaluate the next available section.\n *\n * @returns An array consiting of the computed section index and its to-be\n * length if a new item were to be placed in it.\n */\n private computeNextAvailableSectionAndLengthByBase(currentSectionLengths: number[], base: number): [number, number] {\n if (currentSectionLengths.length !== this.props.sections) throw new Error('Unmatched number of provided section lengths')\n\n const numSections = currentSectionLengths.length\n\n let sectionIdx = NaN\n let minLength = Infinity\n\n for (let i = 0; i < numSections; i++) {\n const length = currentSectionLengths[i]\n const isShorter = length < minLength\n const isEligibleSection = (i + base) <= numSections\n let hasRoomInSubsequentSections = true\n\n for (let j = 1; j < base; j++) {\n if (currentSectionLengths[i + j] > length) {\n hasRoomInSubsequentSections = false\n }\n }\n\n if (isShorter && isEligibleSection && hasRoomInSubsequentSections) {\n sectionIdx = i\n minLength = length\n }\n }\n\n if (isNaN(sectionIdx)) {\n return [0, this.computeMaxLength(currentSectionLengths, base)]\n }\n else {\n return [sectionIdx, minLength]\n }\n }\n\n /**\n * A helper function that computes the max section length of an array of\n * section lengths. Only the first n = `base` sections are inspected.\n *\n * @param currentSectionLengths - An array of section lengths.\n * @param base - The number representing the first n sections to inspect.\n * Any non-numerical values will be ignored and return value\n * will be based on all sections. A `base` value will be clamped\n * between 1 and the maximum length of the array of section\n * lengths.\n *\n * @returns The max section length.\n */\n private computeMaxLength(currentSectionLengths: number[], base?: number): number {\n let arr = currentSectionLengths\n\n if (base !== undefined && base !== null && !isNaN(base)) {\n arr = arr.slice(0, Math.max(1, Math.min(base, currentSectionLengths.length)))\n }\n\n return arr.reduce((out, curr, i) => (curr > out) ? curr : out, 0)\n }\n\n /**\n * Computes the base value of an element from its classes.\n *\n * @param element - The HTML element.\n *\n * @returns The computed base value that is clamped between 1 and max number\n * of sections.\n */\n private computeBaseFromElement(element: HTMLElement): number {\n const classList = element.classList\n\n for (let i = 0; i < classList.length; i++) {\n const c = classList[i]\n\n if (c.startsWith(MasonryGrid.BASE_MODIFIER_CLASS_PREFIX)) {\n const base = parseFloat(c.replace(MasonryGrid.BASE_MODIFIER_CLASS_PREFIX, ''))\n if (!isNaN(base)) return Math.min(Math.max(base, 1), this.props.sections)\n }\n }\n\n return 1\n }\n\n /**\n * Reinitializes the update delegate. If there are images within the body\n * of the masonry grid, the initialization will be deferred until all images\n * are loaded.\n */\n private reconfigureUpdateDelegate() {\n this.updateDelegate?.deinit()\n\n this.updateDelegate = new UpdateDelegate(info => this.update(info), {\n [EventType.RESIZE]: true,\n })\n\n const imageSources = this.getAllImageSources(this.nodeRefs.root.current?.innerHTML)\n\n if (imageSources.length > 0) {\n let loaded = 0\n const numImages = imageSources.length\n\n for (let i = 0; i < numImages; i++) {\n const src = imageSources[i]\n const image = new Image()\n image.src = src\n image.onload = () => {\n if (++loaded === numImages) this.updateDelegate?.init()\n }\n }\n }\n else {\n this.updateDelegate?.init()\n }\n }\n\n /**\n * Scans an HTML string and returns all the image sources.\n *\n * @param htmlString The HTML string.\n *\n * @returns The image sources.\n */\n private getAllImageSources(htmlString?: string): string[] {\n if (!htmlString) return []\n\n const regexImg = /<img.*?src=(\"|')(.*?)(\"|')/g\n const regexSrc = /<img.*?src=(\"|')(.*?)(\"|')/\n const imageTags = htmlString.match(regexImg) ?? []\n\n const out: string[] = []\n\n for (let i = 0; i < imageTags.length; i++) {\n const tag = imageTags[i]\n const src = tag.match(regexSrc)?.[2]\n\n if (!src) continue\n\n out.push(src)\n }\n\n return out\n }\n}\n\nexport default MasonryGrid\n\nconst StyledRoot = styled.div<{\n orientation: Props['orientation']\n}>`\n box-sizing: border-box;\n display: block;\n height: ${props => props.orientation === 'vertical' ? 'auto' : '100%'};\n position: relative;\n width: ${props => props.orientation === 'horizontal' ? 'auto' : '100%'};\n`\n"]}
|
|
1
|
+
{"version":3,"file":"MasonryGrid.js","sourceRoot":"/","sources":["MasonryGrid.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA6F;AAC7F,+BAA4B;AAC5B,wEAAsC;AACtC,4EAAqD;AAGrD,IAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,cAAO,CAAC,CAAA;AAWzG,IAAM,0BAA0B,GAAG,OAAO,CAAA;AAE1C;;;;;;;;;;GAUG;AACH,SAAS,0CAA0C,CAAC,qBAA+B,EAAE,IAAY;IAC/F,IAAM,WAAW,GAAG,qBAAqB,CAAC,MAAM,CAAA;IAEhD,IAAI,UAAU,GAAG,GAAG,CAAA;IACpB,IAAI,SAAS,GAAG,QAAQ,CAAA;IAExB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,EAAE,CAAC,EAAE,EAAE;QACpC,IAAM,QAAM,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAA;QACvC,IAAM,SAAS,GAAG,QAAM,GAAG,SAAS,CAAA;QACpC,IAAM,iBAAiB,GAAG,CAAC,CAAC,GAAG,I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AE5D,EAA6D,KACvE,KAHW,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAlD,CAAkD,EAE5D,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAApD,CAAoD,CACvE,CAAA","sourcesContent":["import React, { HTMLAttributes, PropsWithChildren, useEffect, useRef, useState } from 'react'\nimport { Rect } from 'spase'\nimport styled from 'styled-components'\nimport useResizeEffect from './hooks/useResizeEffect'\nimport { Orientation } from './types'\n\nconst debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:masonry-grid') : () => {}\n\nexport type Props = HTMLAttributes<HTMLDivElement> & PropsWithChildren<{\n areSectionsAligned?: boolean\n horizontalSpacing?: number\n isReversed?: boolean\n orientation?: Orientation\n sections?: number\n verticalSpacing?: number\n}>\n\nconst BASE_MODIFIER_CLASS_PREFIX = 'base-'\n\n/**\n * Computes the index and current length of the next available section for a specific base value,\n * based on a provided array of existing section lengths.\n *\n * @param currentSectionLengths - An array of the current section lengths.\n * @param base - The base value of the item to be inserted into the grid, and to be used to evaluate\n * the next available section.\n *\n * @returns An array consiting of the computed section index and its to-be length if a new item were\n * to be placed in it.\n */\nfunction computeNextAvailableSectionAndLengthByBase(currentSectionLengths: number[], base: number): [number, number] {\n const numSections = currentSectionLengths.length\n\n let sectionIdx = NaN\n let minLength = Infinity\n\n for (let i = 0; i < numSections; i++) {\n const length = currentSectionLengths[i]\n const isShorter = length < minLength\n const isEligibleSection = (i + base) <= numSections\n let hasRoomInSubsequentSections = true\n\n for (let j = 1; j < base; j++) {\n if (currentSectionLengths[i + j] > length) {\n hasRoomInSubsequentSections = false\n }\n }\n\n if (isShorter && isEligibleSection && hasRoomInSubsequentSections) {\n sectionIdx = i\n minLength = length\n }\n }\n\n if (isNaN(sectionIdx)) {\n return [0, computeMaxLength(currentSectionLengths, base)]\n }\n else {\n return [sectionIdx, minLength]\n }\n}\n\n/**\n * A helper function that computes the max section length of an array of section lengths. Only the\n * first n = `base` sections are inspected.\n *\n * @param currentSectionLengths - An array of section lengths.\n * @param base - The number representing the first n sections to inspect. Any non-numerical values\n * will be ignored and return value will be based on all sections. A `base` value will\n * be clamped between 1 and the maximum length of the array of section lengths.\n *\n * @returns The max section length.\n */\nfunction computeMaxLength(currentSectionLengths: number[], base?: number): number {\n let arr = currentSectionLengths\n\n if (base !== undefined && base !== null && !isNaN(base)) {\n arr = arr.slice(0, Math.max(1, Math.min(base, currentSectionLengths.length)))\n }\n\n return arr.reduce((out, curr, i) => (curr > out) ? curr : out, 0)\n}\n\n/**\n * Computes the base value of an element from its classes.\n *\n * @param element - The HTML element.\n * @param numSections - Total number of sections.\n *\n * @returns The computed base value that is clamped between 1 and max number of sections.\n */\nfunction computeBaseFromElement(element: HTMLElement, numSections: number): number {\n const classList = element.classList\n\n for (let i = 0; i < classList.length; i++) {\n const c = classList[i]\n\n if (c.startsWith(BASE_MODIFIER_CLASS_PREFIX)) {\n const base = parseFloat(c.replace(BASE_MODIFIER_CLASS_PREFIX, ''))\n if (!isNaN(base)) return Math.min(Math.max(base, 1), numSections)\n }\n }\n\n return 1\n}\n\n/**\n * Scans an HTML string and returns all the image sources.\n *\n * @param htmlString The HTML string.\n *\n * @returns The image sources.\n */\nfunction getAllImageSources(htmlString?: string): string[] {\n if (!htmlString) return []\n\n const regexImg = /<img.*?src=(\"|')(.*?)(\"|')/g\n const regexSrc = /<img.*?src=(\"|')(.*?)(\"|')/\n const imageTags = htmlString.match(regexImg) ?? []\n\n const out: string[] = []\n\n for (let i = 0; i < imageTags.length; i++) {\n const tag = imageTags[i]\n const src = tag.match(regexSrc)?.[2]\n\n if (!src) continue\n\n out.push(src)\n }\n\n return out\n}\n\n/**\n * This is a React component that arranges all of its immediate children in a masonry grid. Refrain\n * from assigning CSS styles to it via `className` or `style` property, though they are still\n * handled if absolutely necessary. Customize the grid via its supported properties. The grid can be\n * in either vertical or horizontal orientation. The length of every child element *parallel to the\n * direction of the orientation* is automatically set according to the number of sections specified\n * for the grid. This means that in an horizontally oriented grid, the *width* of each child element\n * is automatically set, whereas in a vertically oriented grid the *height* of each child element is\n * automatically set. Additionally, the *number of sections* corresponds to the maximum the number\n * of child elements present in the direction that is parallel to the orientation of the grid.\n * Hence, in a vertically oriented grid, *number of secitons* refers to the *number of rows*,\n * whereas in a horizontally oriented grid, *number of sections* refers to the *number of columns*.\n */\nexport default function MasonryGrid({\n areSectionsAligned = false,\n children,\n horizontalSpacing= 0,\n isReversed = false,\n orientation = 'vertical',\n sections = 3,\n style = {},\n verticalSpacing = 0,\n ...props\n}: Props) {\n const rootRef = useRef<HTMLDivElement>(null)\n\n const [minWidth, setMinWidth] = useState(NaN)\n const [minHeight, setMinHeight] = useState(NaN)\n const [maxWidth, setMaxWidth] = useState(NaN)\n const [maxHeight, setMaxHeight] = useState(NaN)\n\n const getCurrentWidth = () => Rect.from(rootRef.current)?.width ?? 0\n\n const getCurrentHeight = () => Rect.from(rootRef.current)?.height ?? 0\n\n const repositionChildren = () => {\n const rootNode = rootRef.current\n\n if (!rootNode) return\n\n debug('Repositioning children... OK')\n\n const children = rootNode.children\n const numSections = sections\n\n if (numSections <= 0) throw new Error('You must specifiy a minimum of 1 section(s) (a.k.a. row(s) for horizontal orientation, column(s) for vertical orientation) for a MasonryGrid instance')\n\n if (orientation === 'vertical') {\n const sectionHeights: number[] = [...new Array(numSections)].map(() => 0)\n\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const base = computeBaseFromElement(child, sections)\n const [colIdx, y] = computeNextAvailableSectionAndLengthByBase(sectionHeights, base)\n\n child.style.position = 'absolute'\n child.style.width = `calc(${100 / numSections * base}% - ${(horizontalSpacing * (numSections - 1) / numSections) * base}px + ${horizontalSpacing * (base - 1)}px)`\n child.style.height = ''\n child.style.left = `calc(${100 / numSections * colIdx}% - ${(horizontalSpacing * (numSections - 1) / numSections) * colIdx}px + ${horizontalSpacing * colIdx}px)`\n child.style.top = `${y + (y === 0 ? 0 : verticalSpacing)}px`\n\n for (let j = 0; j < base; j++) {\n sectionHeights[colIdx + j] = y + (y === 0 ? 0 : verticalSpacing) + (Rect.from(child)?.height ?? 0)\n }\n\n if (areSectionsAligned && ((colIdx + base) === numSections)) {\n const m = computeMaxLength(sectionHeights)\n\n for (let j = 0; j < numSections; j++) {\n sectionHeights[j] = m\n }\n }\n }\n\n const w = getCurrentWidth()\n const h = computeMaxLength(sectionHeights, numSections)\n\n setMinWidth(w)\n setMinHeight(h)\n\n if (!isNaN(h)) rootNode.style.height = `${h}px`\n\n if (isReversed) {\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const x = parseFloat(child.style.left)\n\n child.style.left = `${w - x - parseFloat(child.style.width)}px`\n }\n }\n }\n else {\n const sectionWidths: number[] = [...new Array(numSections)].map(() => 0)\n\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const base = computeBaseFromElement(child, sections)\n const [rowIdx, x] = computeNextAvailableSectionAndLengthByBase(sectionWidths, base)\n\n child.style.position = 'absolute'\n child.style.width = ''\n child.style.height = `calc(${100 / numSections * base}% - ${(verticalSpacing * (numSections - 1) / numSections) * base}px + ${verticalSpacing * (base - 1)}px)`\n child.style.top = `calc(${100 / numSections * rowIdx}% - ${(verticalSpacing * (numSections - 1) / numSections) * rowIdx}px + ${verticalSpacing * rowIdx}px)`\n child.style.left = `${x + (x === 0 ? 0 : horizontalSpacing)}px`\n\n for (let j = 0; j < base; j++) {\n sectionWidths[rowIdx + j] = x + (x === 0 ? 0 : horizontalSpacing) + (Rect.from(child)?.width ?? 0)\n }\n\n if (areSectionsAligned && ((rowIdx + base) === numSections)) {\n const m = computeMaxLength(sectionWidths)\n\n for (let j = 0; j < numSections; j++) {\n sectionWidths[j] = m\n }\n }\n }\n\n const h = getCurrentHeight()\n const w = computeMaxLength(sectionWidths, numSections)\n\n setMinHeight(h)\n setMinWidth(w)\n\n if (!isNaN(w)) rootNode.style.width = `${w}px`\n\n if (isReversed) {\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n\n if (!(child instanceof HTMLElement)) continue\n\n const y = parseFloat(child.style.top)\n\n child.style.top = `${h - y - parseFloat(child.style.height)}px`\n }\n }\n }\n }\n\n useResizeEffect(rootRef, {\n onResize: maxSize => {\n const currWidth = getCurrentWidth()\n const currHeight = getCurrentHeight()\n\n if ((minWidth !== currWidth) || (minHeight !== currHeight) || (maxSize.width !== maxWidth) || maxSize.height !== maxHeight) {\n repositionChildren()\n setMaxWidth(maxSize.width)\n setMaxHeight(maxSize.height)\n }\n },\n }, [areSectionsAligned, horizontalSpacing, isReversed, sections, verticalSpacing])\n\n useEffect(() => {\n const imageSources = getAllImageSources(rootRef.current?.innerHTML)\n\n if (imageSources.length === 0) return\n\n const numImages = imageSources.length\n\n for (let i = 0; i < numImages; i++) {\n const src = imageSources[i]\n const image = new Image()\n image.src = src\n image.onload = () => repositionChildren()\n }\n }, [])\n\n return (\n <StyledRoot\n {...props}\n ref={rootRef}\n orientation={orientation}\n style={{\n ...style,\n flex: '0 0 auto',\n minHeight: ((orientation === 'vertical' && !isNaN(minHeight)) ? `${minHeight}px` : ''),\n minWidth: ((orientation === 'horizontal' && !isNaN(minWidth)) ? `${minWidth}px` : ''),\n padding: '0',\n }}\n >\n {children}\n </StyledRoot>\n )\n}\n\nconst StyledRoot = styled.div<{\n orientation: Props['orientation']\n}>`\n box-sizing: border-box;\n display: block;\n height: ${props => props.orientation === 'vertical' ? 'auto' : '100%'};\n position: relative;\n width: ${props => props.orientation === 'horizontal' ? 'auto' : '100%'};\n`\n"]}
|
|
@@ -2,22 +2,30 @@ import { Dispatch, SetStateAction } from 'react';
|
|
|
2
2
|
export declare type Options<T> = {
|
|
3
3
|
/**
|
|
4
4
|
* Function for transforming the search param value to the value of the mapped state.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The search param value. `undefined` means the value is unavailable.
|
|
7
|
+
*
|
|
8
|
+
* @returns The equivalent state value.
|
|
5
9
|
*/
|
|
6
|
-
mapSearchParamToState?: (value
|
|
10
|
+
mapSearchParamToState?: (value?: string) => T;
|
|
7
11
|
/**
|
|
8
12
|
* Function for transforming the value of the mapped state to the search param value.
|
|
13
|
+
*
|
|
14
|
+
* @param state - The state value.
|
|
15
|
+
*
|
|
16
|
+
* @returns The equivalent search param value.
|
|
9
17
|
*/
|
|
10
|
-
mapStateToSearchParam?: (state: T
|
|
18
|
+
mapStateToSearchParam?: (state: T) => string | undefined;
|
|
11
19
|
};
|
|
12
20
|
/**
|
|
13
21
|
* Hook for mapping a search param to a state. Whenever the value of the target search param
|
|
14
22
|
* changes, the mapped state will change as well, and vice versa.
|
|
15
23
|
*
|
|
16
24
|
* @param param - The search param key.
|
|
17
|
-
* @param
|
|
25
|
+
* @param defaultValue - The default value of the state.
|
|
18
26
|
* @param options - See {@link Options}.
|
|
19
27
|
*
|
|
20
28
|
* @returns A tuple consisting of a stateful value representing the current value of the mapped
|
|
21
29
|
* state and a function that updates it.
|
|
22
30
|
*/
|
|
23
|
-
export default function useSearchParamState<T>(param: string,
|
|
31
|
+
export default function useSearchParamState<T>(param: string, defaultValue: T, { mapSearchParamToState, mapStateToSearchParam }?: Options<T>): [T, Dispatch<SetStateAction<T>>];
|
|
@@ -24,20 +24,21 @@ var debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:ho
|
|
|
24
24
|
* changes, the mapped state will change as well, and vice versa.
|
|
25
25
|
*
|
|
26
26
|
* @param param - The search param key.
|
|
27
|
-
* @param
|
|
27
|
+
* @param defaultValue - The default value of the state.
|
|
28
28
|
* @param options - See {@link Options}.
|
|
29
29
|
*
|
|
30
30
|
* @returns A tuple consisting of a stateful value representing the current value of the mapped
|
|
31
31
|
* state and a function that updates it.
|
|
32
32
|
*/
|
|
33
|
-
function useSearchParamState(param,
|
|
34
|
-
var _b
|
|
35
|
-
var
|
|
33
|
+
function useSearchParamState(param, defaultValue, _a) {
|
|
34
|
+
var _b;
|
|
35
|
+
var _c = _a === void 0 ? {} : _a, mapSearchParamToState = _c.mapSearchParamToState, mapStateToSearchParam = _c.mapStateToSearchParam;
|
|
36
|
+
var _mapSearchParamToState = function (value, defaultValue) {
|
|
36
37
|
if (mapSearchParamToState) {
|
|
37
38
|
return mapSearchParamToState(value);
|
|
38
39
|
}
|
|
39
|
-
else if (value
|
|
40
|
-
return
|
|
40
|
+
else if (!value) {
|
|
41
|
+
return defaultValue;
|
|
41
42
|
}
|
|
42
43
|
else {
|
|
43
44
|
return value;
|
|
@@ -47,23 +48,23 @@ function useSearchParamState(param, initialState, _a) {
|
|
|
47
48
|
if (mapStateToSearchParam) {
|
|
48
49
|
return mapStateToSearchParam(state);
|
|
49
50
|
}
|
|
50
|
-
else if (state ===
|
|
51
|
-
return
|
|
51
|
+
else if (state === defaultValue) {
|
|
52
|
+
return undefined;
|
|
52
53
|
}
|
|
53
54
|
else {
|
|
54
55
|
return "".concat(state);
|
|
55
56
|
}
|
|
56
57
|
};
|
|
57
|
-
var
|
|
58
|
-
var currentState = _mapSearchParamToState(searchParams.get(param),
|
|
59
|
-
var
|
|
60
|
-
debug('Using search param state...', 'OK', "param=".concat(param, ",
|
|
58
|
+
var _d = __read((0, react_router_dom_1.useSearchParams)(), 2), searchParams = _d[0], setSearchParams = _d[1];
|
|
59
|
+
var currentState = _mapSearchParamToState((_b = searchParams.get(param)) !== null && _b !== void 0 ? _b : undefined, defaultValue);
|
|
60
|
+
var _e = __read((0, react_1.useState)(currentState), 2), state = _e[0], setState = _e[1];
|
|
61
|
+
debug('Using search param state...', 'OK', "param=".concat(param, ", defaultValue=").concat(currentState));
|
|
61
62
|
(0, react_1.useEffect)(function () {
|
|
62
63
|
var value = searchParams.get(param);
|
|
63
64
|
var newValue = _mapStateToSearchParam(state);
|
|
64
65
|
if (newValue === value)
|
|
65
66
|
return;
|
|
66
|
-
if (newValue
|
|
67
|
+
if (!newValue) {
|
|
67
68
|
searchParams.delete(param);
|
|
68
69
|
}
|
|
69
70
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSearchParamState.js","sourceRoot":"/","sources":["hooks/useSearchParamState.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,+BAAqE;AACrE,qDAAkD;AAElD,IAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAO,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"useSearchParamState.js","sourceRoot":"/","sources":["hooks/useSearchParamState.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,+BAAqE;AACrE,qDAAkD;AAElD,IAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAO,CAAC,CAAA;AAsBlG;;;;;;;;;;GAUG;AACH,SAAwB,mBAAmB,CAAI,KAAa,EAAE,YAAe,EAAE,EAAiE;;QAAjE,qBAA+D,EAAE,KAAA,EAA/D,qBAAqB,2BAAA,EAAE,qBAAqB,2BAAA;IAC3H,IAAM,sBAAsB,GAAG,UAAC,KAAyB,EAAE,YAAe;QACxE,IAAI,qBAAqB,EAAE;YACzB,OAAO,qBAAqB,CAAC,KAAK,CAAC,CAAA;SACpC;aACI,IAAI,CAAC,KAAK,EAAE;YACf,OAAO,YAAY,CAAA;SACpB;aACI;YACH,OAAO,KAAkC,CAAA;SAC1C;IACH,CAAC,CAAA;IAED,IAAM,sBAAsB,GAAG,UAAC,KAAQ;QACtC,IAAI,qBAAqB,EAAE;YACzB,OAAO,qBAAqB,CAA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import { useSearchParams } from 'react-router-dom'\n\nconst debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:hooks') : () => {}\n\nexport type Options<T> = {\n /**\n * Function for transforming the search param value to the value of the mapped state.\n *\n * @param value - The search param value. `undefined` means the value is unavailable.\n *\n * @returns The equivalent state value.\n */\n mapSearchParamToState?: (value?: string) => T\n\n /**\n * Function for transforming the value of the mapped state to the search param value.\n *\n * @param state - The state value.\n *\n * @returns The equivalent search param value.\n */\n mapStateToSearchParam?: (state: T) => string | undefined\n}\n\n/**\n * Hook for mapping a search param to a state. Whenever the value of the target search param\n * changes, the mapped state will change as well, and vice versa.\n *\n * @param param - The search param key.\n * @param defaultValue - The default value of the state.\n * @param options - See {@link Options}.\n *\n * @returns A tuple consisting of a stateful value representing the current value of the mapped\n * state and a function that updates it.\n */\nexport default function useSearchParamState<T>(param: string, defaultValue: T, { mapSearchParamToState, mapStateToSearchParam }: Options<T> = {}): [T, Dispatch<SetStateAction<T>>] {\n const _mapSearchParamToState = (value: string | undefined, defaultValue: T): T => {\n if (mapSearchParamToState) {\n return mapSearchParamToState(value)\n }\n else if (!value) {\n return defaultValue\n }\n else {\n return value as unknown as NonNullable<T>\n }\n }\n\n const _mapStateToSearchParam = (state: T): string | undefined => {\n if (mapStateToSearchParam) {\n return mapStateToSearchParam(state)\n }\n else if (state === defaultValue) {\n return undefined\n }\n else {\n return `${state}`\n }\n }\n\n const [searchParams, setSearchParams] = useSearchParams()\n const currentState = _mapSearchParamToState(searchParams.get(param) ?? undefined, defaultValue)\n const [state, setState] = useState(currentState)\n\n debug('Using search param state...', 'OK', `param=${param}, defaultValue=${currentState}`)\n\n useEffect(() => {\n const value = searchParams.get(param)\n const newValue = _mapStateToSearchParam(state)\n\n if (newValue === value) return\n\n if (!newValue) {\n searchParams.delete(param)\n }\n else {\n searchParams.set(param, newValue)\n }\n\n debug('Handling state change...', 'OK', `state=${state}, oldValue=${value}, newValue=${newValue}`)\n\n setSearchParams(searchParams)\n }, [state])\n\n return [state, setState]\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "etudes",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.59.0",
|
|
4
4
|
"description": "A study of styled React components",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"scripts": {
|
|
@@ -29,31 +29,31 @@
|
|
|
29
29
|
"lib"
|
|
30
30
|
],
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@babel/core": "^7.17.
|
|
32
|
+
"@babel/core": "^7.17.8",
|
|
33
33
|
"@babel/plugin-proposal-class-properties": "^7.16.7",
|
|
34
|
-
"@babel/plugin-proposal-decorators": "^7.17.
|
|
34
|
+
"@babel/plugin-proposal-decorators": "^7.17.8",
|
|
35
35
|
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
36
36
|
"@babel/preset-env": "^7.16.11",
|
|
37
37
|
"@babel/preset-react": "^7.16.7",
|
|
38
38
|
"@babel/preset-typescript": "^7.16.7",
|
|
39
|
-
"@babel/runtime": "^7.17.
|
|
39
|
+
"@babel/runtime": "^7.17.8",
|
|
40
40
|
"@types/debug": "^4.1.7",
|
|
41
41
|
"@types/html-webpack-plugin": "^3.2.6",
|
|
42
|
-
"@types/lodash": "^4.14.
|
|
43
|
-
"@types/react": "^17.0.
|
|
42
|
+
"@types/lodash": "^4.14.180",
|
|
43
|
+
"@types/react": "^17.0.40",
|
|
44
44
|
"@types/react-dom": "^17.0.13",
|
|
45
45
|
"@types/react-transition-group": "^4.4.4",
|
|
46
46
|
"@types/styled-components": "^5.1.24",
|
|
47
47
|
"@types/webpack": "^5.28.0",
|
|
48
48
|
"@types/webpack-env": "^1.16.3",
|
|
49
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
50
|
-
"@typescript-eslint/parser": "^5.
|
|
49
|
+
"@typescript-eslint/eslint-plugin": "^5.15.0",
|
|
50
|
+
"@typescript-eslint/parser": "^5.15.0",
|
|
51
51
|
"babel-loader": "^8.2.3",
|
|
52
52
|
"babel-plugin-styled-components": "^2.0.6",
|
|
53
53
|
"concurrently": "^7.0.0",
|
|
54
54
|
"cross-env": "^7.0.3",
|
|
55
|
-
"debug": "^4.3.
|
|
56
|
-
"eslint": "^8.
|
|
55
|
+
"debug": "^4.3.4",
|
|
56
|
+
"eslint": "^8.11.0",
|
|
57
57
|
"file-loader": "^6.2.0",
|
|
58
58
|
"html-webpack-plugin": "^5.5.0",
|
|
59
59
|
"promptu": "^5.7.0",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
"react-router-dom": "^6.2.2",
|
|
66
66
|
"rimraf": "^3.0.2",
|
|
67
67
|
"styled-components": "^5.3.3",
|
|
68
|
-
"ts-loader": "^9.2.
|
|
69
|
-
"ts-node": "^10.
|
|
68
|
+
"ts-loader": "^9.2.8",
|
|
69
|
+
"ts-node": "^10.7.0",
|
|
70
70
|
"typescript": "^4.6.2",
|
|
71
71
|
"url-loader": "^4.1.1",
|
|
72
72
|
"wait-on": "^6.0.1",
|