Reputation: 531
I'm trying to add:
https://github.com/lipis/flag-icon-css
to my app so that when someone clicks on a mapbox country I can show the flag
Is there a way that you would recommend how to do this?
I tried the following but I get TypeError: (void 0) is undefined
<span class="flag-icon" x-bind:class="{ [`flag-icon-${$store.ui.clicked.flag}`]: true }">
<h3 class="pt-3 mb-3 ml-5 text-lg " x-text="$store.ui.clicked.name">Country / City name</h3>
</span>
Spruce.store('ui', {
clicked: {
name: 'Welcome!',
u: 'US',
flag: (this.region_ident ?? this.u).toLowerCase().slice(0, 2),
},
})
edit: inside my on map click function I have this:
Spruce.store('ui').clicked = {
...Spruce.store('ui').clicked,
city: e.features[0].properties,
name,
}
Maybe I could try:
$el.addClass(`flag-icon-${$store.ui.clicked.flag}`)
But I am not sure where to put that.
This works but I think an Alpine.js solution would be more succinct.
document.getElementById('flag-show-bottom').removeAttribute('class')
document
.getElementById('flag-show-bottom')
.classList.add(
'flag-icon',
`flag-icon-${e.features[0].properties.u.toLowerCase().slice(0, 2)}`,
)
Upvotes: 0
Views: 3204
Reputation: 531
I finally figured it out. x-bind:
takes an expression. You can use it on anything. it is really versatile. You don't have to use :class={ className: bool }
format.
You can just use strings:
<img src="../images/blank.gif"
x-bind:class="`flag ${ $store.ui.clicked.country
? 'flag-' + $store.ui.clicked.country.region_ident.toLowerCase().slice(0, 2)
: $store.ui.clicked.city
? 'flag-' + $store.ui.clicked.city.u.toLowerCase().slice(0, 2)
: '' }`" />
(I switched to using a different flag library but the logic is the same)
Upvotes: 5