svelte-floating-ui
Svelte actions for working with floating ui
Last updated 5 months ago by fedorovvvv .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install svelte-floating-ui 
SYNC missed versions from official npm registry.

Svelte Floating UI

🎈Svelte Floating UI

npm version npm downloads license

Floating UI for Svelte with actions. No wrapper components or component bindings required!

npm i svelte-floating-ui @floating-ui/core

Usage

createFloatingActions takes an optional options object for configuring the content placement. The content action also takes an optional options object for updating the options of the content placement.

createFloatingActions also returns an update method as it's third value which can be used to manually update the content position.

Example

<script lang="ts">
  import { offset, flip, shift } from "svelte-floating-ui/dom";
  import { createFloatingActions } from "svelte-floating-ui";

  const [ floatingRef, floatingContent ] = createFloatingActions({
    strategy: "absolute",
    placement: "top",
    middleware: [
      offset(6),
      flip(),
      shift(),
    ]
  });

  let showTooltip: boolean = false;
</script>

<button
  on:mouseenter={() => showTooltip = true}
  on:mouseleave={() => showTooltip = false}
  use:floatingRef
>Hover me</button>

{#if showTooltip}
  <div style="position:absolute" use:floatingContent>
    Tooltip
  </div>
{/if}

API

Setting Floating UI options

Floating UI options can be set statically when creating the 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 [ floatingRef, floatingContent ] = createFloatingActions(initOptions);
</script>

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

Updating the Floating UI position

The content element's position can be manually updated by using the third value returned by createFloatingActions. This method takes an optional options object which will be merged with the initial options.

<script>
  // Get update method
  const [ floatingRef, floatingContent, update] = createFloatingActions(initOptions);

  update(updateOptions)
</script>

Floating UI autoUpdate

You can use autoUpdate options directly in initOptions for createFloatingActions or floatingContent, but not in update

<script>
  import { offset, flip, shift } from "svelte-floating-ui/dom";
  import { createFloatingActions } from "svelte-floating-ui";

  const [ floatingRef, floatingContent ] = createFloatingActions({
    strategy: "absolute",
    placement: "top",
    middleware: [
      offset(6),
      flip(),
      shift(),
    ],
    autoUpdate: { // or false to disable everything
      ancestorResize: false,
      elementResize: false
    }
  });
</script>

What values can autoUpdate have?

Partial<Options>

/**
* false: Don't initialize autoUpdate;
* true: Standard autoUpdate values from the documentation;
* object: All as in the autoUpdate documentation. Your parameters are added to the default ones;
* @default true
*/
autoUpdate?: boolean | Partial<Options>

Virtual Elements

Svelte Floating UI allows you to use the floatingRef (reference node) like VirtualElement

Svelte stores allow you to make these elements reactive and provide full support for them in the Svelte Floating UI

This is an example of creating a tooltip that runs behind the mouse cursor:

<script lang='ts'>
  import type { ClientRectObject, VirtualElement } from 'svelte-floating-ui/core'
  import { createFloatingActions } from 'svelte-floating-ui'
  import { writable } from 'svelte/store'
  
  const [floatingRef, floatingContent] = createFloatingActions({
    strategy: 'fixed', //or absolute
  })

  let x = 0
  let y = 0

  const mousemove = (ev: MouseEvent) => {
    x = ev.clientX
    y = ev.clientY
  }

  $: getBoundingClientRect = ():ClientRectObject => {
    return {
      x,
      y,
      top: y,
      left: x,
      bottom: y,
      right: x,
      width: 0,
      height: 0
    }
  }
  
  const virtualElement = writable<VirtualElement>({ getBoundingClientRect })

  $: virtualElement.set({ getBoundingClientRect })

  floatingRef(virtualElement)
</script>

<svelte:window on:mousemove={mousemove}/>

<main>
  <h2 use:floatingContent>Magic</h2>
</main>

Applying custom styles on compute

To apply styles manually, you can pass the onComputed option to createFloatingActions. This is a function that recieves a ComputePositionReturn. This function is called every time the tooltip's position is computed.

See Arrow Middleware for an example on it's usage.

Arrow Middleware

For convenience, a custom Arrow middleware is provided. Rather than accepting an HTMLElement, this takes a Writable<HTMLElement>. Otherwise, this middleware works exactly as the regular Floating UI one, including needing to manually set the arrow styles.

To set the styles, you can pass the onComputed option. The below implementation is copied from the Floating UI Tutorial.

<script>
  import { writable } from "svelte/store";
  import { arrow } from "svelte-floating-ui";

  const arrowRef = writable(null);
  const [ floatingRef, floatingContent, update] = createFloatingActions({
    strategy: "absolute",
    placement: "bottom",
    middleware: [
      arrow({ element: arrowRef })
    ],
    onComputed({ placement, middlewareData }) {
      const { x, y } = middlewareData.arrow;
      const staticSide = {
        top: 'bottom',
        right: 'left',
        bottom: 'top',
        left: 'right',
      }[placement.split('-')[0]];

      Object.assign($arrowRef.style, {
        left: x != null ? `${x}px` : "",
        top: y != null ? `${y}px` : "",
        [staticSide]: "-4px"
      });
    }
  });
</script>

<button
  on:mouseenter={() => showTooltip = true}
  on:mouseleave={() => showTooltip = false}
  use:floatingRef
>Hover me</button>

{#if showTooltip}
  <div class="tooltip" use:floatingContent>
    Tooltip this is some longer text than the button
    <div class="arrow" bind:this={$arrowRef} />
  </div>
{/if}

Thanks to TehNut/svelte-floating-ui for the foundation for this package

Current Tags

  • 1.5.9                                ...           latest (5 months ago)

32 Versions

  • 1.5.9                                ...           5 months ago
  • 1.5.8                                ...           a year ago
  • 1.5.6                                ...           a year ago
  • 1.5.5                                ...           a year ago
  • 1.5.4                                ...           a year ago
  • 1.5.3                                ...           a year ago
  • 1.5.2                                ...           2 years ago
  • 1.5.1                                ...           2 years ago
  • 1.4.6                                ...           2 years ago
  • 1.4.5                                ...           2 years ago
  • 1.4.2                                ...           2 years ago
  • 1.4.1                                ...           2 years ago
  • 1.2.8                                ...           2 years ago
  • 1.2.7                                ...           2 years ago
  • 1.1.6                                ...           2 years ago
  • 1.1.5                                ...           2 years ago
  • 1.1.4                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.2.7                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.1.7                                ...           2 years ago
  • 0.1.6                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (16)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE