Popover
Morph Popover
PopoverMorph.vue<script setup lang="ts">
import { MorphPopoverContent, MorphPopoverTrigger, PopoverMorph } from 'beui-vue'
import { ref, defineComponent, h, type PropType } from 'vue'
/** Inline Lucide glyphs for the popover and tooltip demos (generated from lucide-react, ISC). */
const ICONS: Record<string, string> = {
"ArrowDown": "<path d=\"M12 5v14\"></path><path d=\"m19 12-7 7-7-7\"></path>",
"ArrowLeft": "<path d=\"m12 19-7-7 7-7\"></path><path d=\"M19 12H5\"></path>",
"ArrowRight": "<path d=\"M5 12h14\"></path><path d=\"m12 5 7 7-7 7\"></path>",
"ArrowUp": "<path d=\"m5 12 7-7 7 7\"></path><path d=\"M12 19V5\"></path>",
"Bold": "<path d=\"M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8\"></path>",
"ChevronDown": "<path d=\"m6 9 6 6 6-6\"></path>",
"Copy": "<rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path>",
"Italic": "<line x1=\"19\" x2=\"10\" y1=\"4\" y2=\"4\"></line><line x1=\"14\" x2=\"5\" y1=\"20\" y2=\"20\"></line><line x1=\"15\" x2=\"9\" y1=\"4\" y2=\"20\"></line>",
"Link2": "<path d=\"M9 17H7A5 5 0 0 1 7 7h2\"></path><path d=\"M15 7h2a5 5 0 1 1 0 10h-2\"></path><line x1=\"8\" x2=\"16\" y1=\"12\" y2=\"12\"></line>",
"Pencil": "<path d=\"M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z\"></path><path d=\"m15 5 4 4\"></path>",
"Share2": "<circle cx=\"18\" cy=\"5\" r=\"3\"></circle><circle cx=\"6\" cy=\"12\" r=\"3\"></circle><circle cx=\"18\" cy=\"19\" r=\"3\"></circle><line x1=\"8.59\" x2=\"15.42\" y1=\"13.51\" y2=\"17.49\"></line><line x1=\"15.41\" x2=\"8.59\" y1=\"6.51\" y2=\"10.49\"></line>",
"Strikethrough": "<path d=\"M16 4H9a3 3 0 0 0-2.83 4\"></path><path d=\"M14 12a4 4 0 0 1 0 8H6\"></path><line x1=\"4\" x2=\"20\" y1=\"12\" y2=\"12\"></line>",
"Trash2": "<path d=\"M10 11v6\"></path><path d=\"M14 11v6\"></path><path d=\"M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6\"></path><path d=\"M3 6h18\"></path><path d=\"M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2\"></path>",
"Underline": "<path d=\"M6 4v6a6 6 0 0 0 12 0V4\"></path><line x1=\"4\" x2=\"20\" y1=\"20\" y2=\"20\"></line>"
}
export const PopIcon = defineComponent({
name: 'PopIcon',
props: { name: { type: String as PropType<keyof typeof ICONS>, required: true }, size: { type: Number, default: 16 } },
setup: props => () => h('svg', {
xmlns: 'http://www.w3.org/2000/svg', width: props.size, height: props.size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor',
'stroke-width': 2, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true', innerHTML: ICONS[props.name] ?? '',
}),
})
const open = ref(false)
const actions = [
{ icon: 'Pencil', label: 'Edit' },
{ icon: 'Copy', label: 'Duplicate' },
{ icon: 'Share2', label: 'Share' },
{ icon: 'Trash2', label: 'Delete' },
]
</script>
<template>
<PopoverMorph v-model:open="open">
<MorphPopoverTrigger>
<button type="button" class="trigger">
Options
<PopIcon name="ChevronDown" class="chevron" />
</button>
</MorphPopoverTrigger>
<MorphPopoverContent align="start" class="morph-demo-panel">
<button v-for="action in actions" :key="action.label" type="button" class="action" @click="open = false">
<PopIcon :name="action.icon" class="icon" />
{{ action.label }}
</button>
</MorphPopoverContent>
</PopoverMorph>
</template>
<style scoped>
.trigger { display: inline-flex; height: 2.5rem; align-items: center; gap: .5rem; border: 1px solid var(--beui-border); border-radius: .75rem; background: var(--beui-background); padding: 0 1rem; font: inherit; font-size: .875rem; font-weight: 500; line-height: 1.25rem; color: var(--beui-foreground); outline: none; cursor: pointer; transition: color .15s cubic-bezier(.4, 0, .2, 1), background-color .15s cubic-bezier(.4, 0, .2, 1), border-color .15s cubic-bezier(.4, 0, .2, 1); }
.trigger:hover { border-color: var(--beui-border-strong); }
.trigger:focus-visible { box-shadow: 0 0 0 2px var(--beui-ring); }
.chevron, .icon { color: var(--beui-muted-foreground); }
.action { display: flex; width: 100%; align-items: center; gap: .625rem; border: 0; border-radius: .5rem; background: transparent; padding: .5rem .625rem; text-align: left; font: inherit; font-size: .875rem; line-height: 1.25rem; color: var(--beui-foreground); outline: none; cursor: pointer; transition: color .15s cubic-bezier(.4, 0, .2, 1), background-color .15s cubic-bezier(.4, 0, .2, 1); }
.action:hover, .action:focus-visible { background: var(--beui-muted); }
:global(.morph-demo-panel) { width: 12rem; padding: .375rem; }
</style><script setup lang="ts">
import { animate, useMotionValue } from 'motion-v'
import { useReducedMotion } from '../../core/reduced-motion'
import { computed, nextTick, onBeforeUnmount, onMounted, onUpdated, provide, shallowRef, useId, useSlots, useTemplateRef, watch, watchEffect, type StyleValue, type VNode } from 'vue'
import { easeOut } from '../../core/motion'
import { elementOf, firstElement, FOCUSABLE_SELECTOR, MORPH_PARTS_KEY, useDismiss, usePortalPosition, type MorphPart } from './pop-helpers'
import MorphPopoverContent from './MorphPopoverContent.vue'
/**
* A popover whose full-size panel reveals from the corner nearest the trigger
* on one spring, then retraces the same clip on close. Content and surface
* edges keep their size throughout. Closes on outside pointer or Escape.
* Ported from `components/motion/popover-morph.tsx`.
*/
const props = withDefaults(defineProps<{
side?: 'top' | 'bottom'
align?: 'start' | 'end'
/** Gap between trigger and panel, in px. */
sideOffset?: number
/** Panel corner radius, in px. */
radius?: number
/** Draw the surface shadow. */
shadow?: boolean
/** Built-in trigger text and fallback panel name. */
label?: string
/** Use the `#trigger` slot's own element as the trigger instead of a built-in button. */
asChild?: boolean
contentClass?: string
contentStyle?: StyleValue
defaultOpen?: boolean
/** Runs after the portalled surface is positioned and visible. Passing it hands focus to the caller. */
onOpenAutoFocus?: (content: HTMLElement) => void
/** Element to focus when a keyboard open shows the panel. Defaults to the first focusable element. */
initialFocus?: HTMLElement | null | (() => HTMLElement | null)
}>(), { side: 'bottom', align: 'start', sideOffset: 8, radius: 16, shadow: true, defaultOpen: false })
const open = defineModel<boolean | undefined>('open')
defineSlots<{
/** The trigger. With `asChild` its own element is the trigger; otherwise it is the label of the built-in button. */
trigger?(): unknown
/** Panel content, or `MorphPopoverTrigger` and `MorphPopoverContent` when composed from parts. `close` shuts the popover. */
default?(props: { close: () => void }): unknown
}>()
const slots = useSlots()
// Composed from parts: the default slot holds `MorphPopoverTrigger` (rendered in place, as the
// trigger) and `MorphPopoverContent` (which hands over the panel's content, classes and options).
const part = shallowRef<MorphPart | null>(null)
provide(MORPH_PARTS_KEY, {
register: next => { part.value = next },
unregister: next => { if (part.value === next) part.value = null },
})
function findContent(nodes: unknown): boolean {
return Array.isArray(nodes) && nodes.some((node: VNode) => node.type === MorphPopoverContent || (typeof node.type === 'symbol' && findContent(node.children)))
}
const composed = computed(() => findContent(slots.default?.({ close: () => {} })))
const eff = computed(() => {
const c = part.value?.props
return {
side: c?.side ?? props.side,
align: c?.align ?? props.align,
sideOffset: c?.sideOffset ?? props.sideOffset,
radius: c?.radius ?? props.radius,
shadow: c?.shadow ?? props.shadow,
initialFocus: c?.initialFocus ?? props.initialFocus,
onOpenAutoFocus: c?.onOpenAutoFocus ?? props.onOpenAutoFocus,
}
})
const PanelRender = () => composed.value ? part.value?.slots.default?.({ close }) : slots.default?.({ close })
const isOpen = computed(() => open.value ?? props.defaultOpen)
function setOpen(next: boolean) { open.value = next }
// Full-distance clip travel needs a shorter curve than the slow overdamped
// panel spring: a non-bouncing spring reaches its rest within 260 ms.
const MORPH_ENTER = { type: 'spring', duration: 0.26, bounce: 0 } as const
const MORPH_EXIT = { duration: 0.2, ease: easeOut } as const
const GUTTER = 12
const reducedMotion = useReducedMotion()
const reduce = computed(() => Boolean(reducedMotion.value))
const uid = useId().replace(/[^a-zA-Z0-9_-]/g, '')
const triggerId = `be-morph-trigger-${uid}`
const contentId = `be-morph-content-${uid}`
const root = useTemplateRef<HTMLElement>('root')
const host = useTemplateRef<HTMLElement>('host')
const button = useTemplateRef<unknown>('button')
const wrapper = useTemplateRef<HTMLElement>('wrapper')
const contentEl = useTemplateRef<HTMLElement>('contentEl')
const triggerEl = shallowRef<HTMLElement | null>(null)
const rootEl = shallowRef<HTMLElement | null>(null)
const mounted = shallowRef(false)
const rendered = shallowRef(isOpen.value)
function resolveTrigger() {
const next = props.asChild || composed.value ? firstElement(host.value) : elementOf(button.value)
if (next !== triggerEl.value) triggerEl.value = next
rootEl.value = root.value
}
onMounted(() => { mounted.value = true; resolveTrigger() })
onUpdated(resolveTrigger)
// The panel measures against the trigger; the root boxes it exactly and stands
// in until a real trigger registers.
const anchor = computed(() => triggerEl.value ?? rootEl.value)
const { layout } = usePortalPosition(anchor, contentEl, isOpen)
const progress = useMotionValue(0)
const fade = useMotionValue(reduce.value ? 0 : 1)
let focusedOnOpen = false
// A new session starts in the render that opens: measure fresh, never start an
// entrance at the previous session's coordinates.
watch(isOpen, value => {
if (!value || rendered.value) return
layout.value = null
progress.jump(0)
fade.jump(reduce.value ? 0 : 1)
focusedOnOpen = false
rendered.value = true
}, { flush: 'sync' })
// Focus -------------------------------------------------------------------------
// A keyboard open arms focus for the panel to take as it appears. Any later key
// or pointer press disarms it.
let focusOnOpen = false
let disarm: AbortController | null = null
function armFocusOnOpen(armed: boolean) {
disarm?.abort()
disarm = null
focusOnOpen = armed
if (!armed) return
const controller = new AbortController()
const clear = () => { focusOnOpen = false; controller.abort() }
document.addEventListener('keydown', clear, { capture: true, signal: controller.signal })
document.addEventListener('pointerdown', clear, { capture: true, signal: controller.signal })
disarm = controller
}
function takeFocusOnOpen() { const armed = focusOnOpen; armFocusOnOpen(false); return armed }
function close() {
setOpen(false)
const focused = document.activeElement
if (!(focused instanceof HTMLElement) || !contentEl.value?.contains(focused)) return
const restore = triggerEl.value ?? (rootEl.value && rootEl.value.tabIndex >= 0 ? rootEl.value : null)
restore?.focus()
}
useDismiss(isOpen, close, target => Boolean(root.value?.contains(target) || contentEl.value?.contains(target)))
// Trigger wiring ----------------------------------------------------------------
let keyboardPress = false
watch(triggerEl, (el, _, cleanup) => {
if (!el) return
const keydown = (event: KeyboardEvent) => { keyboardPress = event.key === 'Enter' || event.key === ' ' }
// A press gesture on the trigger (motion's whilePress) answers Enter with a synthetic pointerdown; only a real
// pointer means the open did not come from the keyboard.
const pointerdown = (event: PointerEvent) => { if (event.isTrusted) keyboardPress = false }
const click = (event: MouseEvent) => {
// Enter and Space reach here as a click with `detail` 0 after their keydown
// on this element. Only a keyboard open moves focus into the panel.
const keyboard = keyboardPress && event.detail === 0
keyboardPress = false
armFocusOnOpen(!isOpen.value && keyboard)
setOpen(!isOpen.value)
}
el.addEventListener('keydown', keydown)
el.addEventListener('pointerdown', pointerdown)
el.addEventListener('click', click)
cleanup(() => {
el.removeEventListener('keydown', keydown)
el.removeEventListener('pointerdown', pointerdown)
el.removeEventListener('click', click)
})
}, { immediate: true, flush: 'post' })
watchEffect(() => {
const el = triggerEl.value
if (!el) return
if (!el.id) el.id = triggerId
el.setAttribute('aria-haspopup', 'dialog')
el.setAttribute('aria-expanded', String(isOpen.value))
if (isOpen.value) el.setAttribute('aria-controls', contentId)
else el.removeAttribute('aria-controls')
})
// Geometry ----------------------------------------------------------------------
const ready = computed(() => layout.value !== null)
const preferredLeft = computed(() => {
const l = layout.value
if (!l) return 0
return eff.value.align === 'end' ? l.trigger.left + l.trigger.width - l.content.width : l.trigger.left
})
// Keep larger compositions, such as calendars, inside the viewport. Prefer the
// requested side, then use the roomier side when it cannot fit there.
const space = computed(() => {
const l = layout.value
if (!l || typeof window === 'undefined') return { below: 0, above: 0 }
return {
below: Math.max(0, window.innerHeight - l.trigger.top - l.trigger.height - eff.value.sideOffset - GUTTER),
above: Math.max(0, l.trigger.top - eff.value.sideOffset - GUTTER),
}
})
const resolvedSide = computed<'top' | 'bottom'>(() => {
const l = layout.value
if (!l) return eff.value.side
const requested = eff.value.side === 'bottom' ? space.value.below : space.value.above
const other = eff.value.side === 'bottom' ? space.value.above : space.value.below
return l.content.height > requested && other > requested ? (eff.value.side === 'bottom' ? 'top' : 'bottom') : eff.value.side
})
const availableHeight = computed(() => (resolvedSide.value === 'bottom' ? space.value.below : space.value.above))
const left = computed(() => {
const l = layout.value
if (!l) return 0
return Math.max(GUTTER, Math.min(preferredLeft.value, window.innerWidth - l.content.width - GUTTER))
})
const top = computed(() => {
const l = layout.value
if (!l) return 0
return resolvedSide.value === 'bottom' ? l.trigger.top + l.trigger.height + eff.value.sideOffset : Math.max(GUTTER, l.trigger.top - l.content.height - eff.value.sideOffset)
})
// An inset clip reveals the nearest corner without scaling borders, shadows or
// keyboard-focus geometry; one progress value keeps clip and opacity in step.
function clipAt(inset: number) {
const side = resolvedSide.value
return `inset(${side === 'top' ? inset : 0}% ${eff.value.align === 'start' ? inset : 0}% ${side === 'bottom' ? inset : 0}% ${eff.value.align === 'end' ? inset : 0}% round ${eff.value.radius}px)`
}
function paint() {
const p = progress.get()
if (wrapper.value) wrapper.value.style.opacity = String(reduce.value ? fade.get() : Math.min(1, Math.max(0, p / 0.15)))
if (contentEl.value) contentEl.value.style.clipPath = reduce.value ? '' : clipAt(92 * (1 - p))
}
watch([wrapper, contentEl, resolvedSide, reduce, () => eff.value.align, () => eff.value.radius], paint, { flush: 'post' })
const stops = [progress.on('change', paint), fade.on('change', paint)]
// Presence ------------------------------------------------------------------------
function gone() { rendered.value = false; armFocusOnOpen(false) }
let animation: { stop: () => void } | undefined
watch([ready, isOpen, reduce, rendered], () => {
animation?.stop()
animation = undefined
if (!rendered.value) return
if (!ready.value) { if (!isOpen.value) gone(); return }
const settle = () => { if (!isOpen.value) gone() }
if (reduce.value) {
progress.jump(1)
animation = animate(fade, isOpen.value ? 1 : 0, { duration: 0.12, onComplete: settle })
return
}
animation = animate(progress, isOpen.value ? 1 : 0, { ...(isOpen.value ? MORPH_ENTER : MORPH_EXIT), onComplete: settle })
}, { flush: 'post' })
// Runs after the portalled surface is positioned and visible.
watch([ready, isOpen], async () => {
if (!ready.value || !isOpen.value || focusedOnOpen || !contentEl.value) return
focusedOnOpen = true
// The wrapper turns visible in the render this measurement schedules, and
// focus() inside a hidden subtree is silently ignored.
await nextTick()
if (!isOpen.value || !contentEl.value) return
eff.value.onOpenAutoFocus?.(contentEl.value)
if (eff.value.onOpenAutoFocus || !takeFocusOnOpen()) return
const panel = contentEl.value
const shell = wrapper.value
if (!shell) return
const from = document.activeElement
const unmoved = () => document.activeElement === from || document.activeElement === panel
// The selector also matches elements focus() refuses; keep the first
// candidate that takes focus, else hold it on the panel.
const focusFirst = () => {
const initial = eff.value.initialFocus
const wanted = typeof initial === 'function' ? initial() : initial
for (const candidate of [wanted, ...panel.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)]) {
if (!candidate) continue
candidate.focus({ preventScroll: true })
if (unmoved()) continue
candidate.scrollIntoView?.({ block: 'nearest' })
return true
}
panel.focus({ preventScroll: true })
return false
}
if (focusFirst()) return
const retry = (event: TransitionEvent) => {
if (event.propertyName !== 'visibility') return
if (!unmoved() || focusFirst()) stop()
}
const stop = () => {
shell.removeEventListener('transitionend', retry)
document.removeEventListener('keydown', stop, true)
document.removeEventListener('pointerdown', stop, true)
}
shell.addEventListener('transitionend', retry)
document.addEventListener('keydown', stop, true)
document.addEventListener('pointerdown', stop, true)
}, { flush: 'post' })
onBeforeUnmount(() => { animation?.stop(); for (const stop of stops) stop(); disarm?.abort() })
defineExpose({ close, open: () => setOpen(true) })
</script>
<template>
<span ref="root" class="be-morph-popover">
<span v-if="asChild || composed" ref="host" class="be-morph-host"><slot name="trigger" /><slot v-if="composed" :close="close" /></span>
<button v-else ref="button" type="button" class="be-morph-trigger"><slot name="trigger">{{ label }}</slot></button>
<Teleport v-if="mounted && rendered" to="body">
<div ref="wrapper" data-morph-popover-portal="" class="be-morph-portal" :class="{ shadow: eff.shadow }" :inert="!isOpen"
:style="{ left: `${left}px`, top: `${top}px`, pointerEvents: isOpen ? 'auto' : 'none', visibility: layout ? 'visible' : 'hidden' }">
<div ref="contentEl" :id="contentId" role="dialog" :aria-labelledby="triggerEl?.id || triggerId" tabindex="-1" class="be-morph-dialog" :class="[contentClass, part?.attrs.class]"
:style="[{ borderRadius: `${eff.radius}px`, maxHeight: layout ? `${availableHeight}px` : undefined }, contentStyle, part?.attrs.style as StyleValue]">
<PanelRender />
</div>
</div>
</Teleport>
</span>
</template>
<style scoped>
.be-morph-popover { position: relative; display: inline-flex; }
.be-morph-host { display: contents; }
.be-morph-trigger { display: inline-flex; height: 2.5rem; align-items: center; gap: .5rem; border: 1px solid var(--beui-border); border-radius: .75rem; background: var(--beui-background); padding: 0 1rem; font: inherit; font-size: .875rem; font-weight: 500; line-height: 1.25rem; color: var(--beui-foreground); outline: none; cursor: pointer; transition: border-color .15s; }
.be-morph-trigger:hover { border-color: var(--beui-border-strong); }
.be-morph-trigger:focus-visible { box-shadow: 0 0 0 2px var(--beui-ring); }
.be-morph-portal { position: fixed; z-index: 9999; }
/* A drop-shadow follows the clipped surface; box-shadow would be cut off by its own clip. */
.be-morph-portal.shadow { filter: drop-shadow(0 10px 18px rgba(0, 0, 0, .14)); }
.be-morph-dialog { box-sizing: border-box; overflow-x: hidden; overflow-y: auto; border: 1px solid var(--beui-border); background: var(--beui-background); color: var(--beui-foreground); outline: none; }
/* An inset ring stays inside the clip that would cut off an outline. */
.be-morph-dialog:focus-visible { box-shadow: inset 0 0 0 2px var(--beui-ring); }
</style>Install
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/popover-morph$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import PopoverMorph from '@/components/beui/controls/popover/PopoverMorph.vue'
import '@/components/beui/styles.css'API Reference
MorphPopover
v-model:open?boolean | undefinedControlled open state.
—defaultOpen?booleanUncontrolled initial open state.
falseclass?string—side?"top" | "bottom"bottomalign?"start" | "end"startsideOffset?numberGap between trigger and panel, in px.
8radius?numberPanel corner radius, in px.
16shadow?booleanDraw the surface shadow.
truelabel?stringBuilt-in trigger text and fallback panel name.
—asChild?booleanUse the `#trigger` slot's own element as the trigger instead of a built-in button.
—contentClass?string—contentStyle?StyleValue—onOpenAutoFocus?(content: HTMLElement) => voidRuns after the portalled surface is positioned and visible. Passing it hands focus to the caller.
—initialFocus?HTMLElement | null | (() => HTMLElement | null)Element to focus when a keyboard open shows the panel. Defaults to the first focusable element.
—#trigger?slotThe trigger. With `asChild` its own element is the trigger; otherwise it is the label of the built-in button.
—#default?{ close: () => void }Panel content, or `MorphPopoverTrigger` and `MorphPopoverContent` when composed from parts. `close` shuts the popover.
—MorphPopoverContent
side?"top" | "bottom"—align?"start" | "end"—sideOffset?numberGap between trigger and panel, in px.
—radius?numberPanel corner radius, in px.
—shadow?booleanDraw the surface shadow.
undefinedonOpenAutoFocus?(content: HTMLElement) => voidRuns after the portalled surface is positioned and visible. Passing it hands focus to the caller.
—initialFocus?HTMLElement | null | (() => HTMLElement | null)Element that takes focus when the panel opens from the keyboard; falls back to the first focusable control.
—class?string—#default?{ close: () => void }Panel content. `close` shuts the popover.
—Related components
Morphing Modal
Family-app-style modal. A single panel that morphs its height as you navigate between inner views, with blur cross-fade on content.
Drawer
Side panel that slides in from the left or right with a spring, backdrop blur, body scroll lock and esc-to-close.
Tooltip
Composable hover, focus and tap tooltip with a subtle scale and fade, collision-aware side and alignment, an optional arrow, a pointer corridor onto hoverable content, one label at a time, keyboard-only focus reveal and optional cursor tracking.
Updated