Not sure whats happening in codepen, but this works, wheras putting the script in the JS window does not
<div x-data="showWindow">
<button @click="toggle()" class="px-4 py-3 text-white bg-blue-500 rounded">Toggle Content</button>
<div x-show="isOpen">
Content...
</div>
</div>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('showWindow', () => ({
open: false,
get isOpen() {
return this.open;
},
toggle() {
this.open = ! this.open
}
}))
})
</script>
also worth noting that this works identically
<div x-data="{
open: false,
get isOpen() {
return this.open;
},
toggle() {
this.open = ! this.open
}
}">
<button @click="toggle()" class="px-4 py-3 text-white bg-blue-500 rounded">Toggle Content</button>
<div x-show="isOpen">
Content...
</div>
</div>