Skip to content

Commit b00e07f

Browse files
committed
feat(Popover): add reference prop
1 parent 5c573b3 commit b00e07f

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script setup lang="ts">
2+
const open = ref(false)
3+
const anchor = ref({ x: 0, y: 0 })
4+
5+
const reference = computed(() => ({
6+
getBoundingClientRect: () =>
7+
({
8+
width: 0,
9+
height: 0,
10+
left: anchor.value.x,
11+
right: anchor.value.x,
12+
top: anchor.value.y,
13+
bottom: anchor.value.y,
14+
...anchor.value
15+
} as DOMRect)
16+
}))
17+
</script>
18+
19+
<template>
20+
<UPopover
21+
:open="open"
22+
:reference="reference"
23+
:content="{ side: 'top', sideOffset: 16, updatePositionStrategy: 'always' }"
24+
>
25+
<div
26+
class="flex items-center justify-center rounded-md border border-dashed border-accented text-sm aspect-video w-72"
27+
@pointerenter="open = true"
28+
@pointerleave="open = false"
29+
@pointermove="(ev) => {
30+
anchor.x = ev.clientX
31+
anchor.y = ev.clientY
32+
}"
33+
>
34+
Hover me
35+
</div>
36+
37+
<template #content>
38+
<div class="p-4">
39+
{{ anchor.x.toFixed(0) }} - {{ anchor.y.toFixed(0) }}
40+
</div>
41+
</template>
42+
</UPopover>
43+
</template>

‎docs/content/3.components/popover.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ name: 'popover-command-palette-example'
202202
---
203203
::
204204

205+
### With following cursor :badge{label="Soon" class="align-text-top"}
206+
207+
You can make the Popover follow the cursor when hovering over an element using the [`reference`](https://reka-ui.com/docs/components/tooltip#trigger) prop:
208+
209+
::component-example
210+
---
211+
name: 'popover-cursor-example'
212+
---
213+
::
214+
205215
### With anchor slot
206216

207217
You can use the `#anchor` slot to position the Popover against a custom element.

‎docs/content/3.components/tooltip.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ name: 'tooltip-cursor-example'
196196
---
197197
::
198198

199-
::note
200-
This example is based on Reka UI's [Tooltip Cursor](https://reka-ui.com/examples/tooltip-cursor) example.
201-
::
202-
203199
## API
204200

205201
### Props

‎src/runtime/components/Popover.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import type { PopoverRootProps, HoverCardRootProps, PopoverRootEmits, PopoverContentProps, PopoverContentEmits, PopoverArrowProps } from 'reka-ui'
2+
import type { PopoverRootProps, HoverCardRootProps, PopoverRootEmits, PopoverContentProps, PopoverContentEmits, PopoverArrowProps, HoverCardTriggerProps } from 'reka-ui'
33
import type { AppConfig } from '@nuxt/schema'
44
import theme from '#build/ui/popover'
55
import type { EmitsToProps, ComponentConfig } from '../types/utils'
@@ -27,6 +27,12 @@ export interface PopoverProps extends PopoverRootProps, Pick<HoverCardRootProps,
2727
* @defaultValue true
2828
*/
2929
portal?: boolean | string | HTMLElement
30+
/**
31+
* The reference (or anchor) element that is being referred to for positioning.
32+
*
33+
* If not provided will use the current component as anchor.
34+
*/
35+
reference?: HoverCardTriggerProps['reference']
3036
/**
3137
* When `false`, the popover will not close when clicking outside or pressing escape.
3238
* @defaultValue true
@@ -100,7 +106,7 @@ const Component = computed(() => props.mode === 'hover' ? HoverCard : Popover)
100106

101107
<template>
102108
<Component.Root v-slot="{ open }" v-bind="rootProps">
103-
<Component.Trigger v-if="!!slots.default" as-child :class="props.class">
109+
<Component.Trigger v-if="!!slots.default || !!reference" as-child :reference="reference" :class="props.class">
104110
<slot :open="open" />
105111
</Component.Trigger>
106112

‎src/runtime/components/Tooltip.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export interface TooltipProps extends TooltipRootProps {
2727
* @defaultValue true
2828
*/
2929
portal?: boolean | string | HTMLElement
30+
/**
31+
* The reference (or anchor) element that is being referred to for positioning.
32+
*
33+
* If not provided will use the current component as anchor.
34+
*/
3035
reference?: TooltipTriggerProps['reference']
3136
class?: any
3237
ui?: Tooltip['slots']

0 commit comments

Comments
 (0)