Skip to content

useClientPoint

The useClientPoint composable positions floating elements relative to the user's pointer by updating the floating context's anchor to a virtual element at the pointer coordinates.

Signature

ts
function useClientPoint(
  pointerTarget: Ref<HTMLElement | null>,
  context: UseClientPointContext,
  options?: UseClientPointOptions
): UseClientPointReturn

Parameters

ParameterTypeRequiredDescription
pointerTargetRef<HTMLElement | null>YesElement whose pointer events are tracked. null disables tracking.
contextUseClientPointContextYesFloating context created by useFloating. Updates its refs.anchorEl to a virtual element.
optionsUseClientPointOptionsNoConfiguration options.

Options

ts
interface UseClientPointOptions {
  enabled?: MaybeRefOrGetter<boolean>
  axis?: MaybeRefOrGetter<'x' | 'y' | 'both'>
  trackingMode?: 'follow' | 'static'
  x?: MaybeRefOrGetter<number | null>
  y?: MaybeRefOrGetter<number | null>
}
OptionTypeDefaultDescription
enabledMaybeRefOrGetter<boolean>trueEnable/disable pointer tracking.
axisMaybeRefOrGetter<'x' | 'y' | 'both'>'both'Constrain movement to an axis.
trackingMode'follow' | 'static''follow'Follow pointer continuously or keep initial point.
xMaybeRefOrGetter<number | null>nullExternal X coordinate for controlled mode.
yMaybeRefOrGetter<number | null>nullExternal Y coordinate for controlled mode.

Return Value

ts
interface UseClientPointReturn {
  coordinates: Readonly<Ref<{ x: number | null; y: number | null }>>
  updatePosition: (x: number, y: number) => void
}
PropertyTypeDescription
coordinatesReadonly<Ref<{ x: number | null; y: number | null }>>Current pointer/controlled coordinates.
updatePosition(x: number, y: number) => voidProgrammatically update position (used in controlled mode).

Examples

Basic Usage

vue
<script setup lang="ts">
import { ref } from 'vue'
import { useFloating, useClientPoint, useHover } from 'v-float'

const anchor = ref<HTMLElement | null>(null)
const floating = ref<HTMLElement | null>(null)
const trackingArea = ref<HTMLElement | null>(null)

const ctx = useFloating(anchor, floating)
useHover(ctx)
useClientPoint(trackingArea, ctx)
</script>

Controlled Mode

vue
<script setup lang="ts">
import { ref } from 'vue'
import { useFloating, useClientPoint } from 'v-float'

const area = ref<HTMLElement | null>(null)
const anchor = ref<HTMLElement | null>(null)
const el = ref<HTMLElement | null>(null)
const x = ref<number | null>(200)
const y = ref<number | null>(100)

const ctx = useFloating(anchor, el)
useClientPoint(area, ctx, { x, y })
</script>

See Also

  • useFloating - Core positioning composable that works with useClientPoint
  • useArrow - Arrow positioning for floating elements

Guides

Released under the MIT License.