# useMediaQuery

> Reads whether a CSS media query matches and follows its changes.

`useMediaQuery` returns whether a media query currently matches and re-renders when the result changes. It is Base UI's `useMediaQuery` from `@base-ui/react/unstable-use-media-query`, re-exported through a Dify UI subpath.

```tsx
import { useMediaQuery } from '@langgenius/dify-ui/use-media-query'
```

## Usage guidelines

- **Use it when the result must leave CSS**: rendering a different component tree, passing a prop, or choosing behavior in JavaScript.
- **Prefer CSS for appearance**: responsive variants such as `sm:` and container queries change layout without a render pass and without a mismatch during hydration.
- **Pass an options object**: `options` is required by the type; pass `{}` for the defaults.
- **Expect `defaultMatches` before hydration**: on the server and during hydration the hook returns `defaultMatches` (`false` by default), or the `ssrMatchMedia` result when it is set, then the real result. Set `defaultMatches` to the more likely value, or `noSsr` when the result is used only on the client.
- **Write the query without `@media`**: a leading `@media` is stripped, so `'(width < 40rem)'` and `'@media (width < 40rem)'` behave the same.

## Options

```tsx
interface UseMediaQueryOptions {
  /**
   * As `window.matchMedia()` is unavailable on the server,
   * it returns a default matches during the first mount.
   * @default false
   */
  defaultMatches?: boolean | undefined
  /**
   * You can provide your own implementation of matchMedia.
   * This can be used for handling an iframe content window.
   */
  matchMedia?: typeof window.matchMedia | undefined
  /**
   * To perform the server-side hydration, the hook needs to render twice.
   * A first time with `defaultMatches`, the value of the server, and a second time with the resolved value.
   * This double pass rendering cycle comes with a drawback: it's slower.
   * You can set this option to `true` if you use the returned value **only** client-side.
   * @default false
   */
  noSsr?: boolean | undefined
  /**
   * You can provide your own implementation of `matchMedia`, it's used when rendering server-side.
   */
  ssrMatchMedia?: ((query: string) => { matches: boolean }) | undefined
}
```

`useMediaQuery.Options` is a namespace alias of `UseMediaQueryOptions`.

## Examples

### Switch to a drawer below the small breakpoint

`40rem` is the Tailwind CSS `sm` breakpoint.

```tsx
import { useMediaQuery } from '@langgenius/dify-ui/use-media-query'

function ConversationSidebar() {
  const isBelowSm = useMediaQuery('(width < 40rem)', {})

  return isBelowSm ? <SidebarDrawer /> : <SidebarPanel />
}
```

## API reference

### useMediaQuery

```ts
useMediaQuery(query: string, options: UseMediaQueryOptions): boolean
```

**Parameters**

| Parameter | Type                   | Description |
| --------- | ---------------------- | ----------- |
| `query`   | `string`               | —           |
| `options` | `UseMediaQueryOptions` | —           |

**Returns:** `boolean`
