# Media query

Crate: `libero`
Import: `use libero::hooks::{use_is_mobile, use_media_query};`
Feature: `use-media-query` (on by default; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/hooks/media_query.rs>
Index: [index.md](index.md) lists every other page
Description: Whether a CSS media query or the 768px mobile breakpoint matches, live, with a documented default where nothing can measure.

`use_media_query(query: &str) -> ReadSignal<bool>` answers whether a CSS media
query matches, and keeps answering as the viewport or the reader's settings
change. `use_is_mobile() -> ReadSignal<bool>` is `(max-width: 767px)`: the 768px
breakpoint. Passing a different query re-subscribes.

## Usage

```rust
use dioxus::prelude::*;
use libero::{
    components::{Flex, Text},
    hooks::{use_is_mobile, use_media_query},
};

#[component]
fn Layout() -> Element {
    let mobile = use_is_mobile();
    let wide = use_media_query("(min-width: 1024px)");

    rsx! {
        Flex { direction: "column", gap: "sm",
            Text {
                if mobile() {
                    "Mobile: under 768px"
                } else {
                    "Not mobile"
                }
            }
            Text {
                if wide() {
                    "Wide: 1024px or more"
                } else {
                    "Narrower than 1024px"
                }
            }
        }
    }
}
```

## API

```rust,ignore
pub fn use_media_query(query: &str) -> ReadSignal<bool>
pub fn use_is_mobile() -> ReadSignal<bool>
```

| Platform | Answer |
|---|---|
| Web | `matchMedia`, live. |
| WebView (desktop, Android) | `matchMedia` over the page's script, after its first reply. |
| Native Blitz | Its style engine, as its `@media` rules match: after mount, then each window resize, up to 500ms late. |
| Server render | `false`, always. |

Call both hooks unconditionally, in the same order every render.

## Accessibility

### Libero handles

- The answer follows the viewport and the reader's own settings live, so a
  zoomed page that narrows past a breakpoint switches layout.

### You must

- Keep the content and every control reachable in both layouts: a breakpoint may
  rearrange a page, never drop what a reader needs.
- Prefer CSS `@media` rules for pure styling. Use the hook when the component
  tree itself differs, such as a drawer in place of a sidebar.

### Example

A settings page that swaps its sidebar for a drawer on
`use_media_query("(max-width: 48em)")`: at 200% zoom the page narrows past the
breakpoint, the drawer takes over, and every link stays reachable from its
burger.

### Limits

- The first render answers `false` and the real answer lands after mount, so a
  layout chosen by the hook flashes its default once. Native Blitz reports no
  pointer, so `(pointer: coarse)` answers `false` there.
