CSS Typed Object Model
CSS Typed Object Model is a work-in-progress polyfill for using CSS Typed OM Level 1 in the browser.
Pull Requests are welcome. Please don’t use this in production until there is a v1.0.0.
npm install css-typed-om
Polyfill the window
object:
; ;
Use CSS Typed Object Model features:
// Element stylesdocumentbodyattributeStyleMap;documentbodyattributeStyleMap /* CSSUnitValue { value: 42, unit: 'px'}.toString() => 42px */ documentbodyattributeStyleMap;typeof documentbodyattributeStyleMapvalue // numberdocumentbodyattributeStyleMapunit // "number" // Stylesheet rulesdocumentstyleSheets0cssRules0styleMap;documentstyleSheets0cssRules0styleMap; /* CSSUnitValue { value: 100, unit: 'px'}.toString() => 100px */ // Math productsCSS /* CSSMathSum { operator: "sum", values: [ CSSUnitValue { value: 15, unit: 'px' }, CSSUnitValue { value: 10, unit: 'rem' }, CSSUnitValu { value: 5, unit: 'em' } ]}.toString() => calc(15px + 10rem + 5em) */ CSS /* CSSMathProduct { operator: "product", values: [ CSSUnitValue { value: 15, unit: 'px' }, CSSUnitValue { value: 10, unit: 'rem' }, CSSUnitValu { value: 5, unit: 'em' } ]}.toString() => calc(15px * 10rem * 5em) */ CSS /* CSSMathSum { operator: "sum", values: [ CSSUnitValue { value: 15, unit: 'px' }, CSSUnitValue { value: -10, unit: 'rem' }, CSSUnitValu { value: -5, unit: 'em' } ]}.toString() => calc(15px + -10rem + -5em) */ CSS /* CSSMathProduct { operator: "product", values: [ CSSUnitValue { value: 15, unit: 'px' }, CSSMathInvert { operator: 'invert', value: CSSUnitValue { value: 10, unit: 'rem' } }, CSSMathInvert { operator: 'invert', value: CSSUnitValue { value: 5, unit: 'em' } } ]}.toString() => calc(15px / 10rem / 5em) */ CSS /* CSSMathMax { operator: 'max', values: [ CSSUnitValue { value: 15, unit: 'px' }, CSSUnitValue { value: 10, unit: 'rem' }, CSSUnitValu { value: 5, unit: 'em' } ],}.toString() => max(15px, 10rem, 5em) */ CSS /* CSSMathMin { operator: 'min', values: [ CSSUnitValue { value: 15, unit: 'px' }, CSSUnitValue { value: 10, unit: 'rem' }, CSSUnitValu { value: 5, unit: 'em' } ],}.toString() => min(15px, 10rem, 5em) */
Features
polyfill
The polyfill
function adds the following functions to window
if they do not
already exist:
CSS
CSSKeywordValue
CSSMathInvert
CSSMathMax
CSSMathMin
CSSMathProduct
CSSMathSum
CSSStyleValue
CSSUnitValue
StylePropertyMap
It then adds the following functions to CSS
if they do not already exist:
number
percent
em
ex
ch
rem
vw
vh
vmin
vmax
cm
mm
in
pt
pc
px
Q
deg
grad
rad
turn
s
ms
Hz
kHz
dpi
dpcm
dppx
fr
The new CSSUnitValue
instances returned by these methods extend
CSSNumericValue
, which allow them to use the following methods:
add
div
max
min
mul
sub
The result of these transforms may be a new CSSUnitValue
instance or a new
CSSMathProduct
, CSSMathMax
, CSSMathMin
, or CSSMathSum
instance.
They all stringify back into compliant CSS.