Skip to content

Commit 266ef1b

Browse files
Bug fix for #899.
Co-authored-by: Yasuhiro Yamamoto <yasuhiroyamamoto@colers.jp> Co-authored-by: David Jerleke <david.jerleke@gmail.com>
1 parent ba2c14c commit 266ef1b

7 files changed

+186
-4
lines changed

packages/embla-carousel/src/__tests__/containScroll-ltr.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
FIXTURE_CONTAIN_SCROLL_LTR_4,
88
FIXTURE_CONTAIN_SCROLL_LTR_5,
99
FIXTURE_CONTAIN_SCROLL_LTR_6,
10-
FIXTURE_CONTAIN_SCROLL_LTR_7
10+
FIXTURE_CONTAIN_SCROLL_LTR_7,
11+
FIXTURE_CONTAIN_SCROLL_LTR_8
1112
} from './fixtures/containScroll-ltr.fixture'
1213

1314
const FIRST_SNAP_INDEX = 0
@@ -250,6 +251,20 @@ describe('➡️ ContainScroll - Horizontal LTR', () => {
250251

251252
expect(engine.slideRegistry).toEqual([[0, 1], [2], [3], [4, 5]])
252253
})
254+
255+
test('Unexpected 1 pixel snaps are NOT present at the end of the carousel', () => {
256+
const emblaApi = EmblaCarousel(
257+
mockTestElements(FIXTURE_CONTAIN_SCROLL_LTR_8),
258+
{ align: 'start' }
259+
)
260+
261+
const engine = emblaApi.internalEngine()
262+
const expectedScrollSnaps = [0, -400, -800]
263+
expect(engine.scrollSnaps).toEqual(expectedScrollSnaps)
264+
expect(engine.location.get()).toBe(expectedScrollSnaps[FIRST_SNAP_INDEX])
265+
266+
expect(engine.slideRegistry).toEqual([[0], [1], [2, 3]])
267+
})
253268
})
254269

255270
describe('"keepSnaps" is correct for slides WITHOUT MARGINS and ALIGN is:', () => {

packages/embla-carousel/src/__tests__/containScroll-rtl.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
FIXTURE_CONTAIN_SCROLL_RTL_4,
88
FIXTURE_CONTAIN_SCROLL_RTL_5,
99
FIXTURE_CONTAIN_SCROLL_RTL_6,
10-
FIXTURE_CONTAIN_SCROLL_RTL_7
10+
FIXTURE_CONTAIN_SCROLL_RTL_7,
11+
FIXTURE_CONTAIN_SCROLL_RTL_8
1112
} from './fixtures/containScroll-rtl.fixture'
1213

1314
const FIRST_SNAP_INDEX = 0
@@ -255,6 +256,20 @@ describe('➡️ ContainScroll - Horizontal RTL', () => {
255256

256257
expect(engine.slideRegistry).toEqual([[0, 1], [2], [3], [4, 5]])
257258
})
259+
260+
test('Unexpected 1 pixel snaps are NOT present at the end of the carousel', () => {
261+
const emblaApi = EmblaCarousel(
262+
mockTestElements(FIXTURE_CONTAIN_SCROLL_RTL_8),
263+
{ direction: 'rtl', align: 'start' }
264+
)
265+
266+
const engine = emblaApi.internalEngine()
267+
const expectedScrollSnaps = [0, -399, -799]
268+
expect(engine.scrollSnaps).toEqual(expectedScrollSnaps)
269+
expect(engine.location.get()).toBe(expectedScrollSnaps[FIRST_SNAP_INDEX])
270+
271+
expect(engine.slideRegistry).toEqual([[0], [1], [2, 3]])
272+
})
258273
})
259274

260275
describe('"keepSnaps" is correct for slides WITHOUT MARGINS and ALIGN is:', () => {

packages/embla-carousel/src/__tests__/containScroll-vertical.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
FIXTURE_CONTAIN_SCROLL_Y_4,
88
FIXTURE_CONTAIN_SCROLL_Y_5,
99
FIXTURE_CONTAIN_SCROLL_Y_6,
10-
FIXTURE_CONTAIN_SCROLL_Y_7
10+
FIXTURE_CONTAIN_SCROLL_Y_7,
11+
FIXTURE_CONTAIN_SCROLL_Y_8
1112
} from './fixtures/containScroll-vertical.fixture'
1213

1314
const FIRST_SNAP_INDEX = 0
@@ -253,6 +254,20 @@ describe('➡️ ContainScroll - Vertical', () => {
253254

254255
expect(engine.slideRegistry).toEqual([[0, 1], [2], [3], [4, 5]])
255256
})
257+
258+
test('Unexpected 1 pixel snaps are NOT present at the end of the carousel', () => {
259+
const emblaApi = EmblaCarousel(
260+
mockTestElements(FIXTURE_CONTAIN_SCROLL_Y_8),
261+
{ axis: 'y', align: 'start' }
262+
)
263+
264+
const engine = emblaApi.internalEngine()
265+
const expectedScrollSnaps = [0, -400, -800]
266+
expect(engine.scrollSnaps).toEqual(expectedScrollSnaps)
267+
expect(engine.location.get()).toBe(expectedScrollSnaps[FIRST_SNAP_INDEX])
268+
269+
expect(engine.slideRegistry).toEqual([[0], [1], [2, 3]])
270+
})
256271
})
257272

258273
describe('"keepSnaps" is correct for slides WITHOUT MARGINS and ALIGN is:', () => {

packages/embla-carousel/src/__tests__/fixtures/containScroll-ltr.fixture.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,49 @@ export const FIXTURE_CONTAIN_SCROLL_LTR_7: TestElementDimensionsType = {
405405
value: 0
406406
}
407407
}
408+
409+
/*
410+
Fixture 8
411+
412+
- Horizontal
413+
- LTR
414+
- No slide margins
415+
*/
416+
export const FIXTURE_CONTAIN_SCROLL_LTR_8: TestElementDimensionsType = {
417+
containerOffset: {
418+
offsetWidth: 799,
419+
offsetHeight: 190,
420+
offsetTop: 176,
421+
offsetLeft: 0
422+
},
423+
slideOffsets: [
424+
{
425+
offsetWidth: 400,
426+
offsetHeight: 190,
427+
offsetTop: 0,
428+
offsetLeft: 0
429+
},
430+
{
431+
offsetWidth: 400,
432+
offsetHeight: 190,
433+
offsetTop: 0,
434+
offsetLeft: 400
435+
},
436+
{
437+
offsetWidth: 400,
438+
offsetHeight: 190,
439+
offsetTop: 0,
440+
offsetLeft: 799
441+
},
442+
{
443+
offsetWidth: 400,
444+
offsetHeight: 190,
445+
offsetTop: 0,
446+
offsetLeft: 1199
447+
}
448+
],
449+
endMargin: {
450+
property: 'marginRight',
451+
value: 0
452+
}
453+
}

packages/embla-carousel/src/__tests__/fixtures/containScroll-rtl.fixture.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,49 @@ export const FIXTURE_CONTAIN_SCROLL_RTL_7: TestElementDimensionsType = {
405405
value: 0
406406
}
407407
}
408+
409+
/*
410+
Fixture 8
411+
412+
- Horizontal
413+
- RTL
414+
- No slide margins
415+
*/
416+
export const FIXTURE_CONTAIN_SCROLL_RTL_8: TestElementDimensionsType = {
417+
containerOffset: {
418+
offsetWidth: 799,
419+
offsetHeight: 190,
420+
offsetTop: 176,
421+
offsetLeft: 0
422+
},
423+
slideOffsets: [
424+
{
425+
offsetWidth: 400,
426+
offsetHeight: 190,
427+
offsetTop: 0,
428+
offsetLeft: 400
429+
},
430+
{
431+
offsetWidth: 400,
432+
offsetHeight: 190,
433+
offsetTop: 0,
434+
offsetLeft: 0
435+
},
436+
{
437+
offsetWidth: 400,
438+
offsetHeight: 190,
439+
offsetTop: 0,
440+
offsetLeft: -399
441+
},
442+
{
443+
offsetWidth: 400,
444+
offsetHeight: 190,
445+
offsetTop: 0,
446+
offsetLeft: -799
447+
}
448+
],
449+
endMargin: {
450+
property: 'marginLeft',
451+
value: 0
452+
}
453+
}

packages/embla-carousel/src/__tests__/fixtures/containScroll-vertical.fixture.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,48 @@ export const FIXTURE_CONTAIN_SCROLL_Y_7: TestElementDimensionsType = {
398398
value: 0
399399
}
400400
}
401+
402+
/*
403+
Fixture 8
404+
405+
- Vertical
406+
- No slide margins
407+
*/
408+
export const FIXTURE_CONTAIN_SCROLL_Y_8: TestElementDimensionsType = {
409+
containerOffset: {
410+
offsetWidth: 799,
411+
offsetHeight: 799,
412+
offsetTop: 0,
413+
offsetLeft: 0
414+
},
415+
slideOffsets: [
416+
{
417+
offsetWidth: 799,
418+
offsetHeight: 400,
419+
offsetTop: 0,
420+
offsetLeft: 0
421+
},
422+
{
423+
offsetWidth: 799,
424+
offsetHeight: 400,
425+
offsetTop: 400,
426+
offsetLeft: 0
427+
},
428+
{
429+
offsetWidth: 799,
430+
offsetHeight: 400,
431+
offsetTop: 799,
432+
offsetLeft: 0
433+
},
434+
{
435+
offsetWidth: 799,
436+
offsetHeight: 400,
437+
offsetTop: 1199,
438+
offsetLeft: 0
439+
}
440+
],
441+
endMargin: {
442+
property: 'marginBottom',
443+
value: 0
444+
}
445+
}

packages/embla-carousel/src/components/ScrollContain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function ScrollContain(
2121
const snapsContained = measureContained()
2222

2323
function usePixelTolerance(bound: number, snap: number): boolean {
24-
return deltaAbs(bound, snap) < 1
24+
return deltaAbs(bound, snap) <= 1
2525
}
2626

2727
function findScrollContainLimit(): LimitType {

0 commit comments

Comments
 (0)