svelte-portal

repository·master·Indexed 19 days ago

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

A utility library for Svelte (version 2.2.1) that allows developers to render components or DOM elements outside of their current parent DOM hierarchy. It provides a <Portal /> component and a portal Svelte action to teleport content to a specified target, such as document.body or a CSS selector, which is typically used for modals, toasts, or tooltips.

Tokens
2.3K
Snippets
18
Records
20
Agent score
67%

What's inside svelte-portal

  1. Preserve component state during HMR

    master

    Hot Module Replacement (HMR) state preservation is disabled by default in this template due to unpredictable behavior. If you have component state that must be retained during development (e.g., when a file is saved and reloaded), move that state into an external Svelte store. External stores are not replaced by HMR and will persist the data.

    // store.ts
    // An extremely simple external store
    import { writable } from "svelte/store";
    export default writable(0);
  2. Get started with the Svelte app template

    master

    To create a new project based on this template, use degit to clone the repository and then install the dependencies.

    1. Clone the template:
      npx degit sveltejs/template svelte-app
      cd svelte-app
    2. Install dependencies:
      npm install
    3. Start the development server:
      npm run dev

    By default, the server responds only to localhost. To allow connections from other computers on your network, edit the sirv commands in package.json to include the --host 0.0.0.0 option.

    npx degit sveltejs/template svelte-app
    cd svelte-app
    npm install
    npm run dev
  3. Enable TypeScript support

    master

    To ensure full type safety when using <script lang="ts">, import the component directly from the source path instead of the package entry point.

    // Instead of: import Portal from "svelte-portal";
    import Portal from "svelte-portal/src/Portal.svelte";