Skip to content

Commit f15e3cc

Browse files
committed
refactor complete
1 parent 538ebfd commit f15e3cc

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/masonry/Masonry.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import throttle from "lodash/throttle";
1+
/**
2+
* Polyfill for CSS Grid masonry layout behavior
3+
*/
4+
5+
import throttle from 'lodash/throttle';
26

37
// Represents a parsed CSS grid column value
48
interface CSSValue {
@@ -13,7 +17,7 @@ interface GridTemplateColumns {
1317
}
1418

1519
// Maps a CSS grid-template-columns value to its type
16-
const parseCSSValue = (value: string): CSSValue['type'] => {
20+
const parseCSSValue = (value: string): CSSValue => {
1721
const tests = [
1822
{ test: /^\[.*\]$/, type: 'line-name' },
1923
{ test: /^repeat\(.+\)$/, type: 'repeat' },
@@ -25,7 +29,8 @@ const parseCSSValue = (value: string): CSSValue['type'] => {
2529
{ test: /.*/, type: 'global' }
2630
] as const;
2731
// Map CSS grid column value to a type (e.g., 'dimension', 'keyword')
28-
return tests.find(({ test }) => test.test(value))?.type || 'global';
32+
const type = tests.find(({ test }) => test.test(value))?.type || 'global';
33+
return { type, value }
2934
};
3035

3136
// Parses the grid-template-columns property of an element
@@ -41,7 +46,7 @@ function parseGridTemplateColumns(grid: HTMLElement): GridTemplateColumns {
4146
}
4247
// Filters out line names to get actual column tracks
4348
const parsed = columns.split(/\s(?=(?:[^()]*\([^()]*\))*[^()]*$)/)
44-
.map(value => ({ type: parseCSSValue(value), value }))
49+
.map(value => parseCSSValue)
4550
.filter(col => 'line-name' !== col.type);
4651
return {
4752
type: 'track-list',
@@ -50,7 +55,6 @@ function parseGridTemplateColumns(grid: HTMLElement): GridTemplateColumns {
5055
}
5156

5257
export class Masonry {
53-
private readonly grid: HTMLElement;
5458
private items: HTMLElement[] = [];
5559
private mutationObserver?: MutationObserver;
5660
private resizeObserver?: ResizeObserver;
@@ -74,12 +78,13 @@ export class Masonry {
7478
*/
7579
create(): void {
7680
this.destroy();
81+
// Observe grid children for changes
7782
this.mutationObserver?.observe(this.grid, {
7883
childList: true,
7984
});
85+
// Assumes children are HTMLElements, may need adjusting if SVG elements are used as grid items...
8086
this.items = Array.from(this.grid.children) as HTMLElement[];
81-
82-
this.items.forEach((item) => {
87+
this.items.forEach(item => {
8388
this.resizeObserver?.observe(item);
8489
});
8590
}
@@ -88,8 +93,8 @@ export class Masonry {
8893
* Disconnects observers and resets the grid layout
8994
*/
9095
destroy(): void {
91-
this.resizeObserver?.disconnect();
9296
this.mutationObserver?.disconnect();
97+
this.resizeObserver?.disconnect();
9398
this.clean();
9499
this.items = [];
95100
}
@@ -103,14 +108,14 @@ export class Masonry {
103108
if (!style.getPropertyValue('display').includes('grid') || 1 >= columns.length) {
104109
return this.clean();
105110
}
106-
const rowGap = parseFloat(style.getPropertyValue('row-gap')) || 0;
111+
const rowGap = Math.max(0, parseFloat(style.getPropertyValue('row-gap')) || 0);
107112
this.adjustColumnSpacing(rowGap, columns);
108113
}
109114

110115
/**
111116
* Adjusts vertical spacing between grid items based on row gap
112-
* @param rowGap - The parsed row gap value in pixels
113-
* @param columns - The parsed grid-template-columns configuration
117+
* @param rowGap - The vertical gap between rows in pixels (non-negative).
118+
* @param columns - The parsed `grid-template-columns` configuration.
114119
*/
115120
private adjustColumnSpacing(rowGap: number, columns: CSSValue[]): void {
116121
// Reset first items in each column
@@ -135,8 +140,8 @@ export class Masonry {
135140
* Removes margin-top style property from all grid items
136141
*/
137142
private clean(): void {
138-
this.items.forEach((item) => {
139-
item.style.removeProperty("margin-top");
143+
this.items.forEach(item => {
144+
item?.style.removeProperty('margin-top');
140145
});
141146
}
142147
}

0 commit comments

Comments
 (0)