Skip to content

useArrow

A composable that automatically registers the arrow middleware and provides ready-to-use positioning styles for an arrow element.

Signature

ts
function useArrow(
  arrowEl: Ref<HTMLElement | null>,
  context: FloatingContext,
  options?: UseArrowOptions
): UseArrowReturn

Parameters

ParameterTypeRequiredDescription
arrowElRef<HTMLElement | null>YesReference to the arrow element
contextFloatingContextYesContext from useFloating
optionsUseArrowOptionsNoConfiguration options

Options

ts
interface UseArrowOptions {
  offset?: string
}
OptionTypeDefaultDescription
offsetstring'-4px'Distance of arrow from floating element edge

Return Value

ts
interface UseArrowReturn {
  arrowX: ComputedRef<number>
  arrowY: ComputedRef<number>
  arrowStyles: ComputedRef<Record<string, string>>
}
PropertyTypeDescription
arrowXComputedRef<number>X-coordinate of the arrow
arrowYComputedRef<number>Y-coordinate of the arrow
arrowStylesComputedRef<Record<string, string>>CSS styles using logical properties

Examples

Basic Usage

vue
<script setup>
import { ref } from 'vue'
import { useFloating, useArrow, offset } from 'v-float'

const anchorEl = ref(null)
const floatingEl = ref(null)
const arrowEl = ref(null)

const context = useFloating(anchorEl, floatingEl, {
  middlewares: [offset(8)]
})

const { arrowStyles } = useArrow(arrowEl, context)
</script>

<template>
  <div ref="floatingEl" :style="context.floatingStyles">
    Content
    <div ref="arrowEl" :style="arrowStyles" />
  </div>
</template>

With Custom Offset

vue
<script setup>
import { useFloating, useArrow } from 'v-float'

const { arrowStyles } = useArrow(arrowEl, context, {
  offset: '-6px'
})
</script>

With Padding

vue
<script setup>
import { useFloating, useArrow } from 'v-float'

const { arrowStyles } = useArrow(arrowEl, context, {
  padding: 8
})
</script>

See Also

Released under the MIT License.