Chuyển tới nội dung

    Carousel

    Hiện một slide mỗi lần. Nút trước và sau nằm trên khung, và mỗi slide có một chấm trên viên kính. `loop` quay từ slide cuối về slide đầu.

    React

    Xem trước

    1 / 4

    Waves on an empty beach

    Props

    • label
    • loop
    import { Carousel } from "@/components/ui/carousel"
    import { Image } from "@/components/ui/image"
    
    const places = {
      coast: {
        src: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Waves on an empty beach"
      },
      ridge: {
        src: "https://images.unsplash.com/photo-1469474968028-56623f02e42e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Sun through a stand of trees"
      },
      lake: {
        src: "https://images.unsplash.com/photo-1501785888041-af3ef285b470?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A lake between mountains"
      },
      cabin: {
        src: "https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A cabin beside a still lake"
      }
    }
    const carouselLabelsVi = {
      previous: "Ảnh trước",
      next: "Ảnh sau",
      indicators: "Các ảnh",
      slide: (current: number, total: number) => `${current} / ${total}`,
      indicator: (current: number) => `Hiện ảnh ${current}`,
    }
    
    <Carousel label="Places" labels={carouselLabelsVi} className="w-full">
      <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.cabin.src} alt={places.cabin.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
    </Carousel>

    Cách dùng

    Khi nào dùng

    • Hiện vài ảnh hoặc vài khung, mỗi lần một cái, chẳng hạn ảnh của một nơi.
    • Cho người đọc đi qua một chuỗi ngắn bằng nút trước, nút sau và một chấm cho mỗi mục.

    Khi nào không dùng

    • Một danh sách dài để người đọc lướt. Dùng một danh sách hoặc Table.
    • Các bước của một quy trình người đọc đi qua. Dùng Stepper.
    • Một ảnh. Dùng Image.

    Hỗ trợ nền tảng

    Mỗi nền tảng có gì, theo spec. Nền tảng đang chọn được tô nổi.

    TênWebiOSAndroidGhi chú
    labelCóCóCó
    countKhôngCóCóChỉ trên iOS, Android.
    indexCóCóCó
    defaultIndexCóCóCó
    onIndexChangeCóCóCó
    loopCóCóCó
    labelsCóKhôngKhôngChỉ trên web. Chưa tạo cho iOS và Android.
    giá trị của keyboard: TabCóChưa tạoChưa tạo
    giá trị của keyboard: ArrowLeft, ArrowRightCóChưa tạoChưa tạo
    giá trị của keyboard: Home, EndCóChưa tạoChưa tạo
    giá trị của keyboard: Enter, SpaceCóChưa tạoChưa tạo

    Ví dụ

    Những nơi

    Nút trước, nút sau và một chấm cho mỗi ảnh, trên viên kính.

    1 of 4

    Waves on an empty beach
    import { Carousel } from "@/components/ui/carousel"
    import { Image } from "@/components/ui/image"
    
    const places = {
      coast: {
        src: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Waves on an empty beach"
      },
      ridge: {
        src: "https://images.unsplash.com/photo-1469474968028-56623f02e42e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Sun through a stand of trees"
      },
      lake: {
        src: "https://images.unsplash.com/photo-1501785888041-af3ef285b470?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A lake between mountains"
      },
      cabin: {
        src: "https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A cabin beside a still lake"
      }
    }
    
    <Carousel label="Places" className="w-full">
      <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.cabin.src} alt={places.cabin.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
    </Carousel>

    Dừng ở hai đầu

    Khi loop tắt, Previous bị vô hiệu ở ảnh đầu và Next ở ảnh cuối.

    1 of 3

    Waves on an empty beach
    import { Carousel } from "@/components/ui/carousel"
    import { Image } from "@/components/ui/image"
    
    const places = {
      coast: {
        src: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Waves on an empty beach"
      },
      ridge: {
        src: "https://images.unsplash.com/photo-1469474968028-56623f02e42e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Sun through a stand of trees"
      },
      lake: {
        src: "https://images.unsplash.com/photo-1501785888041-af3ef285b470?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A lake between mountains"
      },
      cabin: {
        src: "https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A cabin beside a still lake"
      }
    }
    
    <Carousel label="Places" loop={false} className="w-full">
      <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
    </Carousel>

    Nên và không nên

    Nên

    • Đặt tên vùng bằng label, theo ngôn ngữ của người đọc.
    • Giữ chuỗi ngắn, khoảng ba đến tám slide.
    • Cho mỗi slide cùng một khung, chẳng hạn một tỷ lệ, để trang không nhảy.

    Không nên

    • Đừng dùng cho các bước của một quy trình.
    • Đừng đặt một carousel khác bên trong một slide.
    • Đừng tự chạy. Người đọc tự chuyển slide.

    Props

    TênKiểuGiá trịMặc địnhMô tả
    label bắt buộcstring--Đặt tên vùng carousel cho công nghệ hỗ trợ.
    count bắt buộcnumber--Chỉ trên native: số lượng slide trong carousel. Gọi slot slide với từng chỉ số từ 0 đến count - 1. Chỉ trên iOS, Android.
    indexnumber--Slide đang hiện, tính từ 0. Gán cùng onIndexChange để điều khiển slide nào hiện.
    defaultIndexnumber-0Slide hiện đầu tiên, tính từ 0, khi không gán index.
    onIndexChange(index: number) => void--Được gọi với chỉ số tiếp theo khi slide đổi.
    loopboolean-trueQuay từ slide cuối về slide đầu, và từ slide đầu sang slide cuối. Tắt thì Previous bị vô hiệu ở slide đầu và Next ở slide cuối.
    labels{ previous?: string; next?: string; indicators?: string; slide?: (current: number, total: number) => string; indicator?: (current: number) => string }--Chữ cho các nút và tên slide. Mặc định: Previous slide, Next slide, Slides, 2 of 4, Show slide 2. Chỉ trên web. Chưa tạo cho iOS và Android.

    Slot

    TênBắt buộcMô tả
    slidescóMột node cho mỗi slide, theo thứ tự. Mỗi slide hiện một mình.
    slidecóChỉ trên native: slide cho một chỉ số, được gọi từ 0 đến count - 1.

    Cấu tạo

    PhầnMô tả
    rootVùng được đặt tên bởi label.
    viewportKhung cắt các slide, bo góc container.
    trackHàng slide. Nó dịch một bề rộng khung.
    itemMột slide.
    stepPrevious và next: nút kính tròn nằm trên khung.
    hitArea::before trong suốt trên Previous và Next, không nhỏ hơn size-target-min.
    pillViên kính dọc mép dưới, chứa các chấm, không có khoảng cách giữa chúng.
    dotNút cố định 16px, trên chuột và trên cảm ứng. Dấu 8px nằm giữa, khoảng trống xung quanh là vùng bấm.
    iconChấm vẽ bên trong nút đó.

    Khả năng truy cập

    Role: region

    PhímHành động
    TabĐi qua Previous, Next và chấm đang chọn.
    ArrowLeft, ArrowRightHiện slide trước hoặc sau. Với chữ từ phải sang trái, hai mũi tên đổi chỗ.
    Home, EndHiện slide đầu hoặc slide cuối.
    Enter, SpaceKích hoạt nút đang được focus.
    • Vùng được đặt tên bởi label, với aria-roledescription carousel.
    • Mỗi slide là một nhóm, tên kiểu 2 of 4. Slide không đang hiện bị ẩn và inert, nên nội dung của nó không nằm trong thứ tự Tab.
    • Chấm đang chọn có aria-current. Các chấm còn lại ở trong nhóm và không nằm trong thứ tự Tab.
    • Khi loop tắt, Previous bị vô hiệu ở slide đầu và Next ở slide cuối.
    • Previous và Next giữ vùng bấm size-target-min. Mỗi chấm là nút cố định 16px với dấu 8px, trên chuột và trên cảm ứng, nên viên kính không nở khi dùng ngón tay.

    Cài đặt

    npx werewolf-ui add carousel

    Chạy npx werewolf-ui init một lần trong app trước khi thêm component. Init cài token, thêm CSS import và luật cho agent. Add cài component này cùng các dependency và giữ nguyên theme của bạn. Xem Cài đặt để biết yêu cầu và cách cài qua registry.