diff --git a/packages/hub-ui/src/client/components/dock/Dock.vue b/packages/hub-ui/src/client/components/dock/Dock.vue index 4acb1284..6c263535 100644 --- a/packages/hub-ui/src/client/components/dock/Dock.vue +++ b/packages/hub-ui/src/client/components/dock/Dock.vue @@ -2,7 +2,7 @@ import type { DocksContext } from '@devframes/hub/client' import type { CSSProperties } from 'vue' import type { DockLayout } from './dock-layout' -import { useEventListener, useScreenSafeArea, whenever } from '@vueuse/core' +import { useEventListener, whenever } from '@vueuse/core' import { computed, onMounted, reactive, ref, useTemplateRef, watch } from 'vue' import { BUILTIN_ENTRY_CLIENT_AUTH_NOTICE } from '../../constants' import { t } from '../../i18n' @@ -10,6 +10,7 @@ import { docksSplitGroupsWithCapacity, getEntryGroup, resolveNextRecentDockIds, import { setDocksOverflowPanel, useDocksGroupPanel } from '../../state/floating-tooltip' import { useSettings } from '../../state/settings-defaults' import { useIsRpcTrusted } from '../../utils/useIsRpcTrusted' +import { useSafeArea } from '../../utils/useSafeArea' import BrandMark from '../icons/BrandMark.vue' import { dockLayoutCssVars, @@ -39,29 +40,15 @@ const layout = computed(() => resolveDockLayout(props.layout)) const isSafari = navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') -const safeArea = useScreenSafeArea() +const anchorEl = useTemplateRef('anchorEl') -function toNumber(value: string) { - const num = +value - if (Number.isNaN(num)) - return 0 - return num -} +const safeArea = useSafeArea(anchorEl) // Effective spacing from the viewport edge: device safe-area insets plus the // configured viewport margin. Passed down to the panel so both stay in sync. -const panelMargins = computed(() => resolveViewportMargins( - { - left: toNumber(safeArea.left.value), - top: toNumber(safeArea.top.value), - right: toNumber(safeArea.right.value), - bottom: toNumber(safeArea.bottom.value), - }, - layout.value, -)) +const panelMargins = computed(() => resolveViewportMargins(safeArea.value, layout.value)) const dockEl = useTemplateRef('dockEl') -const anchorEl = useTemplateRef('anchorEl') const recalculateCounter = ref(0) const isHovering = ref(false) diff --git a/packages/hub-ui/src/client/style.css b/packages/hub-ui/src/client/style.css index 73a81dc4..ff9da9c3 100644 --- a/packages/hub-ui/src/client/style.css +++ b/packages/hub-ui/src/client/style.css @@ -28,6 +28,10 @@ --app-scrollbar-radius: 1px; --app-scrollbar-thumb: #8884; --app-scrollbar-thumb-hover: #8885; + --devframes-safe-area-top: env(safe-area-inset-top, 0px); + --devframes-safe-area-right: env(safe-area-inset-right, 0px); + --devframes-safe-area-bottom: env(safe-area-inset-bottom, 0px); + --devframes-safe-area-left: env(safe-area-inset-left, 0px); } ::-webkit-scrollbar { diff --git a/packages/hub-ui/src/client/utils/useSafeArea.ts b/packages/hub-ui/src/client/utils/useSafeArea.ts new file mode 100644 index 00000000..eb94ff80 --- /dev/null +++ b/packages/hub-ui/src/client/utils/useSafeArea.ts @@ -0,0 +1,36 @@ +import type { MaybeElementRef } from '@vueuse/core' +import type { DockMargins } from '../components/dock/dock-layout' +import { tryOnMounted, unrefElement, useDebounceFn, useEventListener } from '@vueuse/core' +import { shallowRef } from 'vue' + +/** + * Ported from VueUse's `useScreenSafeArea` for now: it always sets its CSS variables on + * `` and has no option for a custom element, which breaks hydration on SSR hosts. + * Reads the `--devframes-safe-area-*` variables (see `style.css`) from `target`, in px. + */ +export function useSafeArea(target: MaybeElementRef) { + const insets = shallowRef({ top: 0, right: 0, bottom: 0, left: 0 }) + + function read(style: CSSStyleDeclaration, side: keyof DockMargins) { + // The computed value is a length such as `34px`, so parse it as a float. + return Number.parseFloat(style.getPropertyValue(`--devframes-safe-area-${side}`)) || 0 + } + + function update() { + const element = unrefElement(target) + if (!element) + return + const style = getComputedStyle(element) + insets.value = { + top: read(style, 'top'), + right: read(style, 'right'), + bottom: read(style, 'bottom'), + left: read(style, 'left'), + } + } + + tryOnMounted(update) + useEventListener('resize', useDebounceFn(update), { passive: true }) + + return insets +}