Skip to content

Commit

Permalink
fix(BottomSheet): handle possible null when accessing api
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitano committed May 22, 2023
1 parent 0495220 commit 3c180c5
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 129 deletions.
154 changes: 77 additions & 77 deletions packages/bottom-sheets/src/BottomSheetHandle.vue
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
<script setup lang="ts">
import {inject, onMounted, onUnmounted, ref} from 'vue';
import {BottomSheetInjectionKey} from './api';
import type {BottomSheetApi} from './types';
const api = inject<BottomSheetApi>(BottomSheetInjectionKey);
const draggableArea = ref();
let sheetHeight: number | string;
const setSheetHeight = (value: number | string) => {
sheetHeight = Math.max(0, Math.min(100, +value));
api?.setHeight(`${sheetHeight}vh`);
};
const touchPosition = (event: any) =>
event.touches ? event.touches[0] : event;
let dragPosition: number | undefined;
const onDragStart = (event: Event) => {
dragPosition = touchPosition(event).pageY;
draggableArea.value.style.cursor = document.body.style.cursor = 'grabbing';
api?.el?.value?.classList?.add('v-bottom-sheet--dragging');
};
const onDragMove = (event: Event) => {
if (dragPosition === undefined) return;
const y = touchPosition(event).pageY;
const deltaY = dragPosition - y;
const deltaHeight = (deltaY / window.innerHeight) * 100;
setSheetHeight(+sheetHeight + deltaHeight);
dragPosition = y;
};
const onDragEnd = () => {
dragPosition = undefined;
draggableArea.value.style.cursor = document.body.style.cursor = '';
if (sheetHeight < 25) {
api?.close();
} else if (sheetHeight > 75) {
setSheetHeight(100);
} else {
setSheetHeight(sheetHeight);
}
api?.el?.value?.classList?.remove('v-bottom-sheet--dragging');
};
onMounted(() => {
sheetHeight = api?.getHeight() as number;
draggableArea.value.addEventListener('mousedown', onDragStart);
draggableArea.value.addEventListener('touchstart', onDragStart);
window.addEventListener('mousemove', onDragMove);
window.addEventListener('touchmove', onDragMove);
window.addEventListener('mouseup', onDragEnd);
});
onUnmounted(() => {
window.removeEventListener('mousemove', onDragMove);
window.removeEventListener('touchmove', onDragMove);
window.removeEventListener('mouseup', onDragEnd);
});
</script>

<template>
<div ref="draggableArea" class="v-bottom-sheet-handle">
<slot />
</div>
</template>
<script setup lang="ts">
import {inject, onMounted, onUnmounted, ref} from 'vue';
import {BottomSheetInjectionKey} from './api';
import type {BottomSheetApi} from './types';
const api = inject<BottomSheetApi>(BottomSheetInjectionKey);
const draggableArea = ref();
let sheetHeight: number | string;
const setSheetHeight = (value: number | string) => {
sheetHeight = Math.max(0, Math.min(100, +value));
api?.setHeight(`${sheetHeight}vh`);
};
const touchPosition = (event: any) =>
event.touches ? event.touches[0] : event;
let dragPosition: number | undefined;
const onDragStart = (event: Event) => {
dragPosition = touchPosition(event).pageY;
draggableArea.value.style.cursor = document.body.style.cursor = 'grabbing';
api?.el?.value?.classList?.add('v-bottom-sheet--dragging');
};
const onDragMove = (event: Event) => {
if (dragPosition === undefined) return;
const y = touchPosition(event).pageY;
const deltaY = dragPosition - y;
const deltaHeight = (deltaY / window.innerHeight) * 100;
setSheetHeight(+sheetHeight + deltaHeight);
dragPosition = y;
};
const onDragEnd = () => {
dragPosition = undefined;
draggableArea.value.style.cursor = document.body.style.cursor = '';
if (+sheetHeight < 25) {
api?.close();
} else if (+sheetHeight > 75) {
setSheetHeight(100);
} else {
setSheetHeight(sheetHeight);
}
api?.el?.value?.classList?.remove('v-bottom-sheet--dragging');
};
onMounted(() => {
sheetHeight = api?.getHeight() as number;
draggableArea.value.addEventListener('mousedown', onDragStart);
draggableArea.value.addEventListener('touchstart', onDragStart);
window.addEventListener('mousemove', onDragMove);
window.addEventListener('touchmove', onDragMove);
window.addEventListener('mouseup', onDragEnd);
});
onUnmounted(() => {
window.removeEventListener('mousemove', onDragMove);
window.removeEventListener('touchmove', onDragMove);
window.removeEventListener('mouseup', onDragEnd);
});
</script>

<template>
<div ref="draggableArea" class="v-bottom-sheet-handle">
<slot />
</div>
</template>
48 changes: 24 additions & 24 deletions packages/bottom-sheets/src/BottomSheetHeader.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<script setup lang="ts">
import Icon from '@morpheme/icon';
import {inject} from 'vue';
import {BottomSheetInjectionKey} from './api';
import type {BottomSheetApi} from './types';
const api = inject<BottomSheetApi>(BottomSheetInjectionKey);
</script>

<template>
<div class="v-bottom-sheet-header">
<h3 class="v-bottom-sheet-title">
<slot />
</h3>
<button
type="button"
aria-label="Close"
class="v-bottom-sheet-close-button"
@click="api.close"
>
<Icon class="v-bottom-sheet-close-icon" name="ic:round-close" />
</button>
</div>
</template>
<script setup lang="ts">
import Icon from '@morpheme/icon';
import {inject} from 'vue';
import {BottomSheetInjectionKey} from './api';
import type {BottomSheetApi} from './types';
const api = inject<BottomSheetApi>(BottomSheetInjectionKey);
</script>

<template>
<div v-if="api" class="v-bottom-sheet-header">
<h3 class="v-bottom-sheet-title">
<slot />
</h3>
<button
type="button"
aria-label="Close"
class="v-bottom-sheet-close-button"
@click="api.close"
>
<Icon class="v-bottom-sheet-close-icon" name="ic:round-close" />
</button>
</div>
</template>
56 changes: 28 additions & 28 deletions packages/bottom-sheets/src/BottomSheetOverlay.vue
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<script setup lang="ts">
import {inject} from 'vue';
import {BottomSheetInjectionKey} from './api';
import type {BottomSheetApi} from './types';
withDefaults(
defineProps<{
transition?: string;
}>(),
{
transition: 'v-bottom-sheet-overlay-transition',
},
);
const api = inject<BottomSheetApi>(BottomSheetInjectionKey);
</script>

<template>
<Teleport to="body">
<transition :name="transition">
<div
v-if="api.isOpen"
class="v-bottom-sheet-overlay"
@click="api.close"
></div>
</transition>
</Teleport>
</template>
<script setup lang="ts">
import {inject} from 'vue';
import {BottomSheetInjectionKey} from './api';
import type {BottomSheetApi} from './types';
withDefaults(
defineProps<{
transition?: string;
}>(),
{
transition: 'v-bottom-sheet-overlay-transition',
},
);
const api = inject<BottomSheetApi>(BottomSheetInjectionKey);
</script>

<template>
<Teleport to="body">
<transition :name="transition">
<div
v-if="api?.isOpen"
class="v-bottom-sheet-overlay"
@click="api.close"
></div>
</transition>
</Teleport>
</template>

0 comments on commit 3c180c5

Please sign in to comment.