-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcarousel-item.tsx
53 lines (44 loc) · 1.3 KB
/
carousel-item.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React, {
ComponentProps,
FC,
RefObject,
useImperativeHandle,
useRef,
useState
} from 'react'
import { withForwardRef } from '../common/component-utils'
import { getRelativeRects } from './helpers'
import { ListItemRef } from './types'
type ListProps = ComponentProps<'li'>
type CarouselItemProps = ListProps & {
slideWidth?: number;
offset?: number;
className?: string;
forwardedRef?: RefObject<ListItemRef>
};
const EbayCarouselItem: FC<CarouselItemProps> = ({ slideWidth, offset, forwardedRef, children, ...rest }) => {
const itemRef = useRef()
const [isVisible, setIsVisible] = useState(false)
useImperativeHandle(forwardedRef, () => {
if (!itemRef.current) return
const { left, right } = getRelativeRects(itemRef.current)
const fullyVisible = left === undefined ||
(left - offset >= -0.01 && right - offset <= slideWidth + 0.01)
setIsVisible(fullyVisible)
return {
element: itemRef.current,
left,
right,
fullyVisible
}
}, [slideWidth, offset])
return (
<li
ref={itemRef}
aria-hidden={!isVisible}
{...rest}>
{children}
</li>
)
}
export default withForwardRef(EbayCarouselItem)