Skip to content

Commit 1d410bc

Browse files
committed
docs(useIntersection): Add useIntersection demo
1 parent 52f9438 commit 1d410bc

File tree

16 files changed

+137
-41
lines changed

16 files changed

+137
-41
lines changed

src/components/useClickAway/stories/useClickAway.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Vue function that triggers a callback when the user clicks outside of the target
77
```typescript
88
useClickAway(
99
elRef: Ref<null | Element>,
10-
onClickAway: (e: Event) => void,
10+
callback: (e: Event) => void,
1111
events?: string[]
1212
): void;
1313
```
1414

1515
### Parameters
1616

1717
- `elRef: Ref<null | Element>` the element to check for click away events
18-
- `onClickAway: Function` the callback to run when triggering a click away
18+
- `callback: Function` the callback to run when triggering a click away
1919
- `events: string[]` list of events to listen to, defaults to `['mousedown', 'touchstart']`
2020

2121
## Usage

src/components/useClickAway/useClickAway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const defaultEvents = ['mousedown', 'touchstart']
44

55
export function useClickAway(
66
elRef: Ref<null | Element>,
7-
onClickAway: (e: Event) => void,
7+
callback: (e: Event) => void,
88
events = defaultEvents
99
) {
1010
const handler = (e: Event) => {
1111
if (elRef.value && !elRef.value.contains(e.target as Node)) {
12-
onClickAway(e)
12+
callback(e)
1313
}
1414
}
1515

src/components/useHover/stories/UseHoverDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default Vue.extend({
2222
})
2323
</script>
2424

25-
<style>
25+
<style scoped>
2626
.use-hover {
2727
cursor: default;
2828
display: flex;
Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<template>
22
<div class="section">
3-
<div class="intersection-adv" v-for="el in divElements">
3+
<div class="intersection" v-for="(item, index) in divElements">
44
<use-intersection-element-demo
5-
class="intersection-adv__el"
5+
class="intersection__el"
66
:options="intersectionOpts"
77
@change="handleIntersectionChange"
88
>
99
<video controls loop>
10-
<source src=http://techslides.com/demos/sample-videos/small.mp4
11-
type=video/mp4>
10+
<source :src="getAlternateVideoUrl(index)" type="video/mp4" />
1211
</video>
1312
</use-intersection-element-demo>
1413
</div>
@@ -19,6 +18,16 @@
1918
import Vue from 'vue'
2019
import UseIntersectionElementDemo from './UseIntersectionElementDemo.vue'
2120
21+
const getAlternateVideoUrl = (n: number) => {
22+
const videoNumber = n % 2 === 0 ? 1 : 2
23+
return `/demo/video${videoNumber}.mp4`
24+
}
25+
26+
const setVideoPausedValue = ($el: Element, val: string) =>
27+
$el.setAttribute('data-is-paused', val)
28+
29+
const divElements = new Array(10)
30+
2231
export default Vue.extend({
2332
name: 'UseIntersectionDemo',
2433
components: { UseIntersectionElementDemo },
@@ -28,8 +37,6 @@ export default Vue.extend({
2837
threshold: 1
2938
}
3039
31-
const divElements = new Array(100)
32-
3340
const handleIntersectionChange = ({
3441
target,
3542
intersectionRatio
@@ -38,32 +45,43 @@ export default Vue.extend({
3845
const $video = target.querySelector('video')
3946
if (!$video) return
4047
48+
// Video pause logic
4149
if (!isVisible) {
42-
if (!$video.paused) $video.pause()
43-
} else {
44-
if ($video.paused) $video.play()
50+
if ($video.paused) return
51+
$video.pause()
52+
setVideoPausedValue($video, '1')
53+
}
54+
55+
// Video play logic
56+
if (isVisible && $video.dataset.isPaused === '1'){
57+
$video.play()
58+
setVideoPausedValue($video, '0')
4559
}
4660
}
4761
48-
return { divElements, intersectionOpts, handleIntersectionChange }
62+
return {
63+
divElements,
64+
intersectionOpts,
65+
getAlternateVideoUrl,
66+
handleIntersectionChange
67+
}
4968
}
5069
})
5170
</script>
5271

53-
<style>
72+
<style scoped>
5473
.section {
5574
padding: 20px 0;
5675
}
5776
58-
.intersection-adv {
77+
.intersection {
5978
display: flex;
60-
align-items: center;
79+
align-items: flex-start;
6180
justify-content: center;
6281
height: 920px;
6382
}
6483
65-
/* El */
66-
.intersection-adv__el {
84+
.intersection__el {
6785
position: relative;
6886
}
6987
</style>

src/components/useIntersection/stories/UseIntersectionDemo.vue

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class="intersection__el"
66
:options="intersectionOpts"
77
@change="handleIntersectionChange"
8+
@paused="handleIntersectionPaused"
89
/>
910
</div>
1011
</div>
@@ -34,12 +35,21 @@ export default Vue.extend({
3435
target.classList.toggle('-is-active', isVisible)
3536
}
3637
37-
return { divElements, intersectionOpts, handleIntersectionChange }
38+
const handleIntersectionPaused = (target: Element, isPaused: boolean) => {
39+
target.classList.toggle('-is-paused', isPaused)
40+
}
41+
42+
return {
43+
divElements,
44+
intersectionOpts,
45+
handleIntersectionChange,
46+
handleIntersectionPaused
47+
}
3848
}
3949
})
4050
</script>
4151

42-
<style>
52+
<style scoped>
4353
.section {
4454
padding: 20px 0;
4555
}
@@ -58,29 +68,41 @@ export default Vue.extend({
5868
height: 80px;
5969
}
6070
71+
/* Action buttons */
72+
/deep/ .intersection__el .button {
73+
position: absolute;
74+
top: 50%;
75+
left: 130px;
76+
transform: translateY(-50%);
77+
}
78+
6179
/* Circle */
6280
.intersection__el:after {
63-
content: 'OFF';
81+
content: 'Not intersecting';
6482
display: flex;
6583
justify-content: center;
6684
align-items: center;
6785
position: absolute;
6886
top: 50%;
6987
left: 50%;
70-
width: 40px;
88+
width: 100px;
7189
height: 40px;
72-
border-radius: 50%;
90+
border-radius: 8px;
7391
background: red;
7492
color: white;
75-
text-transform: uppercase;
7693
font-size: 12px;
7794
transition: all 0.7s ease-in-out;
7895
transform: translate(-50%, -50%) scale(1);
7996
}
8097
8198
.intersection__el.-is-active:after {
82-
content: 'ON';
99+
content: 'Intersecting';
83100
background: green;
84101
transform: translate(-50%, -50%) scale(1.4);
85102
}
103+
104+
.intersection__el.-is-paused:after {
105+
content: 'Disabled';
106+
background: orange;
107+
}
86108
</style>

src/components/useIntersection/stories/UseIntersectionElementDemo.vue

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
<template>
22
<div ref="elRef">
33
<slot></slot>
4+
<button
5+
class="button is-primary"
6+
@click="handleObserverState"
7+
v-if="!isObserving"
8+
title="Start observing"
9+
>
10+
<span class="icon"><i class="fas fa-play"></i></span>
11+
</button>
12+
<button
13+
class="button is-warning"
14+
@click="handleObserverState"
15+
v-else
16+
title="Stop observing"
17+
>
18+
<span class="icon"><i class="fas fa-pause"></i></span>
19+
</button>
420
</div>
521
</template>
622

@@ -21,16 +37,32 @@ export default Vue.extend({
2137
},
2238
setup({ options }, { emit }) {
2339
const elRef = ref(null)
24-
const { observedEntry } = useIntersection(
40+
41+
const { observedEntry, start, stop } = useIntersection(
2542
elRef,
2643
options as IntersectionObserverInit
2744
)
45+
2846
watch(() => {
2947
if (!observedEntry.value) return
3048
emit('change', observedEntry.value)
3149
})
3250
33-
return { elRef }
51+
let isObserving = ref(true)
52+
const handleObserverState = () => {
53+
if (!observedEntry.value) return
54+
if (isObserving.value) {
55+
stop()
56+
emit('paused', observedEntry.value.target, true)
57+
isObserving.value = false
58+
} else {
59+
start()
60+
emit('paused', observedEntry.value.target, false)
61+
isObserving.value = true
62+
}
63+
}
64+
65+
return { handleObserverState, isObserving, elRef }
3466
}
3567
})
3668
</script>

src/components/useIntersection/stories/useIntersection.story.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ const basicDemo = () => ({
1818
demo-name="UseIntersectionDemo.vue"
1919
>
2020
<template v-slot:title></template>
21-
<template v-slot:intro></template>
21+
<template v-slot:intro>
22+
<p>
23+
<strong>Try to scroll down</strong> in this page to see the intersection observer
24+
changing the element's state when they are intersecting.
25+
</p>
26+
<p>
27+
<strong>Try pressing the pause toggler</strong> to enable/disable the intersection observer.
28+
</p>
29+
</template>
2230
</story-title>
2331
<demo />
2432
</div>`
@@ -34,7 +42,15 @@ const advancedDemo = () => ({
3442
demo-name="UseIntersectionAdvancedDemo.vue"
3543
>
3644
<template v-slot:title>Advanced Demo</template>
37-
<template v-slot:intro></template>
45+
<template v-slot:intro>
46+
<p>
47+
<strong>Start playing a video</strong> then try to scroll up/down. Whenever the video
48+
is not 100% visible, it will pause automatically.
49+
</p>
50+
<p>
51+
<strong>Try pressing the pause toggler</strong> to enable/disable the intersection observer.
52+
</p>
53+
</template>
3854
</story-title>
3955
<demo />
4056
</div>`

src/components/useMedia/stories/UseMediaAdvancedDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default Vue.extend({
7878
})
7979
</script>
8080

81-
<style>
81+
<style scoped>
8282
.query-valid {
8383
color: #fff;
8484
background: #209817;

src/components/useMedia/stories/UseMediaDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default Vue.extend({
3333
})
3434
</script>
3535

36-
<style>
36+
<style scoped>
3737
.query-valid {
3838
color: #fff;
3939
background: #209817;

src/components/useMedia/stories/useMedia.story.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const basicDemo = () => ({
1616
<template v-slot:title></template>
1717
<template v-slot:intro>
1818
<p>
19-
Try to resize the browser's window to see how the value below changes.
19+
<strong>Try to resize the browser's window</strong> to see how the value below changes.
2020
</p>
2121
</template>
2222
</story-title>
@@ -32,7 +32,7 @@ const advancedDemo = () => ({
3232
<template v-slot:title>Advanced demo</template>
3333
<template v-slot:intro>
3434
<p>
35-
Try to resize the browser's window to see how the values below change.
35+
<strong>Try to resize the browser's window</strong> to see how the values below change.
3636
</p>
3737
</template>
3838
</story-title>

0 commit comments

Comments
 (0)