-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Support using tab(focus) to select and navigate carousel
- Loading branch information
Showing
9 changed files
with
107 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { EventStoreType } from './EventStore' | ||
import { ScrollBodyType } from './ScrollBody' | ||
import { ScrollToType } from './ScrollTo' | ||
import { SlideRegistryType } from './SlideRegistry' | ||
import { isNumber } from './utils' | ||
|
||
export type SlideFocusType = { | ||
init: () => void | ||
} | ||
|
||
export function SlideFocus( | ||
root: HTMLElement, | ||
slides: HTMLElement[], | ||
slideRegistry: SlideRegistryType['slideRegistry'], | ||
scrollTo: ScrollToType, | ||
scrollBody: ScrollBodyType, | ||
eventStore: EventStoreType | ||
): SlideFocusType { | ||
let lastTabPressTime = 0 | ||
|
||
function init(): void { | ||
eventStore.add(document, 'keydown', registerTabPress, false) | ||
slides.forEach(addSlideFocusEvent) | ||
} | ||
|
||
function registerTabPress(event: KeyboardEvent): void { | ||
if (event.code === 'Tab') lastTabPressTime = new Date().getTime() | ||
} | ||
|
||
function addSlideFocusEvent(slide: HTMLElement): void { | ||
const focus = (): void => { | ||
const nowTime = new Date().getTime() | ||
const diffTime = nowTime - lastTabPressTime | ||
|
||
if (diffTime > 10) return | ||
|
||
root.scrollLeft = 0 | ||
const index = slides.indexOf(slide) | ||
const group = slideRegistry.findIndex((group) => group.includes(index)) | ||
|
||
if (!isNumber(group)) return | ||
|
||
scrollBody.useDuration(0) | ||
scrollTo.index(group, 0) | ||
} | ||
|
||
eventStore.add(slide, 'focus', focus, { | ||
passive: true, | ||
capture: true | ||
}) | ||
} | ||
|
||
const self: SlideFocusType = { | ||
init | ||
} | ||
return self | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters