Skip to content

Commit bd27a03

Browse files
committed
refactor: navigationPlacement variants
Swiper React manipulates DOM limiting ability to allow Swiper elements to sit in-flow with the custom placement of a SwiperNavigation component (this component will be forced to the end of a `swiper-wrapper` element. Known limitation of Swiper React that this wrapper cannot be manipulated with `wrapperClass`. Alternatively, navigationPlacement variants added to at least enable positioning at bottom and top (default bottom)
1 parent 140ec03 commit bd27a03

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/components/ui/swiper.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from "react"
2+
import { cva, VariantProps } from "class-variance-authority"
23
import { useTranslation } from "next-i18next"
34
import { EffectCards, Keyboard, Navigation, Pagination } from "swiper/modules"
45
import {
@@ -91,7 +92,7 @@ const SwiperNavContainer = React.forwardRef<
9192
<div
9293
ref={ref}
9394
className={cn(
94-
"mx-auto mt-6 flex h-6 w-fit items-center gap-4 rounded-full bg-background-highlight",
95+
"mx-auto flex h-6 w-fit items-center gap-4 rounded-full bg-background-highlight",
9596
className
9697
)}
9798
{...props}
@@ -111,8 +112,21 @@ const SwiperNavigation = React.forwardRef<
111112
))
112113
SwiperNavigation.displayName = "SwiperNavigation"
113114

114-
const Swiper = React.forwardRef<SwiperRef, SwiperReactProps>(
115-
({ children, ...props }, ref) => {
115+
const variants = cva("!flex gap-y-6", {
116+
variants: {
117+
navigationPlacement: {
118+
top: "flex-col-reverse",
119+
bottom: "flex-col",
120+
},
121+
},
122+
defaultVariants: {
123+
navigationPlacement: "bottom",
124+
},
125+
})
126+
127+
export type SwiperProps = SwiperReactProps & VariantProps<typeof variants>
128+
const Swiper = React.forwardRef<SwiperRef, SwiperProps>(
129+
({ className, children, navigationPlacement, ...props }, ref) => {
116130
const { t } = useTranslation("common")
117131
return (
118132
<SwiperReact
@@ -135,6 +149,7 @@ const Swiper = React.forwardRef<SwiperRef, SwiperReactProps>(
135149
slidesPerGroup={1}
136150
lazyPreloadPrevNext={0}
137151
slideClass="swiper-slide"
152+
className={cn(variants({ navigationPlacement, className }))}
138153
{...props}
139154
>
140155
{children}

0 commit comments

Comments
 (0)