npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@f/compute-placement

v1.1.0

Published

Compute the top/left offset of an element to position it near another element

Downloads

26

Readme

compute-placement

Build status Git tag NPM version Code style

Compute the top/left offset of an element to position it near another element

Installation

$ npm install @f/compute-placement

Usage

Use this to position an element near another. At the moment you can pass top/left/bottom/right as your placement options, though this may expand over time. This library does not actually set the position of anything on its own, nor does it accept any DOM nodes as its arguments. It is a pure function of the data you pass into it, so you can use its output to build higher-level things that actually execute the positioning for you.

var computePlacement = require('@f/compute-placement')
var applyStyles = require('@f/apply-styles')
var elementRect = require('@f/element-rect')

function positionElement (node, placement, near, space) {
  var dims = getRect(node)
  var nearRect = getRect(near, true)
  var pos = computePlacement(placement, dims, nearRect, space)

  applyStyles(node, pos)
}

API

computePlacement(placement, nodeDims, nearRect, space)

  • placement - One of top/right/bottom/left. Specifies where you want your node to go relative to the rect specified by nearRect.
  • nodeDims - An object of the form {width, height} containing the width and height of the node you want to position.
  • nearRect - The rect (as returned by getBoundingClientRect or something like element-rect). Note that if the node you are positioning is in the same container as your near element, you will want the top/left offsets to be relative to that container. element-rect will do that for you if you pass true as the second parameter. Otherwise everything will be absolute.
  • options - Optional. Object containing:
    • space - Defaults to 0. The amount of padding in pixels you want to add to the position.
    • relative - Defaults to false. Whether or not the container is relative to its parent.

Returns: An object of the form {top, left} specified as numbers (not <nn>px strings), in pixels.

License

MIT