# react-fit

> Fit a popover element on the screen.

Latest version **3.0.0** (published 2025-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-fit
pnpm add react-fit
yarn add react-fit
bun add react-fit
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2025-05-29 |
| First published | 2018-10-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 25 |
| Author | Wojciech Maj |
| Maintainers | wojtekmaj |
| Keywords | react, collision, collision-detection, position |

## Links

- npm: https://www.npmjs.com/package/react-fit
- Repository: https://github.com/wojtekmaj/react-fit
- Funding: https://github.com/wojtekmaj/react-fit?sponsor=1
- npm.io page: https://npm.io/package/react-fit

## Dependencies (2)

- [warning](https://npm.io/package/warning.md) ^4.0.0
- [detect-element-overflow](https://npm.io/package/detect-element-overflow.md) ^2.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2025-05-29
- 1.0.0-alpha.3 (next) — 2018-12-24
- 2.0.1 — 2024-04-30
- 2.0.0 — 2024-04-29
- 1.7.1 — 2023-10-18
- 1.7.0 — 2023-07-27
- 1.6.0 — 2023-07-21
- 1.5.1 — 2023-03-26
- 1.5.0 — 2023-02-02
- 1.4.0 — 2022-01-19
- 1.3.2 — 2021-09-23
- 1.3.1 — 2020-10-07
- 1.3.0 — 2020-10-07
- 1.2.1 — 2020-08-04
- 1.2.0 — 2020-03-02
- … 13 more at https://npm.io/package/react-fit/versions

## README

[![npm](https://img.shields.io/npm/v/react-fit.svg)](https://www.npmjs.com/package/react-fit) ![downloads](https://img.shields.io/npm/dt/react-fit.svg) [![CI](https://github.com/wojtekmaj/react-fit/actions/workflows/ci.yml/badge.svg)](https://github.com/wojtekmaj/react-fit/actions)

# React-Fit

A component that aligns its child relatively to its parent while being aware where it may and may not fit.

## tl;dr

- Install by executing `npm install react-fit` or `yarn add react-fit`.
- Import by adding `import Fit from 'react-fit'`.
- Do stuff with it!
  ```tsx
  function ElementWithChild() {
    return (
      <Parent>
        <Fit>
          <PopoverChild />
        </Fit>
      </Parent>
    );
  }
  ```

## Getting started

### Compatibility

Your project needs to use React 16.8 or later.

### Installation

Add React-Fit to your project by executing `npm install react-fit` or `yarn add react-fit`.

## How does it work?

1. By default, the element provided to `<Fit />` as a child is displayed below its parent, aligned to the left.
2. If the element can't fit in this position and collides with bottom and/or right border of the container, `<Fit />` checks if there's more space for the element on the other side(s) of the axis/axes the collision(s) has been detected on. If so, the element is moved above its parent and/or aligned to the right, depending on the collision axis.
3. If the element still can't fit where it's placed, `<Fit />` decreases the element's size. If `min-width`/`min-height` are provided, they will be respected.

## Positioning the element

### Vertical axis (default)

By default, the element is displayed below its parent, aligned to the left of its parent.

```
┌────────────┐
│   Parent   │
├────────────┴────────────┐
│                         │
│         Child           │
│                         │
└─────────────────────────┘
```

- To display the element above: provide `invertAxis` flag.
- To align the element to the right: provide `invertSecondaryAxis` flag.

### Horizontal axis (`mainAxis="x"`)

By providing `mainAxis="x"` to `<Fit />`, the element is displayed on the right of its parent, aligned to the top of its parent.

```
┌────────────┬─────────────────────────┐
│   Parent   │                         │
└────────────┤         Child           │
             │                         │
             └─────────────────────────┘
```

- To display the element on the left: provide `invertAxis` flag.
- To align the element to the bottom: provide `invertSecondaryAxis` flag.

### Spacing

By default, React-Fit leaves 8px of space between its child and the borders of the container.

```
┌──────────────────────────────────────────┐
│ ┌────────────┐                           │
│ │   Parent   │                           │
│ ├────────────┴────────────┐              │
│ │                         │              │
│ │         Child           │              │
│ │                         │              │
│ └─────────────────────────┘              │
└──────────────────────────────────────────┘
```

If you wish to change this spacing, you can provide `spacing` to `<Fit />`. For example, if you wish for the child to touch the borders of the container, decrease the spacing by providing `spacing={0}` to `<Fit />`.

```
┌──────────────────────────────────────────┐
│ ┌────────────┐                           │
│ │   Parent   │                           │
│ ├────────────┴────────────┐              │
│ │                         │              │
│ │         Child           │              │
│ │      (now higher)       │              │
│ │                         │              │
└─┴─────────────────────────┴──────────────┘
```

You can also provide different spacing for each side by providing an object, for example `spacing={{ top: 10, bottom: 20, left: 30, right: 40 }}`, to `<Fit />`. **Note:** Memoize the object or define it outside render function to avoid unnecessary re-renders.

## Styling

To avoid unnecessary style recalculations that may be caused by React-Fit applying the styles needed to make it work properly, the element should have absolute position, and its parent element should have relative or absolute position.

## License

The MIT License.

## Author

<table>
  <tr>
    <td >
      <img src="https://avatars.githubusercontent.com/u/5426427?v=4&s=128" width="64" height="64" alt="Wojciech Maj">
    </td>
    <td>
      <a href="https://github.com/wojtekmaj">Wojciech Maj</a>
    </td>
  </tr>
</table>

---
_Source: https://npm.io/package/react-fit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
