Tether Documentation

repository·master·Indexed 27 days ago

https://github.com/shipshapecode/tether

A client-side JavaScript library (v3.0.2) for efficiently attaching absolutely positioned UI elements, such as tooltips, dropdowns, and menus, to other elements on a page. It features GPU-accelerated repositioning via CSS transforms, smart container awareness, edge detection with automatic flipping, and boundary constraints to prevent overflow. Tether includes a module system for extending positioning logic and provides a set of instance methods and events to manage the tethering lifecycle.

Tokens
3.6K
Snippets
14
Records
23
Agent score
92%

What's inside Tether

  1. Overview of Tether positioning capabilities

    master

    Tether is a positioning library designed to handle complex overlay attachments (like tooltips, dropdowns, and info boxes) to page elements. It solves common issues such as element clipping caused by overflow: visible parents by using absolute positioning in the body.

    Key features include:

    • GPU-accelerated repositioning: Uses CSS transforms instead of top/left where possible to ensure 60fps scrolling.
    • Smart Container Awareness: Handles elements attached to fixed-position items or elements inside scrollable containers.
    • Edge Detection & Flipping: Automatically flips the overlay to the opposite side (top, bottom, left, or right) if it hits the edge of the screen or a scrollable container.
    • Boundary Constraints: Supports confining elements to specific areas or hiding them when they leave a defined boundary.
    • Performance Optimization: Designed to optimize multiple repositioning requests into a single repaint.
  2. Handle repositioning for hidden or non-DOM elements

    master

    Tethers may fail to position correctly if the target or the element is display: none or not yet in the DOM.

    To resolve this, you can:

    1. Ensure a position call happens after layouts have finished using setTimeout.
    2. Create the Tether with the enabled: false option, and then enable it once the elements are visible and in the DOM.
    myElement.style.display = 'block'
    
    tether = new Tether({ ... })
    
    setTimeout(function(){
      tether.position();
    })
  3. Install Tether via npm

    master
    You can install Tether using npm to add it to your JavaScript project. To install the stable version, use npm install tether. If you want to use the latest beta version, use npm install tether@next.
    npm install tether
  4. Explore Tether beginner examples

    master

    Tether provides several basic use cases to help you get started:

    • simple: A basic implementation to understand core tethering.
    • out-of-bounds: Techniques for hiding an element when it would otherwise be positioned offscreen.
    • pin: How to pin an element so it remains visible and never goes offscreen.
    • enable-disable: How to programmatically enable and disable Tethering using JavaScript.
  5. Explore Tether advanced usage examples

    master

    For complex positioning requirements, Tether offers advanced patterns:

    • content-visible: Uses the 'visible' targetModifier to align an element with the visible portion of a target.
    • dolls: A performance test involving multiple elements tethered in a chain (each tethered to the previous).
    • element-scroll: Uses the 'scroll-handle' targetModifier to align an element with the scrollbar of a specific element.
    • scroll: Uses the 'scroll-handle' targetModifier to align an element with the body's scroll handle.
    • viewport: Aligns an element with the viewport by using the 'visible' targetModifier while tethered to the body.
  6. Reposition all Tethers on the page

    master
    Tether automatically repositions elements during page resize or when a containing element is scrolled. However, if an element moves due to JavaScript changes, you must trigger a repositioning. To reposition every Tether on the page efficiently in a single repaint, call Tether.position().
    Tether.position()
  7. Embed Tether in other libraries

    master

    To prevent CSS class collisions when embedding Tether into another library, you should customize the generated class names.

    Use the classPrefix option to provide a custom prefix. This prefix will replace the default 'tether' string in all generated classes. Additionally, you can use the classes option to disable specific classes that your implementation does not require.

  8. Manage Tether optimizations

    master

    Tether includes optimizations for performance and smoothness. You can control these via the optimizations object:

    • moveElement: (Boolean) If true, Tether may move the element's DOM node inside its scroll parent to avoid repaints. For this to work best, the scroll parent should be position: relative, fixed, or absolute. Set to false to disable.
    • gpu: (Boolean) If true, Tether uses CSS transforms for positioning to allow the element to move on its own layer. If you experience color shifts or artifacts, set this to false.
    new Tether({
      element: yellowBox,
      target: greenBox,
      optimizations: {
        moveElement: false,
        gpu: false
      }
    });
  9. Configure attachment points

    master

    Tether provides six built-in attachment positions. You can specify these for both the element and the target. If targetAttachment is omitted, it defaults to the mirror image of attachment.

    Available positions:

    • left
    • center
    • right
    • top
    • middle
    • bottom

    Example usage:

    new Tether({
      element: yellowBox,
      target: greenBox,
      attachment: 'middle center',
      targetAttachment: 'middle center'
    });
  10. Tether Instance Events

    master

    Tether extends an Evented class, allowing you to listen to the following events:

    • repositioned: Fired whenever the tethered element is moved.
    • update: Fired whenever the Tether instance runs into a constraint (useful for manual UI tweaks like flipping arrows).