react-material-ui-carousel

repository·master·Indexed 20 days ago

https://github.com/learus/react-material-ui-carousel

A generic and extendible Carousel UI component for React built on top of Material UI. It features smooth animations (fade and slide), navigation buttons, interactive bullet indicators, and autoplay support. The library provides extensive customization options for navigation icons, indicator styling, and custom button rendering via the NavButton prop. It supports MUI 5, with legacy support for MUI 4 via version 2.

Tokens
5.8K
Snippets
17
Records
23
Agent score
67%

What's inside react-material-ui-carousel

  1. Install react-material-ui-carousel

    master

    Install the core carousel package via npm. Note that you must also have Material UI dependencies installed for the component to function.

    npm install react-material-ui-carousel --save
    
    # Required Material UI dependencies
    npm install @mui/material
    npm install @mui/icons-material
    npm install @mui/styles
  2. Customize indicator icons and styling

    master

    The carousel indicators (bullets) can be customized using four props:

    • IndicatorIcon: A ReactNode (JSX or string) or an array of ReactNodes to define the icon for each indicator.
    • indicatorIconButtonProps: Styles for all indicator buttons.
    • activeIndicatorIconButtonProps: Styles specifically for the currently active indicator.
    • indicatorContainerProps: Styles for the container holding the indicators.
    <Carousel
        IndicatorIcon={<HomeIcon />}
        indicatorIconButtonProps={{
            style: { padding: '10px', color: 'blue' }
        }}
        activeIndicatorIconButtonProps={{
            style: { backgroundColor: 'red' }
        }}
        indicatorContainerProps={{
            style: { marginTop: '50px', textAlign: 'right' }
        }}
    >
        {/* slides */}
    </Carousel>
  3. Customize default navigation buttons

    master

    You can customize the default navigation buttons using several props. Note that styles provided via these props are merged with existing styles; to remove a default style, you must explicitly unset or override it.

    <Carousel
        NextIcon={<MyCustomIcon />}
        PrevIcon={<MyCustomIcon />}
        fullHeightHover={false}
        navButtonsProps={{ 
            style: { backgroundColor: 'cornflowerblue', borderRadius: 0 } 
        }}
        navButtonsWrapperProps={{ 
            style: { bottom: '0', top: 'unset' } 
        }}
        NextIcon='next'
        PrevIcon='prev'
    >
        {/* slides */}
    </Carousel>
  4. Basic Usage Example

    master

    To use the carousel, wrap your slide items (any JSX elements) inside the <Carousel> component. Each child of the <Carousel> represents a single slide.

    import React from 'react';
    import Carousel from 'react-material-ui-carousel';
    import { Paper, Button } from '@mui/material';
    
    function Example() {
        const items = [
            { name: "Item 1", description: "Description 1" },
            { name: "Item 2", description: "Description 2" }
        ];
    
        return (
            <Carousel>
                {items.map((item, i) => (
                    <Paper key={i}>
                        <h2>{item.name}</h2>
                        <p>{item.description}</p>
                        <Button>Check it out!</Button>
                    </Paper>
                ))}
            </Carousel>
        );
    }
  5. Handle Next and Previous navigation events

    master

    The <Carousel> component provides next and prev props that act as callbacks when the user navigates. These functions receive the index of the new slide and the index of the previous slide.

    <Carousel
        next={(next, active) => console.log(`we left ${active}, and are now at ${next}`)}
        prev={(prev, active) => console.log(`we left ${active}, and are now at ${prev}`)}
    >
        {/* slides */}
    </Carousel>
  6. Completely customize navigation buttons with NavButton

    master

    If the default button customization is insufficient, use the NavButton prop. This prop accepts a function that returns a component. The function receives an object with the following properties:

    • onClick: The function to handle navigation. Must be called for the buttons to work.
    • className: The className provided by the carousel (used for visibility/hover states). Apply this to your outermost element.
    • style: The style object provided by the carousel (includes navButtonsProps styles). Apply this to your outermost element.
    • next: Boolean indicating if this is the 'next' button.
    • prev: Boolean indicating if this is the 'prev' button.
    import { Button } from '@mui/material';
    
    <Carousel
        NavButton={({ onClick, className, style, next, prev }) => {
            return (
                <Button onClick={onClick} className={className} style={style}>
                    {next && "Next"}
                    {prev && "Previous"}
                </Button>
            );
        }}
    >
        {/* slides */}
    </Carousel>
  7. Customize Carousel indicators

    master

    Indicators (bullet points) can be customized using the following props:

    • IndicatorIcon: A ReactNode to display inside the indicator IconButtons.
    • indicatorContainerProps: Customizes the container/wrapper for the indicators. Accepts {className: string, style: React.CSSProperties}.
    • indicatorIconButtonProps: Customizes the non-active indicator buttons.
    • activeIndicatorIconButtonProps: Customizes the active indicator button.

    All indicator props use the CarouselNavProps interface, allowing for className and style overrides.

  8. Configure the Carousel component with CarouselProps

    master

    The CarouselProps interface defines all available configuration options for the Carousel component. You can control playback, animations, navigation visibility, and styling through these props.

    Key Configuration Categories:

    Playback & Animation

    • autoPlay: Boolean to enable/disable automatic scrolling.
    • interval: Number (ms) defining the time between slides during autoPlay.
    • stopAutoPlayOnHover: Boolean to pause auto-scrolling when the mouse is over the carousel.
    • animation: Set to 'fade' or 'slide' to define the transition style.
    • duration: Number defining the animation speed (in ms).
    • swipe: Boolean to enable/disable touch swiping on mobile devices.
    • indicators: Boolean to show/hide bullet indicators.
    • navButtonsAlwaysVisible: Boolean to keep navigation buttons visible at all times.
    • navButtonsAlwaysInvisible: Boolean to keep navigation buttons hidden at all times.
    • cycleNavigation: Boolean to allow the carousel to loop (next button visible on last slide, prev on first).
    • fullHeightHover: Boolean to make navigation button wrappers cover the full height of the item and show buttons on hover.

    Programmatic Control & Callbacks

    • index: Number to programmatically set the active child.
    • strictIndexing: Boolean determining if index can exceed the children length.
    • onChange: Callback function called after the active index changes. Signature: (now?: number, previous?: number) => any.
    • next: Callback function called after the next() method is triggered. Signature: (now?: number, previous?: number) => any.
    • prev: Callback function called after the prev() method is triggered. Signature: (now?: number, previous?: number) => any.
    • changeOnFirstRender: Boolean to trigger onChange on the initial mount.