Skip to content

Commit 08abb26

Browse files
authored
New user documentation improvements (#111)
* Various doc improvements * Get Started refinement, Navigation styles improved * Remove FloatingArrow hardcoded fill * Feedback implemented
1 parent 370f50d commit 08abb26

File tree

11 files changed

+255
-56
lines changed

11 files changed

+255
-56
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Floating UI Svelte
22

3-
A Floating UI wrapper for Svelte.
3+
Svelte bindings for [Floating UI](https://github.com/floating-ui/floating-ui). Based on [Floating UI React](https://floating-ui.com/docs/react).
44

55
## Attribution
66

7-
Based on [Floating UI](https://github.com/floating-ui/floating-ui) and [Floating UI React](https://floating-ui.com/docs/react). Maintained by [Hugo Korte](https://github.com/Hugos68), [Skeleton Labs](https://www.skeletonlabs.co/), and the [Svelte community](https://svelte.dev/).
7+
Maintained by [Hugo Korte](https://github.com/Hugos68), [Skeleton Labs](https://www.skeletonlabs.co/), and the [Svelte community](https://svelte.dev/).
88

99
## License
1010

src/app.pcss

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,20 @@ footer .anchor {
125125

126126
/* Preview Examples */
127127
.preview {
128-
@apply border-2 border-dashed border-surface-500/30 p-10 flex justify-center items-center rounded-3xl;
128+
@apply bg-surface-100 dark:bg-surface-900 ring-inset p-10 flex justify-center items-center rounded-lg;
129129
}
130130

131131
/* Floating (required) */
132132
.floating {
133133
@apply w-max absolute top-0 left-0 z-10;
134134
}
135135

136+
.popover-neutral {
137+
@apply bg-surface-500 text-white p-8 max-w-sm rounded shadow-xl;
138+
}
139+
136140
/* Buttons */
137-
.btn-cta {
141+
.btn-gradient {
138142
@apply flex items-center gap-2;
139143
@apply py-3 px-6 rounded shadow-lg;
140144
@apply bg-gradient-to-br from-red-300 via-violet-300 to-cyan-400;

src/docs/components/Navigation/Navigation.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@
7070
</a>
7171
</header>
7272
<!-- Nav List -->
73-
<div class="p-4 py-8 pb-32">
73+
<div class="p-8 pt-0 pb-32">
7474
{#each navigation as section}
7575
<nav>
76-
<span class="block font-bold text-white p-4">{section.label}</span>
77-
<ul class="border-l border-surface-500/50 ml-4">
76+
<span class="block font-bold text-white py-4">{section.label}</span>
77+
<ul class="border-l border-surface-500/50">
7878
{#each section.links as link}
7979
<li>
8080
<a

src/routes/(inner)/api/floating-arrow/+page.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const floating = useFloating({
5555
<FloatingArrow
5656
bind:this="{arrowRef}"
5757
context="{floating.context}"
58-
classes="fill-surface-500"
5958
/>
6059
</div>
6160
`}

src/routes/(inner)/docs/getting-started/+page.svelte

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import CodeBlock from '$docs/components/CodeBlock/CodeBlock.svelte';
3-
import SsrRaw from './ssr.svelte?raw';
3+
import SsrRaw from './ExampleSsr.svelte?raw';
44
</script>
55

66
<div class="space-y-10">
@@ -14,16 +14,19 @@
1414
</header>
1515
<!-- Supports -->
1616
<section class="space-y-8">
17-
<h2 class="h2">Support</h2>
18-
<!-- prettier-ignore -->
17+
<h2 class="h2">Requirements</h2>
1918
<p>
20-
Supports <u>Svelte v5</u> projects created with <a class="anchor" href="https://kit.svelte.dev/" target="_blank">SvelteKit</a>, <a class="anchor" href="https://vitejs.dev/" target="_blank">Vite/Svelte</a>, or <a class="anchor" href="https://astro.build/" target="_blank">Astro</a>.
19+
Supports projects created using <a class="anchor" href="https://svelte.dev/" target="_blank">
20+
Svelte v5
21+
</a>.
2122
</p>
2223
</section>
2324
<!-- Install -->
2425
<section class="space-y-8">
2526
<h2 class="h2">Install</h2>
26-
<p>To install Floating UI, use your package manager of choice.</p>
27+
<p>
28+
Install <strong class="highlight">Floating UI Svelte</strong> using your package manager of choice.
29+
</p>
2730
<CodeBlock
2831
lang="bash"
2932
code={`
@@ -32,17 +35,24 @@ npm install @skeletonlabs/floating-ui-svelte
3235
# yarn install @skeletonlabs/floating-ui-svelte
3336
# bun install @skeletonlabs/floating-ui-svelte
3437
`}
35-
highlight={1}
3638
/>
3739
</section>
3840
<!-- Usage -->
3941
<section class="space-y-8">
4042
<h2 class="h2">Usage</h2>
41-
<h3 class="h3">Making elements "float</h3>
43+
<h3 class="h3">Floating UI Features</h3>
44+
<!-- prettier-ignore -->
45+
<p>
46+
Floating UI Svelte exposes all Floating UI <a href="https://floating-ui.com/docs/middleware" target="_blank" class="anchor">middleware</a>, types, etc. You do not need to install <code class="code">@floating-ui/dom</code> seperately.
47+
</p>
48+
<CodeBlock
49+
lang="ts"
50+
code={`import { flip, type Strategy } from '@skeletonlabs/floating-ui-svelte';`}
51+
/>
52+
<h3 class="h3">Making elements "float"</h3>
4253
<p>
43-
The following styles must be applied to any and all floating elements. We recommend using a
44-
class as shown below. Note that Floating UI does not take an opinionated stance on z-index
45-
stacking.
54+
At minimum, the following styles must be applied to ensure floating elements do not disrupt
55+
the flow of the document. This can be handled using a single reusable CSS class.
4656
</p>
4757
<CodeBlock
4858
lang="css"
@@ -56,16 +66,24 @@ npm install @skeletonlabs/floating-ui-svelte
5666
`}
5767
/>
5868
<CodeBlock lang="html" code={`<div class="floating">Some floating element.</div>`} />
69+
<h3 class="h3">Z-Index Stacking</h3>
70+
<p>
71+
Please be aware that Floating UI does not take an opinionated stance on <a
72+
href="https://floating-ui.com/docs/misc#z-index-stacking"
73+
target="_blank"
74+
class="anchor">z-index stacking</a
75+
>.
76+
</p>
5977
</section>
60-
<!-- Ceveats -->
78+
<!-- Caveats -->
6179
<section class="space-y-8">
6280
<h2 class="h2">Caveats</h2>
63-
<h3 class="h3">SSR</h3>
81+
<h3 class="h3">Server-Side Rendering (SSR)</h3>
6482
<p>
65-
When SSR is enabled and the floating element is visible upon pageload it will first be
66-
positioned in the top left of your screen until the position is calculated. This is usually
67-
not desirable. To prevent this, you can utilize the <kbd class="kbd">isPositioned</kbd> prop
68-
returned from the
83+
When SSR is enabled and the floating element is visible upon page load it will first be
84+
positioned in the top left of your screen. It will remain until the position is calculated. To
85+
prevent this, you may utilize the <kbd class="kbd">isPositioned</kbd>
86+
prop returned from the
6987
<kbd class="kbd"><a class="anchor" href="/api/use-floating">useFloating</a></kbd> hook:
7088
</p>
7189
<CodeBlock lang="svelte" code={SsrRaw} />

src/routes/(inner)/examples/popovers/+page.svelte

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,173 @@
1414
described in the sections below.
1515
</p>
1616
</header>
17+
<!-- Essentials -->
18+
<section class="space-y-8">
19+
<h2 class="h2">Essentials</h2>
20+
<p>An accessible popover component has the following qualities:</p>
21+
<ul class="ul">
22+
<li>
23+
<span class="highlight">Dynamic anchor positioning</span>: The popover is positioned next to
24+
its reference element, remaining anchored to it while avoiding collisions.
25+
</li>
26+
<li>
27+
<span class="highlight">Events</span>: When the reference element is clicked, it toggles the
28+
popover open or closed.
29+
</li>
30+
<li>
31+
<span class="highlight">Dismissal</span>: When the user presses the
32+
<kbd class="kbd">esc</kbd> key or outside the popover while it is open, it closes.
33+
</li>
34+
<li>
35+
<span class="highlight">Role</span>: The elements are given relevant role and ARIA
36+
attributes to be accessible to screen readers.
37+
</li>
38+
<li>
39+
<span class="highlight">Focus management</span>: Focus is managed for non-modal or modal
40+
behavior.
41+
</li>
42+
</ul>
43+
</section>
1744
<!-- Example -->
1845
<section class="space-y-8">
46+
<h2 class="h2">Example</h2>
1947
<Preview>
2048
{#snippet preview()}<Example />{/snippet}
2149
{#snippet code()}<CodeBlock code={ExampleRaw} lang="svelte" />{/snippet}
2250
</Preview>
2351
</section>
52+
<!-- Open State -->
53+
<section class="space-y-8">
54+
<h2 class="h2">Open State</h2>
55+
<CodeBlock code={`let open = $state(false);`} lang="ts" />
56+
<p>
57+
<code class="code">open</code> determines whether or not the popover is currently open on the screen.
58+
It is used for conditional rendering.
59+
</p>
60+
</section>
61+
<h2 class="h2">Basic Popover</h2>
62+
<!-- useFloating Hook -->
63+
<section class="space-y-8">
64+
<h3 class="h3">useFloating Hook</h3>
65+
<p>
66+
The <a class="anchor" href="/api/use-floating">useFloating</a> hook provides positioning and context
67+
for our popover. We need to pass it some information:
68+
</p>
69+
<CodeBlock code={`const floating = useFloating({ /* ...settings... */ });`} lang="ts" />
70+
<ul class="ul">
71+
<li>
72+
<code class="code">open</code>: The open state from our <code class="code">useState()</code>
73+
Hook above.
74+
</li>
75+
<li>
76+
<code class="code">onOpenChange</code>: A callback function that will be called when the
77+
popover is opened or closed. We’ll use this to update our <code class="code">open</code> state.
78+
</li>
79+
<li>
80+
<code class="code">middleware</code>: Import and pass middleware to the array that ensure
81+
the popover remains on the screen, no matter where it ends up being positioned.
82+
</li>
83+
<li>
84+
<code class="code">whileElementsMounted</code>: Ensure the popover remains anchored to the
85+
reference element by updating the position when necessary, only while both the reference and
86+
floating elements are mounted for performance.
87+
</li>
88+
</ul>
89+
</section>
90+
<!-- Interaction Hooks -->
91+
<section class="space-y-8">
92+
<h3 class="h3">Interaction Hooks</h3>
93+
<p>
94+
The <a class="anchor" href="/api/use-interactions">useInteractions</a> hooks returns an object
95+
containing keys of props that enable the popover to be opened, closed, or accessible to screen
96+
readers. Using the
97+
<code class="code">context</code> that was returned from the Hook, call the interaction Hooks.
98+
</p>
99+
<CodeBlock
100+
code={`
101+
const role = useRole(floating.context);
102+
const click = useClick(floating.context);
103+
const dismiss = useDismiss(floating.context);
104+
const interactions = useInteractions([role, click, dismiss]);
105+
`}
106+
lang="ts"
107+
/>
108+
<ul class="ul">
109+
<li>
110+
<code class="code">useClick()</code>: adds the ability to toggle the popover open or closed
111+
when the reference element is clicked.
112+
</li>
113+
<li>
114+
<code class="code">useDismiss()</code>: adds the ability to dismiss the popover when the
115+
user presses the <kbd class="kbd">esc</kbd> key or presses outside of the popover.
116+
</li>
117+
<li>
118+
<code class="code">useRole()</code>: adds the correct ARIA attributes for a
119+
<code class="code">dialog</code> to the popover and reference elements.
120+
</li>
121+
</ul>
122+
<p>
123+
Finally, <code class="code">useInteractions()</code> merges all of their props into prop getters
124+
which can be used for rendering.
125+
</p>
126+
</section>
127+
<!-- Rendering -->
128+
<section class="space-y-8">
129+
<h3 class="h3">Rendering</h3>
130+
<p>Now we have all the variables and Hooks set up, we can render out our elements.</p>
131+
<CodeBlock
132+
lang="html"
133+
code={`
134+
<!-- Reference Element -->
135+
<button
136+
bind:this={floating.elements.reference}
137+
{...interactions.getReferenceProps()}
138+
class="btn-gradient"
139+
>
140+
Click Me
141+
</button>\n
142+
<!-- Floating Element -->
143+
{#if open}
144+
<div
145+
bind:this={floating.elements.floating}
146+
style={floating.floatingStyles}
147+
{...interactions.getFloatingProps()}
148+
class="floating popover-neutral"
149+
transition:fade={{ duration: 200 }}
150+
>
151+
<p>
152+
You can press the <kbd class="kbd">esc</kbd> key or click outside to
153+
<strong>*dismiss*</strong> this floating element.
154+
</p>
155+
<FloatingArrow bind:ref={elemArrow} context={floating.context} fill="#575969" />
156+
</div>
157+
{/if}
158+
`}
159+
/>
160+
<ul class="ul">
161+
<li>
162+
<code class="code">{`{...getReferenceProps()}`}</code> /
163+
<code class="code">{`{...getFloatingProps()}`}</code>
164+
spreads the props from the interaction Hooks onto the relevant elements. They contain props like
165+
<code class="code">onClick</code>, <code class="code">aria-expanded</code>, etc.
166+
</li>
167+
<!-- TODO: flush out when FloatingManager available. -->
168+
<li class="opacity-50 line-through">
169+
COMING SOON: <code class="code">{`<FloatingFocusManager />`}</code> is a component that manages
170+
focus of the popover for non-modal or modal behavior. It should directly wrap the floating element
171+
and only be rendered when the popover is also rendered.
172+
</li>
173+
</ul>
174+
</section>
175+
176+
<!-- TODO: flush out when FloatingManager available. -->
177+
178+
<!-- Modals and Non-Modal Behavior -->
179+
<section class="space-y-8">
180+
<h3 class="h3">Modals and Non-Modal Behavior</h3>
181+
<div class="alert">Coming Soon.</div>
182+
</section>
183+
<!-- Reusable Popover Component -->
184+
<h2 class="h2">Reusable Popover Component</h2>
185+
<div class="alert">Coming Soon.</div>
24186
</div>

src/routes/(inner)/examples/popovers/Example.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<button
4343
bind:this={floating.elements.reference}
4444
{...interactions.getReferenceProps()}
45-
class="btn-cta"
45+
class="btn-gradient"
4646
>
4747
Click Me
4848
</button>
@@ -52,14 +52,14 @@
5252
bind:this={floating.elements.floating}
5353
style={floating.floatingStyles}
5454
{...interactions.getFloatingProps()}
55-
class="floating bg-surface-500 text-white p-8 max-w-sm rounded shadow-xl"
55+
class="floating popover-neutral"
5656
transition:fade={{ duration: 200 }}
5757
>
5858
<p>
5959
You can press the <kbd class="kbd">esc</kbd> key or click outside to
6060
<strong>*dismiss*</strong> this floating element.
6161
</p>
62-
<FloatingArrow bind:ref={elemArrow} context={floating.context} class="fill-surface-500" />
62+
<FloatingArrow bind:ref={elemArrow} context={floating.context} fill="#575969" />
6363
</div>
6464
{/if}
6565
</div>

0 commit comments

Comments
 (0)