Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VsModal): move esc eventListener from composable to store #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 9 additions & 29 deletions packages/vlossom/src/composables/esc-close-composable.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import { Ref, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { Ref, ref, watch } from 'vue';
import { store } from '@/stores';
import { MODAL_DURATION } from '@/declaration';

export function useEscClose(id: string, closeOnEsc: Ref<boolean>, isOpen: Ref<boolean>, onEscClose: () => void) {
const closing = ref(false);

function onPressEsc(event: KeyboardEvent) {
if (!closeOnEsc.value || closing.value) {
return;
}

if (event.key === 'Escape' && store.escStack.getTopId() === id) {
closing.value = true;
onEscClose();

setTimeout(() => {
store.escStack.pop();
closing.value = false;
}, MODAL_DURATION);
}
}

watch(
isOpen,
(open) => {
Expand All @@ -29,20 +13,16 @@ export function useEscClose(id: string, closeOnEsc: Ref<boolean>, isOpen: Ref<bo
}

if (open) {
store.escStack.push(id);
store.escStack.push(id, onEscClose);
} else {
closing.value = true;

setTimeout(() => {
closing.value = false;
store.escStack.remove(id);
}, MODAL_DURATION);
}
},
{ immediate: true },
);

onMounted(() => {
document.addEventListener('keydown', onPressEsc);
});

onBeforeUnmount(() => {
if (closeOnEsc.value) {
store.escStack.remove(id);
}
document.removeEventListener('keydown', onPressEsc);
});
}
22 changes: 0 additions & 22 deletions packages/vlossom/src/stores/__tests__/esc-stack-store.test.ts

This file was deleted.

37 changes: 26 additions & 11 deletions packages/vlossom/src/stores/esc-stack-store.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
export class EscStackStore {
private idStack: string[] = [];
private escStack: [string, () => void][] = [];

push(id: string) {
this.idStack.push(id);
constructor() {
document.addEventListener('keydown', (event: KeyboardEvent) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

딱 한 군데서만 event를 등록해야 하는데 그 지점이 store 밖에 잡히는 곳이 없어서 일단 여기에 작성했습니다ㅠ
Vlossom bootstrap 시점에 라이프사이클 함수를 파야할지 고민되는 지점입니다;;;
일단 버그 수정 위해서 여기 작성하고 설계를 고민해보는게 좋을 것 같아요

if (this.escStack.length === 0) {
return;
}
if (event.key === 'Escape') {
this.pop();
}
});
}

pop() {
return this.idStack.pop();
push(id: string, onEscClose: () => void) {
this.escStack.push([id, onEscClose]);
}

remove(id: string) {
const index = this.idStack.indexOf(id);
if (index >= 0) {
this.idStack.splice(index, 1);
pop() {
const pop = this.escStack.pop();
if (!pop) {
return;
}
const [, onEscClose] = pop;
onEscClose();
}

getTopId() {
return this.idStack[this.idStack.length - 1];
remove(id: string) {
const index = this.escStack.findIndex(([stackId]) => stackId === id);
if (index === -1) {
return;
}
const [pop] = this.escStack.splice(index, 1);
const [, onEscClose] = pop;
onEscClose();
}
}
14 changes: 7 additions & 7 deletions packages/vlossom/src/styles/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ $variables: (
width-xs: 100%,

// modal
modal-height-xs: 20%,
modal-height-sm: 30%,
modal-height-md: 50%,
modal-height-lg: 66%,
modal-height-xl: 82%,
modal-height-xs: 25%,
modal-height-sm: 36%,
modal-height-md: 60%,
modal-height-lg: 75%,
modal-height-xl: 90%,
modal-width-xs: 20%,
modal-width-sm: 30%,
modal-width-md: 45%,
modal-width-lg: 70%,
modal-width-md: 48%,
modal-width-lg: 75%,
modal-width-xl: 90%,

// z-index
Expand Down