Skip to content

Commit b23656d

Browse files
committed
fix(carousel): first auto play should be triggered in mount instead of in intro end event
1 parent 49db0f0 commit b23656d

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

.changeset/tiny-poems-switch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@casual-ui/svelte': patch
3+
---
4+
5+
fix(carousel): first auto play should be triggered in mount instead of in intro end event
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script lang="ts">
2+
import { CCarousel, CCarouselSlider } from '@casual-ui/svelte'
3+
4+
let activeIndex = 0
5+
</script>
6+
7+
<CCarousel bind:activeIndex interval={5000} infinity arrowTiming="hover">
8+
<CCarouselSlider>
9+
<div class="item">
10+
<div class="icon i-openmoji-red-apple" />
11+
Do you want some apples?
12+
</div>
13+
</CCarouselSlider>
14+
<CCarouselSlider>
15+
<div class="item">
16+
<div class="icon i-openmoji-banana" />
17+
Would you like some bananas?
18+
</div>
19+
</CCarouselSlider>
20+
<CCarouselSlider>
21+
<div class="item">
22+
<div class="icon i-openmoji-grapes" />
23+
How about some grapes?
24+
</div>
25+
</CCarouselSlider>
26+
</CCarousel>
27+
28+
<style>
29+
.item {
30+
font-size: 32px;
31+
height: 100%;
32+
display: flex;
33+
align-items: center;
34+
justify-content: center;
35+
flex-direction: column;
36+
}
37+
.item .icon {
38+
font-size: 100px;
39+
}
40+
</style>

packages/ui/src/components/carousel/CCarousel.svelte

+13-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
export const hoveringKey = Symbol('c-carousel-hovering')
4949
5050
/**
51-
* The slidering flag key.
51+
* The sliding flag key.
5252
* @type {symbol}
5353
*/
54-
export const slideringKey = Symbol('c-carousel-slidering')
54+
export const slidingKey = Symbol('c-carousel-slidering')
5555
5656
/**
5757
* The pause functions key.
@@ -67,7 +67,7 @@
6767
</script>
6868

6969
<script>
70-
import { setContext } from 'svelte'
70+
import { onMount, setContext } from 'svelte'
7171
import { cubicInOut } from 'svelte/easing'
7272
import { writable } from 'svelte/store'
7373
import { fade } from 'svelte/transition'
@@ -220,7 +220,7 @@
220220
const sliding = writable(false)
221221
222222
setContext(hoveringKey, hovering)
223-
setContext(slideringKey, sliding)
223+
setContext(slidingKey, sliding)
224224
225225
$: paused = ($hovering && pauseOnHover) || $sliding
226226
@@ -253,6 +253,15 @@
253253
$verticalContext = vertical
254254
}
255255
256+
let initialized = false
257+
258+
onMount(() => {
259+
if (!initialized && interval) {
260+
initialized = true
261+
timeoutFlag.set(setTimeout(toNext, interval))
262+
}
263+
})
264+
256265
export { toPrev, toNext, toIndex }
257266
</script>
258267

packages/ui/src/components/carousel/CCarouselSlider.svelte

+9-10
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
intervalKey,
99
pausesKey,
1010
resumesKey,
11-
slideringKey,
1211
slidesKey,
12+
slidingKey,
1313
timeoutKey,
1414
toNextKey,
1515
verticalKey,
@@ -23,7 +23,7 @@
2323
const toNext = getContext(toNextKey)
2424
const timeoutFlag = getContext(timeoutKey)
2525
const hovering = getContext(hoveringKey)
26-
const slidering = getContext(slideringKey)
26+
const sliding = getContext(slidingKey)
2727
2828
const currentIndex = $slides.length
2929
@@ -34,7 +34,7 @@
3434
* @param {*} node
3535
* @param {*} params
3636
*/
37-
const carousel = (node, params) => {
37+
const carousel = (_node, params) => {
3838
const { leave } = params
3939
return {
4040
duration: 300,
@@ -62,18 +62,17 @@
6262
let remain = $interval
6363
6464
const onIntroStart = () => {
65-
$slidering = true
66-
remain = $interval
65+
$sliding = true
6766
}
6867
69-
const onIntroEnd = () => {
70-
$slidering = false
68+
const onIntroEnd = async () => {
69+
$sliding = false
7170
remain = $interval
7271
if ($interval && toNext && !$hovering) {
7372
start = Date.now()
7473
if ($timeoutFlag) clearTimeout($timeoutFlag)
7574
76-
$timeoutFlag = setTimeout(toNext, remain)
75+
$timeoutFlag = setTimeout(toNext, $interval)
7776
}
7877
}
7978
@@ -95,7 +94,7 @@
9594
}
9695
}
9796
98-
if (pauses && Array.isArray(pauses)) pauses.push(pause)
97+
if (Array.isArray(pauses)) pauses.push(pause)
9998
10099
const resume = () => {
101100
if ($activeIndex !== currentIndex) return
@@ -105,7 +104,7 @@
105104
$timeoutFlag = setTimeout(toNext, remain)
106105
}
107106
108-
if (resumes && Array.isArray(resumes)) resumes.push(resume)
107+
if (Array.isArray(resumes)) resumes.push(resume)
109108
110109
onDestroy(() => {
111110
if ($timeoutFlag) clearTimeout($timeoutFlag)

0 commit comments

Comments
 (0)