4 ms·
Great article. I am super excited for :has, I think it will open up a lot of doors to truly declaritive UI with vanilla CSS and HTML. One more handy feature is
by grose 4y ago
Great article. I am super excited for :has, I think it will open up a lot of doors to truly declaritive UI with vanilla CSS and HTML.
One more handy feature is data attributes. You can use them to introduce some state to your UI.
Here's a quick and dirty example of a menu for an RPG game.
<ul id="menu" data-can="">
<li id="menu-attack"><button>attack</button></li>
<li id="menu-magic"><button>magic</button></li>
<li id="menu-run"><button>run</button></li>
</ul>
We can use data attribute selectors to hide menu buttons that the player can't use.
#menu:not([data-can~=attack]) #menu-attack,
#menu:not([data-can~=magic]) #menu-magic,
#menu:not([data-can~=run]) #menu-run {
display: none;
}
Then all we need to update the menu UI is this single line of JS.
document.getElementById("menu").dataset.can = getPossibleActions().join(" ");
You can see a rough example here: https://github.com/kawaiisolutions/tactics https://github.com/kawaiisolutions/tactics. It's very nice for prototyping.
- wildrhythms 4y agoYes, I use data attributes all over! It lets me keep certain data and certain states in the DOM, and I can actually use querySelector to... you know, do a query based on data attribute values. I've met many devs who didn't know that CSS queries can actually string match within the values of attributes! (i.e. [attribute~="value"] and the other variations) https://css-tricks.com/almanac/selectors/a/attribute/ https://css-tricks.com/almanac/selectors/a/attribute/
- johnisgood 4y agoYou can already have states and this game can be implemented in CSS and HTML without a single line of JS, but it would be extremely ugly and hard to follow, IMO.