# Float

Crate: `libero`
Import: `use libero::components::Float;`
Feature: `float` (on by default, turns on `box`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/layout/float.rs>
Index: [index.md](index.md) lists every other page
Description: Anchors its child to a corner or edge of the nearest positioned ancestor, like a badge on an avatar.

Anchors its child to a corner or edge of the nearest positioned ancestor, like a
badge on an avatar. The parent sets `position: relative` itself. The offsets
follow the page, not the placement, so on a `top-end` badge a positive
`offset_x` and a negative `offset_y` hang it off the corner in left-to-right
text. Under `dir=rtl` that corner is at the left edge, so use a negative
`offset_x`. For a button floating over the page, use
[FloatingActionButton](floating_action_button.md).

## Usage

The parent `Box` is the positioned ancestor.

```rust
use dioxus::prelude::*;
use libero::{components::{Box, Float}, sx::sx};

#[component]
fn Demo() -> Element {
    rsx! {
        Box {
            sx: sx().position("relative").width("160px").height("120px").background("primary.1"),
            Float {
                Box { sx: sx().padding("4px 8px").background("primary").color("primary-contrast"), "Badge" }
            }
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `placement` | `Placement` | `center-center` | The corner or edge to anchor to, such as `"top-start"`. |
| `offset_x` | `ThemeAwareValue` | `0px` | Shift to the right, a spacing step or a CSS length. A negative step like `"-md"` shifts left. |
| `offset_y` | `ThemeAwareValue` | `0px` | Shift down, a spacing step or a CSS length. A negative step shifts up. |
| `z_index` | `ThemeAwareValue` | `1` | Stacking order. The default sits below a sticky `Header`; raise it to go over one. |
| `fixed` | `bool` | `false` | Anchors to the viewport instead of the parent, so it stays put while the page scrolls. An ancestor with a `transform`, `filter`, `contain` or `container-type` still captures it. It stays below overlays and modals. The edges it is anchored to clear the safe-area insets: the notch, the status bar and the home indicator. A fixed Float stays over the page; keep it off controls, or a focused control can sit under it (WCAG 2.4.11). |
| `children` | `Element` | required | The anchored content. |

Like every component, `Float` also takes the shared props `sx`, `class`,
`style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes.

## Accessibility

### Libero handles

- `Float` adds no roles, and `start` and `end` follow the text direction.

### You must

- Write the float next to what it marks: screen readers and Tab follow the
  code, not where it shows.
- Keep it off text and controls at 320px wide and at 200% text size: it takes
  no space, so nothing moves out of its way.
- Keep a `fixed` float off the control that has focus, or the focus ring is
  hidden behind it (WCAG 2.4.11).
- Put a notification stack in a status region (`role="status"`), or its
  messages are not announced (WCAG 4.1.3).

### Example

A "New" badge floated over a card's corner, written right after the card's
title in the code: a screen reader reads "Spring sale, New", in that order.

## Theme defaults

`FloatDefaults` on the theme.

| Field | Type | Description |
|---|---|---|
| `offset_x` | `&'static str` | Horizontal offset when the prop is omitted. |
| `offset_y` | `&'static str` | Vertical offset when the prop is omitted. |
| `placement` | `Placement` | Default `placement` when the prop is omitted. |

`z_index` falls back to `1`, below a `Header`. The theme's `z_index.float` is the focused skip link's layer.

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-float-offset-x` | Horizontal offset, from `offset_x` or the theme. |
| `--lsx-float-offset-y` | Vertical offset, from `offset_y` or the theme. |
| `--lsx-float-translate-x` | `-50%` on a horizontally centered placement. |
| `--lsx-float-translate-y` | `-50%` on a vertically centered placement. |
| `--lsx-z-index-float` | Theme stacking order of the focused skip link. |
| `--lsx-z-index-float-override` | A float's own stacking order, from `z_index`. |

## Data attributes

State tokens on the root's `data-state`, space separated, one per axis of
`placement`.

| Token | Condition |
|---|---|
| `vertical-top` / `vertical-center` / `vertical-bottom` | The vertical half of `placement`. |
| `horizontal-start` / `horizontal-center` / `horizontal-end` | The horizontal half of `placement`. |
