Skip to content

Implement CSS selection #2

@0x546F6D

Description

@0x546F6D

Not sure if it is an interesting idea but instead of(/on top of) directives, what about using the attribute of an element to also select it with zts?
Looking at your zig-zag-zoe example, several directives only include 1 element:

.clickable
<div class="{[class]s} player{[player]}" hx-post="/square/{[x]}/{[y]}" hx-trigger="click throttle:1s"
    hx-headers='js:{{"x-player": getPlayer()}}'>
    {[player]}
</div>

which could also be selected with, for example, a data-zts attribute:

<div data-zts="clickable" class="{[class]s} player{[player]}" hx-post="/square/{[x]}/{[y]}" hx-trigger="click throttle:1s"
    hx-headers='js:{{"x-player": getPlayer()}}'>
    {[player]}
</div>

Of course, using directives, you can include more than one element:

.winner-victory
<span class="green">Victory !</span>
<div>
    Take off every ZIG
    <p class="yellow">For great justice</p>
</div>
<script>sing(victoryAudio, 1)</script>

But that could maybe also be solved using something like an "/inner" option?

<div data-zts="winner-victory/inner">
    <span class="green">Victory !</span>
    <div>
        Take off every ZIG
        <p class="yellow">For great justice</p>
    </div>
    <script>sing(victoryAudio, 1)</script>
</div>

The idea behind it is pretty selfish, it's for me to integrate zts and datastar more easily :)
for example, I would modify this code:

<div class="{[class]s}">
    <div class="grid-container" style="--Cols:{[columns]}">
        .square
        <div class="{[class]s} player{[player]}">
            {[player]}
        </div>

        .clickable
        <div class="{[class]s} player{[player]}" hx-post="/square/{[x]}/{[y]}" hx-trigger="click throttle:1s"
            hx-headers='js:{{"x-player": getPlayer()}}'>
            {[player]}
        </div>

        .end
    </div>
</div>

into:

<div data-zts="board" class="{[class]s}">
    <div id="board" class="grid-container" style="--Cols:{[columns]}"></div>
</div>

<div data-zts="square" class="{[class]s} player{[player]}">
    {[player]}
</div>

<div data-zts="clickable"
    class="{[class]s} player{[player]}"
    hx-post="/square/{[x]}/{[y]}" hx-trigger="click throttle:1s"
    hx-headers='js:{{"x-player": getPlayer()}}'>
    {[player]}
</div>

Datastar uses sse, which means that we can send as many html fragments as we want for a single GET http request. Datastar also gives several DOM merging strategies, such as idiomorph/inner/outer/prepend/append/etc..
So the logic would be to answer the http request with the "board" element 1st:

event: datastar-merge-fragments
data: fragments <div data-zts="board"...

and then send as many "square"/"clickable" elements as necessary to append to this #board elements:

event: datastar-merge-fragments
data: selector #board
data: mergeMode append
data: fragments <div data-zts="square"...
data: fragments <div data-zts="clickable"...
data: fragments ...
  • "data: selector #board" Selects the target element of the merge process using a CSS selector.
  • "data: mergeMode append" Appends the fragments to the target’s children.

... I just realised that I could get the same result with zts current implementation 🤦 :

.board
<div class="{[class]s}">
    <div id="board" class="grid-container" style="--Cols:{[columns]}"></div>
</div>

.square
<div class="{[class]s} player{[player]}">
    {[player]}
</div>

.clickable
<div class="{[class]s} player{[player]}"
    hx-post="/square/{[x]}/{[y]}" hx-trigger="click throttle:1s"
    hx-headers='js:{{"x-player": getPlayer()}}'>
    {[player]}
</div>

Anyway, I'll just leave this issue as food for though, maybe using a data-attribute instead of a directive would still make the template.html cleaner and easier to maintain? Not sure 🤷

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions