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: port AppModal component to harvester source code #1249

Merged
merged 1 commit into from
Jan 2, 2025
Merged
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
170 changes: 170 additions & 0 deletions pkg/harvester/components/AppModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
name: 'AppModal',

// emits: ['close'],

inheritAttrs: false,
props: {
/**
* If set to false, it will not be possible to close modal by clicking on
* the background or by pressing Esc key.
*/
clickToClose: {
type: Boolean,
default: true,
},
/**
* Width in pixels or percents (50, "50px", "50%").
*
* Supported string values are <number>% and <number>px
*/
width: {
type: [Number, String],
default: 600,
validator(value) {
if (typeof value === 'number') {
return value > 0;
}

if (typeof value === 'string') {
return /^(0*(?:[1-9][0-9]*|0)\.?\d*)+(px|%)$/.test(value);
}

return false;
}
},
/**
* List of class that will be applied to the modal window
*/
customClass: {
type: String,
default: '',
},
/**
* Style that will be applied to the modal window
*/
styles: {
type: String,
default: '',
},
/**
* Name of the modal
*/
name: {
type: String,
default: '',
}
},
computed: {
modalWidth(): string {
const uom = typeof (this.width) === 'number' ? 'px' : '';

return `${ this.width }${ uom }`;
},
stylesPropToObj(): object {
return this.styles.split(';')
.map(line => line.trim().split(':'))
.reduce((lines, [key, val]) => {
return {
...lines,
[key]: val
};
}, { });
},
modalStyles(): object {
return {
width: this.modalWidth,
...this.stylesPropToObj,
};
}
},
mounted() {
document.addEventListener('keydown', this.handleEscapeKey);
},
beforeDestroy() {
document.removeEventListener('keydown', this.handleEscapeKey);
},
methods: {
handleClickOutside(event: MouseEvent) {
if (
this.clickToClose &&
this.$refs.modalRef &&
!(this.$refs.modalRef as HTMLElement).contains(event.target as Node)
) {
this.$emit('close');
}
},
handleEscapeKey(event: KeyboardEvent) {
if (this.clickToClose && event.key === 'Escape') {
this.$emit('close');
}
}
}
});
</script>

<template>
<mounting-portal
mountTo="#__layout"
Copy link
Collaborator Author

@a110605 a110605 Dec 30, 2024

Choose a reason for hiding this comment

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

The original mountTo is id=modals , but on Rancher 2.8, there is no id=modals in DOM. ( Because it's introduced via shell/public/index.html

Change mounTo #__layout seems no harm

Screenshot 2024-12-28 at 11 16 47 AM

name="source"
append
>
<transition
name="modal-fade"
appear
>
<div
class="modal-overlay"
:data-modal="name"
@click="handleClickOutside"
>
<div
v-bind="$attrs"
ref="modalRef"
:class="customClass"
class="modal-container"
:style="modalStyles"
@click.stop
>
<slot><!--Empty content--></slot>
</div>
</div>
</transition>
</mounting-portal>
</template>

<style lang="scss">
.modal-overlay {
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: var(--overlay-bg);
display: flex;
justify-content: center;
align-items: center;
z-index: 20;

.modal-container {
background-color: var(--modal-bg);
border-radius: var(--border-radius);
max-height: 95vh;
overflow: auto;
border: 2px solid var(--modal-border);
}
}

.modal-fade-enter-active,
.modal-fade-leave-active {
transition: opacity 200ms;
}

.modal-fade-enter,
.modal-fade-leave-to {
opacity: 0;
}
</style>
2 changes: 1 addition & 1 deletion pkg/harvester/dialog/HarvesterSupportBundle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LabeledInput } from '@components/Form/LabeledInput';
import AsyncButton from '@shell/components/AsyncButton';
import GraphCircle from '@shell/components/graph/Circle';
import { Banner } from '@components/Banner';
import AppModal from '@shell/components/AppModal';
import AppModal from '@pkg/harvester/components/AppModal';

export default {
name: 'SupportBundle',
Expand Down
2 changes: 1 addition & 1 deletion pkg/harvester/dialog/RestartVMDialog.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { mapGetters } from 'vuex';
import AsyncButton from '@shell/components/AsyncButton';
import AppModal from '@shell/components/AppModal';
import AppModal from '@pkg/harvester/components/AppModal';
import { Card } from '@components/Card';
import { Banner } from '@components/Banner';
import { exceptionToErrorsArray } from '@shell/utils/error';
Expand Down
Loading