Skip to content

Guidance about reflecting state in HTML attributes #501

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

LeaVerou
Copy link
Member

@LeaVerou LeaVerou commented Jul 15, 2024

@LeaVerou LeaVerou requested review from hober and martinthomson July 15, 2024 18:54
Copy link
Contributor

@hober hober left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@@ -1032,6 +1032,25 @@ The alternative user experience is a flash of unstyled content, which is undesir

See also [[#worker-only]].

<h3 id="html-attribute-property-reflection">Expose content attributes for writable IDL attributes</h3>

When designing a new *writable* IDL attribute to specify element state,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emphasis on "writable" doesn't seem to serve a purpose. (I, for one, find it distracting.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree, I think "writable" is essential to the principle and should be emphasized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a rephrase then.

When designing a new IDL attribute that reflects the state of an element,
expose a corresponding content attribute,
unless the attribute is immutable or
there are other strong reasons not to.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also confused why "writable" matters here. Is there a reason immutable IDL attributes are less suitable to be reflected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of attributes are writable, which can make immutable attributes surprising.

- It takes advantage of HTML reactivity, and alleviates the need to apply changes at a specific moment in time.
- It integrates with existing DOM methods, improving learnability, whereas an API specific to the feature needs to be learned separately.
- Behaviors can be expressed reactively in libraries supporting HTML-based syntax and be handled by any library handling HTML generically.
- UI states can be addressed in CSS via simple attribute selectors rather than new pseudo-classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is a good thing in all cases. For instance, [lang=foo] v. [lang|=foo] v. :lang(foo).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems orthogonal. In that case the pseudo-class serves a distinct purpose by targeting the actual computed language (which may be specified on an ancestor). I’d argue there may even be value in a generic attribute selector for these inherited attributes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd favor Lea's point on this one. :lang() is justified because it is not as much about element state directly and more about the state of the DOM tree, as viewed from that node.

That said, the other point is also good: [lang=foo] is possible, but inadvisable.

<{input}>'s {{HTMLInputElement/indeterminate}} IDL property
which was never exposed as a content attribute making it impossible to fully update checkbox state
by modifying HTML.
</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, indeterminate is a terrible feature that should never have been added to the platform in the first place. Maybe we should try to find a more positive example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate? Is it the UI of an indeterminate checkbox that you think is a mistake or the way it was added to HTML?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the above, IIRC.

Copy link
Member

@annevk annevk Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indeterminate IDL attribute is a bad example as it's like the checked or value IDL attributes which also do not have corresponding content attributes.

You could argue that we should have had a defaultIndeterminate IDL attribute for a corresponding indeterminate content attribute, but that's a separate matter. whatwg/html#6578 goes into this a bit.

(And of course people always like to point out that the IDL attributes not matching the names of the content attributes is confusing, but that's also a separate matter and anyway not something that can actually be solved.)

Copy link
Member Author

@LeaVerou LeaVerou Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s a separate, existing, principle about this: Keep attributes in sync. It’s right below this one actually 😁

indeterminate in addition to that, also has no content attribute counterpart so there is no HTML way to specify it at all (current OR initial status).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hober Interesting. Could you elaborate on the UI part?

Copy link
Member

@annevk annevk Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That principle is about content attributes needing to be reflected right? But indeterminate is not a content attribute. Edit: ah right, it even says so in the note:

This does not hold the other way around. A new IDL attribute does not always warrant a content attribute counterpart.

Co-authored-by: Theresa O'Connor <[email protected]>
<{input}>'s {{HTMLInputElement/indeterminate}} IDL property
which was never exposed as a content attribute making it impossible to fully update checkbox state
by modifying HTML.
</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the above, IIRC.

Copy link
Contributor

@martinthomson martinthomson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this comment a VERY long time ago. I have not re-examined it for whether I still believe it.

- It takes advantage of HTML reactivity, and alleviates the need to apply changes at a specific moment in time.
- It integrates with existing DOM methods, improving learnability, whereas an API specific to the feature needs to be learned separately.
- Behaviors can be expressed reactively in libraries supporting HTML-based syntax and be handled by any library handling HTML generically.
- UI states can be addressed in CSS via simple attribute selectors rather than new pseudo-classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd favor Lea's point on this one. :lang() is justified because it is not as much about element state directly and more about the state of the DOM tree, as viewed from that node.

That said, the other point is also good: [lang=foo] is possible, but inadvisable.

@martinthomson martinthomson changed the title First stab at #289 Guidance about reflecting state in HTML attributes Dec 3, 2024
@torgo torgo added this to the 2025-02-10-week milestone Feb 9, 2025
@jyasskin jyasskin self-requested a review February 18, 2025 23:50
@xiaochengh
Copy link

I'll take a look

@xiaochengh
Copy link

Just a note that the original example <popup> in #289 is no longer there, and got replaced by the popover attribute. And popover visibility state is not exposed as content or IDL attributes, but only via CSS :popover-open selector.

@annevk
Copy link
Member

annevk commented Apr 28, 2025

To be clear, https://github.com/w3ctag/design-principles/pull/501/files#r1680595686 is still applicable. This should not be merged with the indeterminate example.

@martinthomson
Copy link
Contributor

I'm not sure that I understand the concern about indeterminate. It seems like this could be made a content attribute (though what indeterminate + checked means is interesting to contemplate). The popover example might be similarly enlightening.

Basically, I think that our examples need to show where values are reflected as attributes in a good way (maybe; that's most of the platform, so it doesn't make sense to cherry pick one example) and where values were not reflected as attributes, to make the reasons for why that might be a good choice clear.

@annevk
Copy link
Member

annevk commented Apr 29, 2025

The concern is that the indeterminate IDL attribute is akin to the value IDL attribute, which doesn't have a content attribute counterpart. There's a value content attribute, but its IDL counterpart is defaultValue. So this suggestion makes it seem as if the TAG doesn't understand how HTML forms work. Which is reasonable as it's rather involved and essentially everybody gets confused about this, but adding to this confusion seems ill-advised.

@martinthomson
Copy link
Contributor

To be clear, *I* don't understand how forms work; the use of pronoun should have made that clear. I'm just trying to shepherd this to a constructive conclusion.

@annevk, perhaps I can request that you offer an example that would help illustrate this principle. Or, maybe you would prefer that we not pursue this further. Explaining why would be helpful.

@annevk
Copy link
Member

annevk commented Apr 30, 2025

Can you clarify what part about my value example is unclear?

@martinthomson
Copy link
Contributor

Ah, I hadn't interpreted that as a suggestion for a better example. Indeed, I would be OK switching to value and defaultValue in the example.

@annevk
Copy link
Member

annevk commented Apr 30, 2025

Oh my bad, I had in fact misunderstood your question.

I'm actually not sure the HTML community is fully aligned on this principle. While some state certainly deserves to be reflected through a content attribute, that does not go for state that is the result of end user input.

The open content attribute of the dialog and details elements is designed to always reflect the current state of the control, but this has resulted in a number of subtle issues and also seems to go against expectations. Web developers don't expect node tree mutations as the result of end users manipulating a control. We also have to be very careful to not re-introduce mutation events if we want to make these state changes observable to web developers.

cc @smaug---- @lukewarlow

@lukewarlow
Copy link

I do agree that IDL attributes should generally reflect to HTML content attributes. And that the names should match up (value attribute should be defaultvalue imo).

Being able to represent initial state via HTML is good. Indeterminate is an example where we can't currently do that.

The specific point about targeting attributes rather than pseudo-classes seems wrong though. The open attribute is a design mistake and even that we now have the :open pseudo class to target instead of using [open]. Custom elements can similarly follow this pattern using CSS custom states.

Also suggesting that reflecting IDL to HTML content attributes allows for this isn't quite right either, because internal element state that's changed via interaction (other than for open) doesn't reflect to the attribute and so you'd still need a CSS way to access that state.

This note seems to be over reaching slightly.

The note about reactive html also seems potentially wrong depending on if I'm reading it right. The open attribute is reactive and even that doesn't work if your library is setup to act on the attribute (or property, tbf so perhaps not a great example?)

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

Successfully merging this pull request may close these issues.

7 participants