Polished
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
-
TypeError: (0 , polished__WEBPACK_IMPORTED_MODULE_2__.lighten) is not a function
cause Attempting to use a Polished function via a default or star import, which prevents bundlers like Webpack from tree-shaking and correctly exposing the named export at runtime.fixChange `import * as polished from 'polished'` or `import polished from 'polished'` to `import { lighten } from 'polished'` for the specific function being used. -
TS2307: Cannot find module 'polished' or its corresponding type declarations.
cause TypeScript's module resolution failing to locate the package's type definitions.fixEnsure `"moduleResolution": "node"` is set within the `compilerOptions` in your `tsconfig.json`. -
ReferenceError: require is not defined in ES module scope
cause Using CommonJS `require('polished')` syntax in an environment configured for ECMAScript Modules (ESM), typically in modern Node.js projects with `"type": "module"` in `package.json` or in browser environments after bundling.fixSwitch to ESM named imports: `import { functionName } from 'polished';`.
Warnings
- gotcha To ensure optimal bundle size, always use named imports for individual Polished functions (e.g., `import { lighten } from 'polished'`). Avoid `import * as polished from 'polished'` or `import polished from 'polished'` as these prevent tree-shaking.
- breaking The `scarf` analytics package was removed due to yarn incompatibilities and GDPR compliance concerns. This change addresses potential supply chain and privacy issues.
- gotcha When using Polished with TypeScript, ensure your `tsconfig.json` includes `"moduleResolution": "node"` to correctly resolve module imports and type definitions.
- gotcha If you encounter Flow type errors originating from the `polished` package, it may be due to Flow version incompatibilities. You can ignore Polished's source in your Flow configuration.
- gotcha Using object spread properties (e.g., `{ ...other }`) within your styles that integrate with Polished requires a Babel plugin such as `transform-object-rest-spread` or a preset like `stage-3`.
- breaking Versions prior to v4.3.1 might experience incompatibility issues with later versions of Rollup due to the `annotate-pure-calls` plugin. This was resolved in v4.3.1.
Install
-
npm install polished -
yarn add polished -
pnpm add polished
Imports
- lighten
import polished from 'polished'; polished.lighten(...)
import { lighten } from 'polished' - fluidRange
const { fluidRange } = require('polished')import { fluidRange } from 'polished' - rgba
import * as polished from 'polished'; polished.rgba(...)
import { rgba } from 'polished'
Quickstart
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);