-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: put pagination in separate component and make it optional
- Loading branch information
Showing
6 changed files
with
190 additions
and
134 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
88 changes: 88 additions & 0 deletions
88
packages/libs/react-ui/src/components/Tabs/TabsPagination.tsx
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,88 @@ | ||
import { | ||
MonoArrowBackIosNew, | ||
MonoArrowForwardIos, | ||
} from '@kadena/react-icons/system'; | ||
import classNames from 'classnames'; | ||
import React, { ReactElement, RefObject, useEffect, useState } from 'react'; | ||
import { Button } from '../Button'; | ||
import { hiddenClass, paginationButton, tabListControls } from './Tabs.css'; | ||
import { calculateScroll } from './utils/calculateScroll'; | ||
|
||
interface ITabsPaginationProps { | ||
children: ReactElement; | ||
wrapperContainerRef: RefObject<HTMLDivElement>; | ||
scrollContainerRef: RefObject<HTMLDivElement>; | ||
} | ||
|
||
export const TabsPagination = ({ | ||
children, | ||
wrapperContainerRef, | ||
scrollContainerRef, | ||
}: ITabsPaginationProps) => { | ||
const [visibleButtons, setVisibleButtons] = useState({ | ||
left: false, | ||
right: false, | ||
}); | ||
|
||
const determineButtonVisibility = () => { | ||
if (!scrollContainerRef.current || !wrapperContainerRef.current) return; | ||
|
||
const viewWidth = wrapperContainerRef.current.offsetWidth; | ||
const maxWidth = wrapperContainerRef.current.scrollWidth; | ||
const scrollPosition = scrollContainerRef.current.scrollLeft; | ||
|
||
if (scrollPosition === 0) { | ||
setVisibleButtons((prev) => ({ ...prev, left: false })); | ||
} | ||
if (scrollPosition > 0) { | ||
setVisibleButtons((prev) => ({ ...prev, left: true })); | ||
} | ||
// 20 is a margin to prevent having a very small last bit to scroll | ||
if (viewWidth + scrollPosition >= maxWidth - 20) { | ||
setVisibleButtons((prev) => ({ ...prev, right: false })); | ||
} else { | ||
setVisibleButtons((prev) => ({ ...prev, right: true })); | ||
} | ||
}; | ||
|
||
const handlePagination = (direction: 'back' | 'forward') => { | ||
if (!wrapperContainerRef.current || !scrollContainerRef.current) return; | ||
|
||
const nextValue = calculateScroll( | ||
direction, | ||
wrapperContainerRef, | ||
scrollContainerRef, | ||
); | ||
|
||
scrollContainerRef.current.scrollLeft = nextValue; | ||
determineButtonVisibility(); | ||
}; | ||
|
||
useEffect(() => { | ||
determineButtonVisibility(); | ||
}, [scrollContainerRef.current?.scrollLeft]); | ||
|
||
return ( | ||
<div className={tabListControls}> | ||
<Button | ||
variant="transparent" | ||
className={classNames(paginationButton, { | ||
[hiddenClass]: !visibleButtons.left, | ||
})} | ||
onPress={() => handlePagination('back')} | ||
> | ||
<MonoArrowBackIosNew /> | ||
</Button> | ||
{children} | ||
<Button | ||
variant="transparent" | ||
className={classNames(paginationButton, { | ||
[hiddenClass]: !visibleButtons.right, | ||
})} | ||
onPress={() => handlePagination('forward')} | ||
> | ||
<MonoArrowForwardIos /> | ||
</Button> | ||
</div> | ||
); | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/libs/react-ui/src/components/Tabs/utils/calculateScroll.tsx
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,34 @@ | ||
import { RefObject } from 'react'; | ||
import { getMinimalChildWidth } from './getMinimalChildWidth'; | ||
|
||
export const calculateScroll = ( | ||
direction: 'back' | 'forward', | ||
wrapperContainerRef: RefObject<HTMLDivElement>, | ||
scrollContainerRef: RefObject<HTMLDivElement>, | ||
) => { | ||
const maxWidth = wrapperContainerRef.current?.scrollWidth || 0; | ||
const viewWidth = wrapperContainerRef.current?.offsetWidth || 0; | ||
const offset = getMinimalChildWidth(scrollContainerRef); | ||
const currentValue = scrollContainerRef.current?.scrollLeft || 0; | ||
|
||
let nextValue = 0; | ||
|
||
if (direction === 'forward') { | ||
nextValue = Math.abs(currentValue + offset); | ||
|
||
if (nextValue > maxWidth - viewWidth) { | ||
nextValue = maxWidth - viewWidth; | ||
} | ||
|
||
if (nextValue > maxWidth) { | ||
return currentValue; | ||
} | ||
} else { | ||
nextValue = currentValue - offset; | ||
|
||
if (Math.abs(currentValue) < offset) { | ||
nextValue = 0; | ||
} | ||
} | ||
return nextValue; | ||
}; |
Oops, something went wrong.