• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Popper for Svelte with actions, no wrapper components required!

svelte-popperjs-banner

svelte-popperjs

npm version npm downloads license build coverage size

Popper for Svelte with actions, no wrapper components or component bindings required!

Other Popper libraries for Svelte (including the official @popperjs/svelte library) use a wrapper component that takes the required DOM elements as props. Not only does this require multiple bind:this, you also have to pollute your script tag with multiple DOM references.

We can do better with Svelte actions!

Installation

$ npm i -D svelte-popperjs

Since Svelte automatically bundles all required dependencies, you only need to install this package as a dev dependency with the -D flag.

Due to a configuration issue with PopperJS, this will not work in the Svelte REPL. However, svelte-popperjs works with Rollup, Vite, and SvelteKit!

Usage

createPopperActions takes an optional options object for configuring the popper instance, and returns a pair of actions to be used on the reference and popper elements.

The content action also takes an options object for updating the options of the popper instance.

Example

A Svelte version of the standard tutorial.

<script>
  import { createPopperActions } from 'svelte-popperjs';
  const [popperRef, popperContent] = createPopperActions({
    placement: 'right',
    strategy: 'fixed',
  });
  const extraOpts = {
    modifiers: [
      { name: 'offset', options: { offset: [0, 8] } }
    ],
  };

  let showTooltip = false;
</script>

<button
  use:popperRef
  on:mouseenter={() => showTooltip = true}
  on:mouseleave={() => showTooltip = false}
>
  My button
</button>
{#if showTooltip}
  <div id="tooltip" use:popperContent={extraOpts}>
    My tooltip
    <div id="arrow" data-popper-arrow />
  </div>
{/if}

API

Setting popper options

Popper options can be set statically when creating the popper actions, or dynamically on the content action.

If both are set, then the dynamic options will be merged with the initial options.

<script>
  // set once and no longer updated
  const [popperRef, popperContent] = createPopperActions(initOptions);
</script>

<!-- will be merged with initOptions -->
<div use:popperContent={dynamicOptions}/>

Virtual elements

PopperJS allows the reference node to be a virtual element which is not mounted on the DOM and cannot be used with Svelte actions.

Despite this, svelte-popperjs provides first-class support for virtual elements, and even supports reactive updates to the virtual element with Svelte stores.

Here's an example creating a tooltip that follows the mouse cursor.

<script>
  import { writable } from 'svelte/store';
  import { createPopperActions } from 'svelte-popperjs';
  const [popperRef, popperContent] = createPopperActions({
    strategy: 'fixed',
  });
  
  let x = 0;
  let y = 0;
  const mousemove = (ev: MouseEvent) => {
    x = ev.clientX;
    y = ev.clientY;
  }
  
  $: getBoundingClientRect = () => ({
    width: 0, height: 0,
    top: y, bottom: y,
    left: x, right: x,
  });
  const virtualElement = writable({ getBoundingClientRect });
  $: $virtualElement = { getBoundingClientRect };
  popperRef(virtualElement);
</script>
<svelte:window on:mousemove={mousemove} />

<main>
  <div use:popperContent>Tooltip</div>
</main>

Accessing the Popper instance

If access is needed to the raw Popper instance created by the actions, you can reference the third element returned by createPopperActions. The third element is a function that will return the current Popper instance used by the actions.

Using the raw Popper instance to manually recompute the popper's position.

<script>
  import { createPopperActions } from 'svelte-popperjs';
  const [popperRef, popperContent, getInstance] = createPopperActions();

  async function refreshTooltip() {
    const newState = await getInstance().update();
  }
</script>

More Repositories

1

svelte-headless-table

Unopinionated and extensible data tables for Svelte
TypeScript
473
star
2

svelte-previous

A Svelte store that remembers previous values
TypeScript
82
star
3

svelte-keyed

A writable derived store for objects and arrays
TypeScript
69
star
4

svelte-render

Manage complex Svelte behaviors outside of templates with full type safety.
TypeScript
25
star
5

vim-colorscheme-icons

Colorize vim-devicons with the current colorscheme
Vim Script
20
star
6

tailwindcss-global-dark

A TailwindCSS variant for class-based dark mode with CSS Modules.
JavaScript
17
star
7

zoo-ids

Generate predictable and unique identifiers composed of adjectives and animal names
TypeScript
15
star
8

svelte-subscribe

Subscribe to non top-level stores in your Svelte template
TypeScript
13
star
9

godot-multiplayer

A minimal Godot project with cross-platform authentication and matchmaking
GDScript
6
star
10

meetwhen-ui

A simple group scheduling application!
TypeScript
6
star
11

perfect-clear

A browser-based interactive Tetris Perfect Clear solver
Rust
5
star
12

bryanmylee.com

My personal website
Svelte
3
star
13

sequence-crdt

A C library to manage CRDT text documents efficiently
C
3
star
14

dotfiles

My system configuration
Shell
3
star
15

simple-extension

A simple cross-browser extension with Svelte as the popup controller.
JavaScript
2
star
16

tailwindcss-extrude

Extrude your elements for a retro look!
JavaScript
2
star
17

svelte-headless-table.bryanmylee.com

Svelte
2
star
18

wavefocus

The monorepo for all Wave Focus related projects
TypeScript
2
star
19

tex-er-diagram

A LaTeX package for drawing ER diagrams
TeX
1
star
20

vite-svelte-library-mode

Svelte
1
star
21

svelte-testing-library-no-events

TypeScript
1
star
22

meetwhen-service

A serverless REST API and authentication service built on Cloud Firestore for meetwhen.io
TypeScript
1
star
23

astro-dot-notation-components

Astro
1
star
24

meetwhen-store

The data store for all meetwhen.io events.
TypeScript
1
star
25

vitest-await-crashes

TypeScript
1
star
26

learning-godot-3d

Learning some basic Godot 3D
GDScript
1
star
27

meetwhen-telegram

A Telegram bot to help you schedule your group meetings!
TypeScript
1
star