Skip to content

Composables ​

All are exported from vue-xr-ui. useXr and useXrTheme work anywhere in the app once createXrUi is installed; the others need to be called in a component inside <XrScene>.

useXr() ​

ts
function useXr(): XrController

The XR session: its state and how to enter and leave it. Works in DOM components and inside the canvas.

ts
const xr = useXr()
const label = computed(() => (xr.isPresenting.value ? 'Exit' : 'Enter VR'))
MemberTypeDescription
isPresentingReadonly<Ref<boolean>>An immersive session is running.
modeRef<XrMode | null>'immersive-vr', 'immersive-ar' or null outside a session.
supportedRef<{ vr: boolean; ar: boolean }>What the browser (or the emulator) can start.
systemKeyboardReadonly<Ref<boolean>>The session has a system keyboard (Quest browser). <XrKeyboard> hides itself then.
sessionShallowRef<XRSession | undefined>The WebXR session, for anything the library does not wrap.
storeShallowRef<XRStore | undefined>The @pmndrs/xr store <XrScene> created.
enterVR()() => Promise<void>Starts a VR session. Call it from a user gesture (a click).
enterAR()() => Promise<void>Starts a mixed-reality (passthrough) session.
exit()() => Promise<void>Ends the session.

useXrFrame(callback) ​

ts
function useXrFrame(cb: (delta: number, frame: XRFrame | undefined) => void): void

Runs cb every frame, in the browser and in the headset; removed when the component unmounts. delta is in milliseconds. frame is the WebXR frame in a session (use it only inside the callback), undefined otherwise.

Use it instead of TresJS's useLoop(): that one runs on window.requestAnimationFrame, which the browser pauses during an immersive session.

ts
const cube = shallowRef<Mesh>()
useXrFrame((delta) => {
  if (cube.value) cube.value.rotation.y += delta / 1000
})

useXrTheme() ​

ts
function useXrTheme(): Ref<ThemeTokens>

The active theme. Read it to style your own parts the way the components are styled; assign .value to switch the theme at runtime. Inside an <XrThemeProvider> it returns that provider's theme.

ts
const theme = useXrTheme()
theme.value = withAccent(lightTheme, '#22c55e')

See Theming and Tokens.

useXrReducedMotion() ​

ts
function useXrReducedMotion(): Ref<boolean>

Whether to keep motion to a minimum. When true, <XrFollow> jumps back in front of the viewer instead of gliding, and buttons do not grow on hover. It follows the user's prefers-reduced-motion setting unless createXrUi({ reducedMotion }) sets it; assign .value for an in-app comfort setting. Read it in your own animations too.

ts
const reducedMotion = useXrReducedMotion()
useXrFrame((delta) => {
  if (!reducedMotion.value) orb.rotation.y += delta / 1000
})

See Accessibility.

useXrPlanes() ​

ts
function useXrPlanes(): Readonly<ShallowRef<readonly XrPlane[]>>

The real surfaces the headset knows from its room setup: walls, floor, tables… The list changes when planes appear or go; each plane's matrix, center and normal update in place every frame, so read them in useXrFrame. Empty outside a session and on devices without plane detection.

ts
const planes = useXrPlanes()
const tables = computed(() => planes.value.filter((p) => p.label === 'table'))
XrPlane fieldTypeDescription
labelstringwall, floor, ceiling, table, couch, … in lower case; empty when the device does not say.
orientation'horizontal' | 'vertical'
matrixMatrix4Plane space to world. The plane lies in its local x/z; local +y is its normal.
polygonreadonly Vector2[]The outline in local x/z, metres.
min, maxVector2Bounds of the outline in local x/z.
centerVector3World centre of the bounds.
normalVector3World normal.
trackedbooleanTracked this frame.
planeXRPlaneThe WebXR plane.

XrSnapTarget (the snap prop of <XrWindow>) is true (walls and tables), false, or a list of labels.

useXrAnchor(id) ​

ts
function useXrAnchor(id: MaybeRefOrGetter<string>): XrAnchorHandle

A place in the room kept under id, across sessions and page reloads: a persistent WebXR anchor where the headset has them (Quest), the pose in the floor space elsewhere. <XrAnchor> and <XrWindow anchor> are built on it.

ts
const lamp = useXrAnchor('lamp')
await lamp.place(position, quaternion) // world space
useXrFrame(() => {
  if (lamp.anchored.value) mesh.matrix.copy(lamp.matrix)
})
MemberTypeDescription
matrixMatrix4World pose, valid while anchored is true. Updated every frame.
anchoredReadonly<Ref<boolean>>There is a pose for it now.
savedReadonly<Ref<boolean>>Something is saved under this id.
place(position, quaternion)Promise<void>Anchors here (world space), replacing the saved place.
forget()Promise<void>Drops the saved place and the headset's anchor.

See Mixed reality.

Headless controls ​

The behaviour of the built-in controls without their look. See Build your own look for a walk-through. Options take a value, a ref or a getter. target is a template ref to an <XrBox>: it receives Tab, Enter/Space, arrows and the focus ring. Spread the returned handlers into the ui of the box that takes the press.

useXrCheckbox(options) ​

OptionTypeDescription
targetXrTargetThe box that takes focus.
modelValuebooleanChecked or not.
indeterminateboolean"Some selected": a press then checks it.
disabledbooleanPresses and Tab skip it.
onUpdate(value, event) => voidThe new state after a press or Enter/Space.
radius, ringnumber, booleanFocus ring corner radius (dmm); false draws none.

Returns checked, indeterminate, handlers, focused, toggle().

useXrSlider(options) ​

OptionTypeDescription
targetXrTargetThe box that takes presses and focus.
trackXrTargetThe box values map across, left (min) to right (max). Defaults to target.
modelValue, min, max, stepnumberAs on <XrSlider> (0, 100, 1).
disabledboolean
onUpdate(value) => voidWhile dragging and on an arrow key.
onChange(value) => voidWhen a drag ends on a new value, and on an arrow key.

Returns value (follows the pointer while dragging), fraction (0 to 1), dragging, handlers, focused, stepBy(1 | -1).

useXrSelect(options) ​

OptionTypeDescription
targetXrTargetThe trigger.
insideXrTarget[]Boxes a press on which does not close it (your list).
modelValueVThe chosen value.
options{ label, value, disabled? }[]The choices.
disabledbooleanCloses it and keeps it closed.
onUpdate(value, option) => voidA choice (a press on an option, or Up/Down on the trigger).

Returns open, selected, triggerHandlers, optionHandlers(option), choose(option), setOpen(open), focused. It closes on a choice, a press on the trigger, a press outside, and Escape.

useXrPress(options) ​

Pointer handlers for anything pressable: onDown, onMove, onPress (down then up from the same pointer, however long), onRelease, onCancel, disabled, and cancelAfter (metres along the surface after which a drag cancels the press).

useXrFocusable(target, options) ​

Tab order, the focus ring and keys for any box: activate (Enter, Space), arrow(key) (return whether it used the key), disabled, radius, ring. Returns focused.

MIT License