choojs / choojs/nanohtml

Support && operator-based conditionals in expressions

Open
#51 8 comments 6 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
688
Forks
48
PR merge metrics
No merged PRs in 30d

Description

What do you think of supporting `&&` operator-based conditionals in interpolated expressions?

Current syntax:

``` javascript
html``
```

Proposed syntax to support:

``` javascript
html``
```

The proposed syntax is simply more succinct, and I believe it's popular in the react ecosystem. While the above example doesn't save much text, the `: ''` gets in the way more when you have multi-line expressions -- it becomes a trailing piece that is often overlooked.

Currently, the proposed syntax works in bel if the first condition is truthy, but if it is falsy, bel renders its falsy value, and you'd end up with ``. I believe the way around this would be for bel (or maybe hyperx?) to _not_ return anything from an interpolated expression that evaluates to `undefined` or `false`. I believe this is how [JSX does it](https://facebook.github.io/react/tips/false-in-jsx.html).

If for some reason you _want_ to render `undefined` or `false`, I think it would be reasonable to convert it to a string (since it's a string once it's in the DOM anyway). I think the exception to this would be the value `0` -- it may be unexpected to have to convert `0` to a string just to render it to the DOM. `0` comes in to play when you want to do `${rows.length && 'foo'}` vs. `${rows.length > 0 && 'foo'}`.

/cc @yoshuawuyts

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.