Skip to content

Commit bfd8b92

Browse files
committed
refactor(gallery): separate the watchers for the order and layout property
1 parent bd7f613 commit bfd8b92

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

core/src/components/gallery/gallery.spec.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { newSpecPage } from '@stencil/core/testing';
2+
import * as helpers from '@utils/helpers';
23
import * as logging from '@utils/logging';
34

45
import { Gallery } from './gallery';
@@ -42,6 +43,21 @@ describe('gallery', () => {
4243
jest.restoreAllMocks();
4344
});
4445
describe('gallery: columns', () => {
46+
describe('onColumnsOrGapChanged()', () => {
47+
it('should call syncResponsiveLayout when columns or gap change', () => {
48+
const syncResponsiveLayoutSpy = jest.spyOn(sharedGallery as any, 'syncResponsiveLayout');
49+
const warnUnusedOrderSpy = jest.spyOn(sharedGallery as any, 'warnUnusedOrder');
50+
51+
(sharedGallery as any).onColumnsOrGapChanged();
52+
53+
expect(syncResponsiveLayoutSpy).toHaveBeenCalledTimes(1);
54+
expect(warnUnusedOrderSpy).not.toHaveBeenCalled();
55+
56+
syncResponsiveLayoutSpy.mockRestore();
57+
warnUnusedOrderSpy.mockRestore();
58+
});
59+
});
60+
4561
describe('sanitizeColumns()', () => {
4662
it('should return undefined for invalid values', () => {
4763
const invalidValues = [undefined, NaN, Infinity, '0', '-1', '0.5', 'invalid', '', ' ', 0, -1, 0.5];
@@ -673,15 +689,24 @@ describe('gallery', () => {
673689
});
674690
});
675691

676-
describe('propertiesChanged()', () => {
677-
it('should update responsive styles and schedule masonry resize when layout changes', () => {
678-
const updateResponsiveStylesSpy = jest.spyOn(sharedGallery as any, 'updateResponsiveStyles');
679-
const scheduleMasonryResizeSpy = jest.spyOn(sharedGallery as any, 'scheduleMasonryResize');
692+
describe('onLayoutOrOrderChanged()', () => {
693+
it('should call syncResponsiveLayout and warnUnusedOrder when layout or order change', () => {
694+
const syncResponsiveLayoutSpy = jest.spyOn(sharedGallery as any, 'syncResponsiveLayout');
695+
const warnUnusedOrderSpy = jest.spyOn(sharedGallery as any, 'warnUnusedOrder');
696+
const rafSpy = jest.spyOn(helpers, 'raf').mockImplementation((cb) => {
697+
cb(0);
698+
return 0;
699+
});
680700

681-
(sharedGallery as any).propertiesChanged();
701+
(sharedGallery as any).onLayoutOrOrderChanged();
682702

683-
expect(updateResponsiveStylesSpy).toHaveBeenCalledWith(true);
684-
expect(scheduleMasonryResizeSpy).toHaveBeenCalled();
703+
expect(syncResponsiveLayoutSpy).toHaveBeenCalledTimes(1);
704+
expect(rafSpy).toHaveBeenCalledTimes(1);
705+
expect(warnUnusedOrderSpy).toHaveBeenCalledTimes(1);
706+
707+
syncResponsiveLayoutSpy.mockRestore();
708+
warnUnusedOrderSpy.mockRestore();
709+
rafSpy.mockRestore();
685710
});
686711
});
687712

core/src/components/gallery/gallery.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ export class Gallery implements ComponentInterface {
8787

8888
@Watch('columns')
8989
@Watch('gap')
90+
protected onColumnsOrGapChanged() {
91+
this.syncResponsiveLayout();
92+
}
93+
9094
@Watch('layout')
9195
@Watch('order')
92-
protected propertiesChanged() {
93-
this.updateResponsiveStyles(true);
94-
this.scheduleMasonryResize();
96+
protected onLayoutOrOrderChanged() {
97+
this.syncResponsiveLayout();
9598

9699
// Wait until the next animation frame to warn about unused order
97100
// to avoid erroneous warnings when the layout and order are updated
@@ -152,6 +155,15 @@ export class Gallery implements ComponentInterface {
152155
this.scheduleMasonryResize();
153156
};
154157

158+
/**
159+
* Recompute the gallery column and gap variables and masonry placement when
160+
* columns, gap, layout, or order change.
161+
*/
162+
private syncResponsiveLayout() {
163+
this.updateResponsiveStyles(true);
164+
this.scheduleMasonryResize();
165+
}
166+
155167
/**
156168
* Batch masonry measurements to a single animation frame.
157169
* This avoids repeated sync layouts during rapid resize/load/slot changes.

0 commit comments

Comments
 (0)