惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
博客园_首页
T
Tailwind CSS Blog
The Cloudflare Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

Utopia

Fluid responsive typography: easy when you know how Utopian project kickstarter: designing a fluid responsive website in a static tool like Figma Generate all pair permutations in Utopia SCSS Type scale graphs Figma plugin and kickstarter V2 Utopia SCSS library Clamp calculator Utopia Fluid Type + Space Calculator now supports Figma variables Designing a Utopian layout grid: Working with fluid responsive values in a static design tool. Getting started with Utopia Figma Plugins A video introduction to Utopia Painting with a fluid space palette Designing with a fluid space palette Clamp Utopian CSS generator, an iteration Dealing with negativity in fluid type scales Fluid custom properties CSS-only fluid modular type scales Designing with fluid type scales
Readable clamp() with PostCSS Utopia
dev@clearlef · 2024-02-06 · via Utopia

Fast-following from the SCSS plugin, Utopia now has a PostCSS plugin. Thanks to the abstracted utopia-core calculations, creating library-specific generators is a breeze.

So when Stefan asked the question, it seemed rude not to create a PostCSS plugin.

Installation

  1. Install the package through npm
npm install postcss-utopia
  1. Add the plugin to your postcss.config.js file:
module.exports = {
  plugins: {
    'postcss-utopia': {},
  },
}
  1. (Optional tip) You can supply global minWidth and maxWidth viewport sizes in the configuration object and skip passing them to every method in your CSS:
module.exports = {
  plugins: {
    'postcss-utopia': {
      minWidth: 320,
      maxWidth: 1080
    },
  },
}

utopia.clamp()

The most exciting method is utopia.clamp(); a shorthand way to write readable clamp functions:

.my-class {
  margin-block-end: utopia.clamp(16, 24);

  /* Generates */
  margin-block-end: clamp(1rem, 0.7895rem + 1.0526vi, 1.5rem);

  /* If you've not set minWidth & maxWidth in your config, add them here */
  margin-block-end: utopia.clamp(16, 24, 320, 1080);
}

This function generates a fluid clamp that scales from 1rem (16px) to 1.5rem (24px) between the viewports of 320px and 1080px, clamping at either end.

@utopia typeScale()

This generates a complete Utopian type scale using CSS custom properties. Any config that's valid in utopia-core is also valid here, so relativeTo: 'container', usePx: true and prefix: 'custom-name' are all usable.

:root {
  @utopia typeScale({
    minWidth: 320,          /* Defaults to plugin minWidth */
    maxWidth: 1240,         /* Defaults to plugin maxWidth */
    minFontSize: 16,
    maxFontSize: 18,
    minTypeScale: 1.2,
    maxTypeScale: 1.25,
    positiveSteps: 5,
    negativeSteps: 2,
    relativeTo: 'viewport', /* Optional */
    prefix: 'step'          /* Optional */
  });

  /* Generates
  --step--2: clamp(...);
  --step--1: clamp(...); etc.
  */
}

@utopia spaceScale()

Like typeScale above, spaceScale accepts utopia-core config, but generates three sets of custom properties:

  • Space units
  • One-up pairs
  • Custom sizes

These all map to the sizes generated on utopia.fyi:

:root {
  @utopia spaceScale({
    minWidth: 320,             /* Defaults to plugin minWidth */
    maxWidth: 1240,            /* Defaults to plugin maxWidth */
    minSize: 16,
    maxSize: 18,
    positiveSteps: [1.5, 2, 3],
    negativeSteps: [0.75, 0.5],
    customSizes: ['s-l'],
    relativeTo: 'viewport',    /* Optional */
    prefix: 'space',           /* Optional */
    usePx: false,              /* Optional */
  });

  /* Generates
  --space-2xs: clamp(...);
  --space-xs: clamp(...); etc.

  --space-2xs-xs: clamp(...); etc.

  --space-s-l: clamp(...); etc.
  */
}

@utopia clamps()

There's also method to generate multiple clamp custom properties in one go:

:root {
  @utopia clamps({
    minWidth: 320,         /* Defaults to plugin minWidth */
    maxWidth: 1240,        /* Defaults to plugin minWidth */
    pairs: [
      [16, 40]
    ],
    usePx: false,          /* Optional */
    prefix: 'space',       /* Optional */
    relativeTo: 'viewport' /* Optional */
  });

  /* Generates
  --space-16-40: clamp(...);
  */
}

Get your config

Finally, to save manually typing out your config (because who has time for that) you can grab the matching PostCSS configuration object from the type, space and clamp calculators.