Skip to content

Commit

Permalink
♻ refactor: masonry.js is no longer a must (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 authored Dec 25, 2024
1 parent c1076b4 commit 3a92aed
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/Masa.Stack.Components/wwwroot/js/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,40 @@ window.MasaStackComponents.getTimezoneOffset = function() {
let masonryInstances = {};

window.MasaStackComponents.initMasonry = (selector, itemSelector, gutter) => {
if (!selector || !itemSelector) {
console.error('Invalid selector or itemSelector provided.');
return;
}

const elem = document.querySelector(selector);
if (!elem) return;

masonryInstances[selector] = new Masonry(elem, {
itemSelector: itemSelector,
columnWidth: itemSelector,
percentPosition: true,
gutter: gutter
});

setTimeout(() => {
masonryInstances[selector].layout();
}, 1000);
if (!elem) {
console.warn(`Element not found for selector: ${selector}`);
return;
}

if (typeof Masonry === 'undefined') {
console.warn('Masonry is not available. Falling back to normal layout.');
return;
}

if (masonryInstances[selector]) {
masonryInstances[selector].destroy();
}

try {
masonryInstances[selector] = new Masonry(elem, {
itemSelector: itemSelector,
columnWidth: itemSelector,
percentPosition: true,
gutter: gutter || 0
});

requestAnimationFrame(() => {
masonryInstances[selector].layout();
});
} catch (error) {
console.warn('Falling back to normal layout.');
}
};

function debounce(fn, wait) {
Expand Down

0 comments on commit 3a92aed

Please sign in to comment.