Skip to content

Commit 63bb627

Browse files
committed
Add Prevent Hidden Element From Flickering On Load as a JavaScript TIL
1 parent 21385f4 commit 63bb627

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1490 TILs and counting..._
13+
_1491 TILs and counting..._
1414

1515
---
1616

@@ -524,6 +524,7 @@ _1490 TILs and counting..._
524524
- [Open Global npm Config File](javascript/open-global-npm-config-file.md)
525525
- [Parse A Date From A Timestamp](javascript/parse-a-date-from-a-timestamp.md)
526526
- [Pre And Post Hooks For Yarn Scripts](javascript/pre-and-post-hooks-for-yarn-scripts.md)
527+
- [Prevent Hidden Element From Flickering On Load](javascript/prevent-hidden-element-from-flickering-on-load.md)
527528
- [Purge Null And Undefined Values From Object](javascript/purge-null-and-undefined-values-from-object.md)
528529
- [Random Cannot Be Seeded](javascript/random-cannot-be-seeded.md)
529530
- [Reach Into An Object For Nested Data With Get](javascript/reach-into-an-object-for-nested-data-with-get.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Prevent Hidden Element From Flickering On Load
2+
3+
Here is what it might look like to use [Alpine.js](https://alpinejs.dev/) to
4+
sprinkle in some JavaScript for controlling a dropdown menu.
5+
6+
```html
7+
<div x-data="{ profileDropdownOpen: false }">
8+
<button
9+
type="button"
10+
@click="profileDropdownOpen = !profileDropdownOpen"
11+
>
12+
<!-- some inner html -->
13+
</button>
14+
<div x-show="profileDropdownOpen" role="menu">
15+
<a href="/profile" role="menuitem">Your Profile</a>
16+
<a href="/sign-out" role="menuitem">Sign Out</a>
17+
</div>
18+
</div>
19+
```
20+
21+
Functionally that will work. You can click the button to toggle the menu open
22+
and closed.
23+
24+
What you might notice, however, when you refresh the page is that the menu
25+
flickers open as the page first loads and then disappears. This is a quirk of
26+
the element being rendered before Alpine.js is loaded and the
27+
[`x-show`](https://alpinejs.dev/directives/show) directive has a chance to take
28+
effect.
29+
30+
To get around this, we can _cloak_ any element with an `x-show` directive that
31+
should be hidden by default.
32+
33+
```html
34+
<div x-data="{ profileDropdownOpen: false }">
35+
<button
36+
type="button"
37+
@click="profileDropdownOpen = !profileDropdownOpen"
38+
>
39+
<!-- some inner html -->
40+
</button>
41+
<div x-cloak x-show="profileDropdownOpen" role="menu">
42+
<a href="/profile" role="menuitem">Your Profile</a>
43+
<a href="/sign-out" role="menuitem">Sign Out</a>
44+
</div>
45+
</div>
46+
```
47+
48+
This addition needs to be paired with some custom CSS to hide any _cloaked_
49+
elements.
50+
51+
```css
52+
[x-cloak] { display: none !important; }
53+
```
54+
55+
[source](https://alpinejs.dev/directives/cloak)

0 commit comments

Comments
 (0)