This is a Carousel library built using Chakra UI. You can pass all the chakra props to the Carousel buttons as well as you can place it wherver you want inside the <Provider>
wrapper.
You can view the demo live here
$ yarn add @chakra-ui/react @emotion/react @emotion/styled @chakra-ui/icons framer-motion chakra-ui-carousel
# or
$ npm i @chakra-ui/react @emotion/react @emotion/styled @chakra-ui/icons framer-motion chakra-ui-carousel
After installing above libraries, you need to set up the ChakraProvider at the root of your application. This can be either in your index.jsx
, index.tsx
or App.jsx
depending on the framework you use.
import * as React from "react";
// 1. import `ChakraProvider` component
import { ChakraProvider } from "@chakra-ui/react";
function App() {
// 2. Wrap ChakraProvider at the root of your app
return (
<ChakraProvider>
<TheRestOfYourApplication />
</ChakraProvider>
);
}
To use the Carousel, please follow the steps below:
- Wrap your carousel content with the
<Provider>
component.
import { Provider } from "chakra-ui-carousel";
function Example() {
return (
<Box>
<Provider>// Carousel content</Provider>
</Box>
);
}
- Move your Carousel contents to the
Carousel
component imported fromchakra-ui-carousel
library. You need to pass the gap between the slides as a prop.
import { Carousel } from "chakra-ui-carousel";
function Example() {
return (
<Box>
<Provider>
<Carousel gap={50}>// Carousel content</Carousel>
</Provider>
</Box>
);
}
- Add
LeftButton
andRightButton
components to your Carousel to control the current element on the Carousel. You can display these buttons anywhere around your carousel, just make sure that these buttons should be in theProvider
Wrapper. You can also passgap
as a props to theCarousel
component to control the gap between the individual elements.
import { LeftButton, RightButton } from "chakra-ui-carousel";
function Example() {
return (
<Box>
<Provider>
<Carousel gap={50}>// Carousel content</Carousel>
<LeftButton />
<RightButton />
</Provider>
</Box>
);
}
- You can also pass all the props that are available in the
Button
component of Chakra UI to theLeftButton
andRightButton
component.
import { LeftButton, RightButton } from "chakra-ui-carousel";
function Example() {
return (
<Box>
<Provider>
<Carousel gap={50}>// Carousel content</Carousel>
<LeftButton bgColor="red.500" textColor="white" />
<RightButton bgColor="blue.500" />
</Provider>
</Box>
);
}
- You can pass custom button components to the
LeftButton
andRightButton
component.
import { LeftButton, RightButton } from "chakra-ui-carousel";
function Example() {
return (
<Box>
<Provider>
<Carousel gap={50}>// Carousel content</Carousel>
<LeftButton
bgColor="red.500"
customIcon={<ArrowLeftIcon />}
textColor={"white.500"}
/>
<RightButton bgColor="blue.500" customIcon={<ArrowRightIcon />} />
</Provider>
</Box>
);
}