1.0.0 • Published 1 year ago

postcss-relative-rem v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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