Coder Social home page Coder Social logo

Comments (7)

ewinslow avatar ewinslow commented on June 30, 2024 1

@sparhami It seems like the main reason this can't be changed is for backwards compatibility reasons. There is a very clear and cheap (and I'd argue, more correct) solution, which is to use the string value:

elementOpen('div', null, null, 'aria-expanded', 'true');

Or another case, where you just need attribute presence:

elementOpen('button', null, null, 'disabled', '');

But setting-boolean-properties have much more painful workarounds.

I really think setting properties for booleans is the more intuitive behavior here. All code that assumes otherwise can be trivially changed to use strings today and still work fine. But if it's just too much trouble, then I totally get that...

from incremental-dom.

iteriani avatar iteriani commented on June 30, 2024

It's kind of costly to always check for boolean. Is it really necessary to create Boolean objects?

from incremental-dom.

sparhami avatar sparhami commented on June 30, 2024

I'm not sure if the cost of the check is that great, but it is additional code to compile and ship. There is a cost to allocating the Boolean object however when using new, which applies to every diff regardless of whether or not there is a change.

I think a more promising approach would be to customize the behavior as described here: http://google.github.io/incremental-dom/#setting-values

from incremental-dom.

ewinslow avatar ewinslow commented on June 30, 2024

from incremental-dom.

trevorparscal avatar trevorparscal commented on June 30, 2024

Thank you all for the ideas. I ended up making typeof boolean always use setProp, the same way typeof object and typeof function already do. This introduces a typeof check, but it's less costly than constructing the Boolean during the render plus doing an instanceof on the other side.

The customizing behavior seems like a nice idea, but I worry about bugs. If the library behaves differently from project to project in sneaky ways depending on if the same hooks are setup I suspect there will be tricky to diagnose problems from sharing code or practices.

from incremental-dom.

trevorparscal avatar trevorparscal commented on June 30, 2024

I've been playing around with this a bit more. The patch I'm using (based on ewinslow's comment) adds some code to applyAttributeTyped, changing...

if (type === 'object' || type === 'function') {

to...

if (type === 'object' || type === 'function' || type === 'boolean') {

This check is very inexpensive, requires no object construction on the calling side and solves the general case of elegantly setting a boolean as a property. It's also easy to document this behavior - just as we already document the same behavior for objects and functions.

The alternative suggested is something like this:

attributes.checked = applyProp;
attributes.selected = applyProp;
attributes.disabled = applyProp;
// and so on for readOnly, multiple, isMap, defer, declare,
// noResize, noWrap, noShade, compact, etc.

This is a bit complex just to get standard HTML elements to behave as expected. Another problem with hooks is that it introduces potential conflicts when used with other types of elements, such as SVG or shadow-dom, which either have non-boolean attributes or properties with similar names or have boolean attributes or properties not explicitly listed - or both.

Are we sure it's not worth adding the boolean typeof check to the core library to resolve this problem in a simple, general and consistent way?

from incremental-dom.

sparhami avatar sparhami commented on June 30, 2024

I've been playing around with this a bit more. The patch I'm using (based on ewinslow's comment) adds some code to applyAttributeTyped, changing...

if (type === 'object' || type === 'function') {

to...

if (type === 'object' || type === 'function' || type === 'boolean') {

This check is very inexpensive, requires no object construction on the calling side and solves the general case of elegantly setting a boolean as a property. It's also easy to document this behavior - just as we already document the same behavior for objects and functions.

The alternative suggested is something like this:

attributes.checked = applyProp;
attributes.selected = applyProp;
attributes.disabled = applyProp;
// and so on for readOnly, multiple, isMap, defer, declare,
// noResize, noWrap, noShade, compact, etc.

This is a bit complex just to get standard HTML elements to behave as expected. Another problem with hooks is that it introduces potential conflicts when used with other types of elements, such as SVG or shadow-dom, which either have non-boolean attributes or properties with similar names or have boolean attributes or properties not explicitly listed - or both.

Are we sure it's not worth adding the boolean typeof check to the core library to resolve this problem in a simple, general and consistent way?

We cannot change to use type === 'boolean'. Take for example:

let expanded = false; // This variable is set to a boolean value at some point.
...
// It now gets set as an attribute, and  converted to a string.
elementOpen('div', null, null, 'aria-expanded', expanded);

This cannot be set as a property and changing this will break existing users in a way that may not be easy to detect. I think for some uses this might not be a big concern, or they might want to have another way around like:

let expanded = false;
...
elementOpen('div', null, null, 'aria-expanded', toString(expanded));

or automatically do that a higher level construct that wraps elementOpen, but I think it all comes back to not using Incremental DOM directly, but rather configured in some sort of way for higher level usage.

from incremental-dom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.