etudes 1.2.0 → 2.1.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/README.md +1 -1
- package/lib/Accordion.d.ts +80 -71
- package/lib/Accordion.js +173 -183
- package/lib/Accordion.js.map +1 -1
- package/lib/BurgerButton.d.ts +20 -7
- package/lib/BurgerButton.js +113 -20
- package/lib/BurgerButton.js.map +1 -1
- package/lib/Conditional.d.ts +2 -3
- package/lib/Conditional.js +0 -1
- package/lib/Conditional.js.map +1 -1
- package/lib/DebugConsole.d.ts +14 -23
- package/lib/DebugConsole.js +86 -87
- package/lib/DebugConsole.js.map +1 -1
- package/lib/Dial.d.ts +53 -16
- package/lib/Dial.js +121 -35
- package/lib/Dial.js.map +1 -1
- package/lib/Dropdown.d.ts +68 -83
- package/lib/Dropdown.js +226 -161
- package/lib/Dropdown.js.map +1 -1
- package/lib/Each.d.ts +2 -2
- package/lib/Each.js.map +1 -1
- package/lib/ExtractChild.d.ts +5 -4
- package/lib/ExtractChild.js +13 -11
- package/lib/ExtractChild.js.map +1 -1
- package/lib/ExtractChildren.d.ts +2 -2
- package/lib/ExtractChildren.js +7 -5
- package/lib/ExtractChildren.js.map +1 -1
- package/lib/FlatSVG.d.ts +30 -25
- package/lib/FlatSVG.js +28 -37
- package/lib/FlatSVG.js.map +1 -1
- package/lib/List.d.ts +97 -54
- package/lib/List.js +124 -63
- package/lib/List.js.map +1 -1
- package/lib/MasonryGrid.d.ts +27 -15
- package/lib/MasonryGrid.js +163 -148
- package/lib/MasonryGrid.js.map +1 -1
- package/lib/Panorama.d.ts +100 -21
- package/lib/Panorama.js +45 -44
- package/lib/Panorama.js.map +1 -1
- package/lib/PanoramaSlider.d.ts +65 -23
- package/lib/PanoramaSlider.js +150 -45
- package/lib/PanoramaSlider.js.map +1 -1
- package/lib/RangeSlider.d.ts +29 -89
- package/lib/RangeSlider.js +272 -286
- package/lib/RangeSlider.js.map +1 -1
- package/lib/Repeat.d.ts +4 -3
- package/lib/Repeat.js +3 -2
- package/lib/Repeat.js.map +1 -1
- package/lib/RotatingGallery.d.ts +38 -36
- package/lib/RotatingGallery.js +47 -17
- package/lib/RotatingGallery.js.map +1 -1
- package/lib/SelectableButton.d.ts +13 -4
- package/lib/SelectableButton.js +3 -14
- package/lib/SelectableButton.js.map +1 -1
- package/lib/Slider.d.ts +103 -41
- package/lib/Slider.js +182 -82
- package/lib/Slider.js.map +1 -1
- package/lib/StepwiseSlider.d.ts +146 -59
- package/lib/StepwiseSlider.js +248 -142
- package/lib/StepwiseSlider.js.map +1 -1
- package/lib/SwipeContainer.d.ts +13 -5
- package/lib/SwipeContainer.js +5 -15
- package/lib/SwipeContainer.js.map +1 -1
- package/lib/TextField.d.ts +1 -1
- package/lib/TextField.js +5 -15
- package/lib/TextField.js.map +1 -1
- package/lib/Video.d.ts +30 -51
- package/lib/Video.js +77 -119
- package/lib/Video.js.map +1 -1
- package/lib/WithTooltip.d.ts +16 -18
- package/lib/WithTooltip.js +167 -101
- package/lib/WithTooltip.js.map +1 -1
- package/lib/hooks/useDocumentTitle.d.ts +2 -1
- package/lib/hooks/useDocumentTitle.js +2 -1
- package/lib/hooks/useDocumentTitle.js.map +1 -1
- package/lib/hooks/useDragEffect.d.ts +26 -17
- package/lib/hooks/useDragEffect.js +30 -20
- package/lib/hooks/useDragEffect.js.map +1 -1
- package/lib/hooks/useElementRect.d.ts +2 -1
- package/lib/hooks/useElementRect.js +5 -4
- package/lib/hooks/useElementRect.js.map +1 -1
- package/lib/hooks/useInterval.d.ts +3 -3
- package/lib/hooks/useInterval.js +1 -1
- package/lib/hooks/useInterval.js.map +1 -1
- package/lib/hooks/useLoadImageEffect.d.ts +8 -8
- package/lib/hooks/useLoadImageEffect.js.map +1 -1
- package/lib/hooks/usePrevious.d.ts +6 -0
- package/lib/hooks/usePrevious.js +17 -0
- package/lib/hooks/usePrevious.js.map +1 -0
- package/lib/hooks/useResizeEffect.d.ts +3 -3
- package/lib/hooks/useResizeEffect.js +5 -8
- package/lib/hooks/useResizeEffect.js.map +1 -1
- package/lib/hooks/useSearchParamState.d.ts +11 -8
- package/lib/hooks/useSearchParamState.js +14 -10
- package/lib/hooks/useSearchParamState.js.map +1 -1
- package/lib/providers/I18nProvider.d.ts +179 -0
- package/lib/providers/I18nProvider.js +470 -0
- package/lib/providers/I18nProvider.js.map +1 -0
- package/lib/providers/ScrollPositionProvider.d.ts +15 -0
- package/lib/providers/ScrollPositionProvider.js +112 -0
- package/lib/providers/ScrollPositionProvider.js.map +1 -0
- package/lib/utils/asClassNameDict.d.ts +3 -0
- package/lib/utils/asClassNameDict.js +7 -0
- package/lib/utils/asClassNameDict.js.map +1 -0
- package/lib/utils/asComponentDict.d.ts +5 -0
- package/lib/utils/asComponentDict.js +23 -0
- package/lib/utils/asComponentDict.js.map +1 -0
- package/lib/utils/asStyleDict.d.ts +4 -0
- package/lib/utils/asStyleDict.js +7 -0
- package/lib/utils/asStyleDict.js.map +1 -0
- package/lib/utils/cloneStyledElement.d.ts +18 -0
- package/lib/utils/cloneStyledElement.js +63 -0
- package/lib/utils/cloneStyledElement.js.map +1 -0
- package/lib/utils/styles.d.ts +2 -0
- package/lib/utils/styles.js +22 -0
- package/lib/utils/styles.js.map +1 -0
- package/lib/utils/useDebug.d.ts +2 -0
- package/lib/utils/useDebug.js +46 -0
- package/lib/utils/useDebug.js.map +1 -0
- package/package.json +31 -38
- package/lib/AbstractSelectableCollection.d.ts +0 -94
- package/lib/AbstractSelectableCollection.js +0 -151
- package/lib/AbstractSelectableCollection.js.map +0 -1
- package/lib/types/index.d.ts +0 -7
- package/lib/types/index.js +0 -3
- package/lib/types/index.js.map +0 -1
package/lib/MasonryGrid.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
|
|
3
|
-
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
|
|
4
|
-
return cooked;
|
|
5
|
-
};
|
|
6
2
|
var __assign = (this && this.__assign) || function () {
|
|
7
3
|
__assign = Object.assign || function(t) {
|
|
8
4
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
@@ -77,160 +73,68 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
77
73
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
78
74
|
};
|
|
79
75
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
76
|
+
var classnames_1 = __importDefault(require("classnames"));
|
|
80
77
|
var react_1 = __importStar(require("react"));
|
|
81
78
|
var spase_1 = require("spase");
|
|
82
|
-
var styled_components_1 = __importDefault(require("styled-components"));
|
|
83
79
|
var useResizeEffect_1 = __importDefault(require("./hooks/useResizeEffect"));
|
|
84
|
-
var
|
|
80
|
+
var asClassNameDict_1 = __importDefault(require("./utils/asClassNameDict"));
|
|
81
|
+
var asStyleDict_1 = __importDefault(require("./utils/asStyleDict"));
|
|
82
|
+
var useDebug_1 = __importDefault(require("./utils/useDebug"));
|
|
83
|
+
var debug = (0, useDebug_1.default)('masonry');
|
|
85
84
|
var BASE_MODIFIER_CLASS_PREFIX = 'base-';
|
|
86
85
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
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*.
|
|
86
|
+
* This is a React component that arranges all of its immediate children in a
|
|
87
|
+
* masonry grid. Refrain from assigning CSS styles to it via `className` or
|
|
88
|
+
* `style` property, though they are still handled if absolutely necessary.
|
|
89
|
+
* Customize the grid via its supported properties. The grid can be in either
|
|
90
|
+
* vertical or horizontal orientation. The length of every child element
|
|
91
|
+
* *parallel to the direction of the orientation* is automatically set according
|
|
92
|
+
* to the number of sections specified for the grid. This means that in an
|
|
93
|
+
* horizontally oriented grid, the *width* of each child element is
|
|
94
|
+
* automatically set, whereas in a vertically oriented grid the *height* of each
|
|
95
|
+
* child element is automatically set. Additionally, the *number of sections*
|
|
96
|
+
* corresponds to the maximum the number of child elements present in the
|
|
97
|
+
* direction that is parallel to the orientation of the grid. Hence, in a
|
|
98
|
+
* vertically oriented grid, *number of secitons* refers to the *number of
|
|
99
|
+
* rows*, whereas in a horizontally oriented grid, *number of sections* refers
|
|
100
|
+
* to the *number of columns*.
|
|
197
101
|
*/
|
|
198
|
-
function
|
|
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.
|
|
200
|
-
var
|
|
201
|
-
var
|
|
202
|
-
var
|
|
203
|
-
var
|
|
204
|
-
var
|
|
205
|
-
var getCurrentWidth = function () { var _a, _b; return (_b = (_a = spase_1.Rect.from(
|
|
206
|
-
var getCurrentHeight = function () { var _a, _b; return (_b = (_a = spase_1.Rect.from(
|
|
102
|
+
exports.default = (0, react_1.forwardRef)(function (_a, ref) {
|
|
103
|
+
var _b = _a.areSectionsAligned, areSectionsAligned = _b === void 0 ? false : _b, children = _a.children, className = _a.className, _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.verticalSpacing, verticalSpacing = _g === void 0 ? 0 : _g, props = __rest(_a, ["areSectionsAligned", "children", "className", "horizontalSpacing", "isReversed", "orientation", "sections", "verticalSpacing"]);
|
|
104
|
+
var bodyRef = (0, react_1.useRef)(null);
|
|
105
|
+
var _h = __read((0, react_1.useState)(NaN), 2), minWidth = _h[0], setMinWidth = _h[1];
|
|
106
|
+
var _j = __read((0, react_1.useState)(NaN), 2), minHeight = _j[0], setMinHeight = _j[1];
|
|
107
|
+
var _k = __read((0, react_1.useState)(NaN), 2), maxWidth = _k[0], setMaxWidth = _k[1];
|
|
108
|
+
var _l = __read((0, react_1.useState)(NaN), 2), maxHeight = _l[0], setMaxHeight = _l[1];
|
|
109
|
+
var getCurrentWidth = function () { var _a, _b; return (_b = (_a = spase_1.Rect.from(bodyRef.current)) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0; };
|
|
110
|
+
var getCurrentHeight = function () { var _a, _b; return (_b = (_a = spase_1.Rect.from(bodyRef.current)) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0; };
|
|
207
111
|
var repositionChildren = function () {
|
|
208
112
|
var _a, _b, _c, _d;
|
|
209
|
-
var rootNode =
|
|
113
|
+
var rootNode = bodyRef.current;
|
|
210
114
|
if (!rootNode)
|
|
211
115
|
return;
|
|
212
116
|
debug('Repositioning children... OK');
|
|
213
|
-
var
|
|
117
|
+
var nodes = rootNode.children;
|
|
214
118
|
var numSections = sections;
|
|
215
119
|
if (numSections <= 0)
|
|
216
120
|
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');
|
|
217
121
|
if (orientation === 'vertical') {
|
|
218
122
|
var sectionHeights = __spreadArray([], __read(new Array(numSections)), false).map(function () { return 0; });
|
|
219
|
-
for (var i = 0; i <
|
|
220
|
-
var child =
|
|
123
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
124
|
+
var child = nodes[i];
|
|
221
125
|
if (!(child instanceof HTMLElement))
|
|
222
126
|
continue;
|
|
223
127
|
var base = computeBaseFromElement(child, sections);
|
|
224
128
|
var _e = __read(computeNextAvailableSectionAndLengthByBase(sectionHeights, base), 2), colIdx = _e[0], y = _e[1];
|
|
225
129
|
child.style.position = 'absolute';
|
|
226
|
-
child.style.width = "calc(".concat(100 / numSections * base, "% - ").concat(
|
|
130
|
+
child.style.width = "calc(".concat(100 / numSections * base, "% - ").concat(horizontalSpacing * (numSections - 1) / numSections * base, "px + ").concat(horizontalSpacing * (base - 1), "px)");
|
|
227
131
|
child.style.height = '';
|
|
228
|
-
child.style.left = "calc(".concat(100 / numSections * colIdx, "% - ").concat(
|
|
132
|
+
child.style.left = "calc(".concat(100 / numSections * colIdx, "% - ").concat(horizontalSpacing * (numSections - 1) / numSections * colIdx, "px + ").concat(horizontalSpacing * colIdx, "px)");
|
|
229
133
|
child.style.top = "".concat(y + (y === 0 ? 0 : verticalSpacing), "px");
|
|
230
134
|
for (var j = 0; j < base; j++) {
|
|
231
135
|
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);
|
|
232
136
|
}
|
|
233
|
-
if (areSectionsAligned &&
|
|
137
|
+
if (areSectionsAligned && colIdx + base === numSections) {
|
|
234
138
|
var m = computeMaxLength(sectionHeights);
|
|
235
139
|
for (var j = 0; j < numSections; j++) {
|
|
236
140
|
sectionHeights[j] = m;
|
|
@@ -244,8 +148,8 @@ function MasonryGrid(_a) {
|
|
|
244
148
|
if (!isNaN(h))
|
|
245
149
|
rootNode.style.height = "".concat(h, "px");
|
|
246
150
|
if (isReversed) {
|
|
247
|
-
for (var i = 0; i <
|
|
248
|
-
var child =
|
|
151
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
152
|
+
var child = nodes[i];
|
|
249
153
|
if (!(child instanceof HTMLElement))
|
|
250
154
|
continue;
|
|
251
155
|
var x = parseFloat(child.style.left);
|
|
@@ -255,21 +159,21 @@ function MasonryGrid(_a) {
|
|
|
255
159
|
}
|
|
256
160
|
else {
|
|
257
161
|
var sectionWidths = __spreadArray([], __read(new Array(numSections)), false).map(function () { return 0; });
|
|
258
|
-
for (var i = 0; i <
|
|
259
|
-
var child =
|
|
162
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
163
|
+
var child = nodes[i];
|
|
260
164
|
if (!(child instanceof HTMLElement))
|
|
261
165
|
continue;
|
|
262
166
|
var base = computeBaseFromElement(child, sections);
|
|
263
167
|
var _f = __read(computeNextAvailableSectionAndLengthByBase(sectionWidths, base), 2), rowIdx = _f[0], x = _f[1];
|
|
264
168
|
child.style.position = 'absolute';
|
|
265
169
|
child.style.width = '';
|
|
266
|
-
child.style.height = "calc(".concat(100 / numSections * base, "% - ").concat(
|
|
267
|
-
child.style.top = "calc(".concat(100 / numSections * rowIdx, "% - ").concat(
|
|
170
|
+
child.style.height = "calc(".concat(100 / numSections * base, "% - ").concat(verticalSpacing * (numSections - 1) / numSections * base, "px + ").concat(verticalSpacing * (base - 1), "px)");
|
|
171
|
+
child.style.top = "calc(".concat(100 / numSections * rowIdx, "% - ").concat(verticalSpacing * (numSections - 1) / numSections * rowIdx, "px + ").concat(verticalSpacing * rowIdx, "px)");
|
|
268
172
|
child.style.left = "".concat(x + (x === 0 ? 0 : horizontalSpacing), "px");
|
|
269
173
|
for (var j = 0; j < base; j++) {
|
|
270
174
|
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);
|
|
271
175
|
}
|
|
272
|
-
if (areSectionsAligned &&
|
|
176
|
+
if (areSectionsAligned && rowIdx + base === numSections) {
|
|
273
177
|
var m = computeMaxLength(sectionWidths);
|
|
274
178
|
for (var j = 0; j < numSections; j++) {
|
|
275
179
|
sectionWidths[j] = m;
|
|
@@ -283,8 +187,8 @@ function MasonryGrid(_a) {
|
|
|
283
187
|
if (!isNaN(w))
|
|
284
188
|
rootNode.style.width = "".concat(w, "px");
|
|
285
189
|
if (isReversed) {
|
|
286
|
-
for (var i = 0; i <
|
|
287
|
-
var child =
|
|
190
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
191
|
+
var child = nodes[i];
|
|
288
192
|
if (!(child instanceof HTMLElement))
|
|
289
193
|
continue;
|
|
290
194
|
var y = parseFloat(child.style.top);
|
|
@@ -293,11 +197,11 @@ function MasonryGrid(_a) {
|
|
|
293
197
|
}
|
|
294
198
|
}
|
|
295
199
|
};
|
|
296
|
-
(0, useResizeEffect_1.default)(
|
|
200
|
+
(0, useResizeEffect_1.default)(bodyRef, {
|
|
297
201
|
onResize: function (maxSize) {
|
|
298
202
|
var currWidth = getCurrentWidth();
|
|
299
203
|
var currHeight = getCurrentHeight();
|
|
300
|
-
if (
|
|
204
|
+
if (minWidth !== currWidth || minHeight !== currHeight || maxSize.width !== maxWidth || maxSize.height !== maxHeight) {
|
|
301
205
|
repositionChildren();
|
|
302
206
|
setMaxWidth(maxSize.width);
|
|
303
207
|
setMaxHeight(maxSize.height);
|
|
@@ -306,7 +210,7 @@ function MasonryGrid(_a) {
|
|
|
306
210
|
}, [areSectionsAligned, horizontalSpacing, isReversed, sections, verticalSpacing]);
|
|
307
211
|
(0, react_1.useEffect)(function () {
|
|
308
212
|
var _a;
|
|
309
|
-
var imageSources = getAllImageSources((_a =
|
|
213
|
+
var imageSources = getAllImageSources((_a = bodyRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML);
|
|
310
214
|
if (imageSources.length === 0)
|
|
311
215
|
return repositionChildren();
|
|
312
216
|
var numImages = imageSources.length;
|
|
@@ -317,9 +221,120 @@ function MasonryGrid(_a) {
|
|
|
317
221
|
image.onload = function () { return repositionChildren(); };
|
|
318
222
|
}
|
|
319
223
|
}, [children]);
|
|
320
|
-
|
|
224
|
+
var fixedClassNames = (0, asClassNameDict_1.default)({
|
|
225
|
+
root: (0, classnames_1.default)(orientation),
|
|
226
|
+
});
|
|
227
|
+
var fixedStyles = (0, asStyleDict_1.default)({
|
|
228
|
+
body: {
|
|
229
|
+
height: orientation === 'horizontal' ? '100%' : 'auto',
|
|
230
|
+
minHeight: orientation === 'vertical' && !isNaN(minHeight) ? "".concat(minHeight, "px") : '',
|
|
231
|
+
minWidth: orientation === 'horizontal' && !isNaN(minWidth) ? "".concat(minWidth, "px") : '',
|
|
232
|
+
padding: '0',
|
|
233
|
+
width: orientation === 'horizontal' ? 'auto' : '100%',
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
return (react_1.default.createElement("div", __assign({}, props, { ref: ref, className: (0, classnames_1.default)(className, fixedClassNames.root) }),
|
|
237
|
+
react_1.default.createElement("div", { ref: bodyRef, style: fixedStyles.body }, children)));
|
|
238
|
+
});
|
|
239
|
+
/**
|
|
240
|
+
* Computes the index and current length of the next available section for a
|
|
241
|
+
* specific base value, based on a provided array of existing section lengths.
|
|
242
|
+
*
|
|
243
|
+
* @param currentSectionLengths - An array of the current section lengths.
|
|
244
|
+
* @param base - The base value of the item to be inserted into the grid, and to
|
|
245
|
+
* be used to evaluate the next available section.
|
|
246
|
+
*
|
|
247
|
+
* @returns An array consiting of the computed section index and its to-be
|
|
248
|
+
* length if a new item were to be placed in it.
|
|
249
|
+
*/
|
|
250
|
+
function computeNextAvailableSectionAndLengthByBase(currentSectionLengths, base) {
|
|
251
|
+
var numSections = currentSectionLengths.length;
|
|
252
|
+
var sectionIdx = NaN;
|
|
253
|
+
var minLength = Infinity;
|
|
254
|
+
for (var i = 0; i < numSections; i++) {
|
|
255
|
+
var length_1 = currentSectionLengths[i];
|
|
256
|
+
var isShorter = length_1 < minLength;
|
|
257
|
+
var isEligibleSection = i + base <= numSections;
|
|
258
|
+
var hasRoomInSubsequentSections = true;
|
|
259
|
+
for (var j = 1; j < base; j++) {
|
|
260
|
+
if (currentSectionLengths[i + j] > length_1) {
|
|
261
|
+
hasRoomInSubsequentSections = false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (isShorter && isEligibleSection && hasRoomInSubsequentSections) {
|
|
265
|
+
sectionIdx = i;
|
|
266
|
+
minLength = length_1;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (isNaN(sectionIdx)) {
|
|
270
|
+
return [0, computeMaxLength(currentSectionLengths, base)];
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
return [sectionIdx, minLength];
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* A helper function that computes the max section length of an array of section
|
|
278
|
+
* lengths. Only the first n = `base` sections are inspected.
|
|
279
|
+
*
|
|
280
|
+
* @param currentSectionLengths - An array of section lengths.
|
|
281
|
+
* @param base - The number representing the first n sections to inspect. Any
|
|
282
|
+
* non-numerical values will be ignored and return value will be
|
|
283
|
+
* based on all sections. A `base` value will be clamped between 1
|
|
284
|
+
* and the maximum length of the array of section lengths.
|
|
285
|
+
*
|
|
286
|
+
* @returns The max section length.
|
|
287
|
+
*/
|
|
288
|
+
function computeMaxLength(currentSectionLengths, base) {
|
|
289
|
+
var arr = currentSectionLengths;
|
|
290
|
+
if (base !== undefined && base !== null && !isNaN(base)) {
|
|
291
|
+
arr = arr.slice(0, Math.max(1, Math.min(base, currentSectionLengths.length)));
|
|
292
|
+
}
|
|
293
|
+
return arr.reduce(function (out, curr, i) { return curr > out ? curr : out; }, 0);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Computes the base value of an element from its classes.
|
|
297
|
+
*
|
|
298
|
+
* @param element - The HTML element.
|
|
299
|
+
* @param numSections - Total number of sections.
|
|
300
|
+
*
|
|
301
|
+
* @returns The computed base value that is clamped between 1 and max number of
|
|
302
|
+
* sections.
|
|
303
|
+
*/
|
|
304
|
+
function computeBaseFromElement(element, numSections) {
|
|
305
|
+
var classList = element.classList;
|
|
306
|
+
for (var i = 0; i < classList.length; i++) {
|
|
307
|
+
var c = classList[i];
|
|
308
|
+
if (c.startsWith(BASE_MODIFIER_CLASS_PREFIX)) {
|
|
309
|
+
var base = parseFloat(c.replace(BASE_MODIFIER_CLASS_PREFIX, ''));
|
|
310
|
+
if (!isNaN(base))
|
|
311
|
+
return Math.min(Math.max(base, 1), numSections);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return 1;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Scans an HTML string and returns all the image sources.
|
|
318
|
+
*
|
|
319
|
+
* @param htmlString The HTML string.
|
|
320
|
+
*
|
|
321
|
+
* @returns The image sources.
|
|
322
|
+
*/
|
|
323
|
+
function getAllImageSources(htmlString) {
|
|
324
|
+
var _a, _b;
|
|
325
|
+
if (!htmlString)
|
|
326
|
+
return [];
|
|
327
|
+
var regexImg = /<img.*?src=("|')(.*?)("|')/g;
|
|
328
|
+
var regexSrc = /<img.*?src=("|')(.*?)("|')/;
|
|
329
|
+
var imageTags = (_a = htmlString.match(regexImg)) !== null && _a !== void 0 ? _a : [];
|
|
330
|
+
var out = [];
|
|
331
|
+
for (var i = 0; i < imageTags.length; i++) {
|
|
332
|
+
var tag = imageTags[i];
|
|
333
|
+
var src = (_b = tag.match(regexSrc)) === null || _b === void 0 ? void 0 : _b[2];
|
|
334
|
+
if (!src)
|
|
335
|
+
continue;
|
|
336
|
+
out.push(src);
|
|
337
|
+
}
|
|
338
|
+
return out;
|
|
321
339
|
}
|
|
322
|
-
exports.default = MasonryGrid;
|
|
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%'; });
|
|
324
|
-
var templateObject_1;
|
|
325
340
|
//# sourceMappingURL=MasonryGrid.js.map
|
package/lib/MasonryGrid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MasonryGrid.js","sourceRoot":"/","sources":["MasonryGrid.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA0E;AAC1E,+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,IAAI,CAAC,IAAI,WAAW,CAAA;QACnD,I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E5D,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, 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> & {\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 repositionChildren()\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 }, [children])\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"]}
|
|
1
|
+
{"version":3,"file":"MasonryGrid.js","sourceRoot":"/","sources":["MasonryGrid.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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CAAA;YAClE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;gBAAE,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,WAAW,CAAC,CAAA;SAClE;KACF;IAED,OAAO,CAAC,CAAA;AACV,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,UAAmB;;IAC7C,IAAI,CAAC,UAAU;QAAE,OAAO,EAAE,CAAA;IAE1B,IAAM,QAAQ,GAAG,6BAA6B,CAAA;IAC9C,IAAM,QAAQ,GAAG,4BAA4B,CAAA;IAC7C,IAAM,SAAS,GAAG,MAAA,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,mCAAI,EAAE,CAAA;IAElD,IAAM,GAAG,GAAa,EAAE,CAAA;IAExB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACzC,IAAM,GAAG,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA;QACxB,IAAM,GAAG,GAAG,MAAA,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,0CAAG,CAAC,CAAC,CAAA;QAEpC,IAAI,CAAC,GAAG;YAAE,SAAQ;QAElB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KACd;IAED,OAAO,GAAG,CAAA;AACZ,CAAC","sourcesContent":["import classNames from 'classnames'\nimport React, { forwardRef, HTMLAttributes, useEffect, useRef, useState } from 'react'\nimport { Rect } from 'spase'\nimport useResizeEffect from './hooks/useResizeEffect'\nimport asClassNameDict from './utils/asClassNameDict'\nimport asStyleDict from './utils/asStyleDict'\nimport useDebug from './utils/useDebug'\n\nconst debug = useDebug('masonry')\n\ntype Orientation = 'horizontal' | 'vertical'\n\nexport type MasonryGridProps = HTMLAttributes<HTMLDivElement> & {\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 * This is a React component that arranges all of its immediate children in a\n * masonry grid. Refrain from assigning CSS styles to it via `className` or\n * `style` property, though they are still 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 * corresponds to the maximum the number of child elements present in the\n * direction that is parallel to the orientation of the grid. Hence, in a\n * vertically oriented grid, *number of secitons* refers to the *number of\n * rows*, whereas in a horizontally oriented grid, *number of sections* refers\n * to the *number of columns*.\n */\nexport default forwardRef<HTMLDivElement, MasonryGridProps>(({\n areSectionsAligned = false,\n children,\n className,\n horizontalSpacing = 0,\n isReversed = false,\n orientation = 'vertical',\n sections = 3,\n verticalSpacing = 0,\n ...props\n}, ref) => {\n const bodyRef = 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(bodyRef.current)?.width ?? 0\n\n const getCurrentHeight = () => Rect.from(bodyRef.current)?.height ?? 0\n\n const repositionChildren = () => {\n const rootNode = bodyRef.current\n\n if (!rootNode) return\n\n debug('Repositioning children... OK')\n\n const nodes = 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 < nodes.length; i++) {\n const child = nodes[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 < nodes.length; i++) {\n const child = nodes[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 < nodes.length; i++) {\n const child = nodes[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 < nodes.length; i++) {\n const child = nodes[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(bodyRef, {\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(bodyRef.current?.innerHTML)\n\n if (imageSources.length === 0) return repositionChildren()\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 }, [children])\n\n const fixedClassNames = asClassNameDict({\n root: classNames(orientation),\n })\n\n const fixedStyles = asStyleDict({\n body: {\n height: orientation === 'horizontal' ? '100%' : 'auto',\n minHeight: orientation === 'vertical' && !isNaN(minHeight) ? `${minHeight}px` : '',\n minWidth: orientation === 'horizontal' && !isNaN(minWidth) ? `${minWidth}px` : '',\n padding: '0',\n width: orientation === 'horizontal' ? 'auto' : '100%',\n },\n })\n\n return (\n <div\n {...props}\n ref={ref}\n className={classNames(className, fixedClassNames.root)}\n >\n <div ref={bodyRef} style={fixedStyles.body}>\n {children}\n </div>\n </div>\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 to\n * 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 */\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\n * 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. Any\n * non-numerical values will be ignored and return value will be\n * based on all sections. A `base` value will be clamped between 1\n * 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\n * 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"]}
|