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: Vue 2/3 lifecycle type errors and script tag cleanup #79

Merged
merged 3 commits into from
Jun 20, 2023
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
16 changes: 13 additions & 3 deletions packages/vue2/src/google-translate-select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default Vue.extend<
hoveredLanguageCode: '',
setTimeoutId: -1,
jsonCallbackFnName: '',
scriptTag: null,
googleTranslateOriginSelectObserve: {},
htmlAttrLangObserve: {},
ns,
Expand Down Expand Up @@ -153,8 +154,17 @@ export default Vue.extend<
},
// @ts-ignore
beforeUnmount() {
this.googleTranslateOriginSelectObserve!.stop!()
this.htmlAttrLangObserve!.stop!()
if (this.googleTranslateOriginSelectObserve?.stop) {
this.googleTranslateOriginSelectObserve!.stop!()
}

if (this.htmlAttrLangObserve?.stop) {
this.htmlAttrLangObserve!.stop!()
}

if (this.scriptTag?.unload) {
this.scriptTag.unload()
}

// eslint-disable-next-line @typescript-eslint/no-this-alias
const _this = this
Expand Down Expand Up @@ -220,7 +230,7 @@ export default Vue.extend<
createGoogleTranslate() {
this.createStyle()
this.createJsonCallback()
this.createScript()
this.scriptTag = this.createScript()
},
/**
* Triggers translations by observe changes in the DOM of GoogleTranslate's original select.
Expand Down
20 changes: 16 additions & 4 deletions packages/vue3/src/google-translate-select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ import {
} from '@google-translate-select/constants'
import '@google-translate-select/theme-chalk/src/index.scss'
import { googleTranslateProps } from './types'
import type { UseMutationObserverReturn } from '@google-translate-select/utils'
import type {
CreateScriptTagReturn,
UseMutationObserverReturn,
} from '@google-translate-select/utils'

const ns = createNamespace('select')

Expand All @@ -106,6 +109,7 @@ export default defineComponent({
const hoveredLanguageCode = ref<string>('')
const setTimeoutId = ref<number>(-1)
const jsonCallbackFnName = ref<string>('')
const scriptTag = ref<CreateScriptTagReturn | null>(null)
const googleTranslateOriginSelectObserve =
ref<Partial<UseMutationObserverReturn> | null>({})
const htmlAttrLangObserve = ref<Partial<UseMutationObserverReturn> | null>(
Expand Down Expand Up @@ -178,7 +182,7 @@ export default defineComponent({
function createGoogleTranslate() {
createStyle()
createJsonCallback()
createScript()
scriptTag.value = createScript()
}

/**
Expand Down Expand Up @@ -441,9 +445,17 @@ export default defineComponent({
})

onBeforeUnmount(() => {
unref(googleTranslateOriginSelectObserve)!.stop!()
unref(htmlAttrLangObserve)!.stop!()
if (unref(googleTranslateOriginSelectObserve)?.stop) {
unref(googleTranslateOriginSelectObserve)!.stop!()
}

if (unref(htmlAttrLangObserve)?.stop) {
unref(htmlAttrLangObserve)!.stop!()
}

if (unref(scriptTag)?.unload) {
unref(scriptTag)!.unload!()
}
if (props.trigger === 'click')
document.removeEventListener('click', handleDropdownShowOrHideByClick)
})
Expand Down