Skip to main content

Floating

Floating is a layout element that positions content relative to a trigger or screen coordinates, such as a menu or contextual panel.

Import

import { Floating } from '@vortexlabs/vortex-native';
import type { FloatingProps } from '@vortexlabs/vortex-native';

Other links

import { Button, Floating, Stack, Text } from "@vortexlabs/vortex-native";

export const FloatingTrackingExample = () => (
<Floating position="bottom-start">
  <Floating.Trigger>
    {({ onPress, "aria-expanded": expanded }) => (
      <Button onPress={onPress} attributes={{ "aria-expanded": expanded }}>Track order</Button>
    )}
  </Floating.Trigger>
  <Floating.Content attributes={{ "aria-label": "Tracking details" }}>
    <Stack backgroundColor="surface" borderRadius="md" padding={3}>
      <Text>Arrives on Friday</Text>
    </Stack>
  </Floating.Content>
</Floating>
);

Features

  • Anchors custom content to a trigger, a separate element, or window coordinates.
  • Tries alternate positions when the preferred side has too little room.

Anatomy

<Floating>
  <Floating.Trigger>
    {({ onPress }) => <Button onPress={onPress}>Open</Button>}
  </Floating.Trigger>
  <Floating.Content />
</Floating>
Component Part
Description
Manages the panel position and visibility.
Measures the anchor and opens the panel.
Displays content at the resolved position.

Separate Anchor

Set positionRef to attach the panel to another view. Use fallbackPositions to give it room near a screen edge.

import { Floating, Button, Stack, Text } from "@vortexlabs/vortex-native";
import { useRef } from "react";
import { type HostInstance, View } from "react-native";

export const PickupAnchor = () => {
  const counterRef = useRef<HostInstance>(null);
  return (
    <Stack gap={4}>
      <View ref={counterRef} collapsable={false}>
        <Text weight="medium">Pickup counter</Text>
      </View>
      <Floating positionRef={counterRef} position="top-start" fallbackPositions={["bottom-start"]}>
        <Floating.Trigger>
          {({ onPress, "aria-expanded": expanded }) => (
            <Button onPress={onPress} attributes={{ "aria-expanded": expanded }}>Find counter</Button>
          )}
        </Floating.Trigger>
        <Floating.Content attributes={{ "aria-label": "Counter location" }}>
          <Stack padding={3} backgroundColor="surface" borderRadius="md">
            <Text>Ground floor, next to reception.</Text>
          </Stack>
        </Floating.Content>
      </Floating>
    </Stack>
  );
};

Width

Use width="trigger" to match the panel width to its opening control.

import { Button, Floating, Stack, Text } from "@vortexlabs/vortex-native";

export const FloatingWidthExample = () => (
  <Floating width="trigger" position="top-center">
    <Floating.Trigger>
      {({ onPress, "aria-expanded": expanded }) => (
        <Button onPress={onPress} attributes={{ "aria-expanded": expanded }}>Show tracking details</Button>
      )}
    </Floating.Trigger>
    <Floating.Content attributes={{ "aria-label": "Tracking details" }}>
      <Stack backgroundColor="surface" borderRadius="md" padding={3}>
        <Text>Arrives on Friday</Text>
      </Stack>
    </Floating.Content>
  </Floating>
);

Triggers

Use triggerType="long-press" for secondary actions. Forward the accessibility actions so a screen reader can open the same panel.

export const TrackingLongPress = () => (
  <Floating triggerType="long-press" position="bottom-start" fallbackPositions={["top-start"]}>
    <Floating.Trigger>
      {({ onLongPress, disabled, ...attributes }) => (
        <Button onLongPress={onLongPress} disabled={disabled} attributes={attributes}>
          Hold for tracking details
        </Button>
      )}
    </Floating.Trigger>
    <Floating.Content attributes={{ "aria-label": "Tracking details" }}>
      <Stack padding={3} backgroundColor="surface" borderRadius="md">
        <Text>Parcel received at the Sydney depot.</Text>
      </Stack>
    </Floating.Content>
  </Floating>
);

Controlled Visibility

Set active when an action inside the panel should also close it. Update the same state in onOpen and onClose.

import { Floating, Button, Stack } from "@vortexlabs/vortex-native";
import { useState } from "react";

export const PickupTime = () => {
  const [open, setOpen] = useState(false);
  const [reserved, setReserved] = useState(false);
  return (
    <Floating active={open} onOpen={() => setOpen(true)} onClose={() => setOpen(false)}>
      <Floating.Trigger>
        {({ onPress, "aria-expanded": expanded }) => (
          <Button onPress={onPress} attributes={{ "aria-expanded": expanded }}>
            {reserved ? "Pickup at noon" : "Choose pickup time"}
          </Button>
        )}
      </Floating.Trigger>
      <Floating.Content attributes={{ "aria-label": "Pickup time" }}>
        <Stack padding={3} backgroundColor="surface" borderRadius="md">
          <Button onPress={() => { setReserved(true); setOpen(false); }}>Reserve noon</Button>
        </Stack>
      </Floating.Content>
    </Floating>
  );
};

Accessibility

Description
Name the panel with aria-label in Floating.Content’s attributes.
Forward the expanded state from Floating.Trigger to the opening control.