jss icon indicating copy to clipboard operation
jss copied to clipboard

[jss-default-unit] Need configurable, per-property default units

Open jeffmcmahan opened this issue 10 years ago • 2 comments

I think the plugin would be better if it could be used as follows:

let options = {
  fontSize: 'rem',
  lineHeight: 'rem',
  height: 'rem',
  width: 'rem',
  transitionDuration: 's'
}

jss.use(jssDefaultUnit.default(options))

I'd like it to be the case that whatever is not specified by the user's conf object falls back to a sensible default unit for that property. The default for width would be px, and for animationDuration, it'd be ms, for example.


Implementation Proposal

I'm posting this in the hope that feedback may help to make this better before I make a PR.

The above calls for a map of property names to default units. Here's a pretty complete list generated by Chrome (scroll down to see the code that generates it):

export default = {
  "animationDelay": "ms",
  "animationDuration": "ms",
  "backgroundOrigin": "%",
  "backgroundPositionX": "px",
  "backgroundPositionY": "px",
  "backgroundSize": "px",
  "border": "px",
  "borderBottom": "px",
  "borderBottomLeftRadius": "px",
  "borderBottomRightRadius": "px",
  "borderBottomWidth": "px",
  "borderLeft": "px",
  "borderLeftWidth": "px",
  "borderRadius": "px",
  "borderRight": "px",
  "borderRightWidth": "px",
  "borderSpacing": "px",
  "borderTop": "px",
  "borderTopLeftRadius": "px",
  "borderTopRightRadius": "px",
  "borderTopWidth": "px",
  "borderWidth": "px",
  "bottom": "px",
  "columnGap": "px",
  "columnRule": "px",
  "columnRuleWidth": "px",
  "cx": "px",
  "cy": "px",
  "flexBasis": "px",
  "fontSize": "px",
  "height": "px",
  "left": "px",
  "letterSpacing": "px",
  "margin": "px",
  "marginBottom": "px",
  "marginLeft": "px",
  "marginRight": "px",
  "marginTop": "px",
  "maxHeight": "px",
  "maxWidth": "px",
  "minHeight": "px",
  "minWidth": "px",
  "motion": "px",
  "motionOffset": "px",
  "outline": "px",
  "outlineOffset": "px",
  "outlineWidth": "px",
  "padding": "px",
  "paddingBottom": "px",
  "paddingLeft": "px",
  "paddingRight": "px",
  "paddingTop": "px",
  "perspectiveOrigin": "%",
  "r": "px",
  "right": "px",
  "rx": "px",
  "ry": "px",
  "shapeMargin": "px",
  "size": "px",
  "textIndent": "px",
  "top": "px",
  "transformOrigin": "%",
  "transitionDelay": "ms",
  "transitionDuration": "ms",
  "verticalAlign": "px",
  "borderAfterWidth": "px",
  "borderBeforeWidth": "px",
  "borderEndWidth": "px",
  "borderHorizontalSpacing": "px",
  "borderStartWidth": "px",
  "borderVerticalSpacing": "px",
  "fontSizeDelta": "px",
  "logicalHeight": "px",
  "logicalWidth": "px",
  "marginAfter": "px",
  "marginBefore": "px",
  "marginEnd": "px",
  "marginStart": "px",
  "maskOrigin": "%",
  "maskPositionX": "px",
  "maskPositionY": "px",
  "maskSize": "px",
  "maxLogicalHeight": "px",
  "maxLogicalWidth": "px",
  "minLogicalHeight": "px",
  "minLogicalWidth": "px",
  "paddingAfter": "px",
  "paddingBefore": "px",
  "paddingEnd": "px",
  "paddingStart": "px",
  "perspectiveOriginX": "%",
  "perspectiveOriginY": "%",
  "textStroke": "px",
  "textStrokeWidth": "px",
  "transformOriginX": "%",
  "transformOriginY": "%",
  "transformOriginZ": "%",
  "width": "px",
  "wordSpacing": "px",
  "x": "px",
  "y": "px"
}

You can get that list by running the following in your Chrome dev tools console:

// Chrome's DOM API will tell us whether each style prop accepts a 'px' unit or not.
function canonicalize(name) {
  name = name.replace('webkit', '')
  name = name.split('')
  name[0] = name[0].toLowerCase()
  return name.join('')
}

let props = {}
let div = document.createElement('div')
document.body.appendChild(div)

Object.keys(div.style)
  .filter(propName => !/^\d+$/.test(propName) && propName !== 'animation')
  .forEach(function (propName) {
    div.style[propName] = 0
    if (div.style[propName] === '0px') {
      props[canonicalize(propName)] = 'px'
    }
    if (
      propName.indexOf('Duration') !== -1 || 
      propName.indexOf('Delay') !== -1
    ) {
      props[canonicalize(propName)] = 'ms'
    }
    if (propName.indexOf('Origin') !== -1) {
      props[canonicalize(propName)] = '%'
    }
  })

document.body.removeChild(div)
console.log(JSON.stringify(props, null, 2))

We'd drop the ./ignoreProps.js blacklist in favor of ./defaultUnits.js and use it, modulo whatever the user specifies in their conf object, to do as follows for each property in the stylesheet:

// Roughly:
if (typeof propValue === 'number') {
  propValue += defaults[propName] || ''
}

jeffmcmahan avatar Jul 16 '16 22:07 jeffmcmahan

Oh interesting!

kof avatar Jul 17 '16 22:07 kof

Released, thanks a lot @jeffmcmahan for making jss better!

kof avatar Jul 26 '16 00:07 kof