Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wobsoriano/svelte-portal

A Svelte component that lets you render some children into a different part of the DOM.
https://github.com/wobsoriano/svelte-portal

portal svelte teleport

Last synced: 9 days ago
JSON representation

A Svelte component that lets you render some children into a different part of the DOM.

Awesome Lists containing this project

README

        

# svelte-portal

A Svelte 5 component that lets you render some children into a different part of the DOM.

## Installation

```bash
npm install @jsrob/svelte-portal
```

## Usage

To create a portal, use the `` component and pass in a target and children:

```svelte

import { Portal } from '@jsrob/svelte-portal';

This child is placed in the document body.

```

Another example, a modal that is rendered into the body when a button is clicked:

```svelte

import { Portal } from '@jsrob/svelte-portal';

let open = $state(false);

(open = true)}>Open Modal

{#if open}


{/if}

.modal {
position: fixed;
z-index: 999;
top: 20%;
left: 50%;
width: 300px;
margin-left: -150px;
}

```

You can also pass components as children:

```svelte

import { Portal } from '@jsrob/svelte-portal';
import Component from './Component.svelte';

```

### Props

All props can be changed dynamically.

| Prop | Type | Description | Required |
| ---------- | ----------------------- | --------------------------------------------------------------------------------------------------------- | -------- |
| `target` | `string \| HTMLElement` | Specify target container. Can either be a selector or an actual element. | Yes |
| `disabled` | `boolean` | When `true`, the content will remain in its original location instead of moved into the target container. | No |

### Caveats

When updating the `target` or `disabled` prop value, components in the default snippet are unmounted and re-mounted, which means any changes to their local state are lost.

If you need to persist state, use some sort of state management.

## License

MIT