Skip to content

Commit

Permalink
feat(base-id-modal): migrate to composition API
Browse files Browse the repository at this point in the history
  • Loading branch information
victorcg88 committed Jun 3, 2024
1 parent c3f18a2 commit 728d5e9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 63 deletions.
1 change: 1 addition & 0 deletions packages/_vue3-migration-test/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { default as TestHighlight } from './test-highlight.vue';
export { default as TestBaseResultImages } from './result/test-base-result-images.vue';
export { default as TestBasePanel } from './panels/test-base-panel.vue';
export { default as TestBaseKeyboardNavigation } from './test-base-keyboard-navigation.vue';
export { default as TestBaseIdModal } from './modals/test-base-id-modal.vue';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<template>
<div class="base-id-modal">
<BaseIdModalOpen modalId="myModal">Open</BaseIdModalOpen>
<BaseIdModal :animation="Fade" modalId="myModal" contentClass="content" overlayClass="overlay">
<h1>Hello</h1>
<p>The base id modal is working</p>
<BaseIdModalClose modalId="myModal">Close</BaseIdModalClose>
</BaseIdModal>
</div>
</template>

<script setup lang="ts">
import BaseIdModal from '../../../../x-components/src/components/modals/base-id-modal.vue';
import BaseIdModalOpen from '../../../../x-components/src/components/modals/base-id-modal-open.vue';
import BaseIdModalClose from '../../../../x-components/src/components/modals/base-id-modal-close.vue';
import Fade from '../../../../x-components/src/components/animations/fade.vue';
</script>

<style>
.base-id-modal {
.content {
background: white;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
.overlay {
background-color: red;
}
}
</style>
8 changes: 7 additions & 1 deletion packages/_vue3-migration-test/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
TestHighlight,
TestBaseResultImages,
TestBasePanel,
TestBaseKeyboardNavigation
TestBaseKeyboardNavigation,
TestBaseIdModal
} from './';

const routes = [
Expand Down Expand Up @@ -164,6 +165,11 @@ const routes = [
path: '/base-keyboard-navigation',
name: 'TestBaseKeyboardNavigation',
component: TestBaseKeyboardNavigation
},
{
path: '/test-base-id-modal',
name: 'TestBaseIdModal',
component: TestBaseIdModal
}
];

Expand Down
147 changes: 85 additions & 62 deletions packages/x-components/src/components/modals/base-id-modal.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<BaseModal
:ref="el"
@click:overlay="emitClickOutOfModal"
@focusin:body="emitClickOutOfModal"
:animation="animation"
Expand All @@ -11,11 +12,12 @@
</template>

<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { XOn } from '../decorators/bus.decorators';
import { defineComponent, ref } from 'vue';
import { WireMetadata } from '../../wiring/wiring.types';
import { getTargetElement, isElementEqualOrContained } from '../../utils/html';
import { useXBus } from '../../composables';
import { AnimationProp } from '../../types/animation-prop';
import { XEvent } from '../../wiring/events.types';
import BaseModal from './base-modal.vue';
/**
Expand All @@ -26,71 +28,92 @@
*
* @public
*/
@Component({
components: { BaseModal }
})
export default class BaseIdModal extends Vue {
/** Animation to use for opening/closing the modal. */
@Prop()
public animation?: Vue | string;
/** The modalId to use for the open and close event listeners. */
@Prop({ required: true })
public modalId!: string;
/** Whether the modal is open or not. */
protected isOpen = false;
/** The element that opened the modal. */
protected openerElement?: HTMLElement;
/**
* Opens the modal.
*
* @param modalId - The payload of the {@link XEventsTypes.UserClickedOpenModal} event.
* @param metadata - The metadata of the emitted event.
* @public
*/
@XOn('UserClickedOpenModal')
openModal(modalId: string, metadata: WireMetadata): void {
if (!this.isOpen && this.modalId === modalId) {
this.openerElement = metadata.target;
this.isOpen = true;
export default defineComponent({
name: 'BaseIdModal',
components: { BaseModal },
props: {
/**
* Animation to use for opening/closing the modal.
*/
animation: {
type: AnimationProp
},
/**
* The modalId to use for the open and close event listeners.
*/
modalId: {
type: String,
required: true
}
},
setup(props) {
/** The element that opened the modal. */
let openerElement: HTMLElement | undefined;
/** Whether the modal is open or not. */
const isOpen = ref(false);
const el = ref<HTMLElement>();
const xBus = useXBus();
/**
* Opens the modal.
*
* @param modalId - The payload of the {@link XEventsTypes.UserClickedOpenModal} event.
* @param metadata - The metadata of the emitted event.
* @public
*/
function openModal(modalId: string, metadata: WireMetadata): void {
if (!isOpen.value && props.modalId === modalId) {
openerElement = metadata.target;
isOpen.value = true;
}
}
}
/**
* Closes the modal.
*
* @param payload - The payload of the closing events:
* {@link XEventsTypes.UserClickedCloseModal} or {@link XEventsTypes.UserClickedOutOfModal}.
*
* @public
*/
@XOn(['UserClickedCloseModal', 'UserClickedOutOfModal'])
closeModal(payload: string): void {
if (this.isOpen && this.modalId === payload) {
this.isOpen = false;
/**
* Closes the modal.
*
* @param payload - The payload of the closing events:
* {@link XEventsTypes.UserClickedCloseModal} or {@link XEventsTypes.UserClickedOutOfModal}.
*
* @public
*/
function closeModal(payload: string): void {
if (isOpen.value && props.modalId === payload) {
isOpen.value = false;
}
}
}
/**
* Emits a {@link XEventsTypes.UserClickedOutOfModal} event unless the passed event target
* is the button that opened the modal.
*
* @param event - The event that triggered the close attempt.
* @public
*/
protected emitClickOutOfModal(event: MouseEvent | FocusEvent): void {
// Prevents clicking the open button when the panel is already open to close the panel.
if (
!this.openerElement ||
!isElementEqualOrContained(this.openerElement, getTargetElement(event))
) {
this.$x.emit('UserClickedOutOfModal', this.modalId, { target: this.$el as HTMLElement });
/**
* Emits a {@link XEventsTypes.UserClickedOutOfModal} event unless the passed event target
* is the button that opened the modal.
*
* @param event - The event that triggered the close attempt.
* @public
*/
function emitClickOutOfModal(event: MouseEvent | FocusEvent): void {
// Prevents clicking the open button when the panel is already open to close the panel.
if (!openerElement || !isElementEqualOrContained(openerElement, getTargetElement(event))) {
xBus.emit('UserClickedOutOfModal', props.modalId, { target: el.value as HTMLElement });
}
}
xBus
.on('UserClickedOpenModal', true)
.subscribe(({ eventPayload, metadata }) => openModal(eventPayload, metadata));
['UserClickedCloseModal', 'UserClickedOutOfModal'].forEach(event => {
xBus
.on(event as XEvent, false)
.subscribe(eventPayload => closeModal(eventPayload as string));
});
return {
el,
isOpen,
emitClickOutOfModal
};
}
}
});
</script>

<docs lang="mdx">
Expand Down

0 comments on commit 728d5e9

Please sign in to comment.