Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
feat(popup): fallback notification (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 committed Nov 3, 2023
1 parent e3c5b45 commit 5b9ac05
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
24 changes: 19 additions & 5 deletions packages/client/components/Notification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ const icon = ref<string | undefined>()
const text = ref<string | undefined>()
const type = ref<'primary' | 'error' | 'warning' | undefined>()
const duration = ref<number>()
const placement = ref<'top' | 'bottom'>('top')
let timer: ReturnType<typeof setTimeout> | undefined
provideNotification((opt: {
text: string
icon?: string
type?: 'primary' | 'error' | 'warning'
duration?: number
placement?: 'top' | 'bottom'
}) => {
text.value = opt.text
icon.value = opt.icon
show.value = true
type.value = opt.type
duration.value = opt.duration || 1500
placement.value = opt.placement || 'top'
createTimer()
})
Expand Down Expand Up @@ -47,15 +50,26 @@ function createTimer() {

<template>
<div
fixed left-0 right-0 top-0 z-1000 text-center text-base
:class="show ? '' : 'pointer-events-none overflow-hidden'"
fixed z-1000 text-center text-base
:class="[
{
'pointer-events-none overflow-hidden': !show,
},
[
placement === 'top' ? 'top-0' : 'bottom-0',
],
]"
>
<div
border="~ base"
flex="~ inline gap2"
m-3 inline-block items-center rounded px-4 py-1 transition-all duration-300 bg-base
:style="show ? {} : { transform: 'translateY(-300%)' }"
:class="[show ? 'shadow' : 'shadow-none', textColor]"
m-3 inline-block items-center rounded px-4 py-1 transition-transform duration-300 bg-base
:class="[
show
? 'shadow'
: `shadow-none ${placement === 'top' ? 'translate-y--300%' : 'translate-y-300%'}`,
textColor,
]"
@mouseenter="clearTimer"
@mouseleave="createTimer"
>
Expand Down
29 changes: 25 additions & 4 deletions packages/client/components/PopupButton.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
<script setup lang="ts">
import { useDevToolsClient } from '~/logic/client'
defineProps<{
closePopover: () => void
}>()
const client = useDevToolsClient()
// @ts-expect-error missing type
const isSupported = typeof window !== 'undefined' && window.parent.documentPictureInPicture?.requestWindow
const showPopupUnsupported = ref(false)
const copy = useCopy()
const isSupported = typeof window !== 'undefined'
// @ts-expect-error experimental API
&& window.parent?.documentPictureInPicture?.requestWindow
&& checkIsSecurityContext()
const showNotification = useNotification()
function popup() {
if (!isSupported) {
showPopupUnsupported.value = true
return
}
client.value?.panel?.popup()
const popupFn = client.value?.panel?.popup as (() => Promise<boolean>) | undefined
if (popupFn) {
popupFn().then((success) => {
if (!success) {
showNotification({
text: 'Open popup mode failed, check console for more details.',
icon: 'i-carbon-warning',
type: 'error',
duration: 3000,
placement: 'bottom',
})
}
})
}
}
</script>

Expand Down
1 change: 1 addition & 0 deletions packages/client/composables/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const [
icon?: string
type?: 'primary' | 'warning' | 'error'
duration?: number
placement?: 'top' | 'bottom'
}) => void>()

export {
Expand Down
46 changes: 27 additions & 19 deletions packages/node/src/views/composables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ export function usePiPMode(iframeGetter: () => HTMLIFrameElement | undefined, ho
const documentPictureInPicture = window.documentPictureInPicture
async function popup() {
const iframe = iframeGetter()
const pip = popupWindow.value = await documentPictureInPicture.requestWindow({
width: Math.round(window.innerWidth * state.value.width / 100),
height: Math.round(window.innerHeight * state.value.height / 100),
})
const style = pip.document.createElement('style')
style.innerHTML = `
let isSuccess = true
try {
const pip = popupWindow.value = await documentPictureInPicture.requestWindow({
width: Math.round(window.innerWidth * state.value.width / 100),
height: Math.round(window.innerHeight * state.value.height / 100),
})
const style = pip.document.createElement('style')
style.innerHTML = `
body {
margin: 0;
padding: 0;
Expand All @@ -215,19 +217,25 @@ export function usePiPMode(iframeGetter: () => HTMLIFrameElement | undefined, ho
outline: none;
}
`
pip.__VUE_DEVTOOLS_GLOBAL_HOOK__ = hook
pip.__VUE_DEVTOOLS_IS_POPUP__ = true
pip.document.title = 'Vue DevTools'
pip.document.head.appendChild(style)
pip.document.body.appendChild(iframe)
pip.addEventListener('resize', () => {
state.value.width = Math.round(pip.innerWidth / window.innerWidth * 100)
state.value.height = Math.round(pip.innerHeight / window.innerHeight * 100)
})
pip.addEventListener('pagehide', () => {
popupWindow.value = null
pip.close()
})
pip.__VUE_DEVTOOLS_GLOBAL_HOOK__ = hook
pip.__VUE_DEVTOOLS_IS_POPUP__ = true
pip.document.title = 'Vue DevTools'
pip.document.head.appendChild(style)
pip.document.body.appendChild(iframe)
pip.addEventListener('resize', () => {
state.value.width = Math.round(pip.innerWidth / window.innerWidth * 100)
state.value.height = Math.round(pip.innerHeight / window.innerHeight * 100)
})
pip.addEventListener('pagehide', () => {
popupWindow.value = null
pip.close()
})
}
catch (error) {
isSuccess = false
console.error(`[vite-plugin-vue-devtools] Open popup mode failed: ${(error as DOMException).message}`)
}
return isSuccess
}
return {
popup,
Expand Down

0 comments on commit 5b9ac05

Please sign in to comment.