hyperscript icon indicating copy to clipboard operation
hyperscript copied to clipboard

should numbers automatically be num+'px'?

Open dominictarr opened this issue 9 years ago • 11 comments

I've found that when using number values in hyperscript it's normally a pixel value. would it make sense to just interpret a number as a pixel value?

@juliangruber @Raynos @pfraze @airportyh @mmckegg @reqshark ?

dominictarr avatar Jan 06 '16 00:01 dominictarr

I think that's a good idea. FWIW it's also what React does with the style object

pfrazee avatar Jan 06 '16 02:01 pfrazee

There are a few cases when you do actually want to set a number without a unit prefix (e.g flex and lineHeight style properties)

I suppose the trick would be only to prefix "px" when typeof value === 'number', then the user would specify the value as a String if they wanted it to be literal.

Possibly this should be scoped just to style? All other places I can think of don't actually require 'px' - for example the width and height attributes of images can be specified as straight numbers with no unit and it is interpreted as px.

mmckegg avatar Jan 06 '16 03:01 mmckegg

+1 if it's just for style props

juliangruber avatar Jan 06 '16 14:01 juliangruber

Another case where px isn't expected is z-index.

airportyh avatar Jan 06 '16 15:01 airportyh

+1 good call. Looks like couple edge case styles were mentioned, also let's include opacity on that list

reqshark avatar Jan 06 '16 15:01 reqshark

yeah, I was thinking this would just be for style. which is easier to list, the cases where you want px or don't want it? hmm, all the px cases are things light width, height, top, left, right, bottom, margin, border (and subcases -left -right -top -bottom, min- max-) and currently for non-px we have zIndex and opacity.

But of course, there may be other non-px numbers we have missed and so a positive list is safer than a negative list.

dominictarr avatar Jan 07 '16 05:01 dominictarr

yea to be safe, we'll need to make that list positive

I mean, right now I just stumbled across another style (a flexbox thing) that takes a number: order

reqshark avatar Jan 07 '16 07:01 reqshark

In the end, I just added this function px(d) { return +d+'px' } and there after used px(num) and that is nice enough.

dominictarr avatar Jan 18 '16 03:01 dominictarr

@ashaffer created a module listing a lot of the properties which are unitless. Maybe you guys can take the list out of there or include the module if @dominictarr is still interested in this.

  • https://github.com/micro-js/css-default-units
  • https://github.com/micro-js/css-unitless

queckezz avatar May 25 '16 14:05 queckezz

thanks @queckezz given the size of that list I'd say it's unappropiate to set +'px' by default.

dominictarr avatar May 25 '16 23:05 dominictarr

I agree, often one may want to use em or increasingly device size units.

danwdart avatar Aug 26 '16 16:08 danwdart