What are the current versions of Tailwind and Alpine.js being used by Elements?
With the assistance of ChatGPT and Grok, I have developed a small component, outside of Elements, to use PHP to read a CSV file of calendar entries and use Alpine.js to display them in a container. I have tested this using MAMP and it works well.
I have now tried migrating the code into an Elements Custom Component and it is failing with a javascript error:
[Warning] Alpine Expression Error: null is not an object (evaluating 'e._x_dataStack=[t,...j(r||e)]') (alpine.js, line 1)
Expression: "allEventsFiltered"
â
<template x-for="(entry, index) in allEventsFiltered" :key="index">âŚ</template>
I thought I should get the versions of Alpine.js and Tailwind to be the same as Elements and make sure that it still works in MAMP before trying to debug this further in Elements?
Am I correct in assuming that it is alright to use Alpine.js x-init, x-data and x-for statements within an Elements component?
I still canât get it to work in elements. I suspect it is something to do with a script function I have embedded in the inline code in the Custom Component template.
In the MAMP version, I have the code:
<!-- Load AlpineJS v3 from JSDelivr -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script>
// Version 1.2.2
function calendarApp() {
return {
events: <?php echo json_encode($groupedEvents, JSON_HEX_TAG); ?>,
filter: '',
allEventsFiltered: [],
...
In the Elements version I have removed the line that loads alpine.js, as I assume Alpine.js is already loaded:
<!-- Load AlpineJS v3 from JSDelivr -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
I think ChatGPT / Grok recommended doing the âdeferâ as it was causing problems not having it.
none of your Alpine.js code will be executed within the Elements editor - you need to preview your site in an external browser for the javascript code to be executed.
Does that make sense? Let me know if I can help any further
I am testing the Elements version in Preview and via publishing to local files and viewing via MAMP. Both donât work. The non-Elements version works in MAMP as per the image above.
I think it is some sort of timing issue as the errors I am getting imply (I think) that the alpine.js in the html code is trying to retrieve the data before it has been populated via the script which I guess is why there was a âdeferâ when I was loading the script.
@bon : Are there ways to work around this and still show the content in edit mode instead of only in preview? I donât mind checking in preview mode, but if itâs possible to display it within Elements itself, that would add value.
if your content is being fetched from somewhere with javascript, then unfortunately you canât display that in Elements.
The solution here is to display some dummy data in edit mode only. This way you can set the styling in Elements, and when you preview your javascript can inject the fetched content dynamically.
Let me know if you need more information on how to do this!
@bon I have built several custom components to import data from Airtable tables. The result indeed matches what you describe: the styling is visible in edit mode and the full display only appears in preview.
So now I am certain that it cannot be done any other way and that I can finish these components as they are.