Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for conditional attributes? #164

Open
saschanaz opened this issue Oct 24, 2019 · 6 comments
Open

Support for conditional attributes? #164

saschanaz opened this issue Oct 24, 2019 · 6 comments

Comments

@saschanaz
Copy link
Contributor

saschanaz commented Oct 24, 2019

html`
  <div data-something-optional="${nullableValue}"></div>
`

It can be useful if we get a declarative HTML way to conditionally define an attribute. Thoughts?

cc @sidvishnoi

@saschanaz
Copy link
Contributor Author

It seems the feature already exists but is limited to the specific attributes.

module.exports = [
'async', 'autofocus', 'autoplay', 'checked', 'controls', 'default',
'defaultchecked', 'defer', 'disabled', 'formnovalidate', 'hidden',
'ismap', 'loop', 'multiple', 'muted', 'novalidate', 'open', 'playsinline',
'readonly', 'required', 'reversed', 'selected'
]

@sidvishnoi
Copy link

@saschanaz Although I like the implicit nullablitly, a better syntax for above could be:

<div data-something?="${nullableValue}"></div>

It'll also avoid a rare but potential clash with having optional in attribute name.

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Oct 24, 2019

A way to do this today is using object spread:

const optional = {}
if (nullableValue) optional['data-something-optional'] = nullableValue
html`
  <div ${optional}></div>
`

it's a bit verbose but I think I prefer it over introducing custom syntax.

It is a bit strange that undefined and null values still output the attribute imo. I would maybe like to see that change in a future major release.

e; it can be made less verbose with a helper,

function optional (attr, value) {
  if (value) return { [attr]: value }
  return {}
}
html`
  <div ${optional('data-something', nullableValue)}></div>
`

@saschanaz
Copy link
Contributor Author

It'll also avoid a rare but potential clash with having optional in attribute name.

The -optional was just an arbitrary name rather than a way to make it optional 😁

html`
 <div ${optional('data-something', nullableValue)}></div>
`

Seems less verbose than inserting conditional block everywhere 👍, but is <div ${object}> a documented feature?

@goto-bus-stop
Copy link
Member

it looks like it's missing from the docs, yes. it is definitely a tested and supported feature, though!

@saschanaz
Copy link
Contributor Author

<div data-something?="${nullableValue}"></div>

It'll also avoid a rare but potential clash with having optional in attribute name.

? still is a valid attribute character.

document.createRange().createContextualFragment("<a what?='abc'>").children[0].getAttribute("what?")
// "abc"

bennlich added a commit to bennlich/nanohtml that referenced this issue Oct 11, 2022
Updates README.md with javascript object syntax described here: choojs#164 (comment)
goto-bus-stop pushed a commit that referenced this issue Oct 12, 2022
Updates README.md with javascript object syntax described here: #164 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants