Skip to content

Commit 27c757a

Browse files
committed
feat(Popper): allow custom reference el or virtual el for any popper content component
1 parent 0816387 commit 27c757a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

packages/radix-vue/src/Popper/PopperContent.vue

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Ref } from 'vue'
33
import type {
44
Middleware,
55
Placement,
6+
ReferenceElement,
67
} from '@floating-ui/vue'
78
import type { PrimitiveProps } from '@/Primitive'
89
import { createContext, useForwardExpose, useSize } from '@/shared'
@@ -59,7 +60,7 @@ export interface PopperContentProps extends PrimitiveProps {
5960
alignOffset?: number
6061
6162
/**
62-
* When `true`, overrides the side andalign preferences
63+
* When `true`, overrides the side and align preferences
6364
* to prevent collisions with boundary edges.
6465
*
6566
* @defaultValue true
@@ -125,6 +126,14 @@ export interface PopperContentProps extends PrimitiveProps {
125126
* @defaultValue false
126127
*/
127128
prioritizePosition?: boolean
129+
130+
/**
131+
* The custom element or virtual element that will be set as the reference
132+
* to position the floating element.
133+
*
134+
* If provided, it will replace the default anchor element.
135+
*/
136+
reference?: ReferenceElement
128137
}
129138
130139
export interface PopperContentContext {
@@ -140,7 +149,7 @@ export const [injectPopperContentContext, providePopperContentContext]
140149
</script>
141150

142151
<script setup lang="ts">
143-
import { computed, ref, watchEffect } from 'vue'
152+
import { computed, ref, toRefs, watchEffect } from 'vue'
144153
import { computedEager } from '@vueuse/core'
145154
import {
146155
autoUpdate,
@@ -173,8 +182,8 @@ const props = withDefaults(defineProps<PopperContentProps>(), {
173182
const emits = defineEmits<{
174183
placed: [void]
175184
}>()
176-
const rootContext = injectPopperRootContext()
177185
186+
const rootContext = injectPopperRootContext()
178187
const { forwardRef, currentElement: contentElement } = useForwardExpose()
179188
180189
const floatingRef = ref<HTMLElement>()
@@ -270,8 +279,11 @@ const computedMiddleware = computedEager(() => {
270279
] as Middleware[]
271280
})
272281
282+
// If provided custom reference, it will overwrite the defautl anchor element
283+
const reference = computed(() => props.reference ?? rootContext.anchor.value)
284+
273285
const { floatingStyles, placement, isPositioned, middlewareData } = useFloating(
274-
rootContext.anchor,
286+
reference,
275287
floatingRef,
276288
{
277289
strategy: 'fixed',

0 commit comments

Comments
 (0)