# react-floatybox

> A React component for positioning floating components such as tooltips, dropdowns, selects etc. Avoids screen edges!

Latest version **0.3.1** (published 2020-02-06) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2020-02-06 |
| First published | 2019-12-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Damien Clarke |
| Maintainers | dxinteractive |
| Keywords | react, tooltip, dropdown, position, float, fixed position, avoid screen edges |

## Links

- npm: https://www.npmjs.com/package/react-floatybox
- Repository: https://github.com/92green/react-floatybox
- Homepage: https://github.com/92green/react-floatybox#readme
- Issues: https://github.com/92green/react-floatybox/issues
- npm.io page: https://npm.io/package/react-floatybox

## Dependencies (4)

- [prop-types](https://npm.io/package/prop-types.md) ^15.7.2
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.1.5
- [react-useportal](https://npm.io/package/react-useportal.md) ^1.0.13
- [react-use-real-dimensions](https://npm.io/package/react-use-real-dimensions.md) ^0.1.1

## 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

- 0.3.1 (latest) — 2020-02-06
- 0.3.0 — 2020-01-30
- 0.2.0 — 2020-01-23
- 0.1.2 — 2020-01-08
- 0.1.1 — 2020-01-07
- 0.1.0 — 2020-01-06
- 0.0.7 — 2019-12-31
- 0.0.6 — 2019-12-31
- 0.0.5 — 2019-12-31
- 0.0.4 — 2019-12-30
- 0.0.3 — 2019-12-30
- 0.0.2 — 2019-12-30
- 0.0.1 — 2019-12-30

## README

# react-floatybox 🎈🎁🎉

A React component for positioning floating components such as tooltips, dropdowns, selects etc. Avoids screen edges!

* **[See some examples](https://92green.github.io/react-floatybox/)**

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Props](#props)
- [Development](#development)

## Features
- Handles all your bubble positioning
- Avoids screen edges
- Bubble size and position can be determined automatically, specified widths and heights not required
- Built in support for positioning of tails (those little pointy things at the bottom of tooltips)
- Built in behaviour to open and close via hover or click, and to close via click-outside or ESC key
- Can use its own state or can be controlled
- Uses React portals via the [react-useportal](https://github.com/alex-cory/react-useportal) hook

## Installation

```
yarn add react-floatybox
```

## Usage

### Basic

```js
import React from 'react';
import {useCallback} from 'react';
import FloatyBox from 'react-floatybox';

const Basic = (props) => {

    let tooltip = useCallback(() => {
        return <div>I am a tooltip</div>;
    }, []);

    return <FloatyBox open="hover" bubble={tooltip}>hover over me!</FloatyBox>;
};
```

### With a tail

```js
import React from 'react';
import {useCallback} from 'react';
import FloatyBox from 'react-floatybox';
import Point from 'react-floatybox/Point';

const WithTail = (props) => {

    let tooltip = useCallback(({tailProps}) => {
        return <div>I am a tooltip <Point {...tailProps} color="#000" /></div>;
    }, []);

    return <FloatyBox open="hover" bubble={tooltip} tailSize={20}>hover over me!</FloatyBox>
};
```

### Alignment

```js
import React from 'react';
import {useCallback} from 'react';
import FloatyBox from 'react-floatybox';

const Basic = (props) => {

    let tooltip = useCallback(() => {
        return <div>I am a thing</div>;
    }, []);

    return <FloatyBox open="click" side="top" align="left" bubble={tooltip}>click me!</FloatyBox>;
};
```

[See more examples](https://92green.github.io/react-floatybox/)

## Props

### children

```flow
children: React.Node
```

The React element that the bubble is tethered to, called the "anchor".
It can handle click and hover events to control the open state of the bubble.

### bubble

```flow
bubble: ({close, isOpen, tailProps}) => React.Node
```

A function for FloatyBox to call to render the floaty box.
It's recommended you wrap this in a `useCallback` hook to improve rendering performance.
The function is passed an object with a few properties:

|               |                                               |                                                                                     |
| ------------- | --------------------------------------------- | ----------------------------------------------------------------------------------- |
| **close**     | `() => void`                                  | A function that can be called from inside the bubble to close itself.               |
| **isOpen**    | `boolean`                                     | A boolean indicating if the bubble is open.                                         |
| **tailProps** | `{side: string, size: number, style: Object}` | An object that can be spread onto a tail component such as `react-floatybox/Point`. |

### open

```flow
open?: "click"|"hover"|"always" // optional
```

If provided, this sets the kind of interaction that will open and close the bubble.

### side

```flow
side: "top"|"bottom"|"left"|"right" = "top"
```

Chooses the preferred side of the anchor that the bubble should appear on.

### align

```flow
align: string = "center"
```

Sets the bubble's preferred alignment in relation to its tail.

- When `side` is `"top"` or `"bottom"`, valid values are `"center"`, `"left"` or `"right"`.
- When `side` is `"left"` or `"right"`, valid values are `"center"`, `"up"` or `"down"`.

### alignInner

```flow
alignInner: string = "center"
```

Sets the tail's preferred alignment in relation to the anchor.

When building things with small anchors and large bubbles, such as tooltips, this prop is usually best on its default "center" setting. But if your anchor is larger than your bubble then this alignment becomes more useful.

- When `side` is `"top"` or `"bottom"`, valid values are `"center"`, `"left"` or `"right"`.
- When `side` is `"left"` or `"right"`, valid values are `"center"`, `"up"` or `"down"`.

### flip

```flow
flip: boolean = false
```

Set to true to allow FloatyBox to flip the bubble to the opposite side of the anchor if there is not enough space to fit it on the preferred side.

### slide

```flow
slide: boolean = false
```

Set to true to allow FloatyBox to slide the bubble across the side of the anchor if there is not enough space to fit it at the preferred alignment.

### trap

```flow
trap: boolean = false
```

Trap will prevent the bubble from ever leaving the screen.

### gap

```flow
gap: number = 10
```

The gap between the bubble and the anchor, in pixels.

### edge

```flow
edge: number = 10
```

How close the bubble is allowed to be posiitioned near a screen edge, in pixels.

### zIndex

```flow
zIndex: number = 100
```

The `zIndex` of the bubble element.

### closeOnOutsideClick

```flow
closeOnOutsideClick: boolean = true
```

A [react-useportal](https://github.com/alex-cory/react-useportal) option that lets the bubble close when you click outside of it.

### closeOnEsc

```flow
closeOnEsc: boolean = true
```

A [react-useportal](https://github.com/alex-cory/react-useportal) option that lets the bubble close when you press the escape key.

### tailSize

```flow
tailSize?: number // optional
```

If a tail is used on your bubble, `tailSize` must be set so FloatyBox can adjust its positioning.

### wrap

```flow
wrap: React.Component = "span"
```

The component that the FloatyBox anchor gets wrapped in.

### forceUpdate

```flow
forceUpdate: Array<any> = []
```

The forceUpdate prop allows you to force the bubble position to update. Pass it an array of values, and when any of these values change then the bubble position will be recalculated.

### isOpen

```flow
isOpen?: boolean // optional
```

If provided, FloatyBox won't keep its own state and will just be open when this boolean is `true`.

### onChange

```flow
onChange?: (isOpen: boolean) => void (optional)` // optional
```

If provided along with `isOpen`, this will be called when FloatyBox wants to change state.

## Development

React-floatybox is written and maintained by [Damien Clarke](https://damienclarke.me/), with feedback from others at [92green](https://github.com/92green).
All online library discussion happens over on [Github](https://github.com/92green/react-floatybox).

I hope this library helps solve some React positioning problems for you. 🎉

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