Skip to content

Commit

Permalink
fix(BaseSocial): move dialog to an object (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
nWacky authored Jan 18, 2024
1 parent a8cd6c7 commit b919b9f
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/mixins/BaseSocial/BaseSocial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import {
PropType,
h,
DefineComponent,
markRaw,
} from 'vue';
import { IWindowFeatures } from '@/types/common/windowFeatures';
import getFormattedWindowFeatures from '@/utils/getFormattedWindowFeatures';
import getPopupClientRect from '@/utils/getPopupClientRect';
import { isUndefined } from '@/utils/inspect';

export interface IBaseSocialDataOptions {
shareDialog: Window | null;
shareDialogCloseIntervalId: number | undefined
dialog: {
shareDialog: Window | null;
shareDialogCloseIntervalId: number | undefined
}
}

export type TBaseSocialPropsOptions<T> = {
Expand Down Expand Up @@ -81,8 +84,10 @@ export default function BaseSocials<T>(

data(): IBaseSocialDataOptions {
return {
shareDialog: null,
shareDialogCloseIntervalId: undefined,
dialog: markRaw({
shareDialog: null,
shareDialogCloseIntervalId: undefined,
}),
};
},

Expand All @@ -92,7 +97,7 @@ export default function BaseSocials<T>(
* Make sure interval has been cleared
*/
beforeUnmount() {
window.clearInterval(this.shareDialogCloseIntervalId);
window.clearInterval(this.dialog.shareDialogCloseIntervalId);
},

computed: {
Expand Down Expand Up @@ -139,12 +144,12 @@ export default function BaseSocials<T>(
* If the pointer to the window object in memory does not exist
* or if such pointer exists but the window was closed
*/
if (this.shareDialog === null || this.shareDialog?.closed) {
if (this.dialog.shareDialog === null || this.dialog.shareDialog?.closed) {
/**
* then create it. The new window will be created and
* will be brought on top of any other window.
*/
this.shareDialog = window.open(
this.dialog.shareDialog = window.open(
url,
'_blank',
formattedFeatures,
Expand All @@ -153,7 +158,7 @@ export default function BaseSocials<T>(
* If window.open has been blocked – emit 'block' event and then do nothing
* If not – emit 'open' event
*/
if (!this.shareDialog) {
if (!this.dialog.shareDialog) {
this.$emit('popup-block');
return;
}
Expand All @@ -164,15 +169,15 @@ export default function BaseSocials<T>(
* So we check if it has been closed every 300 ms
* @link https://atashbahar.com/post/2010-04-27-detect-when-a-javascript-popup-window-gets-closed
*/
this.shareDialogCloseIntervalId = window.setInterval(() => {
if (this.shareDialog === null || this.shareDialog?.closed) {
window.clearInterval(this.shareDialogCloseIntervalId);
this.dialog.shareDialogCloseIntervalId = window.setInterval(() => {
if (this.dialog.shareDialog === null || this.dialog.shareDialog?.closed) {
window.clearInterval(this.dialog.shareDialogCloseIntervalId);
this.$emit('popup-close');
/**
* Unset reference to the popup window
* @link https://web.dev/detached-window-memory-leaks/#solution-unset-references
*/
this.shareDialog = null;
this.dialog.shareDialog = null;
}
}, 300);
} else {
Expand All @@ -182,7 +187,7 @@ export default function BaseSocials<T>(
* window with the focus() method. There would be no need to re-create
* the window or to reload the referenced resource.
*/
this.shareDialog.focus();
this.dialog.shareDialog.focus();
this.$emit('popup-focus');
}
},
Expand Down

1 comment on commit b919b9f

@vercel
Copy link

@vercel vercel bot commented on b919b9f Jan 18, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

vue-socials – ./

vue-socials.vercel.app
vue-socials-git-main-webistomin.vercel.app
vue-socials-webistomin.vercel.app

Please sign in to comment.