Skip to content

Commit

Permalink
feat: Add watchFocus option
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Shingler authored and davidjerleke committed Jul 19, 2024
1 parent 9d1cc8a commit c1d9ee5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/embla-carousel/src/components/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export function Engine(
containScroll,
watchResize,
watchSlides,
watchDrag
watchDrag,
watchFocus
} = options

// Measurements
Expand Down Expand Up @@ -263,7 +264,8 @@ export function Engine(
scrollTo,
scrollBody,
eventStore,
eventHandler
eventHandler,
watchFocus
)

// Engine
Expand Down
5 changes: 4 additions & 1 deletion packages/embla-carousel/src/components/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DragHandlerOptionType } from './DragHandler'
import { ResizeHandlerOptionType } from './ResizeHandler'
import { SlidesHandlerOptionType } from './SlidesHandler'
import { SlidesInViewOptionsType } from './SlidesInView'
import { FocusHandlerOptionType } from './SlideFocus'

export type LooseOptionsType = {
[key: string]: unknown
Expand Down Expand Up @@ -36,6 +37,7 @@ export type OptionsType = CreateOptionsType<{
watchDrag: DragHandlerOptionType
watchResize: ResizeHandlerOptionType
watchSlides: SlidesHandlerOptionType
watchFocus: FocusHandlerOptionType
}>

export const defaultOptions: OptionsType = {
Expand All @@ -57,7 +59,8 @@ export const defaultOptions: OptionsType = {
active: true,
watchDrag: true,
watchResize: true,
watchSlides: true
watchSlides: true,
watchFocus: true
}

export type EmblaOptionsType = Partial<OptionsType>
15 changes: 12 additions & 3 deletions packages/embla-carousel/src/components/SlideFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { EventStoreType } from './EventStore'
import { ScrollBodyType } from './ScrollBody'
import { ScrollToType } from './ScrollTo'
import { SlideRegistryType } from './SlideRegistry'
import { isNumber } from './utils'
import { isBoolean, isNumber } from './utils'

type FocusHandlerCallbackType = () => void

export type FocusHandlerOptionType = boolean | FocusHandlerCallbackType

export type SlideFocusType = {
init: () => void
Expand All @@ -16,11 +20,14 @@ export function SlideFocus(
scrollTo: ScrollToType,
scrollBody: ScrollBodyType,
eventStore: EventStoreType,
eventHandler: EventHandlerType
eventHandler: EventHandlerType,
watchFocus: FocusHandlerOptionType
): SlideFocusType {
let lastTabPressTime = 0

function init(): void {
if (!watchFocus) return

eventStore.add(document, 'keydown', registerTabPress, false)
slides.forEach(addSlideFocusEvent)
}
Expand All @@ -30,7 +37,7 @@ export function SlideFocus(
}

function addSlideFocusEvent(slide: HTMLElement): void {
const focus = (): void => {
const defaultFocusHandler = (): void => {
const nowTime = new Date().getTime()
const diffTime = nowTime - lastTabPressTime

Expand All @@ -47,6 +54,8 @@ export function SlideFocus(
eventHandler.emit('slideFocus')
}

const focus = isBoolean(watchFocus) ? defaultFocusHandler : watchFocus

eventStore.add(slide, 'focus', focus, {
passive: true,
capture: true
Expand Down

0 comments on commit c1d9ee5

Please sign in to comment.