Skip to content

Commit

Permalink
Resize the container based on media loads.
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed Sep 5, 2023
1 parent 1554cb4 commit d425457
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
18 changes: 16 additions & 2 deletions src/utils/embla/plugins/auto-size/auto-size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EmblaCarouselType } from 'embla-carousel';
import { LooseOptionsType } from 'embla-carousel/components/Options';
import { CreatePluginType, LoosePluginType } from 'embla-carousel/components/Plugins';
import debounce from 'lodash-es/debounce';
import { EmblaReInitController } from '../../reinit-controller';

declare module 'embla-carousel/components/Plugins' {
Expand Down Expand Up @@ -37,6 +38,10 @@ function AutoSize(): AutoSizeType {
intersectionHandler,
);

const debouncedSetContainerHeight = debounce(() => setContainerHeight(), 200, {
trailing: true,
});

function init(emblaApiInstance: EmblaCarouselType): void {
emblaApi = emblaApiInstance;
reInitController = new EmblaReInitController(emblaApi);
Expand All @@ -47,15 +52,24 @@ function AutoSize(): AutoSizeType {
resizeObserver.observe(slide);
}

emblaApi.on('settle', setContainerHeight);
// Need to examine container size on both settle and media load, as settle
// may happen before the media is loaded (which they subsequently changes
// the size to large than the maxHeight is set).
emblaApi
.containerNode()
.addEventListener('frigate-card:media:loaded', debouncedSetContainerHeight);
emblaApi.on('settle', debouncedSetContainerHeight);
}

function destroy(): void {
intersectionObserver.disconnect();
resizeObserver.disconnect();
reInitController?.destroy();

emblaApi.off('settle', setContainerHeight);
emblaApi
.containerNode()
.removeEventListener('frigate-card:media:loaded', debouncedSetContainerHeight);
emblaApi.off('settle', debouncedSetContainerHeight);
}

function intersectionHandler(entries: IntersectionObserverEntry[]): void {
Expand Down
3 changes: 0 additions & 3 deletions src/utils/embla/reinit-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export class EmblaReInitController {

protected _debouncedReInit = debounce(
() => {
// Allow the browser a moment to paint components that are inflight, to
// ensure accurate measurements are taken during the carousel
// reinitialization.
this._scrolling = false;
this._shouldReInitOnScrollStop = false;
this._emblaApi?.reInit();
Expand Down
6 changes: 2 additions & 4 deletions src/utils/media-grid-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ export class MediaGridController {
protected _idAttribute: string;

protected _throttledLayout = throttle(
() => {
window.requestAnimationFrame(() => this._masonry?.layout?.());
},
() => this._masonry?.layout?.(),
// Throttle layout calls to larger than the masonry.js transitionDuration
// value specified below.
400,
300,
{ trailing: true, leading: false },
);

Expand Down
1 change: 0 additions & 1 deletion tests/utils/embla/plugins/auto-size/auto-size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
createTestSlideNodes,
} from '../../test-utils';

// Mock out debouncing (used in the reinit controller).
vi.mock('lodash-es/debounce', () => ({
default: vi.fn((fn) => fn),
}));
Expand Down
7 changes: 1 addition & 6 deletions tests/utils/media-grid-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Masonry from 'masonry-layout';
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { MediaLoadedInfo } from '../../src/types';
import {
Expand All @@ -12,7 +12,6 @@ import {
ResizeObserverMock,
createSlot,
createSlotHost,
requestAnimationFrameMock,
} from '../test-utils';

vi.mock('lodash-es/throttle', () => ({
Expand Down Expand Up @@ -82,10 +81,6 @@ describe('MediaGridController', () => {
height: 20,
};

beforeAll(() => {
window.requestAnimationFrame = requestAnimationFrameMock;
});

beforeEach(() => {
vi.clearAllMocks();
vi.stubGlobal('MutationObserver', MutationObserverMock);
Expand Down

0 comments on commit d425457

Please sign in to comment.