Polished

4.3.1 · active · verified Sun Apr 19

Polished is a lightweight, cross-framework toolset for writing styles in JavaScript, providing Sass-style helper functions and mixins. Currently at version 4.3.1, it receives regular maintenance updates with a focus on bug fixes, performance improvements, and new CSS feature support, as seen in recent v4.2.x and v44.3.x releases. Key differentiators include its functional, curried approach to color manipulation, extensive cross-framework compatibility (working with styled-components, emotion, JSS, etc.), and first-class TypeScript and Flow support. It aims to ease the transition for developers moving from CSS pre-processors to JavaScript-based styling solutions by offering a familiar set of utility functions to enhance productivity and maintain consistency across applications. The library is designed for tree-shaking, promoting optimized bundle sizes by encouraging named imports for individual modules.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates core Polished functionalities including color manipulation (lighten, darken, rgba), responsive design helpers (fluidRange), and unit conversion (em). It highlights the recommended named import pattern.

import { lighten, darken, rgba, fluidRange, em } from 'polished';

// Example: Color manipulation
const baseColor = '#663399'; // A deep purple
const lightenedColor = lighten(0.2, baseColor);
const darkenedColor = darken(0.1, baseColor);
const transparentColor = rgba(baseColor, 0.5);

console.log('Original color:', baseColor);
console.log('Lightened color:', lightenedColor);
console.log('Darkened color:', darkenedColor);
console.log('Transparent color (50% opacity):', transparentColor);

// Example: Responsive typography with fluidRange
// Defines a font size that scales between 16px and 20px
// within viewport widths of 400px to 1200px.
const responsiveFontProps = fluidRange(
  { prop: 'font-size', fromSize: '16px', toSize: '20px' },
  '400px',
  '1200px'
);

console.log('\nResponsive font properties (for CSS-in-JS):');
console.log(responsiveFontProps);

// Example: Em unit conversion
const emValue = em(16, 14); // 16px based on a 14px root font size
console.log('\n16px converted to em (base 14px):', emValue); // '1.1428571428571428em'

// Demonstrate functional composition, a key concept for Polished
// Assuming a 'compose' utility from a library like Ramda or a custom one
// import { compose } from 'ramda';
// const makeBrighterAndMoreTransparent = compose(
//   (color: string) => lighten(0.1, color),
//   (color: string) => rgba(color, 0.8)
// );
// const newColor = makeBrighterAndMoreTransparent('#FF0000');
// console.log('\nComposed color (brighter and more transparent red):', newColor);

view raw JSON →