Popover
Rich content in a floating panel, opened by a button.
Reka UI Popover
<script setup lang="ts">
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import * as Popover from '@/components/ui/popover';
</script>
<template>
<Popover.Root>
<Popover.Trigger>Open popover</Popover.Trigger>
<Popover.Content>
<div class="stack">
<strong>Dimensions</strong>
<div class="field">
<Label for="popover-width">Width</Label>
<Input id="popover-width" model-value="100%" />
</div>
</div>
</Popover.Content>
</Popover.Root>
</template>
<style scoped>
.stack {
display: grid;
gap: var(--dui-space-3);
}
.field {
display: grid;
gap: var(--dui-space-2);
}
</style>
Installation
npx distill-ui add popoverIt also copies Button, which Popover uses.
It installs reka-ui if you don't have it yet.
- Set up the tokens once, if you haven't: see manual installation.
- Install the packages it needs:
npm install reka-ui@2.11.0 - Copy Button too, which it uses.
- Copy these files into your project:
<script setup lang="ts">
import { PopoverRoot } from 'reka-ui';
import { toRef } from 'vue';
import { providePopoverPlacement } from './context.js';
import type { Placement } from './placement.js';
// Use `v-model:open` to control it from outside.
const { placement = 'bottom' } = defineProps<{ placement?: Placement }>();
const open = defineModel<boolean>('open', { default: false });
providePopoverPlacement(toRef(() => placement));
</script>
<template>
<PopoverRoot v-model:open="open">
<slot />
</PopoverRoot>
</template>
<script setup lang="ts">
import { PopoverContent } from 'reka-ui';
import { computed } from 'vue';
import { injectPopoverPlacement } from './context.js';
import { toSideAlign } from './placement.js';
defineOptions({ inheritAttrs: false });
const position = computed(() => toSideAlign(injectPopoverPlacement().value));
</script>
<template>
<!-- No portal: Reka positions it with position: fixed, so it stays inside your styles. -->
<PopoverContent :side="position.side" :align="position.align" :side-offset="4" as-child>
<div v-bind="$attrs" class="content"><slot /></div>
</PopoverContent>
</template>
<!--
Options you can set from a parent or on the popover content itself:
--dui-popover-width, --dui-popover-padding, --dui-popover-radius, --dui-popover-bg, --dui-popover-fg
-->
<style scoped>
.content {
width: var(--dui-popover-width, 18rem);
padding: var(--dui-popover-padding, var(--dui-space-4));
border-radius: var(--dui-popover-radius, var(--dui-radius-md));
background: var(--dui-popover-bg, var(--dui-color-popover));
color: var(--dui-popover-fg, var(--dui-color-popover-foreground));
box-shadow:
0 0 0 1px color-mix(in oklch, var(--dui-color-foreground) 10%, transparent),
var(--dui-shadow-md);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
outline: none;
/* Reka copies this onto the positioning wrapper it puts around the content. */
z-index: var(--dui-z-popover);
/* Reka waits for animationend before removing it, so this uses the shared keyframes. */
&[data-state='open'] {
animation: distill-zoom-in var(--dui-duration-fast) var(--dui-ease-out);
}
&[data-state='closed'] {
animation: distill-zoom-out var(--dui-duration-fast) var(--dui-ease-out);
}
}
</style>
<script setup lang="ts">
import { PopoverTrigger } from 'reka-ui';
import { Button, type ButtonSize, type ButtonVariant } from '../button/index.js';
// Renders our Button, so it takes the same variant and size props.
const { variant = 'outline', size } = defineProps<{ variant?: ButtonVariant; size?: ButtonSize }>();
</script>
<template>
<PopoverTrigger as-child>
<Button :variant="variant" :size="size"><slot /></Button>
</PopoverTrigger>
</template>
import { inject, provide } from 'vue';
import type { PlacementRef } from './placement.js';
// The root takes the placement, and the content reads it to position itself.
const key = Symbol('distill-popover');
export const providePopoverPlacement = (placement: PlacementRef) => provide(key, placement);
export const injectPopoverPlacement = () => inject<PlacementRef>(key)!;
export { default as Root } from './Popover.vue';
export { default as Trigger } from './PopoverTrigger.vue';
export { default as Content } from './PopoverContent.vue';
export type { Placement } from './placement.js';
import type { Ref } from 'vue';
// Placement is written like Floating UI's ('bottom', 'top-start'…), the same as the
// Svelte version. Reka takes it as a side and an alignment, so this splits it.
export type Placement =
'top' | 'right' | 'bottom' | 'left' | `${'top' | 'right' | 'bottom' | 'left'}-${'start' | 'end'}`;
export function toSideAlign(placement: Placement) {
const [side, align = 'center'] = placement.split('-') as [
'top' | 'right' | 'bottom' | 'left',
('start' | 'end' | 'center')?
];
return { side, align };
}
export type PlacementRef = Readonly<Ref<Placement>>;
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-popover-width | 18rem |
--dui-popover-padding | var(--dui-space-4) |
--dui-popover-radius | var(--dui-radius-md) |
--dui-popover-bg | var(--dui-color-popover) |
--dui-popover-fg | var(--dui-color-popover-foreground) |