Preact: Range Input: Order of Attributes Bug

Created on 3 Sep 2017  路  11Comments  路  Source: preactjs/preact

I stumbled upon a bug while working with Preact today. If you create an input range, the initial render of the slider position may be wrong, depending on the order of the attributes. The value attribute needs to be set after min/max/step. If value is applied before min/max/step are properly set, the value will be forced to match the default min=0/max=100/step=1 on a slider.

E.g. if you create two sliders:

// This one renders with the slider at 0
<input type="range" value={0.5} min="0" max="1" step="0.05" />

// This one renders with the slider at 0.5
<input type="range" min="0" max="1" step="0.05" value={0.5} />

image

See minimal code to reproduce here

When creating an dom element from a vnode, it looks like attributes are applied one at a time via a for...in loop. Since the ordering of keys in an object is arbitrary, it seems like diffAttributes needs to apply the attributes in a sorted way. I can't think of other potential ordering pitfalls with attributes (maybe the react source code would have insights), but at the very least, applying value last should fix this issue.

beginner-friendly bug help wanted

Most helpful comment

All 11 comments

Interesting! We might also be able to work around this by setting value as an attribute.

@developit - isn't value already being passed as an attribute to diffAttributes? Or did you mean pulling value out into its own vnode property during h?

I mean special-casing value like we do type and list:
https://github.com/developit/preact/blob/master/src/dom/index.js#L76

Basically, forcing a value prop from VDOM/JSX to be applied to the DOM using setAttribute() instead of as a property.

Hm, but the problem is the order in which value is applied relative to other attributes, not whether it's applied via prop or attribute (vanilla js example). I might be missing something, but by the time we're in setAccessor, it's too late to adjust the order without doing something async.

Interesting, I would have thought the step validation would only apply to the value property, not the attribute itself. You're right, this likely requires a bit of insane workaround magic in diffAttributes.

Happy to contribute a PR to fix it if someone isn't already working on it.

That would be awesome.

I ran into this issue too. When will this be released?

plz fix :)

@marvinhagemeister thanks! Closing my PR

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adriaanwm picture adriaanwm  路  3Comments

nopantsmonkey picture nopantsmonkey  路  3Comments

jasongerbes picture jasongerbes  路  3Comments

SabirAmeen picture SabirAmeen  路  3Comments

marcosguti picture marcosguti  路  3Comments