HTMLKit icon indicating copy to clipboard operation
HTMLKit copied to clipboard

Conditional attributes

Open tonyarnold opened this issue 2 years ago • 4 comments

In HTML, there are attributes that we'd only want to add on some condition, such as "checked" and "disabled". This is currently quite verbose to do, involving something like:

if someCondition {
  Input()
    .checked()
} else {
  Input()
}

It would be really helpful if there were support for something like:

Input()
  .checked(when: someCondition)
  .disabled(when: someConditition)

tonyarnold avatar Jun 29 '23 13:06 tonyarnold

You can use instead:

Input()
   .modify(if: true) {
        $0.checked()
   }

But I like your idea.

I like the fact, that it's a one-liner. Let me check, if we had it in the past and I removed it for any reason.

One good thing about modify(if:), it's the universal approach.

What do you think?

mattesmohr avatar Jun 30 '23 09:06 mattesmohr

Sure, that works for me for the time being :)

There are a few ergonomics changes I'd like to propose once I've used it a bit more in anger - I'll raise separate issues.

tonyarnold avatar Jun 30 '23 09:06 tonyarnold

Sure, let them come. I appreciate it. :-)

mattesmohr avatar Jun 30 '23 09:06 mattesmohr

For alpha.6 I have added a condition option for

  • hidden
  • disabled
  • autoplay
  • readonly
  • checked
  • required

There are more, but I think it makes no sense to add it for them:

  • autofocus
  • muted
  • selected
  • novalidate
  • loop
  • ismap
  • download
  • defer
  • controls
  • async

I went through them, thinking about it from a user standpoint and what could be a possibly an option flag in the user settings.

mattesmohr avatar Jul 02 '23 09:07 mattesmohr