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

contain-by-screen

v2.1.0

Published

Position a dropdown element near a button in a way that fits on the screen.

Downloads

10,494

Readme

contain-by-screen

GitHub license npm version

This function is for positioning an element next to another in a way that fits on the screen. This can be used to position a dropdown menu next to a button. You can pass an argument to make containByScreen prefer to put the dropdown menu below the button, and if there isn't enough room below the button, then the menu will be positioned next to or above the button automatically instead.

This module can be used in Browsers via a bundler such as Webpack or Browserify.

API

containByScreen(target, anchor, options): Choice

import { containByScreen } from "contain-by-screen";

Moves the target element to be positioned next to the anchor element based on the given options. Returns a "Choice" object that may be used as the options parameter to other calls to containByScreen to position another element consistently with a previous call.

target is the element to position, such as a dropdown menu. The element must have its CSS position property set to "fixed".

anchor is the element to position the target relative to. This would usually be the button that triggered a dropdown menu to appear.

options is an object that may have the following optional properties:

  • position sets the prioritized position for the target relative to its anchor. It may be set to null, "top", "bottom", "left", "right", "cover", or an array of some of those string values. The element will attempt to use this position (or each value in the array in order) unless it is not possible to do so while fitting the element on-screen.

  • forcePosition is a boolean which controls whether the configured position value will be used even if it results in the element going off of the screen.

  • hAlign sets the prioritized horizontal alignment mode for the element relative to its anchor. The horizontal alignment mode is used if the element is positioned in the top, bottom, or cover positions relative to the anchor, and causes the element to be moved horizontally in order to make a specific edge align. It may be set to null, "center", "left", "right", "unaligned", or an array of some of those string values. The element will attempt to use this alignment (or each value in the array in order) unless it is not possible to do so while fitting the element on-screen.

  • forceHAlign is a boolean which controls whether the configured hAlign value will be used even if it results in the element going off of the screen.

  • vAlign sets the prioritized vertical alignment mode for the element relative to its anchor. The vertical alignment mode is used if the element is positioned in the left, right, or cover positions relative to the anchor, and causes the element to be moved vertically in order to make a specific edge align. It may be set to null, "center", "top", "bottom", "unaligned", or an array of some of those string values. The element will attempt to use this alignment (or each value in the array in order) unless it is not possible to do so while fitting the element on-screen.

  • forceVAlign is a boolean which controls whether the configured vAlign value will be used even if it results in the element going off of the screen.

  • buffer specifies a number of pixels to be used as a buffer zone around the target. For screen-fitting purposes, the target will be treated as if it was this much larger in all directions, requiring it to be placed with the given amount of space away from the anchor element (when position is not "cover") and the edges of the screen. The buffer option is useful if the element has children which are positioned such that they escape the boundaries of the element. Buffers do not affect alignment with the anchor element.

  • topBuffer, bottomBuffer, leftBuffer, and rightBuffer specify an additional buffer space for a specific edge.

getContainByScreenResults(target, anchor, options): ChoiceAndCoordinates

import { getContainByScreenResults } from "contain-by-screen";

Works like containByScreen, but instead of positioning the element and returning a Choice object, this function does not mutate the element at all, and instead returns a {choice, coordinates} object where the coordinates property is an object with left and right properties specifying where to position the target element.

Related

The project react-float-anchor is a React wrapper around this function, and react-menu-list is a library that uses it for building interactive menus.

Types

TypeScript type definitions for this module are included! The type definitions won't require any configuration to use.