hx-indicator

hx-indicator ์†์„ฑ์€ ์š”์ฒญ์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ htmx-request ํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€๋  ์š”์†Œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋Š” ์š”์ฒญ์ด ์ง„ํ–‰ ์ค‘์ผ ๋•Œ ์Šคํ”ผ๋„ˆ๋‚˜ ์ง„ํ–‰ ํ‘œ์‹œ๊ธฐ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์†์„ฑ์˜ ๊ฐ’์€ ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•  ์š”์†Œ ๋˜๋Š” ์š”์†Œ๋“ค์˜ CSS ์ฟผ๋ฆฌ ์„ ํƒ์ž์ž…๋‹ˆ๋‹ค. ๋˜๋Š” ํ‚ค์›Œ๋“œ closest ๋’ค์— CSS ์„ ํƒ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ, ์ฃผ์–ด์ง„ CSS ์„ ํƒ์ž์™€ ์ผ์น˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ƒ์œ„ ์š”์†Œ๋‚˜ ์ž๊ธฐ ์ž์‹ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (์˜ˆ: closest tr).

๋‹ค์Œ์€ ๋ฒ„ํŠผ ์˜†์— ์Šคํ”ผ๋„ˆ๊ฐ€ ์žˆ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค:

<div>
    <button hx-post="/example" hx-indicator="#spinner">
        Post It!
    </button>
    <img  id="spinner" class="htmx-indicator" src="/img/bars.svg"/>
</div>

์š”์ฒญ์ด ์ง„ํ–‰ ์ค‘์ผ ๋•Œ, ์ด๋Š” #spinner ์ด๋ฏธ์ง€์— htmx-request ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์—๋Š” ๋˜ํ•œ htmx-indicator ํด๋ž˜์Šค๊ฐ€ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์Šคํ”ผ๋„ˆ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ถˆํˆฌ๋ช…๋„ ์ „ํ™˜์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค:

    .htmx-indicator{
        opacity:0;
        transition: opacity 500ms ease-in;
    }
    .htmx-request .htmx-indicator{
        opacity:1
    }
    .htmx-request.htmx-indicator{
        opacity:1
    }

์Šคํ”ผ๋„ˆ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ํšจ๊ณผ๋ฅผ ์„ ํ˜ธํ•œ๋‹ค๋ฉด, ์ž์ฒด indicator CSS๋ฅผ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ถˆํˆฌ๋ช…๋„ ๋Œ€์‹  display๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค (์ฐธ๊ณ ๋กœ htmx-indicator ๋Œ€์‹  my-indicator๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค):

    .my-indicator{
        display:none;
    }
    .htmx-request .my-indicator{
        display:inline;
    }
    .htmx-request.my-indicator{
        display:inline;
    }

hx-indicator ์„ ํƒ์ž์˜ ๋Œ€์ƒ์€ ํ‘œ์‹œํ•˜๋ ค๋Š” ์ •ํ™•ํ•œ ์š”์†Œ์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์‹ญ์‹œ์˜ค. indicator์˜ ์ƒ์œ„ ๊ณ„์ธต์˜ ์–ด๋–ค ์š”์†Œ๋ผ๋„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ, ๊ธฐ๋ณธ์ ์œผ๋กœ htmx-request ํด๋ž˜์Šค๋Š” ์š”์ฒญ์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์š”์†Œ์— ์ถ”๊ฐ€๋˜๋ฏ€๋กœ, ๊ทธ ์š”์†Œ ์•ˆ์— ์ธ๋””์ผ€์ดํ„ฐ๋ฅผ ๋ฐฐ์น˜ํ•˜๋ฉด hx-indicator ์†์„ฑ์„ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค:

<button hx-post="/example">
    Post It!
   <img  class="htmx-indicator" src="/img/bars.svg"/>
</button>

Demo

๋‹ค์Œ์€ ์œ„ ์ƒํ™ฉ์—์„œ ์Šคํ”ผ๋„ˆ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณด์ผ์ง€๋ฅผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค:

Notes

<meta name="htmx-config" content='{"includeIndicatorStyles": false}'>