add slider

This commit is contained in:
Husni Ailatat 2025-12-06 09:22:01 +07:00
parent 1cdae70022
commit 60d701ec00
7 changed files with 199 additions and 9 deletions

View File

@ -12,6 +12,8 @@
"@ant-design/cssinjs": "^2.0.1",
"antd": "^6.0.1",
"clsx": "^2.1.1",
"embla-carousel": "^8.6.0",
"embla-carousel-react": "^8.6.0",
"lucide-react": "^0.556.0",
"next": "16.0.7",
"react": "19.2.0",

31
pnpm-lock.yaml generated
View File

@ -17,6 +17,12 @@ importers:
clsx:
specifier: ^2.1.1
version: 2.1.1
embla-carousel:
specifier: ^8.6.0
version: 8.6.0
embla-carousel-react:
specifier: ^8.6.0
version: 8.6.0(react@19.2.0)
lucide-react:
specifier: ^0.556.0
version: 0.556.0(react@19.2.0)
@ -1249,6 +1255,19 @@ packages:
electron-to-chromium@1.5.266:
resolution: {integrity: sha512-kgWEglXvkEfMH7rxP5OSZZwnaDWT7J9EoZCujhnpLbfi0bbNtRkgdX2E3gt0Uer11c61qCYktB3hwkAS325sJg==}
embla-carousel-react@8.6.0:
resolution: {integrity: sha512-0/PjqU7geVmo6F734pmPqpyHqiM99olvyecY7zdweCw+6tKEXnrE90pBiBbMMU8s5tICemzpQ3hi5EpxzGW+JA==}
peerDependencies:
react: ^16.8.0 || ^17.0.1 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc
embla-carousel-reactive-utils@8.6.0:
resolution: {integrity: sha512-fMVUDUEx0/uIEDM0Mz3dHznDhfX+znCCDCeIophYb1QGVM7YThSWX+wz11zlYwWFOr74b4QLGg0hrGPJeG2s4A==}
peerDependencies:
embla-carousel: 8.6.0
embla-carousel@8.6.0:
resolution: {integrity: sha512-SjWyZBHJPbqxHOzckOfo8lHisEaJWmwd23XppYFYVh10bU66/Pn5tkVkbkCMZVdbUE5eTCI2nD8OyIP4Z+uwkA==}
emoji-regex@9.2.2:
resolution: {integrity: sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==}
@ -3638,6 +3657,18 @@ snapshots:
electron-to-chromium@1.5.266: {}
embla-carousel-react@8.6.0(react@19.2.0):
dependencies:
embla-carousel: 8.6.0
embla-carousel-reactive-utils: 8.6.0(embla-carousel@8.6.0)
react: 19.2.0
embla-carousel-reactive-utils@8.6.0(embla-carousel@8.6.0):
dependencies:
embla-carousel: 8.6.0
embla-carousel@8.6.0: {}
emoji-regex@9.2.2: {}
enhanced-resolve@5.18.3:

View File

@ -55,7 +55,7 @@ const PublicLayout: React.FC<PropsWithChildren> = ({ children }) => {
</div>
</div>
</nav>
<div className="md:mt-[74px] mt-[56px]">
<div>
{children}
</div>
</Box>

View File

@ -0,0 +1,62 @@
import React, {
ComponentPropsWithRef,
useCallback,
useEffect,
useState
} from 'react'
import { EmblaCarouselType } from 'embla-carousel'
type UseDotButtonType = {
selectedIndex: number
scrollSnaps: number[]
onDotButtonClick: (index: number) => void
}
export const useDotButton = (
emblaApi: EmblaCarouselType | undefined
): UseDotButtonType => {
const [selectedIndex, setSelectedIndex] = useState(0)
const [scrollSnaps, setScrollSnaps] = useState<number[]>([])
const onDotButtonClick = useCallback(
(index: number) => {
if (!emblaApi) return
emblaApi.scrollTo(index)
},
[emblaApi]
)
const onInit = useCallback((emblaApi: EmblaCarouselType) => {
setScrollSnaps(emblaApi.scrollSnapList())
}, [])
const onSelect = useCallback((emblaApi: EmblaCarouselType) => {
setSelectedIndex(emblaApi.selectedScrollSnap())
}, [])
useEffect(() => {
if (!emblaApi) return
onInit(emblaApi)
onSelect(emblaApi)
emblaApi.on('reInit', onInit).on('reInit', onSelect).on('select', onSelect)
}, [emblaApi, onInit, onSelect])
return {
selectedIndex,
scrollSnaps,
onDotButtonClick
}
}
type PropType = ComponentPropsWithRef<'button'>
export const DotButton: React.FC<PropType> = (props) => {
const { children, ...restProps } = props
return (
<button type="button" {...restProps}>
{children}
</button>
)
}

View File

@ -0,0 +1,92 @@
import React, {
ComponentPropsWithRef,
useCallback,
useEffect,
useState
} from 'react'
import { EmblaCarouselType } from 'embla-carousel'
type UsePrevNextButtonsType = {
prevBtnDisabled: boolean
nextBtnDisabled: boolean
onPrevButtonClick: () => void
onNextButtonClick: () => void
}
export const usePrevNextButtons = (
emblaApi: EmblaCarouselType | undefined
): UsePrevNextButtonsType => {
const [prevBtnDisabled, setPrevBtnDisabled] = useState(true)
const [nextBtnDisabled, setNextBtnDisabled] = useState(true)
const onPrevButtonClick = useCallback(() => {
if (!emblaApi) return
emblaApi.scrollPrev()
}, [emblaApi])
const onNextButtonClick = useCallback(() => {
if (!emblaApi) return
emblaApi.scrollNext()
}, [emblaApi])
const onSelect = useCallback((emblaApi: EmblaCarouselType) => {
setPrevBtnDisabled(!emblaApi.canScrollPrev())
setNextBtnDisabled(!emblaApi.canScrollNext())
}, [])
useEffect(() => {
if (!emblaApi) return
onSelect(emblaApi)
emblaApi.on('reInit', onSelect).on('select', onSelect)
}, [emblaApi, onSelect])
return {
prevBtnDisabled,
nextBtnDisabled,
onPrevButtonClick,
onNextButtonClick
}
}
type PropType = ComponentPropsWithRef<'button'>
export const PrevButton: React.FC<PropType> = (props) => {
const { children, ...restProps } = props
return (
<button
className="embla__button embla__button--prev"
type="button"
{...restProps}
>
<svg className="embla__button__svg" viewBox="0 0 532 532">
<path
fill="currentColor"
d="M355.66 11.354c13.793-13.805 36.208-13.805 50.001 0 13.785 13.804 13.785 36.238 0 50.034L201.22 266l204.442 204.61c13.785 13.805 13.785 36.239 0 50.044-13.793 13.796-36.208 13.796-50.002 0a5994246.277 5994246.277 0 0 0-229.332-229.454 35.065 35.065 0 0 1-10.326-25.126c0-9.2 3.393-18.26 10.326-25.2C172.192 194.973 332.731 34.31 355.66 11.354Z"
/>
</svg>
{children}
</button>
)
}
export const NextButton: React.FC<PropType> = (props) => {
const { children, ...restProps } = props
return (
<button
className="embla__button embla__button--next"
type="button"
{...restProps}
>
<svg className="embla__button__svg" viewBox="0 0 532 532">
<path
fill="currentColor"
d="M176.34 520.646c-13.793 13.805-36.208 13.805-50.001 0-13.785-13.804-13.785-36.238 0-50.034L330.78 266 126.34 61.391c-13.785-13.805-13.785-36.239 0-50.044 13.793-13.796 36.208-13.796 50.002 0 22.928 22.947 206.395 206.507 229.332 229.454a35.065 35.065 0 0 1 10.326 25.126c0 9.2-3.393 18.26-10.326 25.2-45.865 45.901-206.404 206.564-229.332 229.52Z"
/>
</svg>
{children}
</button>
)
}

View File

@ -33,23 +33,25 @@ export default function HomeBanner() {
if (!isClient) return;
return (
<div className='flex flex-col relative'>
<div className='flex flex-col relative mt-5'>
<Slider
ref={sliderRef}
className='center'
centerMode
infinite
centerPadding='60px'
beforeChange={(current, next) => setActiveIndex(next)}
slidesToShow={3}
slidesToScroll={1}
speed={500}
>
{banners.map((item, index) => (
<Fragment key={index}>
<img
src={item}
className='w-full h-[400px] object-cover'
/>
<div className='mx-3'>
<img
src={item}
className='w-full md:h-[300px] h-[200px] object-cover rounded-2xl'
/>
</div>
</Fragment>
))}
</Slider>

View File

@ -1,10 +1,11 @@
import { Box } from "lucide-react";
import dynamic from "next/dynamic";
import HomeBanner from "./HomeBanner";
import Box from "@/Components/Atoms/Box";
export default function Home(){
return (
<Box className="flex flex-col gap-6">
Home
<HomeBanner/>
</Box>
)
}