Skip to content

Commit 0254e9e

Browse files
committed
feat(useFullscreen): Add useFullscreen function
1 parent 788ddba commit 0254e9e

File tree

33 files changed

+267
-99
lines changed

33 files changed

+267
-99
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Vue.use(VueCompositionAPI);
8989
- UI
9090
- [`useClickAway`](./src/components/useClickAway/stories/useClickAway.md) — triggers callback when user clicks outside target area.
9191
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/ui-useclickaway--demo)
92+
- [`useFullscreen`](./src/components/useFullscreen/stories/useFullscreen.md) — display an element in full-screen mode
93+
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/ui-usefullscreen--demo)
9294
- Utils
9395
- [`getQuery`](./src/components/getQuery/stories/getQuery.md) — get a CSS media query string.
9496
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/utils-getquery--demo)

src/components/getQuery/stories/GetQueryDemo.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@
99
<tbody>
1010
<tr>
1111
<td>getQuery(0, 300)</td>
12-
<td>
13-
<span>{{ mobileQuery }}</span>
14-
</td>
12+
<td>{{ mobileQuery }}</td>
1513
</tr>
1614
<tr>
1715
<td>getQuery(300, 1024)</td>
18-
<td>
19-
<span>{{ tabletQuery }}</span>
20-
</td>
16+
<td>{{ tabletQuery }}</td>
2117
</tr>
2218
<tr>
2319
<td>getQuery(1024)</td>
24-
<td>
25-
<span>{{ desktopQuery }}</span>
26-
</td>
20+
<td>{{ desktopQuery }}</td>
2721
</tr>
2822
</tbody>
2923
</table>

src/components/useClickAway/stories/useClickAway.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ Vue function that triggers a callback when the user clicks outside of the target
66

77
```typescript
88
useClickAway(
9-
elRef: Ref<null | Element>,
9+
elRef: Ref<null | HTMLElement>,
1010
callback: (e: Event) => void,
1111
events?: string[]
1212
): void;
1313
```
1414

1515
### Parameters
1616

17-
- `elRef: Ref<null | Element>` the element to check for click away events
17+
- `elRef: Ref<null | HTMLElement>` the element to check for click away events
1818
- `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

src/components/useClickAway/useClickAway.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { onMounted, onUnmounted, Ref } from '@src/api'
33
const defaultEvents = ['mousedown', 'touchstart']
44

55
export function useClickAway(
6-
elRef: Ref<null | Element>,
6+
elRef: Ref<null | HTMLElement>,
77
callback: (e: Event) => void,
88
events = defaultEvents
99
) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useFullscreen'
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<template>
2+
<div
3+
ref="divRef"
4+
class="fullscreen-wrap"
5+
:class="{ '-is-active': isFullscreen }"
6+
>
7+
<table class="table is-fullwidth">
8+
<thead>
9+
<tr>
10+
<th>Prop</th>
11+
<th>Value</th>
12+
</tr>
13+
</thead>
14+
<tbody>
15+
<tr>
16+
<td>isFullscreen</td>
17+
<td>{{ isFullscreen }}</td>
18+
</tr>
19+
<tr>
20+
<td colspan="2">
21+
<button
22+
class="button is-primary"
23+
@click="start"
24+
v-if="!isFullscreen"
25+
>
26+
Launch fullscreen
27+
</button>
28+
<button class="button is-danger" @click="stop" v-else>
29+
Stop fullscreen
30+
</button>
31+
</td>
32+
</tr>
33+
</tbody>
34+
</table>
35+
</div>
36+
</template>
37+
38+
<script lang="ts">
39+
import Vue from 'vue'
40+
import { ref } from '@src/api'
41+
import { useFullscreen } from '@src/vue-use-kit'
42+
43+
export default Vue.extend({
44+
name: 'UseFullscreenDemo',
45+
setup() {
46+
const divRef = ref(null)
47+
const { isFullscreen, start, stop } = useFullscreen(divRef)
48+
return { isFullscreen, start, stop, divRef }
49+
}
50+
})
51+
</script>
52+
53+
<style scoped>
54+
.fullscreen-wrap.-is-active {
55+
display: flex;
56+
justify-content: center;
57+
align-items: center;
58+
height: 100vh;
59+
background: #f1f1f1;
60+
}
61+
62+
.fullscreen-wrap.-is-active table {
63+
max-width: 500px;
64+
}
65+
</style>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# useFullscreen
2+
3+
Vue function used for displaying an element in fullscreen mode.
4+
5+
## Reference
6+
7+
```typescript
8+
useFullscreen(
9+
elRef: Ref<null | HTMLElement>
10+
): {
11+
isFullscreen: Ref<boolean>;
12+
start: () => void;
13+
stop: () => void;
14+
}
15+
```
16+
17+
### Parameters
18+
19+
- `elRef: Ref<null | HTMLElement>` the element to display fullscreen
20+
21+
### Returns
22+
23+
- `isFullscreen: Ref<boolean>` it is `true` when the element is fullscreen, `false` otherwise
24+
- `start: Function` the function used for starting the fullscreen mode
25+
- `stop: Function` the function used for stopping the fullscreen mode
26+
27+
## Usage
28+
29+
```html
30+
<template>
31+
<div ref="divRef">
32+
<p>isFullscreen: {{ isFullscreen }}</p>
33+
<button @click="start" v-if="!isFullscreen">Launch fullscreen</button>
34+
<button @click="stop" v-else>Stop fullscreen</button>
35+
</div>
36+
</template>
37+
38+
<script lang="ts">
39+
import Vue from 'vue'
40+
import { ref } from '@src/api'
41+
import { useFullscreen } from 'vue-use-kit'
42+
43+
export default Vue.extend({
44+
name: 'UseFullscreenDemo',
45+
setup() {
46+
const divRef = ref(null)
47+
const { isFullscreen, start, stop } = useFullscreen(divRef)
48+
return { isFullscreen, start, stop, divRef }
49+
}
50+
})
51+
</script>
52+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { storiesOf } from '@storybook/vue'
2+
import path from 'path'
3+
import StoryTitle from '@src/helpers/StoryTitle.vue'
4+
import UseFullscreenDemo from './UseFullscreenDemo.vue'
5+
6+
const functionName = 'useFullscreen'
7+
const functionPath = path.resolve(__dirname, '..')
8+
const notes = require(`./${functionName}.md`).default
9+
10+
const basicDemo = () => ({
11+
components: { StoryTitle, demo: UseFullscreenDemo },
12+
template: `
13+
<div class="container">
14+
<story-title
15+
function-path="${functionPath}"
16+
source-name="${functionName}"
17+
demo-name="UseFullscreenDemo.vue"
18+
>
19+
<template v-slot:title></template>
20+
<template v-slot:intro></template>
21+
</story-title>
22+
<demo />
23+
</div>`
24+
})
25+
26+
storiesOf('ui|useFullscreen', module)
27+
.addParameters({ notes })
28+
.add('Demo', basicDemo)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { mount } from '@src/helpers/test'
2+
import { ref } from '@src/api'
3+
import { useFullscreen } from '@src/vue-use-kit'
4+
5+
// This feature is difficult to test therefore
6+
// I've only written a simple test
7+
8+
const testComponent = () => ({
9+
template: `
10+
<div id="divRef" ref="divRef">
11+
<div id="isFullscreen" v-if="isFullscreen"></div>
12+
<button id="start" @click="start"></button>
13+
<button id="stop" @click="stop"></button>
14+
</div>
15+
`,
16+
setup() {
17+
const divRef = ref(null)
18+
const { isFullscreen, start, stop } = useFullscreen(divRef)
19+
return { isFullscreen, start, stop, divRef }
20+
}
21+
})
22+
23+
describe('useFullscreen', () => {
24+
it('should not be fullscreen when initialized', () => {
25+
const wrapper = mount(testComponent())
26+
expect(wrapper.find('#isFullscreen').exists()).toBe(false)
27+
})
28+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ref, Ref, onUnmounted } from '@src/api'
2+
3+
export function useFullscreen(elRef: Ref<null | HTMLElement>) {
4+
const isFullscreen = ref(false)
5+
6+
const handleFullscreenChange = () => {
7+
if (!isFullscreen.value) return
8+
const isCurrentlyFullscreen = !!document.fullscreenElement
9+
isFullscreen.value = isCurrentlyFullscreen
10+
}
11+
12+
const startTracking = () => {
13+
document.addEventListener('fullscreenchange', handleFullscreenChange)
14+
}
15+
16+
const stopTracking = () => {
17+
document.removeEventListener('fullscreenchange', handleFullscreenChange)
18+
}
19+
20+
const start = () => {
21+
if (!elRef.value) return
22+
if (isFullscreen.value) return
23+
elRef.value.requestFullscreen().then(() => {
24+
isFullscreen.value = true
25+
startTracking()
26+
})
27+
}
28+
29+
const stop = () => {
30+
if (!isFullscreen.value) return
31+
document.exitFullscreen().then(() => {
32+
isFullscreen.value = false
33+
stopTracking()
34+
})
35+
}
36+
37+
onUnmounted(stopTracking)
38+
39+
return { isFullscreen, start, stop }
40+
}

0 commit comments

Comments
 (0)