MagicTray
MagicTray is a flexible, touch enabled, unstyled component that clips its content with a draggable, snapping clip path. Useful for things like peeking panels, reveal effects, resizable previews and the like.
<template>
<magic-tray
id="magic-tray-default-demo"
:options="{
snapPoints: { bottom: [0, 0.2, 0.4] },
threshold: { distance: 32 },
}"
style="--magic-tray-radius: 0.5rem; --magic-tray-width: 100%"
class="max-w-full"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="flex aspect-16/9 w-120 max-w-full items-center justify-center select-none"
>
<span>Drag the bottom edge</span>
</div>
</magic-tray>
</template>Anatomy
MagicTray can be used as a single self-contained component or composed from its individual parts for full control.
Simple
<template>
<magic-tray
id="your-tray-id"
:options="{ snapPoints: { bottom: [0, 0.5, 1] } }"
>
<!-- your content -->
</magic-tray>
</template>
<script setup>
const { snapTo } = useMagicTray('your-tray-id')
</script>
Composed
<template>
<magic-tray-provider id="your-tray-id" :options="options">
<magic-tray-trigger side="left">
<!-- content that drags the left side -->
</magic-tray-trigger>
<magic-tray-content>
<!-- your content -->
<template #handle="{ side }">
<!-- custom handle for `side` -->
</template>
</magic-tray-content>
</magic-tray-provider>
</template>
Installation
CLI
Add @maas/vue-equipment to your dependencies.
pnpm add @maas/vue-equipment
Vue
If you are using Vue, import and add MagicTrayPlugin to your app.
import { createApp } from 'vue'
import { MagicTrayPlugin } from '@maas/vue-equipment/plugins/MagicTray'
const app = createApp({})
app.use(MagicTrayPlugin)
Nuxt
The tray is available as a Nuxt module. In your Nuxt config file add @maas/vue-equipment/nuxt to your modules and add MagicTray to the plugins in your configuration.
export default defineNuxtConfig({
modules: ['@maas/vue-equipment/nuxt'],
vueEquipment: {
plugins: ['MagicTray'],
},
})
Direct Import
If you prefer a more granular approach, components can be directly imported.
<script setup>
import {
MagicTrayProvider,
MagicTrayContent,
MagicTrayHandle,
MagicTrayTrigger,
} from '@maas/vue-equipment/plugins/MagicTray'
</script>
Composable
In order to interact with the tray from anywhere within your app, we provide a useMagicTray composable. Import it directly when needed.
import { onMounted } from 'vue'
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
const { snapTo } = useMagicTray('your-tray-id')
onMounted(() => {
snapTo('bottom', 0.5)
})
Peer Dependencies
MagicTray has no optional peer dependencies. It only relies on the core peer dependencies, which npm, pnpm and bun install automatically.
API Reference
MagicTrayProvider
The MagicTrayProvider wraps the tray and configures all child components according to the provided options.
Props
Options
To customize the tray, override the necessary options. Any custom options will be merged with the default options.
MagicTrayContent
Renders the clipped content along with drag and snap behavior. Combines all four sides into a single clip-path.
Slots
CSS Variables
MagicTrayHandle
Renders an invisible, draggable hit area along an edge. Has no appearance of its own — provide visuals through the handle slot. Rendered automatically by MagicTrayContent for each draggable side.
Slot Props
CSS Variables
MagicTrayTrigger
Makes an element a drag surface for one side of the tray, so the user can drag a sidebar or panel shut from anywhere on it. Nest it inside MagicTrayProvider, or place it anywhere in your app and pass the tray’s id.
<template>
<magic-tray-trigger id="your-tray-id" side="left" as-child>
<aside>
<!-- your sidebar -->
</aside>
</magic-tray-trigger>
</template>
A press starts the drag right away, so the side follows the pointer from the first pixel. A release within threshold.lock counts as a tap and passes its click to the element inside, while a longer drag swallows it. Presses on form fields and editable text never start a drag, and links and images inside the trigger never start a native drag while the side is being dragged.
Pass an array of sides to drag more than one from the same element, like the view between a left and a right sidebar. The first move of a press picks the side and starts its drag: a move closes the side it points toward when that side is open, and opens the side it points away from otherwise, so a move to the right opens the left side or closes the right one. With sides along both axes, the axis the move leans to wins. A press without a move starts nothing, so a tap passes its click through without a drag and without the tray’s events. Only sides with snap points count: a trigger left with one of them drags on press like a trigger with one side.
<template>
<magic-tray-trigger :side="['left', 'right']" as-child>
<main>
<!-- your content -->
</main>
</magic-tray-trigger>
</template>
Props
Slot Props
CSS Variables
MagicTrayTransform
Translates a nested layer alongside the clip. As an edge is dragged inward, the wrapped content is pushed away from that edge by the same distance, while a background layer stays anchored.
Props
MagicTray
A self-contained component that composes the provider and content internally. Use this for simple, inline cases where you don’t need custom markup.
Props
Slots
useMagicTray
The composable returns the tray’s reactive state and a set of functions to control it programmatically.
Events
The tray emits the following events through MagicEmitter. Listen to them with useMagicEmitter.
Errors
Caveats
The tray handles situations where dragging and scrolling might interfere with each other on touch devices. In order for the tray to differentiate when the user scrolls and when the user drags, any scrollable containers within the tray need to have their overflow value explicitly set to auto or scroll.
On iOS, a touch that starts near a vertical viewport edge arms the browser’s back and forward navigation swipe, in Safari as well as in installed web apps. Since a closed side’s handle sits exactly there, the tray cancels touchstart on its handles and triggers within 44px of either edge, which is the one signal WebKit honors, so grabbing a handle drags the tray instead of navigating. Canceling also swallows the tap’s synthesized click; the tray dispatches a replacement when the touch ends within 12px of where it started, so click listeners keep firing. A touch on an SVG icon counts as well, and its replacement click goes to the HTML element that holds the icon. A canceled touch never scrolls either. Set the preventEdgeNavigation prop to false on a trigger whose content needs to scroll next to a viewport edge, and the option to false to opt out for the whole tray.
The clip insets hide part of the content, but the content still covers the whole tray. Taps respect the clip, so an element showing through the clipped area receives them. On iOS, the native hit test that decides which scroller takes a pan ignores the clip, so a scrollable element behind the clipped area never scrolls. Set --magic-tray-content-pointer-events to none while that area needs to scroll, for example while a side is open over a sidebar. Content that sets its own pointer-events still catches touches.
While a handle is being dragged, the tray disables text selection on the whole document and restores it on release. Without this, a pointer that leaves the handle would keep extending a selection across the page. A trigger disables it only once its drag moves past threshold.lock, so a long press on its content still selects text.
In order for a trigger to tell a scroll from a drag on touch devices, it sets touch-action to pan-y on the left and right sides, to pan-x on the top and bottom, and to none with sides along both axes: the browser scrolls along the other axis and leaves the drag axis to the tray. The browser reads touch-action only up to the nearest scroll container, so give any scrollable element inside a trigger the same value. Without it, a swipe along the drag axis that starts on that element cancels the drag.
A trigger starts its drag on press, so a tap on an element inside it emits beforeDrag, willSnapTo, drag, beforeSnap, afterSnap and afterDrag as well, while the side stays where it rests. A trigger with several sides starts its drag on the first move of the press, so a tap on it emits nothing.
If an element inside a trigger runs its own drag, like a swipe action on a list row, stop pointerdown from propagating on that element. Otherwise the tray starts a drag from the same press and moves along with it.
Examples
Combined Sides
<template>
<magic-tray
id="magic-tray-combined-demo"
:options="{
snapPoints: {
top: [0, 0.2, 0.4],
right: [0, 0.2, 0.4],
bottom: [0, 0.2, 0.4],
left: [0, 0.2, 0.4],
},
threshold: {
distance: 32,
},
}"
style="--magic-tray-radius: 0.75rem; --magic-tray-width: 100%"
class="max-w-full"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="flex aspect-16/9 w-120 max-w-full items-center justify-center select-none"
>
<span>Drag any edge</span>
</div>
</magic-tray>
</template>Transform
Translate a layer alongside the clip with MagicTrayTransform, pushing content away from each edge as it is dragged in.
Drag the right or bottom edge
<template>
<magic-tray
id="magic-tray-transform-demo"
:options="{
snapPoints: {
right: [0, 0.25, 0.5],
bottom: [0, 0.25, 0.5],
},
threshold: { distance: 32 },
}"
style="--magic-tray-radius: 0.75rem; --magic-tray-width: 100%"
class="max-w-full"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<magic-tray-transform :factor="0.5">
<div
class="flex aspect-16/9 w-120 max-w-full flex-col items-center justify-center gap-8 p-8 select-none"
>
<p class="text-surface max-w-xs text-center">
Drag the right or bottom edge
</p>
</div>
</magic-tray-transform>
</magic-tray>
</template>Reveal
A composed tray layered over its own container, easing into its initial snap points on mount via initial.transition.
<template>
<div
class="bg-surface-low text-surface-muted relative flex aspect-16/9 w-120 max-w-full items-center justify-between overflow-x-clip rounded-[0.25rem] select-none"
>
<span class="w-16 text-center">L</span>
<span class="w-16 text-center">R</span>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content
:style="{
'--magic-tray-radius': radius,
'--magic-tray-drag-overshoot-outer': '3.5rem',
}"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="text-surface flex h-full w-full items-center justify-center p-8 text-center"
>
Drag left or right edge
</div>
</magic-tray-content>
</magic-tray-provider>
</div>
</template>
<script lang="ts" setup>
import { computed } from 'vue'
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import type {
MagicTrayOptions,
MagicTraySide,
} from '@maas/vue-equipment/plugins/MagicTray'
const id = 'magic-tray-reveal-demo'
const sides: MagicTraySide[] = ['top', 'right', 'bottom', 'left']
const options: MagicTrayOptions = {
snapPoints: {
top: ['8px'],
right: ['64px', '128px'],
bottom: ['8px'],
left: ['64px', '128px'],
},
initial: {
snapPoints: { top: '8px', right: '64px', bottom: '8px', left: '64px' },
transition: true,
},
threshold: { distance: 32 },
inset: true,
}
const { state } = useMagicTray(id, options)
// Roundedness tracks the first 8px of clip, maxing out at 0.5rem
const radius = computed(() => {
const clip = (side: MagicTraySide) =>
Math.max(0, state.dragged[side] - state.overshoot.outer[side])
const peek = Math.max(...sides.map(clip))
return `${Math.min(1, peek / 8) * 0.5}rem`
})
</script>Step Snapping
With snap.mode set to 'step', the edge advances to the adjacent snap point on each drag instead of jumping to the closest one, moving through the configured points one at a time.
<template>
<magic-tray
id="magic-tray-step-snap-demo"
:options="{
snapPoints: { bottom: [0, 0.25, 0.5, 0.75, 1] },
snap: { mode: 'step' },
threshold: { distance: 32 },
}"
style="--magic-tray-radius: 0.5rem; --magic-tray-width: 100%"
class="max-w-full"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="flex aspect-16/9 w-120 max-w-full items-center justify-center select-none"
>
<span>Drag the bottom edge</span>
</div>
</magic-tray>
</template>Drag Mode
With drag.mode set to 'snap', the edge commits straight to the snap point closest to the pointer while dragging. The cut is immediate, with no animation, and ignores snap.mode.
<template>
<magic-tray
id="magic-tray-drag-mode-demo"
:options="{
snapPoints: { bottom: [0, 0.25, 0.5, 0.75, 1] },
drag: { mode: 'snap' },
}"
style="--magic-tray-radius: 0.5rem; --magic-tray-width: 100%"
class="max-w-full"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="flex aspect-16/9 w-120 max-w-full items-center justify-center select-none"
>
<span>Drag the bottom edge</span>
</div>
</magic-tray>
</template>Ranges
Pass { min, max } in place of a snap point to let a side rest anywhere between two insets. A release inside the range does not snap, while a release outside snaps to the closest point. Momentum detection is preserved. Add momentum: false to disable momentum based snapping inside the range.
const options: MagicTrayOptions = {
snapPoints: {
left: [0, { min: 0.15, max: 0.4 }, 0.6],
},
}
Events and activeSnapPoint report the resting value to four decimals, like 0.2734, if both ends of the range are decimals, and in whole pixels, like '184px', otherwise.
<template>
<div class="flex w-120 max-w-full flex-col gap-4 select-none">
<div
class="bg-surface-low relative flex aspect-16/9 w-full overflow-hidden rounded-[0.5rem]"
>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content
:style="{
'--magic-tray-radius': '0.5rem',
'--magic-tray-drag-overshoot-outer': '0px',
}"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div class="relative h-full w-full">
<div
class="text-surface absolute inset-y-0 right-0 flex items-center justify-center p-4 text-center"
:style="{ left: percent(progress.left) }"
>
Drag the left edge
</div>
</div>
</magic-tray-content>
</magic-tray-provider>
</div>
<div class="relative h-4 w-full">
<div
v-for="(entry, index) in entries"
:key="index"
class="absolute top-1/2 -translate-y-1/2 rounded-full transition-colors duration-150"
:class="entryClass(entry)"
:style="entryStyle(entry)"
/>
<div
v-if="isDragging && ring !== undefined"
class="border-accent-muted absolute top-1/2 size-4 -translate-1/2 rounded-full border-2"
:style="{ left: percent(ring) }"
/>
<div
class="bg-primary-solid absolute inset-y-0 w-0.5 -translate-x-1/2 rounded-full"
:style="{ left: percent(progress.left) }"
/>
</div>
<div
class="bg-surface-base flex w-full flex-wrap items-center justify-between gap-x-4 gap-y-1 rounded-[0.5rem] py-2 pr-2 pl-4 font-mono text-sm"
>
<span>
<span class="text-surface-muted">activeSnapPoint.left</span>
<span class="text-surface ml-3">{{ activeSnapPoint.left ?? '–' }}</span>
</span>
<div class="flex gap-1">
<m-button
v-for="option in modes"
:key="option"
:mode="mode === option ? 'subtle' : 'ghost'"
size="xs"
class="select-none"
@click="mode = option"
>
{{ option }}
</m-button>
<m-button
:mode="momentum ? 'subtle' : 'ghost'"
:aria-pressed="momentum"
size="xs"
class="ml-2 select-none"
@click="momentum = !momentum"
>
momentum
</m-button>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, onBeforeUnmount, ref } from 'vue'
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import { useMagicEmitter } from '@maas/vue-equipment/plugins/MagicEmitter'
import type { MagicEmitterEvents } from '@maas/vue-equipment/plugins/MagicEmitter'
import type {
MagicTrayOptions,
MagicTraySnapMode,
MagicTraySnapPoint,
} from '@maas/vue-equipment/plugins/MagicTray'
type RangeDemoEntry = number | { min: number; max: number; momentum?: boolean }
const id = 'magic-tray-range-demo'
const modes: Array<MagicTraySnapMode> = ['closest', 'step']
const mode = ref<MagicTraySnapMode>('closest')
const momentum = ref(true)
const entries = computed<Array<RangeDemoEntry>>(() => [
0,
{ min: 0.15, max: 0.4, momentum: momentum.value },
0.6,
])
const options = computed<MagicTrayOptions>(() => ({
snapPoints: { left: entries.value },
snap: { mode: mode.value },
threshold: { distance: 32 },
inset: true,
}))
const { state, progress, activeSnapPoint } = useMagicTray(id, options.value)
const emitter = useMagicEmitter()
// Where a release would come to rest, reported while the left edge is dragged
const target = ref<number>()
const isDragging = computed(() => state.draggingSide === 'left')
const active = computed(() => toNumber(activeSnapPoint.value.left))
// willSnapTo only reports where the target entered a range, so inside one the
// ring follows the edge instead
const ring = computed(() => {
const value = target.value
if (value === undefined) {
return undefined
}
const isInsideRange = entries.value.some(
(entry) =>
typeof entry !== 'number' && value > entry.min && value < entry.max
)
return isInsideRange ? progress.value.left : value
})
// Whether the edge rests at an entry, or will once released
function isResting(entry: RangeDemoEntry) {
const value = isDragging.value ? target.value : active.value
if (value === undefined) {
return false
}
return typeof entry === 'number'
? value === entry
: value >= entry.min && value <= entry.max
}
function entryClass(entry: RangeDemoEntry) {
return [
typeof entry === 'number' ? 'size-2 -translate-x-1/2' : 'h-2',
isResting(entry) ? 'bg-accent-solid' : 'text-surface-subtle bg-current',
]
}
function entryStyle(entry: RangeDemoEntry) {
return typeof entry === 'number'
? { left: percent(entry) }
: { left: percent(entry.min), width: percent(entry.max - entry.min) }
}
function toNumber(point?: MagicTraySnapPoint) {
return typeof point === 'number' ? point : undefined
}
function percent(value: number) {
return `${value * 100}%`
}
function onWillSnapTo(payload: MagicEmitterEvents['willSnapTo']) {
if (payload.id !== id || !('side' in payload) || payload.side !== 'left') {
return
}
target.value = toNumber(payload.snapPoint)
}
emitter.on('willSnapTo', onWillSnapTo)
onBeforeUnmount(() => {
emitter.off('willSnapTo', onWillSnapTo)
})
</script>Will Snap To
The willSnapTo event fires during a drag whenever the snap point the edge will animate to on release changes. It only fires when the target changes, not on every move.
<template>
<div class="flex w-120 max-w-full flex-col gap-4 select-none">
<div
class="bg-surface-low text-surface-muted relative flex aspect-16/9 w-full items-center justify-between overflow-hidden rounded-[0.5rem]"
>
<span class="w-16 text-center">L</span>
<span class="w-16 text-center">R</span>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content
:style="{
'--magic-tray-radius': '0.5rem',
'--magic-tray-drag-overshoot-outer': '3.5rem',
}"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="text-surface flex h-full w-full items-center justify-center p-8 text-center"
>
Drag the left or right edge
</div>
</magic-tray-content>
</magic-tray-provider>
</div>
<div
class="bg-surface-low text-surface w-full rounded-[0.5rem] p-4 font-mono text-sm"
>
<div class="text-surface-muted mb-3 text-xs tracking-widest uppercase">
snap points
</div>
<div class="mb-4 flex flex-col gap-3">
<div
v-for="side in ['left', 'right']"
:key="side"
class="flex items-center gap-3"
>
<span class="text-surface-muted w-8 text-xs">{{ side }}</span>
<div class="flex flex-1 justify-between">
<div
v-for="point in side === 'right'
? [...snapPointKeys].reverse()
: snapPointKeys"
:key="point"
class="flex flex-col items-center gap-2"
>
<div
class="h-2 w-2 rounded-full"
:class="dotClass(side, point)"
/>
<span class="text-surface-muted text-xs">{{ point }}</span>
</div>
</div>
</div>
</div>
<div class="text-surface-muted mb-2 text-xs tracking-widest uppercase">
willSnapTo events
</div>
<div class="flex flex-col gap-1.5">
<template v-if="log.length">
<div
v-for="(entry, i) in log"
:key="i"
class="flex justify-between"
:class="i === 0 ? 'text-surface' : 'text-surface-muted'"
>
<span>
<span class="opacity-50">{{ entry.side }}</span>
{{ entry.snapPoint }}
</span>
<span>{{ directionArrow(entry.side, entry.snapPoint) }}</span>
</div>
</template>
<span v-else class="text-surface-muted">—</span>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { onBeforeUnmount, reactive, ref } from 'vue'
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import { useMagicEmitter } from '@maas/vue-equipment/plugins/MagicEmitter'
import type { MagicEmitterEvents } from '@maas/vue-equipment/plugins/MagicEmitter'
import type {
MagicTrayOptions,
MagicTraySnapPoint,
MagicTraySnapPointPayload,
MagicTrayWillSnapToPayload,
} from '@maas/vue-equipment/plugins/MagicTray'
const id = 'magic-tray-will-snap-to-demo'
const snapPointKeys = [0, 0.25, 0.5, 0.75]
const options: MagicTrayOptions = {
snapPoints: {
left: snapPointKeys,
right: snapPointKeys,
},
threshold: { distance: 32 },
inset: true,
}
const { activeSnapPoint } = useMagicTray(id, options)
const emitter = useMagicEmitter()
// The pending snap target per side during drag — null when not dragging
const willSnapTo = reactive<Record<string, MagicTraySnapPoint | null>>({
left: null,
right: null,
})
function dotClass(side: string, point: number) {
if (willSnapTo[side] === point) {
return 'bg-warning-solid'
}
if (
(activeSnapPoint.value as Record<string, MagicTraySnapPoint | undefined>)[
side
] === point
) {
return 'bg-success-solid'
}
return 'bg-surface-base'
}
// Arrow reflects the spatial direction on screen
function directionArrow(side: string, snapPoint: MagicTraySnapPoint) {
const p = Number(snapPoint)
return side === 'right' ? (p === 0 ? '→' : '←') : p === 0 ? '←' : '→'
}
const log = ref<MagicTrayWillSnapToPayload[]>([])
// willSnapTo is shared across plugins, so the payload is a union — narrow it
// to this plugin’s shape via the side field before use
function onWillSnapTo(payload: MagicEmitterEvents['willSnapTo']) {
if (payload.id !== id || !('side' in payload)) {
return
}
willSnapTo[payload.side] = payload.snapPoint
log.value.unshift(payload)
if (log.value.length > 5) {
log.value.length = 5
}
}
function isTraySnapPointPayload(v: unknown): v is MagicTraySnapPointPayload {
return typeof v === 'object' && v !== null && 'side' in v && 'point' in v
}
function onAfterSnap(payload: { id: string; snapPoint: unknown }) {
if (payload.id !== id) {
return
}
if (isTraySnapPointPayload(payload.snapPoint)) {
willSnapTo[payload.snapPoint.side] = null
}
}
emitter.on('willSnapTo', onWillSnapTo)
emitter.on('afterSnap', onAfterSnap)
onBeforeUnmount(() => {
emitter.off('willSnapTo', onWillSnapTo)
emitter.off('afterSnap', onAfterSnap)
})
</script>Handle Click
A handle passes clicks through to its slotted content, so a clickable element inside a handle slot works as expected.
or drag to move freely
<template>
<div class="flex w-120 max-w-full flex-col gap-4 select-none">
<div
class="bg-surface-low text-surface-muted relative flex aspect-16/9 w-full items-center justify-between overflow-hidden rounded-[0.5rem]"
>
<span class="w-16 text-center">L</span>
<span class="w-16 text-center">R</span>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content :style="{ '--magic-tray-radius': '0.5rem' }">
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<template #handle="{ side }">
<button class="h-full w-full" @click="toggle(side)" />
</template>
<div
class="text-surface flex h-full w-full items-center justify-center p-8 text-center"
>
Click a handle button to toggle <br />
or drag to move freely
</div>
</magic-tray-content>
</magic-tray-provider>
</div>
</div>
</template>
<script lang="ts" setup>
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import type {
MagicTrayOptions,
MagicTraySide,
} from '@maas/vue-equipment/plugins/MagicTray'
const id = 'magic-tray-handle-click-demo'
const options: MagicTrayOptions = {
snapPoints: { left: [0, 0.25], right: [0, 0.25] },
threshold: { distance: 32 },
inset: true,
}
const { snapTo, activeSnapPoint } = useMagicTray(id, options)
// Toggle the side between its open and closed snap points on a genuine tap.
// A drag suppresses the click, so the handle still moves freely.
function toggle(side: MagicTraySide) {
const current = activeSnapPoint.value[side]
snapTo(side, current === 0 ? 0.25 : 0)
}
</script>Trigger
Wrap an element in MagicTrayTrigger to drag a side from anywhere on it. The sidebar sits outside the provider and passes the tray’s id, the chat view inside is a second trigger wrapped in MagicTrayTransform so it moves with the drag, and taps still select a chat or open the menu.
How do I close the sidebar?
Drag it to the left. Drag this view or tap the menu to open it again.
<template>
<div
class="bg-surface-low text-surface rounded-surface-lg relative aspect-[402/874] w-72 max-w-full overflow-clip select-none"
>
<magic-tray-trigger :id="id" side="left" as-child>
<aside class="absolute inset-y-0 left-0 flex w-56 flex-col pt-7 pb-4">
<div class="flex items-center justify-between px-4">
<span class="type-surface-callout-md">Vue Equipment</span>
<span
class="bg-surface-base flex size-8 items-center justify-center rounded-full"
>
<svg v-bind="svg" class="size-4"><path :d="icons.search" /></svg>
</span>
</div>
<ul class="mt-3 flex flex-col px-2">
<li v-for="item in nav" :key="item.label">
<button
class="type-surface-body-sm flex w-full items-center gap-3 px-2 py-2 text-left"
>
<svg v-bind="svg" class="size-4 shrink-0">
<path :d="item.icon" />
</svg>
{{ item.label }}
</button>
</li>
</ul>
<div class="type-surface-caption text-surface-subtle mt-4 px-4">
Recents
</div>
<ul class="mt-1 flex flex-col px-2">
<li v-for="chat in chats" :key="chat">
<button
class="type-surface-body-sm rounded-component-sm flex w-full items-center gap-3 px-2 py-2 text-left"
:class="{ 'bg-surface-base': chat === selected }"
@click="selected = chat"
>
<svg v-bind="svg" class="size-4 shrink-0">
<path :d="icons.chat" />
</svg>
<span class="truncate">{{ chat }}</span>
</button>
</li>
</ul>
<div class="mt-auto flex items-center justify-between px-4 pt-3">
<span
class="bg-surface-base type-surface-caption flex size-8 items-center justify-center rounded-full"
>
VE
</span>
<m-button size="sm">New chat</m-button>
</div>
</aside>
</magic-tray-trigger>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content
:style="{ '--magic-tray-radius': 'var(--radius-surface-lg)' }"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<magic-tray-transform axis="x" class="h-full">
<magic-tray-trigger side="left" as-child>
<div class="flex h-full w-full flex-col">
<header class="flex items-center gap-3 px-4 pt-6 pb-3">
<button
class="bg-surface-high flex size-8 shrink-0 items-center justify-center rounded-full"
@click="snapTo('left', '224px')"
>
<svg v-bind="svg" class="size-4">
<path :d="icons.menu" />
</svg>
</button>
<span class="type-surface-callout-md truncate">{{
selected
}}</span>
</header>
<div
class="type-surface-body-sm flex flex-1 flex-col justify-end gap-3 px-5 pb-3"
>
<p
class="bg-surface-high rounded-component-xl max-w-[85%] self-end px-3.5 py-2"
>
How do I close the sidebar?
</p>
<p class="text-surface-muted">
Drag it to the left. Drag this view or tap the menu to open it
again.
</p>
</div>
<div class="px-4 pb-4">
<div
class="bg-surface-high text-surface-muted type-surface-body-sm flex items-center gap-2 rounded-full px-4 py-2.5"
>
<svg v-bind="svg" class="size-4">
<path :d="icons.plus" />
</svg>
Ask anything
</div>
</div>
</div>
</magic-tray-trigger>
</magic-tray-transform>
</magic-tray-content>
</magic-tray-provider>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import type { SVGAttributes } from 'vue'
import type { MagicTrayOptions } from '@maas/vue-equipment/plugins/MagicTray'
const id = 'magic-tray-trigger-demo'
const svg: SVGAttributes = {
viewBox: '0 0 16 16',
fill: 'none',
stroke: 'currentColor',
'stroke-width': '1.5',
'stroke-linecap': 'round',
'stroke-linejoin': 'round',
}
const icons = {
search: 'M10.5 10.5 13.5 13.5M11.5 7a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Z',
menu: 'M3 5h10M3 8h10M3 11h10',
plus: 'M8 3v10M3 8h10',
folder:
'M2 4.5A1.5 1.5 0 0 1 3.5 3h3L8 4.5h4.5A1.5 1.5 0 0 1 14 6v5.5a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 2 11.5z',
code: 'm5.5 4.5-3.5 3.5 3.5 3.5M10.5 4.5 14 8l-3.5 3.5',
clock: 'M8 14A6 6 0 1 0 8 2a6 6 0 0 0 0 12ZM8 5v3l2.5 1.5',
chat: 'M3 4.5A1.5 1.5 0 0 1 4.5 3h7A1.5 1.5 0 0 1 13 4.5v5a1.5 1.5 0 0 1-1.5 1.5H7l-3 2.5V11h-.5A1.5 1.5 0 0 1 3 9.5z',
}
const nav = [
{ label: 'Projects', icon: icons.folder },
{ label: 'Code', icon: icons.code },
{ label: 'Scheduled', icon: icons.clock },
]
const chats = [
'Closing the sidebar',
'Plan a weekend in Lisbon',
'Explain clip-path insets',
'Sourdough starter schedule',
'Rename these variables',
'Draft a release note',
]
const selected = ref('Closing the sidebar')
const options: MagicTrayOptions = {
snapPoints: { left: [0, '224px'] },
initial: { snapPoints: { left: '224px' } },
threshold: { distance: 32 },
handles: false,
inset: true,
}
const { snapTo } = useMagicTray(id, options)
</script>Magnetic
With magnetism.sides, an edge is pulled toward the cursor as it nears the handle, gaining as it closes in. Each snap point sets its direction: 'inner' pulls in from inside, 'outer' out from outside, 'both' from either side.
<template>
<div
class="bg-surface-low text-surface-muted relative flex aspect-16/9 w-120 max-w-full items-center justify-between overflow-hidden rounded-[0.5rem] select-none"
>
<span class="w-16 text-center">L</span>
<span class="w-16 text-center">R</span>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content
:style="{
'--magic-tray-radius': '0.5rem',
'--magic-tray-drag-overshoot-outer': '3.5rem',
'--magic-tray-handle-width-y': '256px',
}"
>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="text-surface flex h-full w-full items-center justify-center p-8 text-center"
>
Move toward the right edge
</div>
</magic-tray-content>
</magic-tray-provider>
</div>
</template>
<script lang="ts" setup>
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import type { MagicTrayOptions } from '@maas/vue-equipment/plugins/MagicTray'
const id = 'magic-tray-magnetic-demo'
const options: MagicTrayOptions = {
snapPoints: {
right: [0, 0.5, 1],
},
snap: { mode: 'step' },
magnetism: {
sides: {
right: { 0: 'inner', 0.5: 'both', 1: 'outer' },
},
},
threshold: { distance: 24 },
inset: true,
}
useMagicTray(id, options)
</script>Virtual Magnetism
With magnetism.virtual set to true, magnetism is still calculated, but the clip path and handle stay put. The magnet event still fires, so you can drive custom effects such as transforms or parallax.
[clip stays frozen]
<template>
<div class="flex w-120 max-w-full flex-col gap-4 select-none">
<div
class="bg-surface-low text-surface-muted relative flex aspect-16/9 w-full items-center justify-between overflow-hidden rounded-[0.25rem]"
>
<magic-tray-provider :id="id" :options="options">
<magic-tray-content
:style="{
'--magic-tray-radius': '0.25rem',
'--magic-tray-drag-overshoot-outer': '3.5rem',
'--magic-tray-handle-offset-x-left': 'calc(-50% + 0.5rem)',
'--magic-tray-handle-offset-x-right': 'calc(50% - 0.5rem)',
}"
>
<template #handle-left>
<div
class="text-surface h-[calc(2.5rem+var(--handle-v,0)*1rem)] w-1 rounded-full bg-current opacity-[calc(0.1+var(--handle-v,0)*0.9)]"
:style="{ '--handle-v': handleV('left') }"
/>
</template>
<template #handle-right>
<div
class="text-surface h-[calc(2.5rem+var(--handle-v,0)*1rem)] w-1 rounded-full bg-current opacity-[calc(0.1+var(--handle-v,0)*0.9)]"
:style="{ '--handle-v': handleV('right') }"
/>
</template>
<template #background>
<div class="bg-surface-base h-full w-full" />
</template>
<div
class="text-surface flex h-full w-full items-center justify-center p-8 text-center"
>
Move toward the left or right edge<br />
[clip stays frozen]
</div>
</magic-tray-content>
</magic-tray-provider>
</div>
<div
class="bg-surface-low text-surface w-full rounded-[0.25rem] p-4 font-mono text-sm"
>
<div class="text-surface-muted mb-3 text-xs tracking-widest uppercase">
magnet event
</div>
<div class="mb-1.5 flex justify-between text-xs">
<span class="text-surface-muted">
left
<span class="text-surface">{{ magnetValues.left.toFixed(3) }}</span>
</span>
<span class="text-surface-muted">
right
<span class="text-surface">{{ magnetValues.right.toFixed(3) }}</span>
</span>
</div>
<div class="bg-surface-base relative h-1 overflow-hidden rounded-full">
<div
class="bg-success-solid absolute right-1/2 h-full w-[calc(var(--bar-v,0)*50%)] opacity-(--bar-v,0)"
:style="{ '--bar-v': magnetValues.left }"
/>
<div
class="bg-success-solid absolute left-1/2 h-full w-[calc(var(--bar-v,0)*50%)] opacity-(--bar-v,0)"
:style="{ '--bar-v': magnetValues.right }"
/>
<div
class="text-surface-subtle absolute top-0 left-1/2 h-full w-px -translate-x-1/2 bg-current"
/>
</div>
<div
class="text-surface-muted mt-4 mb-3 text-xs tracking-widest uppercase"
>
state
</div>
<div class="flex flex-col gap-1.5">
<template v-for="side in sides" :key="side">
<div class="flex justify-between">
<span class="text-surface-muted">activeSnapPoint.{{ side }}</span>
<span>{{ state.activeSnapPoint[side] ?? '—' }}</span>
</div>
<div class="flex justify-between">
<span class="text-surface-muted">progress.{{ side }}</span>
<span>{{ state.progress[side].toFixed(3) }}</span>
</div>
</template>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { onBeforeUnmount, reactive } from 'vue'
import { useMagicTray } from '@maas/vue-equipment/plugins/MagicTray'
import { useMagicEmitter } from '@maas/vue-equipment/plugins/MagicEmitter'
import type {
MagicTrayOptions,
MagicTraySide,
} from '@maas/vue-equipment/plugins/MagicTray'
const id = 'magic-tray-virtual-magnetic-demo'
const sides: MagicTraySide[] = ['left', 'right']
const options: MagicTrayOptions = {
snapPoints: {
left: [0, 0.25],
right: [0, 0.25],
},
snap: { mode: 'step' },
magnetism: {
sides: {
left: { 0: 'inner', 0.25: 'inner' },
right: { 0: 'inner', 0.25: 'inner' },
},
radius: { start: 32, stop: 8 },
virtual: true,
},
threshold: { distance: 32 },
inset: true,
}
const { state } = useMagicTray(id, options)
const emitter = useMagicEmitter()
const magnetValues = reactive<Record<MagicTraySide, number>>({
top: 0,
right: 0,
bottom: 0,
left: 0,
})
function handleV(side: MagicTraySide) {
return state.draggingSide === side ? 1 : magnetValues[side]
}
function onMagnet(payload: { id: string; side: MagicTraySide; value: number }) {
if (payload.id === id) {
magnetValues[payload.side] = payload.value
}
}
emitter.on('magnet', onMagnet)
onBeforeUnmount(() => {
emitter.off('magnet', onMagnet)
})
</script>