import React from "react"; import { styled, theme } from "../theme"; import type * as WPDS from "../theme"; import { CarouselContext } from "./CarouselRoot"; import { isItemShown } from "./utils"; const NAME = "CarouselItem"; const Container = styled("div", { flexShrink: "0", variants: { focused: { true: { "& > *": { outline: `2px solid ${theme.colors.signal}`, outlineOffset: "-2px", position: "relative", zIndex: 1, }, }, false: {}, }, }, }); export type CarouselItemProps = { css?: WPDS.CSS; id?: string; index?: number; itemsShownPerPage?: number; onKeyDown?: (event: React.KeyboardEvent) => void; } & React.ComponentPropsWithRef; export const CarouselItem = React.forwardRef( ({ children, id, ...props }, ref) => { const internalRef = React.useRef(null); const [isShown, setIsShown] = React.useState(false); const { activeId, isTransitioning } = React.useContext(CarouselContext); React.useEffect(() => { if (!ref) return; typeof ref === "function" ? ref(internalRef.current) : (ref.current = internalRef.current); }, [ref, internalRef]); React.useEffect(() => { if (!isTransitioning) { setIsShown(isItemShown(internalRef)); } }, [setIsShown, internalRef, isTransitioning]); return ( {children} ); } ); CarouselItem.displayName = NAME;