> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/click-away-listener.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# ClickAwayListener

Calls a function when the user clicks, taps or moves focus outside an element.

{% code title="click-away-listener/demo.tsx" %}

```tsx
import { useState } from "react";
import { Button, ClickAwayListener } from "@rierino-open/core-ui";

export default function ClickAwayDemo() {
  const [open, setOpen] = useState(false);

  return (
    <div style={{ display: "grid", gap: 12, justifyItems: "center", fontSize: 13 }}>
      <Button appearance="outline" onPress={() => setOpen(true)}>
        Show filters
      </Button>
      {open && (
        <ClickAwayListener onClickAway={() => setOpen(false)}>
          <div style={{ padding: 16, borderRadius: 8, background: "#f4f0ff", width: 240 }}>
            Filters panel. Click anywhere outside it to close it.
          </div>
        </ClickAwayListener>
      )}
    </div>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { ClickAwayListener } from '@rierino-open/core-ui';

<ClickAwayListener onClickAway={() => setOpen(false)}>
  <div className="Rie-Filters-panel">…</div>
</ClickAwayListener>
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import CoreUI from '@rierino-open/core-ui';
const { ClickAwayListener } = CoreUI;

<ClickAwayListener onClickAway={() => setOpen(false)}>
  <div className="Rie-Filters-panel">…</div>
</ClickAwayListener>
```

{% endtab %}
{% endtabs %}

ClickAwayListener renders nothing of its own. It wraps a **single child element** and calls `onClickAway` with the event when the user clicks or taps outside that element, or moves focus outside it. Use it to close panels, inline editors or custom dropdowns.

* The child must be able to hold a ref: a DOM element such as a `div`, or a component made with `forwardRef`.
* Clicks inside content that the child renders through a React portal count as inside, even though that content is elsewhere in the page.
* Events in the first moment after it mounts are ignored, so the click that opened the panel doesn't close it straight away.

{% hint style="info" %}
**Overlays already close on their own**

[Modal](https://docs.rierino.com/sdk-core-ui/components/modal.md), [Popover](https://docs.rierino.com/sdk-core-ui/components/popover.md) and [Menu](https://docs.rierino.com/sdk-core-ui/components/menu.md) close on outside clicks and `Esc` by themselves. Use ClickAwayListener for your own elements.
{% endhint %}

## Examples

### Choosing the events

`mouseEvent` sets which mouse event counts as a click outside: `click` (the default), `mousedown` or `mouseup`. `mousedown` reacts as soon as the button goes down, before a click completes. `touchEvent` does the same for touch, and `focusEvent` for focus: by default, moving focus to an element outside, for example with `Tab`, also calls `onClickAway`.

{% code title="click-away-listener/events.tsx" %}

```tsx
import { useState } from "react";
import { ClickAwayListener } from "@rierino-open/core-ui";

export default function ClickAwayEvents() {
  const [log, setLog] = useState<string[]>([]);
  const add = (text: string) => setLog((current) => [text, ...current].slice(0, 4));

  return (
    <div style={{ display: "grid", gap: 12, justifyItems: "center", fontSize: 12 }}>
      {/* mousedown reacts as soon as the button goes down, before a click completes. */}
      <ClickAwayListener mouseEvent="mousedown" onClickAway={(e) => add(`outside: ${e.type}`)}>
        <div style={{ padding: 16, borderRadius: 8, background: "#f4f0ff", width: 240 }}>
          Press outside this box, or move focus out of it with Tab.
          <input aria-label="Inside" placeholder="Focusable inside" style={{ marginTop: 8, width: "100%" }} />
        </div>
      </ClickAwayListener>
      <input aria-label="Outside" placeholder="Focusable outside" />
      <div>{log.length ? log.map((l, i) => <div key={i}>{l}</div>) : "Nothing yet"}</div>
    </div>
  );
}
```

{% endcode %}

## Accessibility

ClickAwayListener doesn't add anything for keyboard users by itself. When you use it to close something, also close it on `Esc`.

## API reference

| Prop          | Type                                                                  | Default      | Description                                                                                      |
| ------------- | --------------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------ |
| `onClickAway` | `(event: Events) => void`                                             |              | **Required.** Called with the event when the user clicks, taps or moves focus outside the child. |
| `focusEvent`  | `FocusEvents`                                                         | `"focusin"`  | The focus event that counts as leaving the child.                                                |
| `mouseEvent`  | `MouseEvents`                                                         | `"click"`    | The mouse event that counts as a click outside.                                                  |
| `touchEvent`  | `TouchEvents`                                                         | `"touchend"` | The touch event that counts as a tap outside.                                                    |
| `children`    | `React.ReactElement<any, string \| React.JSXElementConstructor<any>>` |              | **Required.** A single element that can hold a ref, such as a `div` or a `forwardRef` component. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/click-away-listener.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
