Floating Vue

repository·main·Indexed 26 days ago

https://github.com/akryum/floating-vue

A lightweight tool for implementing tooltips, dropdowns, and menus in Vue.js applications, powered by the Floating UI engine. Formerly known as v-tooltip, it supports Vue 2 and Vue 3. The library provides the v-tooltip and v-close-popper directives, as well as VTooltip, VDropdown, and VMenu components for advanced content and interaction management.

Tokens
16.6K
Snippets
53
Records
96
Agent score
85%

What's inside floating-vue

  1. Handle sub menus and nesting

    main
    Nesting poppers inside other poppers automatically prevents parent poppers from hiding when a child is shown. Floating Vue includes logic to handle the 'diagonal submenu problem' by detecting if the user's mouse is aiming at the popper content from the reference element.
  2. Use the v-tooltip directive

    main

    The v-tooltip directive allows you to add tooltips to elements. It supports all component props.

    Basic Usage

    Pass a string directly for simple text:

    <button v-tooltip="'Hello world'">Hover me</button>

    Advanced Options

    Pass an object to configure behavior:

    • content: The text or a function/Promise returning content. Note: Pass the function reference, not the result of the call (e.g., { content: fetchTooltip }, not { content: fetchTooltip() }).
    • html: Boolean to allow HTML content.
    • loadingContent: Content to show while the main content is loading.
    <template>
      <!-- Using an object with HTML and loading state -->
      <button v-tooltip="{ content: fetchTooltip, loadingContent: 'Loading...', html: true }">
        Hover me
      </button>
    </template>
  3. Create a reusable themed component

    main

    To organize your code, you can create a reusable component that uses a specific theme by extending the PopperWrapper component and setting the vPopperTheme option.

    <script>
    import { PopperWrapper } from 'floating-vue'
    
    export default {
      ...PopperWrapper,
      name: 'VInfoDropdown',
      vPopperTheme: 'info-dropdown',
    }
    </script>
    
    <style>
    /* Scoped or global styles for the theme */
    .v-popper--theme-info-dropdown .v-popper__inner {
      background: #004499;
    }
    
    .v-popper--theme-info-dropdown .v-popper__arrow-inner {
      border-color: #004499;
    }
    </style>
    
    <!-- Usage -->
    <VInfoDropdown>
      <button>My customized dropdown</button>
    
      <template #popper>
        Hello world
      </template>
    </VInfoDropdown>
  4. Migrate from v-tooltip 3 & 4 to floating-vue

    main

    If you are upgrading from v-tooltip to floating-vue, note the following package mapping and compatibility:

    v-tooltipfloating-vueNPM TagVue compatibility
    4.x-beta.x2.xlatest3.x
    3.x-beta.x1.xvue22.x

    Key architectural changes include the transition from popperjs 2 to floating-ui for positioning.

  5. Use VMenu for hover menus

    main

    The VMenu component uses the menu theme, which extends the dropdown theme. It is specifically configured for mouse-over interactions by overriding triggers and delay.

    <VMenu>
      <button>Documentation</button>
    
      <template #popper>
        <button>Guide</button>
        <button>API Reference</button>
      </template>
    </VMenu>
  6. Configure global options for Floating Vue

    main

    You can set default values for all available props globally so they don't need to be specified on every popper directive or component.

    There are two ways to apply configuration:

    1. During Vue installation: Pass an options object as the second argument to Vue.use().
    2. Directly on the FloatingVue object: Modify the FloatingVue.options property.
    // Option 1: During installation
    import FloatingVue from 'floating-vue'
    Vue.use(FloatingVue, options)
    
    // Option 2: Directly on the FloatingVue object
    import FloatingVue from 'floating-vue'
    FloatingVue.options.distance = 12
  7. Style Target Elements using data-popper-shown

    main

    When a popper is visible, the target element (the element triggering the popper) automatically receives the data-popper-shown attribute. You can use this attribute in CSS to style the trigger element based on the popper's visibility.

    Example: Change button background when tooltip is shown

    button[data-popper-shown] {
      background: #f00;
    }
  8. Install floating-vue for Vue 2

    main

    For Vue 2 projects, use the vue2 NPM tag.

    Install the package:

    npm i floating-vue@vue2
    # or
    yarn add floating-vue@vue2
    # or
    pnpm add floating-vue@vue2

    Register the plugin or components using the Vue 2 API:

    import Vue from 'vue'
    import FloatingVue from 'floating-vue'
    import 'floating-vue/dist/style.css'
    
    // As a plugin
    Vue.use(FloatingVue)
    
    // Or manually
    import {
      vTooltip,
      vClosePopper,
      Dropdown,
      Tooltip,
      Menu
    } from 'floating-vue'
    
    Vue.directive('tooltip', vTooltip)
    Vue.directive('close-popper', vClosePopper)
    Vue.component('VDropdown', Dropdown)
    Vue.component('VTooltip', Tooltip)
    Vue.component('VMenu', Menu)