Skip to content

Commit 2d75062

Browse files
committed
docs(portal): add examples
1 parent a47215e commit 2d75062

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script>
2+
import { Portal, FloatingPortal } from "carbon-components-svelte";
3+
import Preview from "../../components/Preview.svelte";
4+
</script>
5+
6+
The `Portal` component renders its content into a fixed container appended to `document.body`, allowing you to escape parent overflow constraints and z-index stacking contexts. The `FloatingPortal` component extends this functionality with automatic positioning relative to a reference element using the [Floating UI](https://floating-ui.com/) library.
7+
8+
## Default Portal
9+
10+
Render content in a portal container. This is useful for modals, tooltips, and menus that need to escape parent containers.
11+
12+
<FileSource src="/framed/Portal/BasicPortal" />
13+
14+
## Multiple Portals
15+
16+
Multiple portal instances share the same container, which is automatically cleaned up when all instances are removed.
17+
18+
<FileSource src="/framed/Portal/MultiplePortals" />
19+
20+
## Floating portal
21+
22+
The `FloatingPortal` component automatically positions content relative to a reference element using [Floating UI](https://floating-ui.com/). This is useful for dropdowns, popovers, and tooltips.
23+
24+
<FileSource src="/framed/Portal/FloatingPortal" />
25+
26+
## Floating portal placement
27+
28+
Control the placement of the floating portal using the `placement` prop.
29+
30+
<FileSource src="/framed/Portal/FloatingPortalPlacement" />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { Portal } from "carbon-components-svelte";
3+
</script>
4+
5+
<Portal>Hello world</Portal>
6+
<Portal>Another portal</Portal>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { Button, FloatingPortal } from "carbon-components-svelte";
3+
4+
let buttonRef = null;
5+
let showPortal = false;
6+
</script>
7+
8+
<Button bind:this={buttonRef} on:click={() => (showPortal = !showPortal)}>
9+
Toggle Floating Portal
10+
</Button>
11+
12+
{#if showPortal && buttonRef}
13+
<FloatingPortal reference={buttonRef} placement="bottom" offset={8}>
14+
<div style="padding: 1rem; background: var(--cds-layer-01); border: 1px solid var(--cds-border-subtle-01); box-shadow: var(--cds-shadow-03); border-radius: 4px; min-width: 200px;">
15+
<p style="margin: 0 0 0.5rem 0;">Floating portal content</p>
16+
<p style="margin: 0; font-size: 0.875rem; color: var(--cds-text-secondary);">
17+
This content is positioned relative to the button above.
18+
</p>
19+
</div>
20+
</FloatingPortal>
21+
{/if}
22+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<script>
2+
import { Button, FloatingPortal } from "carbon-components-svelte";
3+
4+
let buttonRefs = {};
5+
let openPlacements = {};
6+
7+
const placements = [
8+
"top",
9+
"top-start",
10+
"top-end",
11+
"right",
12+
"right-start",
13+
"right-end",
14+
"bottom",
15+
"bottom-start",
16+
"bottom-end",
17+
"left",
18+
"left-start",
19+
"left-end",
20+
];
21+
22+
function togglePlacement(placement) {
23+
openPlacements[placement] = !openPlacements[placement];
24+
openPlacements = { ...openPlacements };
25+
}
26+
</script>
27+
28+
<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 2rem; padding: 4rem;">
29+
{#each placements as placement}
30+
<div style="display: flex; justify-content: center; align-items: center;">
31+
<Button
32+
bind:this={buttonRefs[placement]}
33+
on:click={() => togglePlacement(placement)}
34+
>
35+
{placement}
36+
</Button>
37+
{#if openPlacements[placement] && buttonRefs[placement]}
38+
<FloatingPortal
39+
reference={buttonRefs[placement]}
40+
placement={placement}
41+
offset={8}
42+
>
43+
<div
44+
style="padding: 0.75rem; background: var(--cds-layer-01); border: 1px solid var(--cds-border-subtle-01); box-shadow: var(--cds-shadow-03); border-radius: 4px; white-space: nowrap;"
45+
>
46+
{placement}
47+
</div>
48+
</FloatingPortal>
49+
{/if}
50+
</div>
51+
{/each}
52+
</div>
53+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import { Button, Portal } from "carbon-components-svelte";
3+
</script>
4+
5+
<Button>Button 1</Button>
6+
<Portal>
7+
<div style="position: absolute; top: 100px; left: 20px; padding: 1rem; background: var(--cds-layer-01); border: 1px solid var(--cds-border-subtle-01);">
8+
Portal content 1
9+
</div>
10+
</Portal>
11+
12+
<Button>Button 2</Button>
13+
<Portal>
14+
<div style="position: absolute; top: 150px; left: 20px; padding: 1rem; background: var(--cds-layer-01); border: 1px solid var(--cds-border-subtle-01);">
15+
Portal content 2
16+
</div>
17+
</Portal>
18+

0 commit comments

Comments
 (0)