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()
function useXr(): XrControllerThe XR session: its state and how to enter and leave it. Works in DOM components and inside the canvas.
const xr = useXr()
const label = computed(() => (xr.isPresenting.value ? 'Exit' : 'Enter VR'))| Member | Type | Description |
|---|---|---|
isPresenting | Readonly<Ref<boolean>> | An immersive session is running. |
mode | Ref<XrMode | null> | 'immersive-vr', 'immersive-ar' or null outside a session. |
supported | Ref<{ vr: boolean; ar: boolean }> | What the browser (or the emulator) can start. |
systemKeyboard | Readonly<Ref<boolean>> | The session has a system keyboard (Quest browser). <XrKeyboard> hides itself then. |
session | ShallowRef<XRSession | undefined> | The WebXR session, for anything the library does not wrap. |
store | ShallowRef<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)
function useXrFrame(cb: (delta: number, frame: XRFrame | undefined) => void): voidRuns 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.
const cube = shallowRef<Mesh>()
useXrFrame((delta) => {
if (cube.value) cube.value.rotation.y += delta / 1000
})useXrTheme()
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.
const theme = useXrTheme()
theme.value = withAccent(lightTheme, '#22c55e')useXrReducedMotion()
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.
const reducedMotion = useXrReducedMotion()
useXrFrame((delta) => {
if (!reducedMotion.value) orb.rotation.y += delta / 1000
})See Accessibility.
useXrPlanes()
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.
const planes = useXrPlanes()
const tables = computed(() => planes.value.filter((p) => p.label === 'table'))XrPlane field | Type | Description |
|---|---|---|
label | string | wall, floor, ceiling, table, couch, … in lower case; empty when the device does not say. |
orientation | 'horizontal' | 'vertical' | |
matrix | Matrix4 | Plane space to world. The plane lies in its local x/z; local +y is its normal. |
polygon | readonly Vector2[] | The outline in local x/z, metres. |
min, max | Vector2 | Bounds of the outline in local x/z. |
center | Vector3 | World centre of the bounds. |
normal | Vector3 | World normal. |
tracked | boolean | Tracked this frame. |
plane | XRPlane | The WebXR plane. |
XrSnapTarget (the snap prop of <XrWindow>) is true (walls and tables), false, or a list of labels.
useXrAnchor(id)
function useXrAnchor(id: MaybeRefOrGetter<string>): XrAnchorHandleA 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.
const lamp = useXrAnchor('lamp')
await lamp.place(position, quaternion) // world space
useXrFrame(() => {
if (lamp.anchored.value) mesh.matrix.copy(lamp.matrix)
})| Member | Type | Description |
|---|---|---|
matrix | Matrix4 | World pose, valid while anchored is true. Updated every frame. |
anchored | Readonly<Ref<boolean>> | There is a pose for it now. |
saved | Readonly<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)
| Option | Type | Description |
|---|---|---|
target | XrTarget | The box that takes focus. |
modelValue | boolean | Checked or not. |
indeterminate | boolean | "Some selected": a press then checks it. |
disabled | boolean | Presses and Tab skip it. |
onUpdate | (value, event) => void | The new state after a press or Enter/Space. |
radius, ring | number, boolean | Focus ring corner radius (dmm); false draws none. |
Returns checked, indeterminate, handlers, focused, toggle().
useXrSlider(options)
| Option | Type | Description |
|---|---|---|
target | XrTarget | The box that takes presses and focus. |
track | XrTarget | The box values map across, left (min) to right (max). Defaults to target. |
modelValue, min, max, step | number | As on <XrSlider> (0, 100, 1). |
disabled | boolean | |
onUpdate | (value) => void | While dragging and on an arrow key. |
onChange | (value) => void | When 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)
| Option | Type | Description |
|---|---|---|
target | XrTarget | The trigger. |
inside | XrTarget[] | Boxes a press on which does not close it (your list). |
modelValue | V | The chosen value. |
options | { label, value, disabled? }[] | The choices. |
disabled | boolean | Closes it and keeps it closed. |
onUpdate | (value, option) => void | A 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.