Top Related Projects
A library for creating dropdowns and other floating elements. #hubspot-open-source
A JavaScript library to position floating elements and create interactions for them.
A JavaScript library to position floating elements and create interactions for them.
:ok_hand: Drag and drop so simple it hurts
The JavaScript Drag & Drop library your grandparents warned you about.
Quick Overview
Tether is a JavaScript library that efficiently positions elements on a page. It allows you to create elements that stay fixed in relation to another element, even when scrolling or resizing the browser window. This library is particularly useful for creating tooltips, dropdowns, and other floating UI elements.
Pros
- Lightweight and performant, with minimal impact on page load times
- Flexible positioning options, allowing for precise control over element placement
- Works well across different browsers and devices
- Actively maintained with regular updates and bug fixes
Cons
- Learning curve for more complex positioning scenarios
- May require additional CSS styling for optimal visual results
- Limited built-in animation capabilities
- Potential conflicts with other positioning libraries if not properly integrated
Code Examples
Creating a simple tooltip:
const tooltip = document.querySelector('.tooltip');
const target = document.querySelector('.target');
const tether = new Tether({
element: tooltip,
target: target,
attachment: 'top center',
targetAttachment: 'bottom center'
});
Positioning an element with offset:
const floatingElement = document.querySelector('.floating');
const anchorElement = document.querySelector('.anchor');
const tether = new Tether({
element: floatingElement,
target: anchorElement,
attachment: 'top left',
targetAttachment: 'bottom left',
offset: '0 10px'
});
Updating Tether options dynamically:
const tether = new Tether({
// initial options
});
// Later in your code
tether.setOptions({
attachment: 'bottom right',
targetOffset: '20px 0'
});
Getting Started
-
Install Tether via npm:
npm install tether
-
Import Tether in your JavaScript file:
import Tether from 'tether';
-
Create a new Tether instance:
const tether = new Tether({ element: document.querySelector('.element-to-position'), target: document.querySelector('.target-element'), attachment: 'top left', targetAttachment: 'bottom left' });
-
Optionally, include the Tether CSS file for default styles:
<link rel="stylesheet" href="path/to/tether.css">
Competitor Comparisons
A library for creating dropdowns and other floating elements. #hubspot-open-source
Pros of Drop
- More actively maintained with recent updates and releases
- Larger community support and adoption
- More comprehensive documentation and examples
Cons of Drop
- Larger file size and potentially more complex implementation
- May include features not needed for simpler positioning tasks
- Steeper learning curve for beginners
Code Comparison
Drop:
const drop = new Drop({
target: document.querySelector('#target'),
content: document.querySelector('#content'),
position: 'bottom center'
});
Tether:
new Tether({
element: document.querySelector('#content'),
target: document.querySelector('#target'),
attachment: 'top center',
targetAttachment: 'bottom center'
});
Both libraries aim to position elements relative to others, but Drop offers a more high-level API with additional features like automatic repositioning and event handling. Tether provides a more lightweight solution focused solely on positioning.
Drop is better suited for complex positioning scenarios and applications requiring extensive customization. Tether might be preferable for simpler use cases or projects where minimizing bundle size is crucial.
The choice between Drop and Tether depends on specific project requirements, desired features, and performance considerations.
A JavaScript library to position floating elements and create interactions for them.
Pros of Floating UI
- More actively maintained with frequent updates and bug fixes
- Offers a wider range of positioning options and customization
- Better performance, especially for complex layouts and large numbers of elements
Cons of Floating UI
- Steeper learning curve due to more advanced API and features
- Larger bundle size, which may impact load times for smaller projects
- Less straightforward integration with some existing frameworks and libraries
Code Comparison
Tether:
new Tether({
element: '#element',
target: '#target',
attachment: 'top left',
targetAttachment: 'bottom left'
});
Floating UI:
import {computePosition, flip, shift, offset} from '@floating-ui/dom';
computePosition(referenceEl, floatingEl, {
placement: 'top',
middleware: [offset(6), flip(), shift({padding: 5})]
}).then(({x, y}) => {
Object.assign(floatingEl.style, {
left: `${x}px`,
top: `${y}px`,
});
});
Both libraries aim to position elements relative to others, but Floating UI offers more advanced positioning logic and middleware for complex scenarios. Tether provides a simpler API for basic positioning needs, while Floating UI allows for more fine-grained control and adaptability to different situations.
A JavaScript library to position floating elements and create interactions for them.
Pros of Floating UI
- More actively maintained with frequent updates and bug fixes
- Offers a wider range of positioning options and customization
- Better performance, especially for complex layouts and large numbers of elements
Cons of Floating UI
- Steeper learning curve due to more advanced API and features
- Larger bundle size, which may impact load times for smaller projects
- Less straightforward integration with some existing frameworks and libraries
Code Comparison
Tether:
new Tether({
element: '#element',
target: '#target',
attachment: 'top left',
targetAttachment: 'bottom left'
});
Floating UI:
import {computePosition, flip, shift, offset} from '@floating-ui/dom';
computePosition(referenceEl, floatingEl, {
placement: 'top',
middleware: [offset(6), flip(), shift({padding: 5})]
}).then(({x, y}) => {
Object.assign(floatingEl.style, {
left: `${x}px`,
top: `${y}px`,
});
});
Both libraries aim to position elements relative to others, but Floating UI offers more advanced positioning logic and middleware for complex scenarios. Tether provides a simpler API for basic positioning needs, while Floating UI allows for more fine-grained control and adaptability to different situations.
:ok_hand: Drag and drop so simple it hurts
Pros of Dragula
- Specialized for drag-and-drop functionality, offering a more focused and optimized solution
- Lightweight and easy to integrate into existing projects
- Supports touch events, making it mobile-friendly
Cons of Dragula
- Limited to drag-and-drop functionality, less versatile than Tether
- May require additional libraries or custom code for more complex positioning scenarios
- Less actively maintained, with fewer recent updates compared to Tether
Code Comparison
Tether (positioning an element):
new Tether({
element: '#element-to-position',
target: '#target-element',
attachment: 'top left',
targetAttachment: 'bottom left'
});
Dragula (setting up drag-and-drop):
dragula([
document.querySelector('#left-container'),
document.querySelector('#right-container')
]);
Summary
Tether is a more versatile positioning library, while Dragula focuses specifically on drag-and-drop functionality. Tether offers greater flexibility for various positioning scenarios, whereas Dragula provides a simpler, more specialized solution for drag-and-drop interactions. The choice between the two depends on the specific requirements of your project and whether you need broader positioning capabilities or a dedicated drag-and-drop solution.
The JavaScript Drag & Drop library your grandparents warned you about.
Pros of Draggable
- More comprehensive drag-and-drop functionality, including sortable lists and multi-item selection
- Actively maintained with regular updates and a larger community
- Extensive documentation and examples for various use cases
Cons of Draggable
- Larger file size and potentially higher performance overhead
- Steeper learning curve due to more complex API and configuration options
- May be overkill for simple positioning tasks
Code Comparison
Tether (basic positioning):
new Tether({
element: '#element',
target: '#target',
attachment: 'top left',
targetAttachment: 'bottom left'
});
Draggable (basic dragging):
new Draggable.Draggable(document.querySelectorAll('ul'), {
draggable: 'li',
mirror: {
appendTo: 'body',
constrainDimensions: true
}
});
Summary
Tether focuses on positioning elements relative to others, while Draggable provides comprehensive drag-and-drop functionality. Tether is lighter and simpler for basic positioning tasks, whereas Draggable offers more features but with increased complexity. Choose based on your specific needs: Tether for simple positioning, Draggable for advanced drag-and-drop interactions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Tether
ð Project status ð
We at Ship Shape have recently taken over Tether's maintenance and hope to modernize and revitalize it. Stay tuned for updates!
Install
npm
npm install tether
For the latest beta:
npm install tether@next
download
Or download from the releases.
Introduction
Tether is a small, focused JavaScript library for defining and managing the position of user interface (UI) elements in relation to one another on a web page. It is a tool for web developers building features that require certain UI elements to be precisely positioned based on the location of another UI element.
There are often situations in UI development where elements need to be attached to other elements, but placing them right next to each other in the DOM tree can be problematic based on the context. For example, what happens if the element weâre attaching other elements to is fixed to the center of the screen? Or what if the element is inside a scrollable container? How can we prevent the attached element from being clipped as it disappears from view while a user is scrolling? Tether can solve all of these problems and more.
Some common UI elements that have been built with Tether are tooltips, select menus, and dropdown menus. Tether is flexible and can be used to solve all kinds of interesting problems; it ensures UI elements stay where they need to be, based on the various user interactions (click, scroll, etc) and layout contexts (fixed positioning, inside scrollable containers, etc).
Please have a look at the documentation for a more detailed explanation of why you might need Tether for your next project.
What to Use Tether for and When to Use It
Tether is a small, focused JavaScript library. For those who might be new to JavaScript, a library is simply a JavaScript file (or files) that contain useful JavaScript code to help achieve tasks easier and faster. Since Tether is a JavaScript user interface (UI) library, it contains code to help you to manage the way your website or web app appears.
Tetherâs goal to is to help you position your elements side-by-side when needed.
Letâs say youâve started working on your dream project—a fancy web app thatâs sure to become the next big thing! An important feature of your new app is to allow users to comment on shared photos. However, due to limited vertical space and the overall layout of your new app, youâd like to display the comments next to the image, similar to how Instagram does it.
Your HTML code might look something like this:
<div class="container">
<img src="awesome-picture.jpg" alt="Awesome Picture" class="picture">
<div class="comments">
...
</div>
</div>
Now, you could achieve this with some CSS using its position
property, but going this route can be problematic since many of position
âs values take elements out of the natural DOM flow. For example, if you have an element at the bottom of your HTML document, using position: absolute
or position: fixed
might could move it all the way to the top of your website in the browser.
Not only that, but you also have to make manual adjustments to ensure other elements arenât negatively affected by the positioned elements. Not to mention, you probably want your comment box to be responsive, and look good across different device sizes. Coding a solution for this manually is a challenge all on its own.
Enter Tether!
After installing Tether and including it in your project, you can begin using it!
-
In your JavaScript file, create a new instance (or constructor function) of the
Tether
object:new Tether({});
-
Within the curly braces (
{}
) you can configure the libraryâs options. Tetherâs extensive list of options can be found in the Tether documentation.new Tether({ element: '.comments', target: '.picture', attachment: 'top right', targetAttachment: 'top left' });
Now you have a perfectly placed comment section to go with your awesome picture! Itâll even stay attached to the element when a user resizes their browser window.
There are tons of other useful features of Tether as well, instead of âcomment boxesâ you could also build:
- Tooltips for useful hints and tricks,
- Dropdown menus,
- Autocomplete popups for forms,
- and more!
Usage
You only need to include tether.min.js
in your page:
<script src="path/to/dist/js/tether.min.js"></script>
Or use a CDN:
<script src="https://cdn.jsdelivr.net/npm/tether@2.0.0-beta.5/dist/js/tether.min.js"></script>
The css files are not required to get tether running.
For more details jump straight in to the detailed Usage page.
Contributing
We encourage contributions of all kinds. If you would like to contribute in some way, please review our guidelines for contributing.
License
Copyright © 2019-2022 Ship Shape Consulting LLC - MIT License Copyright © 2014-2018 HubSpot - MIT License
Top Related Projects
A library for creating dropdowns and other floating elements. #hubspot-open-source
A JavaScript library to position floating elements and create interactions for them.
A JavaScript library to position floating elements and create interactions for them.
:ok_hand: Drag and drop so simple it hurts
The JavaScript Drag & Drop library your grandparents warned you about.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot