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

postcss-relative-rem

v1.0.0

Published

A postcss plugin to convert rem units to units relative a CSS variable.

Downloads

177

Readme

postcss-relative-rem

A PostCSS plugin to convert rem units to units relative a CSS variable (instead of the root element's font size).

This will convert something like this:

h1 {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

To:

h1 {
  font-size: calc(2 * var(--rem-relative-base));
  margin-bottom: calc(0.5 * var(--rem-relative-base));
}

Why?

The driver for this was to be able to use rem units inside shadow DOM stylesheets. The shadow DOM mostly isolates a component's styles, but rem units still reference the <html> root's font-size, making rem unit sizes unpredictable if your component is injected into pages that may have variable <html> root font sizes defined that are outside of your control.

By changing the CSS so all rem units are relative to another CSS variable, this allows for better control over how to define your own root font-size. You can still use rem units in your variable definition, allowing for better accessibility than perhaps other alternatives (like converting everything to pixels).

Installation

npm install --save-dev postcss postcss-relative-rem

Usage

Add this plugin to your postcss.config.js:

module.exports = {
  plugins: [require("postcss-relative-rem")],
};

The --rem-relative-base CSS variable must then also be defined in some way:

<style>
  :root {
    --rem-relative-base: 1.6rem;
  }
</style>

Shadow DOM Example

For a more concrete example of how this plugin can be used to dynamically deal with a shadow DOM component being injected into an unknown page (with variable root font size definitions), here's how you could set the --rem-relative-base CSS variable based on the component's inherited font size (instead of the root <html> font size):

// Grab the container and setup the shadow DOM.
const containerEl = document.querySelector(".my-shadow-dom-container");
containerEl.attachShadow({ mode: "open" });

// Note: If the container will set its own font size or reset things, make sure
// this style is in place before the following font size calculations (if this
// is applied later by a stylesheet that hasn't yet loaded, then this may cause
// the calculations to be incorrect).
containerEl.style.setProperty("all", "initial");

// Determine the root `<html>` font size.
const rootFontSize = parseFloat(
  window
    .getComputedStyle(document.documentElement)
    .getPropertyValue("font-size"),
);

// Determine the font size of the shadow DOM container.
const containerFontSize = parseFloat(
  window.getComputedStyle(containerEl).getPropertyValue("font-size"),
);

// Calculate the relative base font size (to be used by the calculations done
// by this plugin) based on comparing the root font size to the contianer's
// font size.
const remRelativeBaseSize = `${containerFontSize / rootFontSize}rem`;

// Set the CSS variable on the container which will then be used by the calcs()
// this plugin provides.
containerEl.style.setProperty("--rem-relative-base", remRelativeBaseSize);

Options

baseCssVariable

Default: --rem-relative-base

Allows you to set a different CSS variable name to use.

module.exports = {
  plugins: [
    require("postcss-relative-rem", {
      baseCssVariable: "--my-app-rem-base`,
    }),
  ],
};

References

  • https://discourse.wicg.io/t/hem-rem-for-web-component/2098
  • https://github.com/cuth/postcss-pxtorem
  • https://css-tricks.com/encapsulating-style-and-structure-with-shadow-dom/#aa-what-is-the-shadow-dom
  • https://stackoverflow.com/questions/66147461/change-shadow-dom-rem-size
  • https://stackoverflow.com/questions/24953647/font-size-css-values-based-on-shadow-dom-root