fix: Fix thumbnail carousel scroll position (#1606)

* fix: Fix thumbnail carousel scroll position

* Small formatting fix
This commit is contained in:
Dermot Duffy
2024-10-02 20:28:13 -07:00
committed by GitHub
parent 1f5d4bd8b0
commit 21b468734c
7 changed files with 26 additions and 20 deletions
+1 -1
View File
@@ -27,7 +27,7 @@
"crypto": "^1.0.1", "crypto": "^1.0.1",
"date-fns": "^3.6.0", "date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3", "date-fns-tz": "^3.1.3",
"embla-carousel": "^8.1.4", "embla-carousel": "^8.3.0",
"embla-carousel-wheel-gestures": "^8.0.1", "embla-carousel-wheel-gestures": "^8.0.1",
"home-assistant-js-websocket": "^9.4.0", "home-assistant-js-websocket": "^9.4.0",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
-3
View File
@@ -15,7 +15,6 @@ import { ExtendedHomeAssistant } from '../types.js';
import { stopEventFromActivatingCardWideActions } from '../utils/action.js'; import { stopEventFromActivatingCardWideActions } from '../utils/action.js';
import { dispatchFrigateCardEvent } from '../utils/basic.js'; import { dispatchFrigateCardEvent } from '../utils/basic.js';
import { CarouselDirection } from '../utils/embla/carousel-controller.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 { MediaQueriesResults } from '../view/media-queries-results';
import './carousel.js'; import './carousel.js';
import './thumbnail.js'; import './thumbnail.js';
@@ -43,7 +42,6 @@ export class FrigateCardThumbnailCarousel extends LitElement {
public fadeThumbnails = false; public fadeThumbnails = false;
protected _thumbnailSlides: TemplateResult[] = []; protected _thumbnailSlides: TemplateResult[] = [];
protected _plugins = [AutoSize()];
protected willUpdate(changedProps: PropertyValues): void { protected willUpdate(changedProps: PropertyValues): void {
if (changedProps.has('config')) { if (changedProps.has('config')) {
@@ -144,7 +142,6 @@ export class FrigateCardThumbnailCarousel extends LitElement {
return html`<frigate-card-carousel return html`<frigate-card-carousel
direction=${direction} direction=${direction}
.plugins=${this._plugins}
.selected=${this._getSelectedSlide() ?? 0} .selected=${this._getSelectedSlide() ?? 0}
.dragFree=${true} .dragFree=${true}
> >
+1 -4
View File
@@ -128,10 +128,7 @@ export class CarouselController {
// This controller manages slide changes (including shadow DOM // This controller manages slide changes (including shadow DOM
// assignments, which the stock watcher does not handle). // assignments, which the stock watcher does not handle).
watchSlides: false, watchSlides: false,
watchResize: true,
// We use the auto-size plugin to manage resizes without carousel resets
// mid-scroll.
watchResize: false,
watchDrag: this._draggable, watchDrag: this._draggable,
}, },
[ [
+11 -6
View File
@@ -38,9 +38,13 @@ function AutoSize(): AutoSizeType {
intersectionHandler, intersectionHandler,
); );
const debouncedSetContainerHeight = debounce(() => setContainerHeight(), 200, { const debouncedSetContainerHeight = debounce(
trailing: true, () => setContainerHeightAndReInit(),
}); 200,
{
trailing: true,
},
);
function init(emblaApiInstance: EmblaCarouselType): void { function init(emblaApiInstance: EmblaCarouselType): void {
emblaApi = emblaApiInstance; emblaApi = emblaApiInstance;
@@ -87,8 +91,9 @@ function AutoSize(): AutoSizeType {
if (isContainerIntersectingNow !== previousContainerIntersecting) { if (isContainerIntersectingNow !== previousContainerIntersecting) {
// Don't reinitialize on first call (intersectionHandler is always called // Don't reinitialize on first call (intersectionHandler is always called
// on initial observation). // on initial observation), nor when the viewport is not intersecting.
const callReInit = previousContainerIntersecting !== null; const callReInit =
isContainerIntersectingNow && previousContainerIntersecting !== null;
previousContainerIntersecting = isContainerIntersectingNow; previousContainerIntersecting = isContainerIntersectingNow;
if (callReInit) { if (callReInit) {
reInitController?.reinit(); reInitController?.reinit();
@@ -122,7 +127,7 @@ function AutoSize(): AutoSizeType {
} }
} }
function setContainerHeight(): void { function setContainerHeightAndReInit(): void {
const { const {
slideRegistry, slideRegistry,
options: { axis }, options: { axis },
@@ -207,7 +207,7 @@ describe('CarouselController', () => {
loop: true, loop: true,
containScroll: 'trimSnaps', containScroll: 'trimSnaps',
watchSlides: false, watchSlides: false,
watchResize: false, watchResize: true,
watchDrag: false, watchDrag: false,
}, },
plugins, plugins,
@@ -59,10 +59,17 @@ describe('AutoSize', () => {
// First intersection handler call sets the state only. // First intersection handler call sets the state only.
callIntersectionHandler(true); callIntersectionHandler(true);
// When not visible, will not re-init.
callIntersectionHandler(false); callIntersectionHandler(false);
callIntersectionHandler(false); 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); expect(emblaApi.reInit).toBeCalledTimes(1);
}); });
+5 -5
View File
@@ -4236,10 +4236,10 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"embla-carousel@npm:^8.1.4": "embla-carousel@npm:^8.3.0":
version: 8.1.4 version: 8.3.0
resolution: "embla-carousel@npm:8.1.4" resolution: "embla-carousel@npm:8.3.0"
checksum: 10c0/8dcd7fa5561fc354dd422440f3e24da8e2bc75617d8c053b88d44a603b69cb123a7ff3deae3eaa174b60a3a2d6bd8852b8ad21c7a3cca4692ce7da4078febf66 checksum: 10c0/0240156d6a736603d82ddfe93b03ce296e385e9c18ed2cca9465634c8adb7560bfc2fbef6368a4da2a54926c4ba6de1012ebb33d2fc92c052030ea2288e4cc92
languageName: node languageName: node
linkType: hard linkType: hard
@@ -5149,7 +5149,7 @@ __metadata:
date-fns: "npm:^3.6.0" date-fns: "npm:^3.6.0"
date-fns-tz: "npm:^3.1.3" date-fns-tz: "npm:^3.1.3"
docsify-cli: "npm:^4.4.4" 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" embla-carousel-wheel-gestures: "npm:^8.0.1"
eslint: "npm:^8.57.0" eslint: "npm:^8.57.0"
eslint-config-airbnb-base: "npm:^15.0.0" eslint-config-airbnb-base: "npm:^15.0.0"