Skip to content

Commit 7047186

Browse files
authored
Merge pull request #426 from ismail9k/feat-support-enable-disable-carousel
Feat support enable disable carousel
2 parents 4ccba59 + 0ff9998 commit 7047186

File tree

8 files changed

+99
-38
lines changed

8 files changed

+99
-38
lines changed

docs/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
| Prop | Default | Description |
66
| ---------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7+
| `enabled` | true | Controlled weather the carousel is enabled or disabled. <Badge text="0.8.0"/> |
78
| `itemsToShow` | 1 | Count of items to showed per view (can be a fraction). |
89
| `itemsToScroll` | 1 | Number of slides to be scrolled |
910
| `wrapAround` | false | Enable infinite scrolling mode. |

docs/examples/ExampleDisable.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<script setup>
2+
import { ref } from 'vue'
3+
import { Carousel, Pagination, Navigation, Slide } from '../../dist/carousel.es'
4+
import '../../dist/carousel.css'
5+
const enabled = ref(true)
6+
const config = { enabled }
7+
</script>
8+
9+
<template>
10+
<div>
11+
<span>Enabled: </span>
12+
<input id="status-checkbox" type="checkbox" v-model="enabled" />
13+
</div>
14+
15+
<Carousel v-bind="config">
16+
<Slide v-for="slide in 10" :key="slide">
17+
<div class="carousel__item">{{ slide }}</div>
18+
</Slide>
19+
<template #addons>
20+
<Navigation />
21+
<Pagination />
22+
</template>
23+
</Carousel>
24+
</template>
25+
26+
<style scoped>
27+
.carousel.is-disabled {
28+
width: 100%;
29+
display: flex;
30+
gap: 5px;
31+
height: 200px;
32+
}
33+
</style>

docs/test.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Test
22

3+
34
## Basic
45

56
<ExampleBasic />
@@ -24,6 +25,11 @@
2425

2526
<ExampleActiveClasses />
2627

28+
29+
## Disabled
30+
31+
<ExampleDisable />
32+
2733
## Custom Navigation
2834

2935
<ExampleCustomNavigation />
@@ -42,6 +48,7 @@ import ExampleActiveClasses from './examples/ExampleActiveClasses.vue';
4248
import ExampleCustomNavigation from './examples/ExampleCustomNavigation.vue';
4349
import ExampleGallery from './examples/ExampleGallery.vue';
4450
import ExampleVertical from './examples/ExampleVertical.vue';
51+
import ExampleDisable from './examples/ExampleDisable.vue';
4552

4653
export default {
4754
components: {
@@ -52,7 +59,8 @@ export default {
5259
ExampleActiveClasses,
5360
ExampleCustomNavigation,
5461
ExampleGallery,
55-
ExampleVertical
62+
ExampleVertical,
63+
ExampleDisable
5664
}
5765
}
5866
</script>

src/components/Carousel.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ export default defineComponent({
9090
provide('normalizeDir', normalizeDir)
9191

9292
function updateBreakpointsConfig(): void {
93-
if (!props.breakpoints) return
94-
9593
// Determine the width source based on the 'breakpointMode' config
9694
const widthSource =
9795
(config.breakpointMode === 'carousel'
@@ -391,10 +389,7 @@ export default defineComponent({
391389
}
392390

393391
// Update the carousel on props change
394-
Object.keys(carouselProps).forEach((prop) => {
395-
if (['modelValue'].includes(prop)) return
396-
watch(() => props[prop as keyof typeof carouselProps], restartCarousel)
397-
})
392+
watch(() => ({ ...props }), restartCarousel, { deep: true })
398393

399394
// Handle changing v-model value
400395
watch(
@@ -437,17 +432,58 @@ export default defineComponent({
437432
data,
438433
})
439434

435+
/**
436+
* Track style
437+
*/
438+
const trackTransform: ComputedRef<string> = computed(() => {
439+
// Calculate the scrolled index with wrapping offset if applicable
440+
const scrolledIndex = getScrolledIndex({
441+
config,
442+
currentSlide: currentSlideIndex.value,
443+
slidesCount: slidesCount.value,
444+
})
445+
446+
const cloneOffset = config.wrapAround ? slidesCount.value : 0
447+
448+
// Determine direction multiplier for orientation
449+
const isReverseDirection = ['rtl', 'btt'].includes(normalizeDir.value)
450+
const directionMultiplier = isReverseDirection ? -1 : 1
451+
452+
// Calculate the total offset for slide transformation
453+
const totalOffset =
454+
(scrolledIndex + cloneOffset) * effectiveSlideSize.value * directionMultiplier
455+
456+
// Include user drag interaction offset
457+
const dragOffset = isVertical.value ? dragged.y : dragged.x
458+
459+
// Generate the appropriate CSS transformation
460+
const translateAxis = isVertical.value ? 'Y' : 'X'
461+
return `translate${translateAxis}(${dragOffset - totalOffset}px)`
462+
})
463+
440464
const slotSlides = slots.default || slots.slides
441465
const slotAddons = slots.addons
442466
const slotsProps = reactive(data)
443467

444468
return () => {
469+
if (!config.enabled) {
470+
return h(
471+
'section',
472+
{
473+
ref: root,
474+
class: ['carousel', 'is-disabled'],
475+
},
476+
slotSlides?.()
477+
)
478+
}
479+
445480
const slidesElements = getSlidesVNodes(slotSlides?.(slotsProps))
446481
const addonsElements = slotAddons?.(slotsProps) || []
447482
slidesElements.forEach(
448483
(el: typeof SlideComponent, index: number) => (el.props.index = index)
449484
)
450485
let output = slidesElements
486+
451487
if (config.wrapAround) {
452488
const slidesBefore = slidesElements.map((el: VNode, index: number) =>
453489
cloneVNode(el, {
@@ -469,35 +505,6 @@ export default defineComponent({
469505
slides.value = slidesElements
470506
slidesCount.value = Math.max(slidesElements.length, 1)
471507

472-
/**
473-
* Track style
474-
*/
475-
const trackTransform: ComputedRef<string> = computed(() => {
476-
// Calculate the scrolled index with wrapping offset if applicable
477-
const scrolledIndex = getScrolledIndex({
478-
config,
479-
currentSlide: currentSlideIndex.value,
480-
slidesCount: slidesCount.value,
481-
})
482-
483-
const cloneOffset = config.wrapAround ? slidesCount.value : 0
484-
485-
// Determine direction multiplier for orientation
486-
const isReverseDirection = ['rtl', 'btt'].includes(normalizeDir.value)
487-
const directionMultiplier = isReverseDirection ? -1 : 1
488-
489-
// Calculate the total offset for slide transformation
490-
const totalOffset =
491-
(scrolledIndex + cloneOffset) * effectiveSlideSize.value * directionMultiplier
492-
493-
// Include user drag interaction offset
494-
const dragOffset = isVertical.value ? dragged.y : dragged.x
495-
496-
// Generate the appropriate CSS transformation
497-
const translateAxis = isVertical.value ? 'Y' : 'X'
498-
return `translate${translateAxis}(${dragOffset - totalOffset}px)`
499-
})
500-
501508
const trackEl = h(
502509
'ol',
503510
{

src/components/Slide.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ export default defineComponent({
5858
: { width: dimension, height: '' }
5959
})
6060

61-
return () =>
62-
h(
61+
return () => {
62+
if (!config.enabled) {
63+
return slots.default?.()
64+
}
65+
66+
return h(
6367
'li',
6468
{
6569
style: slideStyle.value,
@@ -83,5 +87,6 @@ export default defineComponent({
8387
isVisible: isVisible.value,
8488
})
8589
)
90+
}
8691
},
8792
})

src/partials/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const I18N_DEFAULT_CONFIG = {
2525
}
2626

2727
export const DEFAULT_CONFIG: CarouselConfig = {
28+
enabled: true,
2829
itemsToShow: 1,
2930
itemsToScroll: 1,
3031
modelValue: 0,

src/partials/props.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import {
66
} from '@/partials/defaults'
77

88
export const carouselProps = {
9+
// enable/disable the carousel component
10+
enabled: {
11+
default: DEFAULT_CONFIG.enabled,
12+
type: Boolean,
13+
},
914
// count of items to showed per view
1015
itemsToShow: {
1116
default: DEFAULT_CONFIG.itemsToShow,

src/types/carousel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type BreakpointMode = (typeof BREAKPOINT_MODE_OPTIONS)[number]
1515

1616
export type I18nKeys = keyof typeof I18N_DEFAULT_CONFIG
1717
export interface CarouselConfig {
18+
enabled: boolean
1819
itemsToShow: number
1920
itemsToScroll: number
2021
modelValue?: number

0 commit comments

Comments
 (0)