MCPcopy
hub / github.com/untitleduico/react / CarouselIndicator

Function CarouselIndicator

components/application/carousel/carousel-base.tsx:249–282  ·  view source on GitHub ↗
({ index, isSelected = false, children, asChild, className, style }: CarouselIndicatorProps)

Source from the content-addressed store, hash-verified

247}
248
249const CarouselIndicator = ({ index, isSelected = false, children, asChild, className, style }: CarouselIndicatorProps) => {
250 const { api, selectedIndex } = useCarousel();
251
252 isSelected = isSelected || selectedIndex === index;
253
254 const handleClick = () => {
255 api?.scrollTo(index);
256 };
257 const computedClassName = typeof className === "function" ? className({ isSelected }) : className;
258
259 const defaultAriaLabel = "Go to slide" + (index + 1);
260
261 // If the children is a render prop, we need to pass the necessary props to the render prop.
262 if (typeof children === "function") {
263 return <>{children({ isSelected, onClick: handleClick })}</>;
264 }
265
266 // If the children is a valid element, we need to clone it and pass the necessary props to the cloned element.
267 if (asChild && isValidElement(children)) {
268 return cloneElement(children, {
269 onClick: handleClick,
270 "aria-label": defaultAriaLabel,
271 "aria-current": isSelected ? "true" : undefined,
272 style: { ...(children.props as HTMLAttributes<HTMLElement>).style, ...style },
273 className: [computedClassName, (children.props as HTMLAttributes<HTMLElement>).className].filter(Boolean).join(" ") || undefined,
274 } as HTMLAttributes<HTMLElement>);
275 }
276
277 return (
278 <button aria-label={defaultAriaLabel} aria-current={isSelected ? "true" : undefined} className={computedClassName} onClick={handleClick}>
279 {children}
280 </button>
281 );
282};
283
284interface CarouselIndicatorGroupProps extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
285 children: ReactNode | ((props: { index: number }) => ReactNode);

Callers

nothing calls this directly

Calls 1

useCarouselFunction · 0.85

Tested by

no test coverage detected