-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve multi path component for moblie (#2094)
Co-authored-by: Colin Rosati <colin.rosati@commercetools.com>
- Loading branch information
1 parent
07fe8b7
commit 5fe6164
Showing
3 changed files
with
214 additions
and
24 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
61 changes: 61 additions & 0 deletions
61
packages/ui-kit/src/components/multi-path-block/useArrowNavigation.ts
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,61 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { LabelSyncPair } from '.'; | ||
|
||
/** | ||
* Dedicated hook for arrow navigation | ||
* This sets up intersection observers, threshold, display states for navigation | ||
*/ | ||
export const useArrowNavigation = (labelSyncItems: LabelSyncPair[]) => { | ||
const [displayStartScroll, setDisplayStartScroll] = useState(false); | ||
const [displayEndScroll, setDisplayEndScroll] = useState(false); | ||
const tabsRef = useRef<HTMLDivElement>(null); | ||
const tabListRef = useRef<HTMLDivElement>(null); | ||
|
||
const handleScrollButtonStart = (entries: IntersectionObserverEntry[]) => { | ||
setDisplayStartScroll(!entries[0].isIntersecting); | ||
}; | ||
const handleScrollButtonEnd = (entries: IntersectionObserverEntry[]) => { | ||
setDisplayEndScroll(!entries[entries.length - 1].isIntersecting); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!tabListRef.current?.children) { | ||
return; | ||
} | ||
|
||
const tabListChildren = Array.from(tabListRef.current?.children); | ||
|
||
if (labelSyncItems.length > 0) { | ||
const firstTab = tabListChildren[0]; | ||
const lastTab = tabListChildren[tabListChildren.length - 1]; | ||
const observerOptions = { | ||
root: tabsRef.current, | ||
threshold: 0.99, | ||
}; | ||
|
||
const firstObserver = new IntersectionObserver( | ||
handleScrollButtonStart, | ||
observerOptions | ||
); | ||
firstObserver.observe(firstTab); | ||
|
||
const lastObserver = new IntersectionObserver( | ||
handleScrollButtonEnd, | ||
observerOptions | ||
); | ||
lastObserver.observe(lastTab); | ||
|
||
return () => { | ||
firstObserver.disconnect(); | ||
lastObserver.disconnect(); | ||
}; | ||
} | ||
}, [labelSyncItems.length]); | ||
|
||
return { | ||
displayStartScroll, | ||
displayEndScroll, | ||
tabsRef, | ||
tabListRef, | ||
}; | ||
}; |
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