/** * * The useCarousel manages the state and functionality of a Carousel component. * * [Live Demo](https://www.primereact.org/Carousel/) * * @module useCarousel * @group headless * */ import type { HeadlessInstance } from '@primereact/types/core'; import type * as React from 'react'; /** * Event fired when the carousel's value changes. */ export interface UseCarouselChangeEvent { /** * The original event that triggered the change. */ originalEvent?: React.SyntheticEvent | Event; /** * The value of the carousel. */ value?: string | number; } /** * Defines valid properties in useCarousel. */ export interface UseCarouselProps { /** * Whether the carousel should auto size. * @default false */ autoSize?: boolean; /** * Orientation of the carousel. * @default 'horizontal' */ orientation?: 'horizontal' | 'vertical'; /** * Alignment of the carousel items. * @default 'start' */ align?: 'start' | 'center' | 'end'; /** * Whether the carousel should loop. * @default false */ loop?: boolean; /** * Scroll snap type applied to the track. * @default 'mandatory' */ snapType?: 'mandatory' | 'proximity'; /** * Spacing between carousel items. * @default 16 */ spacing?: number; /** * How many slides are visible per page. Supports fractions (e.g. 1.5). * @default 1 */ slidesPerPage?: number; /** * Index of the active slide. * @default undefined */ page?: number; /** * Default index of the active slide. * @default 0 */ defaultPage?: number; /** * Index of the active slide. * @default undefined */ slide?: number; /** * Callback fired when the carousel's page changes. * @param event The event that triggered the change. * @param event.value The value of the active page. * @returns void */ onPageChange?: (event: UseCarouselChangeEvent) => void; /** * Callback fired when the active slide (by value) changes. * @param event The event that triggered the change. * @param event.value The value of the active slide. * @returns void */ onSlideChange?: (event: UseCarouselChangeEvent) => void; } /** * Defines valid state in useCarousel. */ export interface UseCarouselState { /** * Whether the "previous" navigation button should be disabled. */ isPrevDisabled: boolean; /** * Whether the "next" navigation button should be disabled. */ isNextDisabled: boolean; /** * The snap points of the carousel. */ snapPoints: Set; /** * The current page of the carousel. */ page: number; /** * Whether the user is currently swiping the carousel by touch. */ swiping: boolean; } /** * Pre-built props for the carousel root element. */ export interface UseCarouselRootProps { /** * ARIA role: a labelled carousel is a `region` landmark. */ role: 'region'; /** * Describes the region as a carousel to assistive technology. */ 'aria-roledescription': 'carousel'; /** * Tab index for keyboard focus. */ tabIndex: number; /** * Keydown handler for carousel navigation. */ onKeyDown: (event: React.KeyboardEvent) => void; /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'root'; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; /** * Data attribute when auto size is enabled. */ 'data-autosize'?: ''; } /** * CSS custom properties set on the content element's style. * * These variables are consumed by item elements for sizing and layout. */ export interface UseCarouselContentCSSProperties extends React.CSSProperties { /** * Number of slides visible per page. Used by items to calculate their flex-basis. * @example `calc(100% / var(--px-slides-per-page))` */ '--px-slides-per-page': number; /** * Spacing between carousel items in pixels. Used for gap and item size calculations. * @example `gap: var(--px-spacing-items)` */ '--px-spacing-items': string; /** * The resolved scroll-snap-type value based on orientation and snap behavior. * @example `x mandatory` | `y proximity` */ '--px-scroll-snap-type': string; } /** * Pre-built props for the carousel content (track) element. */ export interface UseCarouselContentProps { /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'content'; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when auto size is enabled. */ 'data-autosize'?: ''; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; /** * Pointer down handler for the content element. */ onPointerDown: (event: React.PointerEvent) => void; /** * Pointer move handler for the content element. */ onPointerMove: (event: React.PointerEvent) => void; /** * Pointer up handler for the content element. */ onPointerUp: (event: React.PointerEvent) => void; /** * Wheel handler for the content element. */ onWheel: (event: React.WheelEvent) => void; /** * Structural style including CSS custom properties for layout. */ style: UseCarouselContentCSSProperties; } /** * Pre-built props for a carousel item element. */ export interface UseCarouselItemProps { /** * ARIA role for the slide. */ role: 'group'; /** * Describes the element as a slide to assistive technology. */ 'aria-roledescription': 'slide'; /** * Positional accessible name (e.g. "2 / 5") when index/total are provided. */ 'aria-label'?: string; /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'item'; /** * Data attribute for the item's value/identifier. */ 'data-value': unknown; /** * Data attribute marking this element as a carousel item. */ 'data-item': ''; /** * Data attribute indicating whether the item is currently in view. */ 'data-inview': 'false'; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; /** * Data attribute when auto size is enabled. */ 'data-autosize'?: ''; /** * Structural style for the item element (sizing and layout). */ style: React.CSSProperties; } /** * Pre-built props for the carousel next navigation button. */ export interface UseCarouselNextProps { /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'next'; /** * Button type. */ type: 'button'; /** * Whether the button is disabled. */ disabled: boolean; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when disabled. */ 'data-disabled': boolean; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; /** * Click handler to navigate to the next page. */ onClick: (event: React.MouseEvent) => void; } /** * Pre-built props for the carousel previous navigation button. */ export interface UseCarouselPrevProps { /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'prev'; /** * Button type. */ type: 'button'; /** * Whether the button is disabled. */ disabled: boolean; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when disabled. */ 'data-disabled': boolean; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; /** * Click handler to navigate to the previous page. */ onClick: (event: React.MouseEvent) => void; } /** * Pre-built props for a carousel indicator element. */ export interface UseCarouselIndicatorProps { /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'indicator'; /** * Button type. */ type: 'button'; /** * Marks the indicator of the current page. */ 'aria-current'?: true; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when the indicator is active. */ 'data-active'?: ''; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; /** * Click handler to navigate to the indicator's page. */ onClick: (event: React.MouseEvent) => void; } /** * Pre-built props for the carousel indicators container element. */ export interface UseCarouselIndicatorsProps { /** * Data attribute for scope identification. */ 'data-scope': 'carousel'; /** * Data attribute for part identification. */ 'data-part': 'indicators'; /** * Data attribute for orientation. */ 'data-orientation': string | undefined; /** * Data attribute for alignment. */ 'data-align': string | undefined; /** * Data attribute for the current page index. */ 'data-page': number; /** * Data attribute when the user is swiping. */ 'data-swiping'?: ''; } /** * Defines the methods and properties exposed by useCarousel. */ export interface UseCarouselExposes { /** * The state of the useCarousel. */ state: UseCarouselState; /** * Number of slides displayed per page. */ slidesPerPage: number; /** * Navigates to the previous slide. * @param originalEvent - The browser event that triggered the navigation, when available. * @returns void */ prev: (originalEvent?: React.SyntheticEvent | Event) => void; /** * Navigates to the next slide. * @param originalEvent - The browser event that triggered the navigation, when available. * @returns void */ next: (originalEvent?: React.SyntheticEvent | Event) => void; /** * Scrolls to the specified value. * @param value The index of the slide to scroll to. * @returns void */ scrollTo: (value: number) => void; /** * Scrolls to the specified page. * @param page The index of the page to scroll to. * @returns void */ scrollToPage: (page: number) => void; /** * Scrolls to the specified slide value. * @param slide The slide index to scroll to. * @returns void */ scrollToSlide: (slide: number) => void; /** * Scrolls to the closest snap point. * @returns void */ setToClosest: () => void; /** * Event handler for content pointer down. * @param event The event that triggered the pointer down. * @returns void */ onContentPointerDown: (event: React.PointerEvent) => void; /** * Event handler for content pointer move. * @param event The event that triggered the pointer move. * @returns void */ onContentPointerMove: (event: React.PointerEvent) => void; /** * Event handler for content pointer up. * @param event The event that triggered the pointer up. * @returns void */ onContentPointerUp: (event: React.PointerEvent) => void; /** * Event handler for content wheel. * @param event The event that triggered the wheel. * @returns void */ onContentWheel: (event: React.WheelEvent) => void; /** * Computes the appropriate CSS `scroll-snap-type` value * based on orientation and snapping behavior. * @returns The resolved `scroll-snap-type` CSS value. */ resolveSnapType: () => string; /** * Props for the root element. */ rootProps: UseCarouselRootProps; /** * Props for the content (track) element. */ contentProps: UseCarouselContentProps; /** * Props for the indicators container element. */ indicatorsProps: UseCarouselIndicatorsProps; /** * Returns props for an individual carousel item. * @param value The value/identifier of the item. * @param index The index of the item in the carousel. * @param total The total number of items in the carousel. * @returns Props object for the item element. */ getItemProps: (value: unknown) => UseCarouselItemProps; /** * Returns props for the next navigation button. * @param disabled Whether the button is disabled. * @returns Props object for the next button element. */ getNextProps: (disabled: boolean) => UseCarouselNextProps; /** * Returns props for the prev navigation button. * @param disabled Whether the button is disabled. * @returns Props object for the prev button element. */ getPrevProps: (disabled: boolean) => UseCarouselPrevProps; /** * Returns props for an individual indicator button. * @param page The page index this indicator represents. * @returns Props object for the indicator element. */ getIndicatorProps: (page: number) => UseCarouselIndicatorProps; } /** * Instance of useCarousel headless. */ export type UseCarouselInstance = HeadlessInstance;