# flip

A visibility optimizer that changes the placement of the floating
element in order to keep it in view, with the ability to flip to
any placement.

<div className="flex flex-col gap-4">
  <Chrome
    label="Scroll up"
    center
    scrollable="y"
    className="h-96"
  >
    <Floating middleware={[{name: 'flip'}]}>
      <div className="grid h-32 w-32 place-items-center border-2 border-dashed border-gray-1000">
        Default
      </div>
    </Floating>
  </Chrome>
</div>

This is useful to prevent the floating element from overflowing
on the main axis of its placement.

## Usage

```js
import {computePosition, flip} from '@floating-ui/dom';

computePosition(referenceEl, floatingEl, {
  middleware: [flip()],
});
```

<Notice type="warning" title="Note">
  This is an alternative to `autoPlacement(){:js}`, so only one of either can be used.
  See [how they differ here](/docs/flip#conflict-with-autoplacement).
</Notice>

## Options

These are the options you can pass to `flip(){:js}`.

```ts
interface Options extends DetectOverflowOptions {
  mainAxis?: boolean;
  crossAxis?: boolean;
  fallbackAxisSideDirection?: 'none' | 'start' | 'end';
  flipAlignment?: boolean;
  fallbackPlacements?: Array<Placement>;
  fallbackStrategy?: 'bestFit' | 'initialPlacement';
}
```

### mainAxis

default: `true{:js}`

This is the main axis in which overflow is checked to perform a
flip. By disabling this, it will ignore overflow.

- `y`-axis for `'top'{:js}` and `'bottom'{:js}` placements
- `x`-axis for `'left'{:js}` and `'right'{:js}` placements

```js
flip({
  mainAxis: false,
});
```

<div className="flex flex-col gap-4">
  <Chrome
    label="Scroll up"
    center
    scrollable="y"
    className="h-96"
  >
    <Floating
      content="I will ignore main axis overflow"
      middleware={[
        {
          name: 'flip',
          options: {
            mainAxis: false,
          },
        },
      ]}
    >
      <div className="grid h-32 w-32 place-items-center border-2 border-dashed border-gray-1000" />
    </Floating>
  </Chrome>
</div>

### crossAxis

default: `true{:js}`

This is the cross axis in which overflow is checked to perform a
flip, the axis perpendicular to `mainAxis{:.objectKey}`. By
disabling this, it will ignore overflow.

```js
flip({
  crossAxis: false,
});
```

<div className="flex flex-col gap-4">
  <Chrome
    label="Scroll around"
    center
    scrollable="both"
    className="h-96"
  >
    <Floating
      placement="bottom-start"
      content="I will check cross axis overflow (default)"
      middleware={[
        {
          name: 'flip',
          options: {
            crossAxis: true,
            rootBoundary: 'document'
          },
        },
      ]}
    >
      <div className="grid place-items-center w-32 h-32 border-2 border-gray-1000 border-dashed" />
    </Floating>
  </Chrome>

  <Chrome
    label="Scroll around"
    center
    scrollable="both"
    className="h-96"
  >
    <Floating
      placement="bottom-start"
      content="I will ignore cross axis overflow"
      middleware={[
        {
          name: 'flip',
          options: {
            crossAxis: false,
            rootBoundary: 'document'
          },
        },
      ]}
    >
      <div className="grid place-items-center w-32 h-32 border-2 border-gray-1000 border-dashed" />
    </Floating>
  </Chrome>
</div>

### fallbackAxisSideDirection

default: `'none'{:js}`

Whether to allow fallback to the opposite axis if no placements
along the preferred placement axis fit, and if so, which side
direction along that axis to choose. If necessary, it will
fallback to the other direction.

- `'none'{:js}` signals that no fallback to the opposite axis
  should take place.
- `'start'{:js}` represents `'top'{:js}` or `'left'{:js}`.
- `'end'{:js}` represents `'bottom'{:js}` or `'right'{:js}`.

<Notice gap="above">
  In RTL writing direction, the x-axis directions are reversed.
</Notice>

For instance, by default, if the initial `placement{:.objectKey}`
is set to `'right'{:js}`, then the placements to try (in order)
are:

`['right', 'left']{:js}`

On a narrow viewport, it's possible or even likely that _neither_
of these will fit.

By specifying a string other than `'none'{:js}`, you allow
placements along the perpendicular axis of the initial placement
to be tried. The direction determines which side of placement is
tried first:

```js
flip({
  fallbackAxisSideDirection: 'start',
});
```

The above results in: `['right', 'left', 'top', 'bottom']{:js}`.

```js
flip({
  fallbackAxisSideDirection: 'end',
});
```

The above results in: `['right', 'left', 'bottom', 'top']{:js}`.

As an example, if you'd like a tooltip that has a placement of
`'right'{:js}` to be placed on top on mobile (assuming it doesn't
fit), then you'd use `'start'{:js}`. For an interactive popover,
you likely want to use `'end'{:js}` so it's placed on the bottom,
closer to the user's fingers.

In each of the following demos, the `placement{:.objectKey}` is
`'right'{:js}`.

<div className="flex flex-col gap-4">
  <Chrome
    label="Scroll horizontally"
    center
    scrollable="x"
    className="h-96"
  >
    <Floating
      placement="right"
      content="fallbackAxisSideDirection is set to 'none' (default)"
      middleware={[
        {
          name: 'flip',
          options: {
            fallbackAxisSideDirection: 'none',
            rootBoundary: 'document'
          },
        },
      ]}
    >
      <div className="grid place-items-center w-32 h-32 border-2 border-gray-1000 border-dashed mt-12" />
    </Floating>
    <p>Notice that it can overflow.</p>
  </Chrome>

<Chrome
  label="Scroll horizontally"
  center
  scrollable="x"
  className="h-96"
>
  <Floating
    placement="right"
    content="fallbackAxisSideDirection is set to 'start'"
    middleware={[
      {
        name: 'flip',
        options: {
          fallbackAxisSideDirection: 'start',
          rootBoundary: 'document',
        },
      },
    ]}
  >
    <div className="mt-12 grid h-32 w-32 place-items-center border-2 border-dashed border-gray-1000" />
  </Floating>
  <p>Notice that it prefers `top` if it doesn't fit.</p>
</Chrome>

  <Chrome
    label="Scroll horizontally"
    center
    scrollable="x"
    className="h-96"
  >
    <Floating
      placement="right"
      content="fallbackAxisSideDirection is set to 'end'"
      middleware={[
        {
          name: 'flip',
          options: {
            fallbackAxisSideDirection: 'end',
            rootBoundary: 'document'
          },
        },
      ]}
    >
      <div className="grid place-items-center w-32 h-32 border-2 border-gray-1000 border-dashed mt-12" />
    </Floating>
    <p>Notice that it prefers `bottom` if it doesn't fit.</p>
  </Chrome>
</div>

#### Combining with `shift`

If `shift(){:js}` is in use in the middleware array, you may
desire to disable `crossAxis{:.objectKey}` overflow checking,
which will allow `shift(){:js}` to perform its work without
falling back to the opposite axis (therefore preserving the
original axis as best as possible):

```js {4}
const middleware = [
  flip({
    fallbackAxisSideDirection: 'start',
    crossAxis: false,
  }),
  shift(),
];
```

If your placement is not edge aligned, disabling it is usually a
good choice.

### flipAlignment

default: `true{:js}`

When an alignment is specified, e.g. `'top-start'{:js}` instead
of just `'top'{:js}`, this will flip to `'top-end'{:js}` if
`start{:.string}` doesn't fit.

```js
flip({
  flipAlignment: false,
});
```

When using this with the `shift(){:js}` middleware, ensure
`flip(){:js}` is placed **before** `shift(){:js}` in your
middleware array. This ensures the `flipAlignment{:.objectKey}`
logic can act before `shift(){:js}`'s does.

### fallbackPlacements

default: `[oppositePlacement]{:js}`

This describes an **explicit** array of placements to try if the
initial `placement{:.objectKey}` doesn't fit on the axes in which
overflow is checked.

```js
flip({
  fallbackPlacements: ['right', 'bottom'],
});
```

In the above example, if `placement{:.objectKey}` is set to
`'top'{:js}`, then the placements to try (in order) are:

`['top', 'right', 'bottom']{:js}`

<div className="flex flex-col gap-4">
  <Chrome
    label="Scroll down"
    center
    scrollable="y"
    className="h-96"
  >
    <Floating
      placement="top"
      content="Floating"
      middleware={[
        {
          name: 'flip',
          options: {
            fallbackPlacements: ['right', 'bottom'],
            rootBoundary: 'document',
          },
        },
      ]}
    >
      <div className="grid h-32 w-32 place-items-center border-2 border-dashed border-gray-1000" />
    </Floating>
  </Chrome>
</div>

<Notice gap="above">
  The options `flipAlignment{:.objectKey}` and
  `fallbackAxisSideDirection{:.objectKey}` no longer have an effect if
  this option is explicitly specified, as they are only shortcuts to
  create a computed list of fallback placements. To ensure your
  explicit list is preferred, they will be ignored.
</Notice>

### fallbackStrategy

default: `'bestFit'{:js}`

When no placements fit, then you'll want to decide what happens.
`'bestFit'{:js}` will use the placement which fits best on the
checked axes. `'initialPlacement'{:js}` will use the initial
`placement{:.objectKey}` specified.

```js
flip({
  fallbackStrategy: 'initialPlacement',
});
```

### ...detectOverflowOptions

All of [`detectOverflow`](/docs/detectOverflow#options)'s options
can be passed. For instance:

```js
flip({
  padding: 5, // 0 by default
});
```

### Deriving options from state

You can derive the options from the
[middleware lifecycle state](/docs/middleware#middlewarestate):

```js
flip((state) => ({
  padding: state.rects.reference.width,
}));
```

## Final placement

The placement returned from the function is always the final one,
not necessarily the one you passed in as the "preferred" one.

```js
computePosition(referenceEl, floatingEl, {
  placement: 'bottom',
  middleware: [flip()],
}).then(({placement}) => {
  console.log(placement); // 'top' or 'bottom'
});
```

## Conflict with autoPlacement

`flip(){:js}` and `autoPlacement(){:js}` cannot be used together
inside the same middleware array, make sure you choose only one
of them to use.

The reason is they both try to perform work on the placement but
with opposing strategies. Therefore, they will continually try to
change the result or work of the other one, leading to a reset
loop.

- `flip(){:js}` — Uses a fallback "no space" strategy. Ensures
  the preferred placement is kept unless there is no space left.
- `autoPlacement(){:js}` — Uses a primary "most space" strategy.
  Always chooses the placement with the most space available.
