# Anchor

Crate: `libero`
Import: `use libero::components::Anchor;`
Feature: `anchor` (on by default, turns on `use-icon`, `visually-hidden`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/navigation/anchor.rs>
Index: [index.md](index.md) lists every other page
Description: A real link styled and sized like `Text`, router-aware through `to`; without `to`, a button with the link look.

A link styled and sized like [`Text`](text.md). A typed route in `to` navigates
through the app's router without a page reload. Without a router it renders a
plain `href` Without `to` it renders a button with the same
look, for an action.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::Anchor;

#[component]
fn Demo() -> Element {
    rsx! {
        Anchor {
            to: "https://dioxuslabs.com",
            target: "_blank",
            new_tab_icon: true,
            underline: "not-hover",
            "Read the Dioxus docs"
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `size` | `Responsive<Size>` | `md` | Text size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. Breakpoints follow the window, not the parent. |
| `to` | `NavigationTarget` | - | A path, a URL or a typed route (`Route::Foo {}`). Unset, the anchor renders a `<button type="button">` with the same look, for an action such as "Show details"; `target`, `new_tab_hint` and `new_tab_icon` then do nothing. With a router mounted and `target` unset, an internal target navigates without a page reload; with `"_blank"` it opens a new tab with a full page load. A `javascript:`, `data:` or `vbscript:` URL is blocked, in every link `to` and in `MenuItem::href`: the element renders without an `href`, so a click does nothing, and a warning is logged. |
| `onclick` | `EventHandler<MouseEvent>` | - | Runs on a click, or on Enter and Space without `to`. With `to` it runs too, after the router's own navigation. |
| `target` | `String` | - | The link's `target` attribute. `"_blank"` adds a hidden "(opens in a new tab)". |
| `new_tab_hint` | `bool` | `true` | `false` drops the hidden text a `"_blank"` target adds, and the icon. |
| `new_tab_icon` | `bool` | `false` | With the hint, draws a small external icon after the text. Unset, the theme's `anchor.new_tab_icon`, which is off. `Button`, `ActionIcon`, `Chip`, `NavLink`, `Repository` and `MenuItem` take it too. |
| `underline` | `AnchorUnderline` | `hover` | When the underline draws: `always`, `hover` (under the pointer, and at rest in a dark theme), `not-hover` (at rest, gone under the pointer) or `never`. On a touch screen nothing hovers, so in a light theme `hover` draws no underline and `not-hover` always does. |
| `children` | `Element` | required | The link's content. |
| `parts` | `Parts<AnchorPart>` | - | Styles for the inner parts in the Style API tab, under `sx`. |

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

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Styling](styling.md#style-api) explains how
parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `AnchorPart::NewTab` | `new-tab` | The new-tab icon after the text, with `target: "_blank"` and `new_tab_icon` only. |

## Accessibility

### Libero handles

- Without `to` it is a real `<button>`: a screen reader announces a button,
  and Enter and Space run `onclick`.
- A `target: "_blank"` link reads a hidden "(opens in a new tab)".
  `new_tab_icon: true` also draws a small external icon, for sighted users.
  `new_tab_hint: false` drops both, for a link whose text already says it.
- In a filled, tonal or gradient `Alert`, a colored `Header` and a `Mark`,
  a link takes the text color, so `underline: "hover"` draws its underline at
  rest there too.

### You must

- Leave `to` unset for an action that does not go anywhere, so it is announced
  as a button, not a link.
- Make the link text say where the link goes: it is the accessible name.
- Set `underline: "always"` on a link inside a paragraph: the default `hover`
  draws it only on hover (and at rest in a dark theme) and `never` not at all,
  so at rest in a light theme the link stands out by color alone.
- Give a nav row of Anchors its own padding for a 44 px finger target. Running
  text is exempt from WCAG 2.5.8, and an Anchor cannot tell which it is in.

### Example

A link in a paragraph, `Anchor { to: "https://www.rust-lang.org", target:
"_blank", underline: "always", "The Rust website" }`: it keeps its underline,
and a screen reader reads "The Rust website (opens in a new tab)".

## Theme defaults

`AnchorDefaults` on the theme. Sizing itself comes from `TextDefaults`, so an
`Anchor` and the `Text` around it step together.

| Field | Type | Description |
|---|---|---|
| `size` | `Size` | Default `size`, `md`. |
| `underline` | `AnchorUnderline` | Default `underline`, `hover`. |
| `color` | `Color` | The link color, `primary`. |
| `new_tab_icon` | `bool` | Default `new_tab_icon` of `Anchor`, `Button`, `ActionIcon`, `Chip`, `NavLink`, `Repository` and `MenuItem`, `false`. |

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-anchor-color` | The link color, from `AnchorDefaults::color`. |

Font size and family come from `Text`'s own `--lsx-text-*` variables.

## Data attributes

State tokens on the root's `data-state`, space separated.

| Token | Condition |
|---|---|
| `size-<size>` | The `size` in effect. |
| `underline-always` / `underline-hover` / `underline-not-hover` / `underline-never` | The `underline` in effect. |
