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

@micropackage/vw

v1.1.0

Published

Viewport units handling sass mixins

Downloads

295

Readme

Viewport Width

BracketSpace Micropackage npm License

🧬 About Viewport Width

Note: Despite the name this package works with both viewport width and height units

This package is meant to solve the common problem with viewport units in CSS. These are simply speaking percent units which always remain relative to viewport.

.wrap {
	width: 800px;
	position: relative;
}

.wrap img {
	width: 100%;
}

.wrap .alignfull {
	width: 100vw;
	position: relative;
	left: 50%;
	transform: translateX(-50%);
}

In the example above img will have 100% of the parent width, which is 800px while any element with class alignfull will have the 100% of a viewport. This is a simple solution for making full width section inside a narrower container: the element is positioned relatively which allows us to move it to the center of parent div by setting left: 50% and then move it back to the edge of the screen with transform property (where percentage values are relative to the element itself, not the parent).

The Problem

There is one problem: viewport dimmensions include potential scrollbars. So on a typical website with vertical scrollbar any .alignfull element from the example will be wider than the actual body width. This could be easily corrected using calc function. Scrollbar width is in most cases 17px wide.

.wrap .alignfull {
	width: calc(100vw - 17px);
	...
}

But...

In mobile browsers scrollbar does not count into viewport (it usually only appears as a narrow scrolling indicator while user drags the page around). We could write some media queries for it and use calc only in desktop browser. But it won't be easy, it won't be enought to check the viewport size, we should use some JS to determine whether current browser has permanently visible scrollbar or not.

Also we can't be sure if the scrollbar is 17px wide since this value is not standarized. Most desktop browsers use this size, but some of them have templating mechanisms which allow the templates to modifu UI elements. Firefox has an option to use narrow scrollbars...

The Solution

That's why this package was created. it contains very simple JS code and a bunch of Sass mixins to work with viewport units in a more convenient way. Using this package you just use the mixin:

.element {
	width: vw(100);
}

instead of

.element {
	width: 100vw;
}

See Usage section for more information.

💾 Installation

npm install @micropackage/vw

or

yarn add @micropackage/vw

🕹 Usag

This package contains few Sass mixins and is intended to be used with Sass. Hovewer the script might be useful also in plain CSS or with any other CSS extension language.

The Script

Before you start you need to add the script to your site. You can just include the script directly in your HTML:

<script src="/node_modules/@micropackage/vw/dist/vw.js"></script>

or import the package:

import '@micropackage/vw';

What the script does is it recognizes if the browser contains the scrollbar and stores the scrollbar width in a css variable. The value is accessible in plain css using var function:

.element {
	width: calc(100vw - var(--scrollbar-width));
}

The value is updated on resize event, and is set to 0 if there is no scrollbar.

If you would like to use just the script, this is it.

The Mixins

While you have the script imported you will also need to import sass mixins:

@import "@micropackage/vw/src/scss/mixins";

Depending on the build tool you might need to use some special config to import SCSS files from node_modules.

vw()

This mixin is a replacement for the default vw unit.

Example:

.element {
	width: vw(47); // This is replacement for "width: 47vw";
}

Generated CSS:

.element {
	width: calc(0.47 * (100vw - var(--scrollbar-width))); // This is replacement for "width: 47vw";
}

Fine, but let's suppose we need to use the viewport-relative value in a more complex calc call. What then?

vw-raw()

This mixin will generate the calculation formula without the calc() wrapped arround, so it can be used in custom calc() call.

Example:

.element {
	width: vw(50);
	margin-left: calc(50% - #{ vw-raw(50) });
}

Generated CSS:

.element {
	width: 0.5 * (100vw - var(--scrollbar-width));
	margin-left: calc(50% - 0.5 * (100vw - var(--scrollbar-width)));
}

If the .element would be placed inside some centrally positioned container it would stretch to the left side of the screen while it's right edge would be aligned to the center of the screen. It's actually useful, trust me :)

vh() and vh-raw()

In most cases horizontal scrollbar is not desired on a website. It might be useful in a web app UI, but usually will be added to a specific elements, not entire document. But someone might need it so...

This two mixins are the same as the first two, but they use vh unit instead of vw.

Example:

.element {
	position: absolute;
	width: 100%;
	top: vh(50);
	height: calc(#{ vh-raw(50) } - 40px);
}

📦 About the Micropackage project

Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.

The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.

Micropackages are maintained by BracketSpace.

📖 Changelog

See the changelog file.

📃 License

GNU General Public License (GPL) v3.0. See the LICENSE file for more information.