From 077bff783223a10f35a8a7b6d9a860a55fd8e941 Mon Sep 17 00:00:00 2001
From: Patrick McDougle <pmcdougle@rothys.com>
Date: Mon, 2 Dec 2024 14:55:57 -0600
Subject: [PATCH] Fix Race Condition if goToSlide is called too early

---
 src/components/carousel/carousel.component.ts | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/components/carousel/carousel.component.ts b/src/components/carousel/carousel.component.ts
index 55cbbc1a..3b42e1d3 100644
--- a/src/components/carousel/carousel.component.ts
+++ b/src/components/carousel/carousel.component.ts
@@ -506,6 +506,9 @@ export default class SlCarousel extends ShoelaceElement {
   }
 
   private scrollToSlide(slide: HTMLElement, behavior: ScrollBehavior = 'smooth') {
+    // This can happen if goToSlide is called before the scroll container is rendered
+    // We will have correctly set the activeSlide in goToSlide which will get picked up when initializeSlides is called.
+    if (!this.scrollContainer) { return; }
     const scrollContainer = this.scrollContainer;
     const scrollContainerRect = scrollContainer.getBoundingClientRect();
     const nextSlideRect = slide.getBoundingClientRect();