Skip to content

Commit

Permalink
Clear pixel tolerance during SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjerleke committed Feb 4, 2025
1 parent 4ae9e0c commit 196cbdf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
29 changes: 14 additions & 15 deletions packages/embla-carousel/src/components/EmblaCarousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ function EmblaCarousel(
): EngineType {
nodeHandler.clearOffsets(container, ...slides)

// TODO: Pass pixelTolerance, should be 0 when SSR
const engine = Engine(
root,
container,
Expand Down Expand Up @@ -137,21 +136,21 @@ function EmblaCarousel(
]).forEach((query) => mediaHandlers.add(query, 'change', reActivate))

if (!options.active) return
if (!ownerWindow) return
if (isSsr) return

engine.translate.to(engine.location.get())
engine.animation.init(ownerWindow)
engine.resizeHandler.init(ownerWindow)
engine.slidesInView.init(ownerWindow)
engine.slidesHandler.init(ownerWindow)
engine.eventHandler.init(self)
engine.watchHandler.init(self)
engine.slideFocus.init()

if (engine.options.loop) engine.slideLooper.loop()
if (container.offsetParent && slides.length) {
engine.dragHandler.init(ownerWindow)
if (!isSsr && ownerWindow) {
engine.translate.to(engine.location.get())
engine.animation.init(ownerWindow)
engine.resizeHandler.init(ownerWindow)
engine.slidesInView.init(ownerWindow)
engine.slidesHandler.init(ownerWindow)
engine.slideFocus.init(ownerWindow)
engine.eventHandler.init(self)
engine.watchHandler.init(self)

if (engine.options.loop) engine.slideLooper.loop()
if (container.offsetParent && slides.length) {
engine.dragHandler.init(ownerWindow)
}
}

pluginApis = pluginsHandler.init(self, pluginList)
Expand Down
2 changes: 1 addition & 1 deletion packages/embla-carousel/src/components/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function Engine(
} = options

// Measurements
const pixelTolerance = 2
const pixelTolerance = isSsr ? 0 : 2
const containerRect = nodeHandler.getRect(container)
const slideRects = slides.map(nodeHandler.getRect)
const axis = Axis(scrollAxis, direction)
Expand Down
2 changes: 1 addition & 1 deletion packages/embla-carousel/src/components/ScrollContain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function ScrollContain(
const snapsContained = measureContained()

function usePixelTolerance(bound: number, snap: number): boolean {
return deltaAbs(bound, snap) <= 1
return pixelTolerance ? deltaAbs(bound, snap) <= 1 : false
}

function findScrollContainLimit(): LimitType {
Expand Down
8 changes: 4 additions & 4 deletions packages/embla-carousel/src/components/SlideFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { EventStoreType } from './EventStore'
import { ScrollBodyType } from './ScrollBody'
import { ScrollToType } from './ScrollTo'
import { SlideRegistryType } from './SlideRegistry'
import { isNumber } from './utils'
import { isNumber, WindowType } from './utils'

export type SlideFocusType = {
init: () => void
init: (ownerWindow: WindowType) => void
}

export function SlideFocus(
Expand All @@ -24,10 +24,10 @@ export function SlideFocus(
const focusListenerOptions = { passive: true, capture: true }
let lastTabPressTime = 0

function init(): void {
function init(ownerWindow: WindowType): void {
if (!active) return

eventStore.add(document, 'keydown', onKeyDown, false)
eventStore.add(ownerWindow.document, 'keydown', onKeyDown, false)

slides.forEach((slide, slideIndex) => {
eventStore.add(
Expand Down

0 comments on commit 196cbdf

Please # to comment.