Skip to content

API Reference

This reference documents all configuration options and methods available in postcss-logical-polyfill.

The plugin accepts a LogicalPolyfillOptions object with the following properties:

  • Type: object
  • Description: Configuration for RTL (Right-to-Left) direction
  • Type: string
  • Default: '[dir="rtl"]'
  • Description: CSS selector used to target RTL content
require('postcss-logical-polyfill')({
rtl: {
selector: '[dir="rtl"]'
}
})
// Input CSS
.element { margin-inline-start: 1rem; }
// Output CSS
[dir="ltr"] .element { margin-left: 1rem; }
[dir="rtl"] .element { margin-right: 1rem; }

Examples:

  • '[dir="rtl"]' - Attribute selector (default)
  • ':dir(rtl)' - CSS4 direction pseudo-class
  • '.rtl' - Class-based approach
  • Type: object
  • Description: Configuration for LTR (Left-to-Right) direction
  • Type: string
  • Default: '[dir="ltr"]'
  • Description: CSS selector used to target LTR content
require('postcss-logical-polyfill')({
ltr: {
selector: '[dir="ltr"]'
}
})
  • Type: 'ltr-first' | 'rtl-first'
  • Default: 'ltr-first'
  • Description: Controls the order in which LTR and RTL rules are output in the generated CSS
// LTR rules first (default)
require('postcss-logical-polyfill')({
outputOrder: 'ltr-first'
})
// RTL rules first
require('postcss-logical-polyfill')({
outputOrder: 'rtl-first'
})

This can be useful for CSS specificity control or debugging purposes.

Logical PropertyLTR PhysicalRTL Physical
margin-inlinemargin-left, margin-rightmargin-left, margin-right
margin-inline-startmargin-leftmargin-right
margin-inline-endmargin-rightmargin-left
margin-blockmargin-top, margin-bottommargin-top, margin-bottom
margin-block-startmargin-topmargin-top
margin-block-endmargin-bottommargin-bottom
Logical PropertyLTR PhysicalRTL Physical
padding-inlinepadding-left, padding-rightpadding-left, padding-right
padding-inline-startpadding-leftpadding-right
padding-inline-endpadding-rightpadding-left
padding-blockpadding-top, padding-bottompadding-top, padding-bottom
padding-block-startpadding-toppadding-top
padding-block-endpadding-bottompadding-bottom
Logical PropertyLTR PhysicalRTL Physical
border-inlineborder-left, border-rightborder-left, border-right
border-inline-startborder-leftborder-right
border-inline-endborder-rightborder-left
border-blockborder-top, border-bottomborder-top, border-bottom
border-block-startborder-topborder-top
border-block-endborder-bottomborder-bottom
Logical PropertyLTR PhysicalRTL Physical
border-start-start-radiusborder-top-left-radiusborder-top-right-radius
border-start-end-radiusborder-top-right-radiusborder-top-left-radius
border-end-start-radiusborder-bottom-left-radiusborder-bottom-right-radius
border-end-end-radiusborder-bottom-right-radiusborder-bottom-left-radius
Logical PropertyLTR PhysicalRTL Physical
inset-inlineleft, rightleft, right
inset-inline-startleftright
inset-inline-endrightleft
inset-blocktop, bottomtop, bottom
inset-block-starttoptop
inset-block-endbottombottom
Logical PropertyPhysical Equivalent
block-sizeheight
inline-sizewidth
min-block-sizemin-height
min-inline-sizemin-width
max-block-sizemax-height
max-inline-sizemax-width
Logical PropertyPhysical Equivalent
overflow-blockoverflow-y
overflow-inlineoverflow-x

The plugin also transforms logical values in certain properties:

PropertyLogical ValueLTR PhysicalRTL Physical
floatinline-startleftright
floatinline-endrightleft
clearinline-startleftright
clearinline-endrightleft
text-alignstartleftright
text-alignendrightleft
const postcss = require('postcss')
const logicalPolyfill = require('postcss-logical-polyfill')
postcss([
logicalPolyfill({
preserve: false,
dir: 'ltr'
})
])
.process(css)
.then(result => {
console.log(result.css)
})
const postcss = require('postcss')
const logicalPolyfill = require('postcss-logical-polyfill')
async function transformCSS(css) {
const result = await postcss([
logicalPolyfill()
]).process(css, { from: undefined })
return result.css
}
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
['postcss-logical-polyfill', {
preserve: process.env.NODE_ENV === 'development',
experimental: true
}]
]
}
}
}
]
}
]
}
}

The main processing method that transforms logical properties to physical properties.

Parameters:

  • root (PostCSS Root): The PostCSS AST root node
  • options (Object): Plugin configuration options

Returns: Modified PostCSS AST with transformed properties

These methods are used internally by the plugin:

Transforms a single logical property to its physical equivalent.

Generates direction-specific selectors when needed.

Checks if a property is a logical property that should be transformed.

The plugin generates CSS that supports:

  • Modern browsers: Use native logical properties when available
  • Legacy browsers: Fall back to physical properties
  • IE11: Full support with physical properties only

When supports: true is enabled, the plugin generates progressive enhancement CSS:

/* Modern browsers use logical properties */
.element {
margin-inline: 1rem;
}
/* Legacy browsers get physical properties */
@supports not (margin-inline: 1rem) {
.element {
margin-left: 1rem;
margin-right: 1rem;
}
}

The plugin includes TypeScript definitions:

import postcss from 'postcss'
import logicalPolyfill, { Options } from 'postcss-logical-polyfill'
const options: Options = {
preserve: false,
dir: 'ltr',
supports: true,
experimental: false
}
const processor = postcss([logicalPolyfill(options)])

The plugin handles various edge cases and provides informative warnings:

  • Invalid logical property values
  • Conflicting logical and physical properties
  • Unsupported property combinations
// Warning example
console.warn('postcss-logical-polyfill: Conflicting properties detected')
  • Caching: The plugin caches transformed selectors for better performance
  • Selective Processing: Only processes rules that contain logical properties
  • Minimal Output: Generates only necessary fallbacks when using @supports

For large projects, consider:

  1. Using preserve: false in production
  2. Enabling supports: true for progressive enhancement
  3. Processing only relevant CSS files