Skip to content

Commit

Permalink
Merge pull request #595 from davidjerleke/feature/#594
Browse files Browse the repository at this point in the history
Replace option `align:number` with a callback
  • Loading branch information
davidjerleke authored Oct 11, 2023
2 parents 3e6d9f0 + 4d7f79b commit 39dc78b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ Setting this to `false` will not activate or deactivate the carousel. Useful whe

### align

Type: <BrandPrimaryText>`string | number`</BrandPrimaryText>
Type: <BrandPrimaryText>`string | (viewSize: number, snapSize: number, index: number) => number`</BrandPrimaryText>
Default: <BrandSecondaryText>`center`</BrandSecondaryText>

Align the slides relative to the carousel viewport. Use one of the predefined alignments `start`, `center` or `end`. Alternatively, provide a number between `0 - 1` to align the slides, where **0.5 equals 50%**.
Align the slides relative to the carousel viewport. Use one of the predefined alignments `start`, `center` or `end`. Alternatively, provide your own callback to fully customize the alignment.

---

Expand Down
12 changes: 6 additions & 6 deletions packages/embla-carousel/src/__tests__/align.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('➡️ Align - Horizontal LTR', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [100, -700, -1100, -1300, -1800]
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('➡️ Align - Horizontal LTR', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [90, -730, -1150, -1370, -1890]
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('➡️ Align - Horizontal RTL', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [100, -700, -1100, -1300, -1800]
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('➡️ Align - Horizontal RTL', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [90, -730, -1150, -1370, -1890]
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('➡️ Align - Vertical', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [100, -700, -1100, -1300, -1800]
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('➡️ Align - Vertical', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [90, -730, -1150, -1370, -1890]
Expand Down
22 changes: 11 additions & 11 deletions packages/embla-carousel/src/__tests__/containScroll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('➡️ ContainScroll - Horizontal LTR', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [0, -200, -350, -600, -660]
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('➡️ ContainScroll - Horizontal LTR', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [0, -30, -250, -420, -690, -840, -860]
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('➡️ ContainScroll - Horizontal LTR', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('➡️ ContainScroll - Horizontal LTR', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('➡️ ContainScroll - Horizontal RTL', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [0, -200, -350, -600, -660]
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('➡️ ContainScroll - Horizontal RTL', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [0, -30, -250, -420, -690, -840, -860]
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('➡️ ContainScroll - Horizontal RTL', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [
Expand Down Expand Up @@ -391,7 +391,7 @@ describe('➡️ ContainScroll - Horizontal RTL', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [
Expand Down Expand Up @@ -441,7 +441,7 @@ describe('➡️ ContainScroll - Vertical', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [0, -200, -350, -600, -660]
Expand Down Expand Up @@ -527,7 +527,7 @@ describe('➡️ ContainScroll - Vertical', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [
Expand Down Expand Up @@ -577,7 +577,7 @@ describe('➡️ ContainScroll - Vertical', () => {
})

test('Custom', () => {
emblaApi.reInit({ align: 0.1 })
emblaApi.reInit({ align: (viewSize) => viewSize * 0.1 })

const engine = emblaApi.internalEngine()
const expectedScrollSnaps = [
Expand Down
20 changes: 10 additions & 10 deletions packages/embla-carousel/src/components/Alignment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { isNumber } from './utils'
import { isString } from './utils'

export type AlignmentOptionType = 'start' | 'center' | 'end' | number
export type AlignmentOptionType =
| 'start'
| 'center'
| 'end'
| ((viewSize: number, snapSize: number, index: number) => number)

export type AlignmentType = {
measure: (n: number) => number
measure: (n: number, index: number) => number
}

export function Alignment(
Expand All @@ -24,13 +28,9 @@ export function Alignment(
return viewSize - n
}

function percent(): number {
return viewSize * Number(align)
}

function measure(n: number): number {
if (isNumber(align)) return percent()
return predefined[align](n)
function measure(n: number, index: number): number {
if (isString(align)) return predefined[align](n)
return align(viewSize, n, index)
}

const self: AlignmentType = {
Expand Down

0 comments on commit 39dc78b

Please sign in to comment.