




















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.
npmnpm install postcss-utopia
postcss.config.js file:module.exports = {
plugins: {
'postcss-utopia': {},
},
}
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
},
},
}
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.
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.
*/
}
Like typeScale above, spaceScale accepts utopia-core config, but generates three sets of custom properties:
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.
*/
}
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(...);
*/
}
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.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。