Skip to content

Commit

Permalink
fix: Fix thumbnail carousel scroll position (#1606)
Browse files Browse the repository at this point in the history
* fix: Fix thumbnail carousel scroll position

* Small formatting fix
  • Loading branch information
dermotduffy authored Oct 3, 2024
1 parent 1f5d4bd commit 21b4687
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"crypto": "^1.0.1",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"embla-carousel": "^8.1.4",
"embla-carousel": "^8.3.0",
"embla-carousel-wheel-gestures": "^8.0.1",
"home-assistant-js-websocket": "^9.4.0",
"js-yaml": "^4.1.0",
Expand Down
3 changes: 0 additions & 3 deletions src/components/thumbnail-carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { ExtendedHomeAssistant } from '../types.js';
import { stopEventFromActivatingCardWideActions } from '../utils/action.js';
import { dispatchFrigateCardEvent } from '../utils/basic.js';
import { CarouselDirection } from '../utils/embla/carousel-controller.js';
import AutoSize from '../utils/embla/plugins/auto-size/auto-size.js';
import { MediaQueriesResults } from '../view/media-queries-results';
import './carousel.js';
import './thumbnail.js';
Expand Down Expand Up @@ -43,7 +42,6 @@ export class FrigateCardThumbnailCarousel extends LitElement {
public fadeThumbnails = false;

protected _thumbnailSlides: TemplateResult[] = [];
protected _plugins = [AutoSize()];

protected willUpdate(changedProps: PropertyValues): void {
if (changedProps.has('config')) {
Expand Down Expand Up @@ -144,7 +142,6 @@ export class FrigateCardThumbnailCarousel extends LitElement {

return html`<frigate-card-carousel
direction=${direction}
.plugins=${this._plugins}
.selected=${this._getSelectedSlide() ?? 0}
.dragFree=${true}
>
Expand Down
5 changes: 1 addition & 4 deletions src/utils/embla/carousel-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ export class CarouselController {
// This controller manages slide changes (including shadow DOM
// assignments, which the stock watcher does not handle).
watchSlides: false,

// We use the auto-size plugin to manage resizes without carousel resets
// mid-scroll.
watchResize: false,
watchResize: true,
watchDrag: this._draggable,
},
[
Expand Down
17 changes: 11 additions & 6 deletions src/utils/embla/plugins/auto-size/auto-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ function AutoSize(): AutoSizeType {
intersectionHandler,
);

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

function init(emblaApiInstance: EmblaCarouselType): void {
emblaApi = emblaApiInstance;
Expand Down Expand Up @@ -87,8 +91,9 @@ function AutoSize(): AutoSizeType {

if (isContainerIntersectingNow !== previousContainerIntersecting) {
// Don't reinitialize on first call (intersectionHandler is always called
// on initial observation).
const callReInit = previousContainerIntersecting !== null;
// on initial observation), nor when the viewport is not intersecting.
const callReInit =
isContainerIntersectingNow && previousContainerIntersecting !== null;
previousContainerIntersecting = isContainerIntersectingNow;
if (callReInit) {
reInitController?.reinit();
Expand Down Expand Up @@ -122,7 +127,7 @@ function AutoSize(): AutoSizeType {
}
}

function setContainerHeight(): void {
function setContainerHeightAndReInit(): void {
const {
slideRegistry,
options: { axis },
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/embla/carousel-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ describe('CarouselController', () => {
loop: true,
containScroll: 'trimSnaps',
watchSlides: false,
watchResize: false,
watchResize: true,
watchDrag: false,
},
plugins,
Expand Down
7 changes: 7 additions & 0 deletions tests/utils/embla/plugins/auto-size/auto-size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ describe('AutoSize', () => {
// First intersection handler call sets the state only.
callIntersectionHandler(true);

// When not visible, will not re-init.
callIntersectionHandler(false);
callIntersectionHandler(false);
callIntersectionHandler(false);

expect(emblaApi.reInit).not.toBeCalled();

// When visible, will re-initialize once.
callIntersectionHandler(true);
callIntersectionHandler(true);

expect(emblaApi.reInit).toBeCalledTimes(1);
});

Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4236,10 +4236,10 @@ __metadata:
languageName: node
linkType: hard

"embla-carousel@npm:^8.1.4":
version: 8.1.4
resolution: "embla-carousel@npm:8.1.4"
checksum: 10c0/8dcd7fa5561fc354dd422440f3e24da8e2bc75617d8c053b88d44a603b69cb123a7ff3deae3eaa174b60a3a2d6bd8852b8ad21c7a3cca4692ce7da4078febf66
"embla-carousel@npm:^8.3.0":
version: 8.3.0
resolution: "embla-carousel@npm:8.3.0"
checksum: 10c0/0240156d6a736603d82ddfe93b03ce296e385e9c18ed2cca9465634c8adb7560bfc2fbef6368a4da2a54926c4ba6de1012ebb33d2fc92c052030ea2288e4cc92
languageName: node
linkType: hard

Expand Down Expand Up @@ -5149,7 +5149,7 @@ __metadata:
date-fns: "npm:^3.6.0"
date-fns-tz: "npm:^3.1.3"
docsify-cli: "npm:^4.4.4"
embla-carousel: "npm:^8.1.4"
embla-carousel: "npm:^8.3.0"
embla-carousel-wheel-gestures: "npm:^8.0.1"
eslint: "npm:^8.57.0"
eslint-config-airbnb-base: "npm:^15.0.0"
Expand Down

0 comments on commit 21b4687

Please sign in to comment.