Skip to content

Commit 5cd74c9

Browse files
endigo9740Hugos68
andauthored
Add FloatingArrow component (#35)
* Initial component structure * Refinement * Move styleParser to utils * Add component to index * Progress * Merge plus Platform import * Updated FloatingContext type. Updated ref type. Import `platform` from dom package * Updated type for style parser. * Refinement * Minor adjustments * Styles, comments, temp pre output * Add offset middlware * Added changeset * Runified FloatingArrow * Preview styles, readme updates * Readme props added * Cleanup * Unify styles parser, type defaults added * Linting pass * Remove SSR setting * Cleaned up reactivity with derived destructure * Reactivity cleanup * Moved to switch case statement. Makes it more clear * Review feedback implemented * Basic test case, fix rest spread --------- Co-authored-by: hugos68 <[email protected]>
1 parent a8a0904 commit 5cd74c9

File tree

10 files changed

+3316
-3633
lines changed

10 files changed

+3316
-3633
lines changed

.changeset/nine-hounds-flow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@skeletonlabs/floating-ui-svelte": minor
3+
---
4+
5+
feature: added the FloatingArrow component

README.md

+75-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ npm install @skeletonlabs/floating-ui-svelte
1313

1414
## Usage
1515

16+
### Making elements "float"
17+
18+
We want it to float on top of the UI though, so it doesn’t disrupt the flow of the document. Add this class to all floating elements. Note that Floating UI does not have opinions about how your elements stack on the z-axis.
19+
20+
```css
21+
.floating {
22+
width: max-content;
23+
position: absolute;
24+
top: 0;
25+
left: 0;
26+
}
27+
28+
```
29+
30+
### The Basics
31+
1632
Import the desired hook or component from floating-ui-svelte. [View each example](https://floating-ui-svelte.vercel.app/) for additional guidance.
1733

1834
```js
@@ -38,8 +54,8 @@ The `useFloating` Svelte hook acts as a controller for all other Floating UI Sve
3854
const floating = useFloating({ elements });
3955
</script>
4056

41-
<div bind:this="{elements.reference}">Reference</div>
42-
<div bind:this="{elements.floating}" style="{floating.floatingStyles}">Floating</div>
57+
<button bind:this="{elements.reference}">Reference</button>
58+
<div bind:this="{elements.floating}" style="{floating.floatingStyles}" class="floating">Floating</div>
4359
```
4460

4561
> [!WARNING]
@@ -135,7 +151,63 @@ This will ensure all event handlers will be registered rather being overruled by
135151

136152
### FloatingArrow
137153

138-
(tbd)
154+
Renders a customizable `<svg>` pointing arrow triangle inside the floating element that gets automatically positioned.
155+
156+
```html
157+
<script lang="ts">
158+
import { arrow, useFloating, FloatingArrow, autoUpdate, offset } from '$lib/index.js';
159+
160+
let arrowRef: HTMLElement | null = $state(null);
161+
162+
const elements: { reference: HTMLElement | null; floating: HTMLElement | null } = $state({
163+
reference: null,
164+
floating: null
165+
});
166+
167+
const floating = useFloating({
168+
elements,
169+
get middleware() {
170+
return [
171+
offset(10),
172+
arrowRef && arrow({ element: arrowRef })
173+
];
174+
}
175+
});
176+
</script>
177+
178+
<button bind:this={elements.reference}>Reference</button>
179+
<div bind:this={elements.floating} style={floating.floatingStyles} class="floating">
180+
<div>Floating</div>
181+
<FloatingArrow
182+
bind:ref={arrowRef}
183+
context={floating.context}
184+
classes="fill-surface-500"
185+
/>
186+
</div>
187+
```
188+
189+
#### Props
190+
191+
| Prop | Description | Default | Type |
192+
| -------- | ----------- | ---- | ---- |
193+
| ref* | Binded element reference. | - | HTMLElement, null |
194+
| context* | The context object returned from useFloating(). | - | FloatingContext |
195+
| width | The width of the arrow. | `14` | number |
196+
| height | The height of the arrow. | `7` | number |
197+
| tipRadius | The radius (rounding) of the arrow tip. | `0` (sharp) | number |
198+
| staticOffset | A static offset override of the arrow from the floating element edge. Often desirable if the floating element is smaller than the reference element along the relevant axis and has an edge alignment (`start`/`end`). | `undefined` (use dynamic path) | string, number, null |
199+
| d | A custom path for the arrow. Useful if you want fancy rounding. The path should be inside a square SVG and placed at the `bottom` of it. The path is designed for the 'bottom' placement, which will be rotated for other placements. | `"black"` (browser default) | string |
200+
| fill | The color of the arrow. | xxx | string |
201+
| stroke | The stroke (border) color of the arrow. This must match (or be less than) the floating element’s border width. | `"none"` | string |
202+
| strokeWidth | The stroke (border) width of the arrow. | `0` | number |
203+
204+
#### Utility Classes and Styles
205+
206+
Provide artibrary utility classes using the standard attribute.
207+
208+
```html
209+
<FloatingArrow class="fill-white" />
210+
```
139211

140212
### FloatingOverlay
141213

0 commit comments

Comments
 (0)