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

Warn against use of show() and hide() #277

Open
Krinkle opened this issue Jun 5, 2020 · 1 comment
Open

Warn against use of show() and hide() #277

Krinkle opened this issue Jun 5, 2020 · 1 comment

Comments

@Krinkle
Copy link
Member

Krinkle commented Jun 5, 2020

Example

@Krinkle wrote at https://phabricator.wikimedia.org/T253938#6194923

The current patch uses .show() is generally stands out as an anti-pattern due to the akward legacy semantics is has to uphold. Consider the question, given an alement that has no inline style attribute, but some css classes set that make it display: none, how would jQuery know which inline style to set? Is it display: inline, display: block, display: table, display: list-item, etc. Moreover, consider a page with a <div> and a CSS rule that makes it behave as a table (like our Table of Contents), or a page with and some default display rule in your site's stylesheet. jQuery does a lot of guess work to try and get this right (involving temporary DOM elements and what not).

The caller ought to know which display is preferred, using .css('display', …). That isn't only orders of magnitude faster, it also less likely to get it wrong and is easier to debug.

Upstream

Upstream jQuery would love to get rid of or radically simplify these methods. However, what it boils down to is that when all the developer tells jQuery is "show" the element, it is surprisingly hard to do that correctly.

Further reading:

Rule

In general, this entire area is already grey as best practice is to leave this to the stylesheet to control. That separates the concerns and also avoids interrupts that cause the browser to forcefully recompute styles. Toggling a class merely tells the browser that the next frame it will render it with those styles. Setting an inline style directly with css() also has that same benefit. However, show() and hide() come with a much higher cost.

The good news is, that one generally always knows as caller, what the intended effect is. So we'd be much better off requiring that to be done explicitly. In other words, use .css('display', 'none') to hide something and .css('display', '') to make it re-appear. Or if you know the element is hidden by default and know what you want the display value to become, then use something like .css('display', 'block').

@edg2s
Copy link
Member

edg2s commented Jun 6, 2020

This is enforced in VE and OOUI. At the time I thought it was too disruptive to enforce globally and for most projects the performance isn't an issue. But if we can provide a useful error message this would be good to have if not just for new projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants