Socket
Socket
Sign inDemoInstall

placement.js

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    placement.js

A tiny library for positioning overlays. Useful for tooltips, popovers etc.


Version published
Weekly downloads
2.4K
decreased by-30.35%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

[1.0.0-beta.5] - 2021-05-20

Added

  • New bound option to turn off bounding the overlay position to the scroll container.

Fixed

  • Fixed incorrect placement when page is scrolled horizontally.

Readme

Source

Placement.js

A tiny library for positioning overlays. Useful for tooltips, popovers etc.

Size

Demo

Why? Positioning UI overlays like popovers and tooltips can be challenging. If you position them with CSS, then at some stage you are likely to run into problems with z-indices, parent overflows, or content going off the edge of the screen. JavaScript to the rescue!

Placement.js addresses a single use-case: positioning an overlay to one side of an anchor element, optimizing the placement depending on the size of the overlay:

  • If the overlay is too big, it may flip to the other side if there is more room available.
  • The dimensions of the overlay will be capped so that it will never go off-screen.

Small filesize is a priority for Placement.js so that it can be included in web components. For a more powerful, configurable library, check out Popper.js.

Installation

npm install placement.js --save

Usage

import placement from 'placement.js';

placement(
    anchor: HTMLElement,
    overlay: HTMLElement,
    options?: Options
);

type Options = {
    // The overlay placement relative to the anchor. 
    // Defaults to 'bottom'.
    placement?: 
        | 'top'
        | 'top-start'
        | 'top-end'
        | 'bottom'
        | 'bottom-start'
        | 'bottom-end'
        | 'right'
        | 'right-start'
        | 'right-end'
        | 'left'
        | 'left-start'
        | 'left-end',

    // Whether or not the overlay can flip to the other side if there's more
    // room available. Defaults to true.
    flip?: boolean,

    // Whether or not the overlay size should be capped to the available space.
    // Defaults to true.
    cap?: boolean,

    // Whether or not the overlay position should be bound to the scroll 
    // container. Defaults to true.
    bound?: boolean,
};

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

FAQs

Last updated on 20 May 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc