|
| 1 | +# Floating panel |
| 2 | + |
| 3 | +A thin wrapper around [Floating UI](https://floating-ui.com/) that positions the floating panel with respect to the |
| 4 | +anchor (trigger). |
| 5 | + |
| 6 | +## Imports |
| 7 | + |
| 8 | +```js |
| 9 | +import 'tailwindcss-elements/elements/floating_panel'; |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +```html |
| 15 | +<twc-floating-panel> |
| 16 | + <button type="button" data-target="twc-floating-panel.trigger">Anchor</button> |
| 17 | + <div data-target="twc-floating-panel.panel">Contents!</div> |
| 18 | +</twc-floating-panel> |
| 19 | +``` |
| 20 | + |
| 21 | +```js |
| 22 | +const element = document.querySelector('twc-floating-panel'); |
| 23 | + |
| 24 | +// Start the positioning logic |
| 25 | +element.active = true; |
| 26 | + |
| 27 | +// Stop the positioning logic |
| 28 | +element.active = false; |
| 29 | + |
| 30 | +// Update the position of the panel |
| 31 | +await element.position(); |
| 32 | +``` |
| 33 | + |
| 34 | +## Examples |
| 35 | + |
| 36 | +### Setting the initial position |
| 37 | + |
| 38 | +By default, the initial placement is set to `bottom-start`, but you can set it to one of `top`, `top-start`, `top-end`, |
| 39 | +`right`, `right-start`, `right-end`, `bottom`, `bottom-start`, `bottom-end`, `left`, `left-start`, or `left-end` by |
| 40 | +setting the `placement` attibute on the element. |
| 41 | + |
| 42 | +```html |
| 43 | +<twc-floating-panel placement="top-start"> |
| 44 | + ... |
| 45 | +</twc-floating-panel> |
| 46 | +``` |
| 47 | + |
| 48 | +For more info, visit the [official docs](https://floating-ui.com/docs/computePosition#placement). |
| 49 | + |
| 50 | +### Setting the positioning strategy |
| 51 | + |
| 52 | +By default, the positioning strategy is set as `absolute`, but you can set it to one of `absolute` or `fixed` by |
| 53 | +setting the `strategy` attribute on the element. |
| 54 | + |
| 55 | +```html |
| 56 | +<twc-floating-panel strategy="fixed"> |
| 57 | + ... |
| 58 | +</twc-floating-panel> |
| 59 | +``` |
| 60 | + |
| 61 | +For more info, visit the [official docs](https://floating-ui.com/docs/computeposition#strategy). |
| 62 | + |
| 63 | +### Adding space between the trigger and the panel element |
| 64 | + |
| 65 | +You can set the `offset-options` attribute as a number of a JSON object. |
| 66 | + |
| 67 | +```html |
| 68 | +<twc-floating-panel offset-options="10"> |
| 69 | + ... |
| 70 | +</twc-floating-panel> |
| 71 | + |
| 72 | +<twc-floating-panel offset-options='{"mainAxis": 10, "crossAxis": 20}'> |
| 73 | + ... |
| 74 | +</twc-floating-panel> |
| 75 | +``` |
| 76 | + |
| 77 | +For more info, visit the [official docs](https://floating-ui.com/docs/offset). |
| 78 | + |
| 79 | +### Syncing the width, height, or both with respect to the trigger element |
| 80 | + |
| 81 | +You can set the `sync` attribute on the element which accepts one of `width`, `height`, or `both`. |
| 82 | + |
| 83 | +- `width` -> Make the width of the panel equal to that of the trigger. |
| 84 | +- `height` -> Make the height of the panel equal to that of the trigger. |
| 85 | +- `both` -> Make both the width and height of the panel equal to that of the trigger. |
| 86 | + |
| 87 | +```html |
| 88 | +<twc-floating-panel sync="width"> |
| 89 | + ... |
| 90 | +</twc-floating-panel> |
| 91 | +``` |
| 92 | + |
| 93 | +## Events |
| 94 | + |
| 95 | +| Name | Bubbles | Description | |
| 96 | +| ------ | --------- | ------------ | |
| 97 | +| `twc-floating-panel:changed` | `true` | This event fires when the panel is positioned with respect to the trigger element. | |
0 commit comments