Skip to content

Commit

Permalink
feat: added the optional argument to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandrshy committed Jul 5, 2020
1 parent 460e6ca commit 4ed6254
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
44 changes: 27 additions & 17 deletions src/index.ts
Expand Up @@ -7,10 +7,10 @@ export const Keukenhof = ((): KeukenhofType => {
*/
class Modal {
$modal: HTMLElement | null;
onOpen: () => void;
onClose: () => void;
beforeOpen: () => boolean;
beforeClose: () => boolean;
onOpen: (event?: Event) => void;
onClose: (event?: Event) => void;
beforeOpen: (event?: Event) => boolean;
beforeClose: (event?: Event) => boolean;
openAttribute: string;
closeAttribute: string;
openClass: string;
Expand Down Expand Up @@ -71,65 +71,75 @@ export const Keukenhof = ((): KeukenhofType => {
registerNodes(nodeList: HTMLElement[]) {
nodeList
.filter(Boolean)
.forEach((element) => element.addEventListener('click', () => this.open()));
.forEach((element) =>
element.addEventListener('click', (event) => this.open(event)),
);
}

/**
* Open moda window
*
* @param {Event} event - Event data
*/
open() {
const isContinue = this.beforeOpen();
open(event?: Event) {
const isContinue = this.beforeOpen(event);
if (!isContinue) return;
this.$modal?.classList.add(this.openClass);
this.changeScrollBehavior(SCROLL_STATE.DISABLE);
this.addEventListeners();
this.preparationOpeningModal();
this.preparationOpeningModal(event);
}

/**
* Preparing a modal window for opening
*
* @param {Event} event - Event data
*/
preparationOpeningModal() {
preparationOpeningModal(event?: Event) {
if (this.hasAnimation) {
this.$modal?.classList.add(CLASS_NAMES.IS_OPENING);
const handler = () => {
this.$modal?.classList.remove(CLASS_NAMES.IS_OPENING);
this.onOpen();
this.onOpen(event);
this.$modal?.removeEventListener('animationend', handler);
};
this.$modal?.addEventListener('animationend', handler);
} else {
this.onOpen();
this.onOpen(event);
}
}

/**
* Close modal window
*
* @param {Event} event - Event data
*/
close() {
const isContinue = this.beforeClose();
close(event?: Event) {
const isContinue = this.beforeClose(event);
if (!isContinue) return;
this.changeScrollBehavior(SCROLL_STATE.ENABLE);
this.removeEventListeners();
this.preparationClosingModal();
this.preparationClosingModal(event);
}

/**
* Preparing a modal window for closing
*
* @param {Event} event - Event data
*/
preparationClosingModal() {
preparationClosingModal(event?: Event) {
if (this.hasAnimation) {
this.$modal?.classList.add(CLASS_NAMES.IS_CLOSING);
const handler = () => {
this.$modal?.classList.remove(CLASS_NAMES.IS_CLOSING);
this.$modal?.classList.remove(this.openClass);
this.onClose();
this.onClose(event);
this.$modal?.removeEventListener('animationend', handler);
};
this.$modal?.addEventListener('animationend', handler);
} else {
this.$modal?.classList.remove(this.openClass);
this.onClose();
this.onClose(event);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/types.ts
Expand Up @@ -10,18 +10,18 @@ export type ConfigType = {
container?: string;
defaultValue?: string;
};
onOpen?: () => void;
onClose?: () => void;
beforeOpen?: () => boolean;
beforeClose?: () => boolean;
onOpen?: (event?: Event) => void;
onClose?: (event?: Event) => void;
beforeOpen?: (event?: Event) => boolean;
beforeClose?: (event?: Event) => boolean;
};

export type ModalType = {
registerNodes: (nodeList: HTMLElement[]) => void;
open: () => void;
preparationOpeningModal: () => void;
close: () => void;
preparationClosingModal: () => void;
open: (event?: Event) => void;
preparationOpeningModal: (event?: Event) => void;
close: (event?: Event) => void;
preparationClosingModal: (event?: Event) => void;
closeBySelector: (selector: string) => void;
onClick: (event: Event) => void;
onKeydown: (event: KeyboardEvent) => void;
Expand Down

0 comments on commit 4ed6254

Please sign in to comment.