|
21 | 21 | ```bash
|
22 | 22 | npm install sb-floating-panel-vue
|
23 | 23 | # or
|
24 |
| -yarn add sb-floating-panel-vue |
| 24 | +yarn add sb-floating-panel-vue |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 🧩 Usage |
| 30 | + |
| 31 | +The `useSbFloatingPanel` composable provides an easy-to-integrate API to handle floating panels, such as tooltips, popovers, dropdowns, and other UI overlays. |
| 32 | + |
| 33 | +### Importing |
| 34 | + |
| 35 | +```ts |
| 36 | +import { useSbFloatingPanel } from 'sb-floating-panel-vue'; |
| 37 | +``` |
| 38 | + |
| 39 | +### 🧠 API |
| 40 | + |
| 41 | +```ts |
| 42 | +const { |
| 43 | + reference, |
| 44 | + floating, |
| 45 | + popperArrow, |
| 46 | + isOpen, |
| 47 | + floatingPosition, |
| 48 | + floatingStyles, |
| 49 | + floatingArrowStyles, |
| 50 | + changeFloatingVisibility, |
| 51 | +} = useSbFloatingPanel(settings); |
| 52 | +``` |
| 53 | + |
| 54 | +### Parameters |
| 55 | + |
| 56 | +The composable takes a `settings` object: |
| 57 | + |
| 58 | +```ts |
| 59 | +interface FloatingSettings { |
| 60 | + placement: Placement; // e.g., 'bottom', 'top-start', etc. |
| 61 | + strategy: Strategy; // 'absolute' | 'fixed' |
| 62 | + offsetValue: number; // offset in pixels from the reference element |
| 63 | + hasArrow?: boolean; // enable arrow positioning |
| 64 | + hasResize?: boolean; // enable auto-resizing with anchor element |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## 🔍 Computed Values Explained |
| 71 | + |
| 72 | +### `floatingStyles` |
| 73 | + |
| 74 | +A Vue `computed` value containing the CSS style object that must be applied to your floating panel. It is dynamically calculated by `@floating-ui/vue` to maintain optimal placement relative to the reference element. |
| 75 | + |
| 76 | +### `floatingPosition` |
| 77 | + |
| 78 | +The actual resolved placement string, e.g., `'bottom-start'`, useful for adding custom animations or behavior depending on where the panel is placed. |
| 79 | + |
| 80 | +### `floatingArrowStyles` |
| 81 | + |
| 82 | +An object that includes styles for positioning the optional arrow element (enabled via the `hasArrow` setting). You can directly bind this to the arrow’s `style` attribute. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## 🧪 Example |
| 87 | + |
| 88 | +```ts |
| 89 | +<script setup lang="ts"> |
| 90 | +import { ref } from 'vue'; |
| 91 | +import { useSbFloatingPanel } from 'sb-floating-panel-vue'; |
| 92 | + |
| 93 | +const { |
| 94 | + reference, |
| 95 | + floating, |
| 96 | + popperArrow, |
| 97 | + isOpen, |
| 98 | + floatingStyles, |
| 99 | + floatingArrowStyles, |
| 100 | + changeFloatingVisibility, |
| 101 | +} = useSbFloatingPanel({ |
| 102 | + placement: 'bottom', |
| 103 | + strategy: 'absolute', |
| 104 | + offsetValue: 8, |
| 105 | + hasArrow: true, |
| 106 | + hasResize: true, |
| 107 | +}); |
| 108 | + |
| 109 | +const togglePanel = () => { |
| 110 | + changeFloatingVisibility(!isOpen.value); |
| 111 | +}; |
| 112 | +</script> |
| 113 | +``` |
| 114 | + |
| 115 | +```vue |
| 116 | +<template> |
| 117 | + <button ref="reference" @click="togglePanel">Toggle Panel</button> |
| 118 | + <div |
| 119 | + v-if="isOpen" |
| 120 | + ref="floating" |
| 121 | + :style="floatingStyles" |
| 122 | + class="popover" |
| 123 | + > |
| 124 | + I'm a floating panel! |
| 125 | + <div ref="popperArrow" :style="floatingArrowStyles.arrow" class="arrow" /> |
| 126 | + </div> |
| 127 | +</template> |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## 🎨 Styling |
| 133 | + |
| 134 | +You can use your own CSS or utility classes (like Tailwind CSS) to style the panel and the arrow. Example: |
| 135 | + |
| 136 | +```css |
| 137 | +.popover { |
| 138 | + background: white; |
| 139 | + border: 1px solid #ccc; |
| 140 | + padding: 0.75rem; |
| 141 | + border-radius: 0.5rem; |
| 142 | + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); |
| 143 | +} |
| 144 | + |
| 145 | +.arrow { |
| 146 | + width: 10px; |
| 147 | + height: 10px; |
| 148 | + background: white; |
| 149 | + transform: rotate(45deg); |
| 150 | + position: absolute; |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## 📄 License |
| 157 | + |
| 158 | +MIT © [Stefano Biddau](https://github.com/stefanobiddau) |
0 commit comments