Expandable Action Bar
Preview
<script setup lang="ts">
import { AnimatePresence, motion } from 'motion-v'
import { computed, ref } from 'vue'
import { ExpandableActionBar } from 'beui-vue'
import { DemoIcon, type DemoIconName } from './action-bar-icons'
const ACTIONS: { id: string; label: string; icon: DemoIconName; shortcut?: string; badge?: string }[] = [
{ id: 'send', label: 'Send', icon: 'Send', shortcut: 'S' },
{ id: 'copy', label: 'Copy', icon: 'Copy', shortcut: 'C' },
{ id: 'download', label: 'Export', icon: 'Download', shortcut: 'E' },
{ id: 'archive', label: 'Archive', icon: 'Archive' },
{ id: 'alerts', label: 'Alerts', icon: 'Bell', badge: '3' },
{ id: 'settings', label: 'Settings', icon: 'Settings' },
]
const expanded = ref(false)
const activeId = ref('send')
const items = computed(() => ACTIONS.map(({ id, label, shortcut, badge }) => ({ id, label, shortcut, badge, active: id === activeId.value })))
const iconOf = new Map(ACTIONS.map(action => [action.id, action.icon]))
const SPRING = { type: 'spring', bounce: 0, duration: 0.3 } as const
</script>
<template>
<div class="bar-stage">
<div class="bar-row">
<ExpandableActionBar v-model:expanded="expanded" :items="items" :active-id="activeId" :class-names="{ item: 'bar-item' }" @action="activeId = $event.id">
<template #icon="{ item }"><DemoIcon :name="iconOf.get(item.id)!" :class="`bar-icon bar-icon-${item.id}`" /></template>
</ExpandableActionBar>
</div>
<div class="bar-controls">
<motion.button type="button" class="toggle" :while-hover="{ scale: 1.02 }" :while-press="{ scale: 0.96 }" @click="expanded = !expanded">
<motion.div layout class="toggle-inner">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="toggle-icon">
<motion.path :initial="false" :animate="{ d: expanded ? 'M 10 20 L 10 14 L 4 14' : 'M 9 21 L 3 21 L 3 15' }" :transition="SPRING" />
<motion.path :initial="false" :animate="{ d: expanded ? 'M 14 4 L 14 10 L 20 10' : 'M 15 3 L 21 3 L 21 9' }" :transition="SPRING" />
<line x1="14" x2="21" y1="10" y2="3" />
<line x1="3" x2="10" y1="21" y2="14" />
</svg>
<AnimatePresence mode="popLayout" :initial="false">
<motion.span :key="expanded ? 'expanded' : 'collapsed'" :initial="{ opacity: 0, y: -25, filter: 'blur(4px)' }"
:animate="{ opacity: 1, y: 0, filter: 'blur(0px)' }" :exit="{ opacity: 0, y: 25, filter: 'blur(4px)' }" :transition="SPRING">
{{ expanded ? 'Collapse' : 'Expand' }}
</motion.span>
</AnimatePresence>
</motion.div>
</motion.button>
</div>
</div>
</template>
<style scoped>
.bar-stage { display: flex; min-height: 18rem; width: 100%; flex-direction: column; align-items: center; justify-content: center; gap: 1.5rem; }
.bar-row { display: flex; min-height: 6rem; width: 100%; align-items: center; justify-content: center; }
.bar-controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .5rem; }
.toggle { position: relative; display: flex; box-sizing: border-box; width: 110px; height: 2.25rem; align-items: center; justify-content: center; overflow: hidden; margin: 0; padding: 0; border: 1px solid var(--beui-border); border-radius: 9999px; background: var(--beui-card); color: var(--beui-foreground); font: inherit; font-size: .75rem; line-height: 1rem; font-weight: 500; 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); }
.toggle:hover { border-color: var(--border-strong, color-mix(in srgb, var(--beui-foreground) 16%, transparent)); }
.toggle-inner { display: flex; align-items: center; gap: .375rem; }
.toggle-icon { width: .875rem; height: .875rem; flex: none; }
.bar-icon { width: 1rem; height: 1rem; }
.bar-icon-alerts { transform-origin: top; }
@media (prefers-reduced-motion: no-preference) {
:deep(.bar-item:hover) .bar-icon-send { animation: action-send 520ms var(--ease-out, cubic-bezier(.16, 1, .3, 1)) both; }
:deep(.bar-item:hover) .bar-icon-copy { animation: action-copy 520ms var(--ease-out, cubic-bezier(.16, 1, .3, 1)) both; }
:deep(.bar-item:hover) .bar-icon-download { animation: action-download 540ms var(--ease-out, cubic-bezier(.16, 1, .3, 1)) both; }
:deep(.bar-item:hover) .bar-icon-archive { animation: action-archive 520ms var(--ease-out, cubic-bezier(.16, 1, .3, 1)) both; }
:deep(.bar-item:hover) .bar-icon-alerts { animation: action-bell 680ms var(--ease-out, cubic-bezier(.16, 1, .3, 1)) both; }
:deep(.bar-item:hover) .bar-icon-settings { animation: action-settings 720ms var(--ease-out, cubic-bezier(.16, 1, .3, 1)) both; }
}
@keyframes action-send { 0%, 100% { transform: translate(0, 0); } 45% { transform: translate(2px, -2px); } }
@keyframes action-copy { 0%, 100% { transform: translate(0, 0); } 40% { transform: translate(1.5px, -1.5px); } 70% { transform: translate(-1px, 1px); } }
@keyframes action-download { 0%, 100% { transform: translateY(0); } 42% { transform: translateY(2.5px); } }
@keyframes action-archive { 0%, 100% { transform: scale(1); } 45% { transform: scale(.9); } }
@keyframes action-bell { 0%, 100% { transform: rotate(0deg); } 18% { transform: rotate(14deg); } 36% { transform: rotate(-12deg); } 54% { transform: rotate(8deg); } 72% { transform: rotate(-5deg); } }
@keyframes action-settings { 0% { transform: rotate(0deg); } 100% { transform: rotate(90deg); } }
</style><script lang="ts">
export type ExpandableActionBarSize = 'sm' | 'md'
export type BlockAction = {
id: string
label: string
icon?: string
onClick?: () => void
disabled?: boolean
active?: boolean
badge?: string
shortcut?: string
ariaLabel?: string
title?: string
}
export type ExpandableActionBarClassNames = {
root?: string
track?: string
item?: string
activeItem?: string
icon?: string
label?: string
badge?: string
shortcut?: string
}
</script>
<script setup lang="ts">
import { LayoutGroup, motion } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, onBeforeUnmount, shallowRef, watchEffect } from 'vue'
const props = withDefaults(defineProps<{
items: readonly BlockAction[]
expanded?: boolean
defaultExpanded?: boolean
activeId?: string
/**
* Expand when a hovering pointer rests on the bar. It also governs the touch
* equivalent: with no hover to reveal the labels, the first tap expands the
* bar and runs no action, and the second one acts. Set false to make every
* tap and click act immediately.
*/
expandOnHover?: boolean
expandOnFocus?: boolean
collapseDelay?: number
/** Default `md`. */
size?: ExpandableActionBarSize
className?: string
classNames?: ExpandableActionBarClassNames
}>(), {
// `undefined`, not `false`: a boolean prop that is absent is cast to `false`
// by Vue, which would read as a controlled bar that is always collapsed.
expanded: undefined,
defaultExpanded: false,
expandOnHover: true,
expandOnFocus: true,
collapseDelay: 90,
size: 'md',
})
const emit = defineEmits<{
'update:expanded': [value: boolean]
expandedChange: [value: boolean]
action: [item: BlockAction]
}>()
defineSlots<{
/** Replaces an action's icon. */
icon?(props: { item: BlockAction }): unknown
/** Replaces a whole action's contents (icon, label, shortcut and badge). */
item?(props: { item: BlockAction; expanded: boolean; active: boolean }): unknown
/** Alias of `item`, named after the React `renderItem` prop. */
renderItem?(props: { item: BlockAction; expanded: boolean; active: boolean }): unknown
}>()
const reduce = useReducedMotion()
// A pointer session on an action button, recorded where the click cannot report
// it: which input started the gesture, and whether the bar was already open.
type TapRecord = { pointerType: string; state: boolean }
let tapRecord: TapRecord | null = null
const tapStart = (event: PointerEvent, state: boolean) => { tapRecord = { pointerType: event.pointerType, state } }
const tapTake = () => { const spent = tapRecord; tapRecord = null; return spent }
const tapDrop = () => { tapRecord = null }
const localExpanded = shallowRef(props.defaultExpanded)
const isExpanded = computed(() => (props.expanded !== undefined ? props.expanded : localExpanded.value))
function setIsExpanded(next: boolean) {
if (props.expanded === undefined) localExpanded.value = next
emit('update:expanded', next)
emit('expandedChange', next)
}
const hoveredId = shallowRef<string | null>(null)
// Set by the tap that expands the bar, and the reason the outside-tap dismisser
// exists at all — a hovering pointer has its own way out.
const tapExpanded = shallowRef(false)
let collapseTimer: number | null = null
function clearCollapseTimer() {
if (collapseTimer !== null) window.clearTimeout(collapseTimer)
collapseTimer = null
}
function open() {
clearCollapseTimer()
setIsExpanded(true)
}
function close() {
clearCollapseTimer()
collapseTimer = window.setTimeout(() => {
setIsExpanded(false)
hoveredId.value = null
tapExpanded.value = false
}, props.collapseDelay)
}
// Enter and leave are paired per pointer: a pen that arrives in contact taps
// rather than hovers, and a mouse that pressed and dragged off still leaves.
const hoverContact = new Set<number>()
function hoverEnter(event: PointerEvent) {
const hovering = event.pointerType !== 'touch' && event.buttons === 0
if (hovering) hoverContact.delete(event.pointerId)
else hoverContact.add(event.pointerId)
return hovering
}
function hoverLeave(event: PointerEvent) {
const arrivedInContact = hoverContact.delete(event.pointerId)
return !arrivedInContact && event.pointerType !== 'touch'
}
function onRootPointerEnter(event: PointerEvent) {
// The gesture is told about every enter, `expandOnHover` or not: it is what
// the matching leave is read against.
if (hoverEnter(event) && props.expandOnHover) open()
}
function onRootPointerLeave(event: PointerEvent) {
if (!hoverLeave(event)) return
hoveredId.value = null
if (props.expandOnHover) close()
}
function onRootFocus() {
if (props.expandOnFocus) open()
}
function onRootBlur(event: FocusEvent) {
const next = event.relatedTarget
if (next instanceof Node && (event.currentTarget as HTMLElement | null)?.contains(next)) return
if (props.expandOnFocus) close()
}
function onItemEnter(item: BlockAction, event: PointerEvent) {
if (!hoverEnter(event)) return
clearCollapseTimer()
hoveredId.value = item.id
}
function onItemDown(event: PointerEvent) {
tapStart(event, isExpanded.value)
}
function onItemClick(item: BlockAction, event: MouseEvent) {
;(event.currentTarget as HTMLElement | null)?.blur()
const gesture = tapTake()
// Nothing reveals the labels to a finger, so the first tap expands the bar and
// the next one runs the action. The bar state is read from the gesture's
// start: a browser that focuses the button on contact expands it mid-tap, and
// that first tap would otherwise fire the action it was meant to reveal.
// `tapExpanded` arms the second tap, so a controlled bar that declines to
// expand still runs the action rather than swallowing every tap.
const firstTap = gesture !== null && gesture.pointerType !== 'mouse' && !gesture.state && !tapExpanded.value
if (firstTap && props.expandOnHover) {
tapExpanded.value = true
open()
hoveredId.value = item.id
return
}
if (item.disabled) return
item.onClick?.()
emit('action', item)
}
const activeItemId = computed(() => props.activeId ?? props.items.find(item => item.active)?.id)
const highlightId = computed(() => hoveredId.value ?? activeItemId.value)
/* ---- dismissal for a bar a tap opened ----------------------------------- */
// A finger never hovers and Safari does not focus a button on tap, so a bar a
// tap expanded would have nothing to close it. The tap that lands elsewhere
// stands in for the pointer leaving — and it is consumed rather than passed
// through, because the labelled bar is exactly the kind of surface people
// dismiss by tapping just past it, over whatever control is there.
const dismissOpen = computed(() => tapExpanded.value && isExpanded.value)
let dismissRoot: HTMLElement | null = null
function consumeActivation(source: Event) {
const swallow = (event: MouseEvent) => {
event.preventDefault()
event.stopPropagation()
release()
}
const restart = (event: Event) => { if (event !== source) release() }
const release = () => {
window.removeEventListener('click', swallow, true)
window.removeEventListener('pointerdown', restart, true)
window.removeEventListener('pointercancel', restart, true)
window.removeEventListener('keydown', release, true)
}
window.addEventListener('click', swallow, true)
window.addEventListener('pointerdown', restart, true)
window.addEventListener('pointercancel', restart, true)
window.addEventListener('keydown', release, true)
}
function outsideDismiss(event: PointerEvent) {
if (event.target instanceof Node && dismissRoot?.contains(event.target)) return
// The click that this gesture ends with is the dismissal, so it is swallowed.
consumeActivation(event)
close()
}
function escapeDismiss(event: KeyboardEvent) {
if (event.key === 'Escape') close()
}
function bindDismiss(element: unknown) {
const node = element as (HTMLElement & { $el?: HTMLElement }) | null
dismissRoot = node?.$el ?? node ?? null
applyDismiss()
}
function applyDismiss() {
if (typeof window === 'undefined') return
window.removeEventListener('pointerdown', outsideDismiss, true)
window.removeEventListener('keydown', escapeDismiss)
if (!dismissOpen.value) return
window.addEventListener('pointerdown', outsideDismiss, true)
window.addEventListener('keydown', escapeDismiss)
}
// Re-evaluated whenever the open flag or the track changes, as the React effect
// keyed to the open flag does.
watchEffect(applyDismiss)
onBeforeUnmount(() => {
clearCollapseTimer()
window.removeEventListener('pointerdown', outsideDismiss, true)
window.removeEventListener('keydown', escapeDismiss)
tapDrop()
})
/* ---- transitions and sizes ---------------------------------------------- */
const ITEM_TRANSITION = { type: 'spring', stiffness: 460, damping: 34, mass: 0.62 } as const
const LABEL_TRANSITION = { type: 'spring', stiffness: 380, damping: 32, mass: 0.7 } as const
const labelTarget = computed(() => (isExpanded.value
? { width: 'auto', opacity: 1, x: 0, marginLeft: 8, filter: 'blur(0px)' }
: { width: 0, opacity: 0, x: reduce.value ? 0 : -4, marginLeft: 0, filter: reduce.value ? 'blur(0px)' : 'blur(3px)' }))
const shortcutTarget = computed(() => (isExpanded.value
? { width: 'auto', opacity: 1, marginLeft: 4 }
: { width: 0, opacity: 0, marginLeft: 0 }))
</script>
<template>
<LayoutGroup>
<!-- Pointer events, not the mouse pair: a tap fires compatibility
mouseenter/mouseleave that carry no pointerType, and the bar growing
under a stationary finger fired the leave before the click ever landed. -->
<motion.div
layout="size"
class="action-bar"
:class="[classNames?.root, className]"
:transition="ITEM_TRANSITION"
@pointerenter="onRootPointerEnter"
@pointerleave="onRootPointerLeave"
@focusin="onRootFocus"
@focusout="onRootBlur">
<motion.div
:ref="bindDismiss"
layout="size"
class="action-track"
:class="[classNames?.track, `is-${size}`]"
:transition="ITEM_TRANSITION">
<motion.button
v-for="item in items"
:key="item.id"
layout="position"
type="button"
class="action-item"
:class="[classNames?.item, `is-${size}`, { 'is-highlighted': highlightId === item.id }, (item.active || activeId === item.id) && classNames?.activeItem]"
:disabled="item.disabled"
:title="item.title ?? (typeof item.label === 'string' ? item.label : undefined)"
:aria-label="item.ariaLabel ?? item.label"
:transition="ITEM_TRANSITION"
:while-press="reduce || item.disabled ? undefined : { scale: 0.96 }"
@pointerenter="onItemEnter(item, $event)"
@pointerdown="onItemDown($event)"
@pointercancel="tapDrop"
@keydown="tapDrop"
@click="onItemClick(item, $event)">
<motion.span v-if="highlightId === item.id" layout-id="action-bar-highlight" class="action-highlight" :transition="ITEM_TRANSITION" />
<slot name="renderItem" :item="item" :expanded="isExpanded" :active="item.active || activeId === item.id">
<slot name="item" :item="item" :expanded="isExpanded" :active="item.active || activeId === item.id">
<span class="action-icon" :class="[classNames?.icon, `is-${size}`]">
<slot name="icon" :item="item">{{ item.icon ?? item.label.slice(0, 1) }}</slot>
</span>
<motion.span
:aria-hidden="!isExpanded"
:animate="reduce ? { width: isExpanded ? 'auto' : 0, opacity: isExpanded ? 1 : 0, marginLeft: isExpanded ? 8 : 0, x: 0, filter: 'blur(0px)' } : labelTarget"
:transition="reduce ? { duration: 0 } : LABEL_TRANSITION"
class="action-label"
:class="classNames?.label">{{ item.label }}</motion.span>
<motion.span
v-if="item.shortcut"
:aria-hidden="!isExpanded"
:animate="shortcutTarget"
:transition="LABEL_TRANSITION"
class="action-shortcut"
:class="classNames?.shortcut">{{ item.shortcut }}</motion.span>
<span
v-if="item.badge"
class="action-badge"
:class="[classNames?.badge, { 'is-collapsed': !isExpanded }]">{{ item.badge }}</span>
</slot>
</slot>
</motion.button>
</motion.div>
</motion.div>
</LayoutGroup>
</template>
<style scoped>
.action-bar {
display: inline-flex;
max-width: 100%;
color: var(--beui-foreground);
}
.action-track {
position: relative;
display: inline-flex;
align-items: center;
max-width: 100%;
overflow-x: auto;
overflow-y: hidden;
scrollbar-width: none;
border: 1px solid var(--beui-border);
border-radius: 999px;
background: color-mix(in srgb, var(--beui-card) 90%, transparent);
box-shadow: 0 25px 50px -12px rgb(0 0 0 / .25);
backdrop-filter: blur(24px);
}
.action-track::-webkit-scrollbar { display: none; }
.action-track.is-sm { min-height: 2.25rem; gap: .25rem; padding: .25rem; font-size: .75rem; line-height: calc(1 / .75); }
.action-track.is-md { min-height: 2.75rem; gap: .375rem; padding: .375rem; font-size: .875rem; line-height: calc(1.25 / .875); }
.action-item {
position: relative;
isolation: isolate;
display: inline-flex;
flex: none;
align-items: center;
justify-content: center;
overflow: hidden;
border: 0;
border-radius: 999px;
background: transparent;
color: var(--beui-muted-foreground);
font: inherit;
font-weight: 500;
outline: none;
cursor: pointer;
transition: color .15s cubic-bezier(.16, 1, .3, 1), background-color .15s cubic-bezier(.16, 1, .3, 1);
}
.action-item.is-sm { height: 1.75rem; min-width: 1.75rem; padding: 0 .375rem; }
.action-item.is-md { height: 2rem; min-width: 2rem; padding: 0 .5rem; }
.action-item.is-highlighted { color: var(--beui-foreground); }
.action-item:focus-visible { color: var(--beui-foreground); outline: 2px solid var(--beui-ring); outline-offset: 2px; }
.action-item:disabled { pointer-events: none; opacity: .4; }
.action-highlight { position: absolute; inset: 0; z-index: -10; border-radius: 999px; background: color-mix(in srgb, var(--beui-muted) 80%, transparent); }
.action-icon { display: inline-flex; flex: none; align-items: center; justify-content: center; }
.action-icon.is-sm { width: .875rem; height: .875rem; }
.action-icon.is-md { width: 1rem; height: 1rem; }
.action-label { display: inline-block; overflow: hidden; white-space: nowrap; }
.action-shortcut { display: none; overflow: hidden; white-space: nowrap; font-size: 10px; color: var(--beui-muted-foreground); }
@media (min-width: 640px) { .action-shortcut { display: inline-block; } }
.action-badge {
display: inline-flex;
align-items: center;
justify-content: center;
margin-inline-start: .125rem;
height: 1rem;
min-width: 1rem;
padding: 0 .25rem;
border-radius: 999px;
background: var(--beui-destructive);
color: var(--beui-primary-foreground);
font-size: 10px;
line-height: 1;
}
.action-badge.is-collapsed { position: absolute; right: .125rem; top: .125rem; margin: 0; }
@media (prefers-reduced-motion: reduce) {
.action-item { transition: none; }
}
</style>Install
Add it with your package manager, or copy the source manually.
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/expandable-action-bar$ 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 ExpandableActionBar from '@/components/beui/blocks/ExpandableActionBar.vue'
import '@/components/beui/styles.css'API Reference
itemsreadonly BlockAction[]—v-model:expanded?booleanundefineddefaultExpanded?booleanfalseactiveId?string—@action?(item: BlockAction) => void—size?ExpandableActionBarSizeDefault `md`.
mdexpandOnHover?booleanExpand when a hovering pointer rests on the bar. It also governs the touch equivalent: with no hover to reveal the labels, the first tap expands the bar and runs no action, and the second one acts. Set false to make every tap and click act immediately.
trueexpandOnFocus?booleantruecollapseDelay?number90className?string—classNames?ExpandableActionBarClassNames—#renderItem?{ item: BlockAction; expanded: boolean; active: boolean }Alias of `item`, named after the React `renderItem` prop.
—@expanded-change?(value: boolean) => void—#icon?{ item: BlockAction }Replaces an action's icon.
—#item?{ item: BlockAction; expanded: boolean; active: boolean }Replaces a whole action's contents (icon, label, shortcut and badge).
—Related components
Expandable Tabs
Icon tab bar where the active tab expands to a labelled pill, with a panel above that morphs height and slides content direction-aware on switch.
Overflow Actions
Connected pill rail for primary actions that springs open to reveal extra controls.
Bloom Menu
A button that morphs open into a menu and blooms iris-out from the center, the grid revealing in every direction with radially staggered items.
Updated