| 1 |
/** |
| 2 |
* Slider block constants and Swiper config helpers |
| 3 |
*/ |
| 4 |
|
| 5 |
import type { SwiperOptions } from "swiper/types"; |
| 6 |
|
| 7 |
import { getTransitionEffectConfig } from "./swiper-effects"; |
| 8 |
|
| 9 |
/** |
| 10 |
* Slider block attributes interface (mirrors block.json) |
| 11 |
*/ |
| 12 |
export interface SliderAttributes { |
| 13 |
align: string; |
| 14 |
type: string; |
| 15 |
autoplay: boolean; |
| 16 |
autoplaySpeed: number; |
| 17 |
pauseOnHover: boolean; |
| 18 |
transitionEffect: string; |
| 19 |
transitionDuration: number; |
| 20 |
transitionEasing: string; |
| 21 |
navigationType: string; |
| 22 |
navigationPlacement: string; |
| 23 |
navigationShape: string; |
| 24 |
navigationSize: string; |
| 25 |
navigationSizeValue: Record<string, string>; |
| 26 |
navigationColor: string; |
| 27 |
navigationBgColor: string; |
| 28 |
navigationOpacity: number; |
| 29 |
navigationVerticalPosition: number; |
| 30 |
navigationHorizontalPosition: number; |
| 31 |
navHorizontal: string; |
| 32 |
navVertical: string; |
| 33 |
navOutside: boolean; |
| 34 |
navFreePosition: boolean; |
| 35 |
navFreeX: number; |
| 36 |
navFreeY: number; |
| 37 |
prevFreeX: number; |
| 38 |
prevFreeY: number; |
| 39 |
nextFreeX: number; |
| 40 |
nextFreeY: number; |
| 41 |
dotsFreeX: number; |
| 42 |
dotsFreeY: number; |
| 43 |
prevArrowH: string; |
| 44 |
prevArrowV: string; |
| 45 |
nextArrowH: string; |
| 46 |
nextArrowV: string; |
| 47 |
dotsHorizontal: string; |
| 48 |
dotsVertical: string; |
| 49 |
dotColor: string; |
| 50 |
dotActiveColor: string; |
| 51 |
hideDots: boolean; |
| 52 |
hideNavigation: boolean; |
| 53 |
widthPreset: string; |
| 54 |
customWidth: string; |
| 55 |
widthUnit: string; |
| 56 |
/** "" = layout not yet picked (chooser UI), "slider", or "carousel". */ |
| 57 |
mode: string; |
| 58 |
slidesToShow: number; |
| 59 |
slidesToScroll: number; |
| 60 |
slideSpacing: number; |
| 61 |
infiniteLoop: boolean; |
| 62 |
tabletSlidesToShow: number; |
| 63 |
tabletSlidesToScroll: number; |
| 64 |
tabletSlideSpacing: number; |
| 65 |
mobileSlidesToShow: number; |
| 66 |
mobileSlidesToScroll: number; |
| 67 |
mobileSlideSpacing: number; |
| 68 |
minHeight: number; |
| 69 |
sliderDirection: string; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Navigation type options |
| 74 |
*/ |
| 75 |
export const NAVIGATION_TYPES = [ |
| 76 |
{ label: "Bottom", value: "bottom" }, |
| 77 |
{ label: "Top", value: "top" }, |
| 78 |
{ label: "Split", value: "split" }, |
| 79 |
]; |
| 80 |
|
| 81 |
/** |
| 82 |
* Navigation placement options |
| 83 |
*/ |
| 84 |
export const NAVIGATION_PLACEMENTS = [ |
| 85 |
{ label: "Overlay", value: "overlay" }, |
| 86 |
{ label: "Outside", value: "outside" }, |
| 87 |
]; |
| 88 |
|
| 89 |
/** |
| 90 |
* Navigation shape options |
| 91 |
*/ |
| 92 |
export const NAVIGATION_SHAPES = [ |
| 93 |
{ label: "Circle", value: "circle" }, |
| 94 |
{ label: "Square", value: "square" }, |
| 95 |
]; |
| 96 |
|
| 97 |
/** |
| 98 |
* Navigation size options |
| 99 |
*/ |
| 100 |
export const NAVIGATION_SIZES = [ |
| 101 |
{ label: "Small", value: "small" }, |
| 102 |
{ label: "Medium", value: "medium" }, |
| 103 |
{ label: "Large", value: "large" }, |
| 104 |
]; |
| 105 |
|
| 106 |
/** |
| 107 |
* Transition effect options |
| 108 |
*/ |
| 109 |
export const TRANSITION_EFFECTS = [ |
| 110 |
{ label: "Slide", value: "slide" }, |
| 111 |
{ label: "Fade", value: "fade" }, |
| 112 |
{ label: "Zoom", value: "zoom" }, |
| 113 |
]; |
| 114 |
|
| 115 |
/** |
| 116 |
* Transition easing options |
| 117 |
*/ |
| 118 |
export const TRANSITION_EASINGS = [ |
| 119 |
{ label: "Ease", value: "ease" }, |
| 120 |
{ label: "Ease In", value: "ease-in" }, |
| 121 |
{ label: "Ease Out", value: "ease-out" }, |
| 122 |
{ label: "Ease In Out", value: "ease-in-out" }, |
| 123 |
{ label: "Linear", value: "linear" }, |
| 124 |
]; |
| 125 |
|
| 126 |
/** |
| 127 |
* Width preset options |
| 128 |
*/ |
| 129 |
export const WIDTH_PRESETS = [ |
| 130 |
{ label: "Default", value: "default" }, |
| 131 |
{ label: "Full Width", value: "full" }, |
| 132 |
{ label: "Wide", value: "wide" }, |
| 133 |
{ label: "Custom", value: "custom" }, |
| 134 |
]; |
| 135 |
|
| 136 |
/** |
| 137 |
* Responsive breakpoints (matches Swiper breakpoints convention) |
| 138 |
*/ |
| 139 |
export const BREAKPOINTS = { |
| 140 |
MOBILE: 0, |
| 141 |
TABLET: 768, |
| 142 |
DESKTOP: 1024, |
| 143 |
}; |
| 144 |
|
| 145 |
/** |
| 146 |
* Build Swiper configuration from slider block attributes. |
| 147 |
* Used both in editor (React Swiper) and serialized to data-swiper-data on frontend. |
| 148 |
* @param attributes |
| 149 |
*/ |
| 150 |
export function buildSwiperConfig(attributes: SliderAttributes): SwiperOptions { |
| 151 |
const { |
| 152 |
transitionEffect, |
| 153 |
transitionDuration, |
| 154 |
autoplay, |
| 155 |
autoplaySpeed, |
| 156 |
pauseOnHover, |
| 157 |
infiniteLoop, |
| 158 |
mode, |
| 159 |
slidesToShow, |
| 160 |
slidesToScroll, |
| 161 |
slideSpacing, |
| 162 |
hideNavigation, |
| 163 |
hideDots, |
| 164 |
tabletSlidesToShow, |
| 165 |
tabletSlidesToScroll, |
| 166 |
tabletSlideSpacing, |
| 167 |
mobileSlidesToShow, |
| 168 |
mobileSlidesToScroll, |
| 169 |
mobileSlideSpacing, |
| 170 |
sliderDirection, |
| 171 |
} = attributes; |
| 172 |
|
| 173 |
const isCarouselMode = mode === "carousel"; |
| 174 |
|
| 175 |
const config: SwiperOptions = { |
| 176 |
...getTransitionEffectConfig(transitionEffect, isCarouselMode), |
| 177 |
speed: transitionDuration, |
| 178 |
loop: infiniteLoop, |
| 179 |
slidesPerView: isCarouselMode ? slidesToShow : 1, |
| 180 |
slidesPerGroup: isCarouselMode ? slidesToScroll : 1, |
| 181 |
spaceBetween: isCarouselMode ? slideSpacing : 0, |
| 182 |
cssMode: false, |
| 183 |
direction: (sliderDirection === "vertical" ? "vertical" : "horizontal") as |
| 184 |
| "horizontal" |
| 185 |
| "vertical", |
| 186 |
}; |
| 187 |
|
| 188 |
// Autoplay |
| 189 |
if (autoplay) { |
| 190 |
config.autoplay = { |
| 191 |
delay: autoplaySpeed, |
| 192 |
disableOnInteraction: false, |
| 193 |
pauseOnMouseEnter: pauseOnHover, |
| 194 |
}; |
| 195 |
} |
| 196 |
|
| 197 |
// Navigation |
| 198 |
if (!hideNavigation) { |
| 199 |
config.navigation = { |
| 200 |
nextEl: ".swiper-button-next", |
| 201 |
prevEl: ".swiper-button-prev", |
| 202 |
}; |
| 203 |
} |
| 204 |
|
| 205 |
// Pagination |
| 206 |
if (!hideDots) { |
| 207 |
config.pagination = { |
| 208 |
el: ".swiper-pagination", |
| 209 |
clickable: true, |
| 210 |
}; |
| 211 |
} |
| 212 |
|
| 213 |
// Responsive breakpoints (only in carousel mode) |
| 214 |
if (isCarouselMode) { |
| 215 |
config.breakpoints = { |
| 216 |
[BREAKPOINTS.MOBILE]: { |
| 217 |
slidesPerView: mobileSlidesToShow, |
| 218 |
slidesPerGroup: mobileSlidesToScroll, |
| 219 |
spaceBetween: mobileSlideSpacing, |
| 220 |
}, |
| 221 |
[BREAKPOINTS.TABLET]: { |
| 222 |
slidesPerView: tabletSlidesToShow, |
| 223 |
slidesPerGroup: tabletSlidesToScroll, |
| 224 |
spaceBetween: tabletSlideSpacing, |
| 225 |
}, |
| 226 |
[BREAKPOINTS.DESKTOP]: { |
| 227 |
slidesPerView: slidesToShow, |
| 228 |
slidesPerGroup: slidesToScroll, |
| 229 |
spaceBetween: slideSpacing, |
| 230 |
}, |
| 231 |
}; |
| 232 |
} |
| 233 |
|
| 234 |
// A11y |
| 235 |
config.a11y = { |
| 236 |
prevSlideMessage: "Previous slide", |
| 237 |
nextSlideMessage: "Next slide", |
| 238 |
firstSlideMessage: "This is the first slide", |
| 239 |
lastSlideMessage: "This is the last slide", |
| 240 |
}; |
| 241 |
|
| 242 |
return config; |
| 243 |
} |
| 244 |
|