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

set-scrollbar-width

v1.0.5

Published

Calculates the scrollbar width and injects it into a CSS variable, for use in CSS.

Downloads

1,369

Readme

Set Scrollbar Width

A small JS library to calculate the scrollbar width and inject it into the browser as a CSS variable.

Used to accompany https://github.com/LucidNinja/tailwind-container-break-out, to get the width of scrollbars, since each browser and OS handles this differently. This package can be used standalone.

Installation

npm i set-scrollbar-width

QuickStart

Import setScrollbarWidth into your app and run on page mount.

In JS

In NextJs, this could look like this


// _app.js

import { useEffect } from 'react';
import setScrollBarWidth from 'set-scrollbar-width';
import '../styles/globals.css';

function MyApp({ Component, pageProps }) {
  useEffect(() => {
    setScrollbarWidth();
  }, []);

  return <Component {...pageProps} />;
}

export default MyApp;

You can also set the CSS variable name, if not using with Tailwind Container Break Out.

  setScrollbarWidth('--my-css-var');

In CSS

// eg. Making a div the true width of the viewport

div {
  width: calc((100vw - var(--twcb-scrollbar-width)));
}
// eg. Breaking out of a container div that is 1078px wide, using a 'negative margin hack', 
// in order to emulate a full width div inside of a boxed div.

.wider-than-container {
  margin-left: calc((-100vw + var(--twcb-scrollbar-width)) / 2 + 1078 / 2 );
  margin-right: calc((-100vw + var(--twcb-scrollbar-width)) / 2 + 1078 / 2 );
}

Rationale

As per the CSS spec (https://www.w3.org/TR/css-values-3/#viewport-relative-lengths), the vw unit assumes that scrollbars do not exist. This helper package calculates the scrollbar width using Javascript event listeners, allowing you to use --twb-scrollbar-width (or your own variable name), inside of CSS.

Can't this just be handled with CSS?

There are cases when this can work. EG:

body {
  width: calc(100vw - (100vw - 100%));
}

However, this only works on the body or elements that are as wide as the body. Hence, we need to revert to CSS variables. However, a CSS variable is a reusable piece of math that isn't computed until it is used. If used on the body, then the 100% in this calculation uses the body's width (which is generally the viewport width), but if used inside a nested div that isn't the full width of the viewport, then the 100% referes to the width of the inner element.

See https://stackoverflow.com/questions/33606565/is-it-possible-to-calculate-the-viewport-width-vw-without-scrollbar for more discussion on this topic.