When you are using alpine JS and load the page, at first, it displays the content for some milliseconds. In other words, you will have a flickering effect.
Using the x-cloak attribute in Alpine JS
According to alpine JS documentation, you can fix this issue with the x-cloak attribute. But before using x-cloak, you must add the following line in your CSS file.
1 2 3 4 5 |
[x-cloak] { display: none !important; } |
Here is what your style will look like.
1 2 3 4 5 6 7 8 9 |
<style> [x-cloak] { display: none !important; } </style> |
Now, you can add the x-cloak attribute to the opening tag where you are using x-data. Here is an example.
1 2 3 4 5 6 7 8 |
<div x-data="{open:false}" x-cloak > |
Let’s understand everything with an example. We have a button that will toggle a modal. If we don’t use the x-cloak attribute, the modal will start flickering every time the page loads.
How to Prevent Modal Flickering in Alpine JS
The code below uses TailwindCSS and Alpine JS to toggle a modal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<div class="mx-auto mt-20 flex w-1/2 flex-col items-center justify-center" x-data="{open:false}" > <h1 class="text-2xl font-bold">TailwindCSS with Alpine JS</h1> <div class="mt-10"> <button x-on:click="open=!open" class="rounded-md bg-blue-600 px-4 py-2 text-center text-white" > Open Modal </button> </div> <div> <div x-show="open" class="overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true" > <div class="min- flex items-end justify-center px-4 pt-4 pb-20 text-center sm:block sm:p-0" > <div class="bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true" ></div> <span class="sm: hidden sm:inline-block sm:align-middle" aria-hidden="true" ></span > <div class="inline-block transform overflow-hidden rounded-lg bg-white p-5 text-left align-bottom shadow-2xl transition-all sm:my-8 sm:w-full sm:max-w-xl sm:align-middle lg:p-16" > <div> <div class="mt-3 text-left sm:mt-5"> <h1 class="mb-8 text-2xl font-semibold leading-none tracking-tighter text-neutral-600" > Unsubscribe </h1> <p class="mx-auto text-base leading-relaxed text-gray-500"> Free and Premium themes, UI Kit's, templates and landing pages built with Tailwind CSS, HTML & Next.js. </p> </div> </div> <form action="" method="post" id="revue-form" name="revue-form" target="_blank" class="border2 mt-8 transform rounded-xl bg-gray-50 p-2 transition duration-500 ease-in-out sm:flex sm:max-w-lg md:mx-auto" > <div class="revue-form-group min-w-0 flex-1"> <label for="member_email" class="sr-only" >Email address</label > <input id="cta-email" type="email" class="block w-full transform rounded-md border border-transparent bg-transparent px-5 py-3 text-base text-neutral-600 placeholder-gray-300 transition duration-500 ease-in-out focus:border-transparent focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-300" placeholder="Enter your email " /> </div> <div class="revue-form-actions mt-4 sm:mt-0 sm:ml-3"> <button type="submit" value="Subscribe" name="member[subscribe]" id="member_submit" class="block w-full rounded-lg border border-transparent bg-blue-600 px-5 py-3 text-base font-medium text-white shadow hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-300 sm:px-10" > Notify me </button> </div> </form> <div class="sm:flex sm:max-w-lg md:mx-auto"> <p class="mt-3 text-xs text-gray-500"> By subscribing, you agree with Revue’s <a target="_blank" href="https://www.getrevue.co/terms" >Terms of Service</a > and <a target="_blank" href="https://www.getrevue.co/privacy" >Privacy Policy</a >. </p> </div> </div> </div> </div> </div> </div> |
Now, add the following style in the head section.
1 2 3 4 5 |
<style> [x-cloak] { display: none !important; } </style> |
After adding styles, add the x-cloak attribute to the tag where you are using x-data.
1 2 3 4 5 6 7 8 9 10 11 |
<div class="mx-auto mt-20 flex w-1/2 flex-col items-center justify-center" x-data="{open:false}" x-cloak > ..... </div> |
Conclusion
You can get rid of the flickering effect with the help of x-cloak and a single line of the stylesheet. You just have to make sure you are using x-cloak to the element which initializes the alpine JS with x-data.