Skip to content

Commit f29bc5a

Browse files
committed
Add htmx_lazy_load.sc example
1 parent b9b0781 commit f29bc5a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//> using scala "3.3.1"
2+
//> using dep ba.sake::sharaf:0.0.22
3+
4+
// https://htmx.org/examples/lazy-load/
5+
6+
import io.undertow.Undertow
7+
import scalatags.Text.all.*
8+
import ba.sake.hepek.html.HtmlPage
9+
import ba.sake.hepek.htmx.*
10+
import ba.sake.sharaf.*, routing.*
11+
12+
object IndexView extends HtmlPage with HtmxDependencies:
13+
override def bodyContent = div(hx.get := "/graph", hx.trigger := "load")(
14+
img(src := "/img/bars.svg", alt := "Result loading...", cls := "htmx-indicator")
15+
)
16+
17+
override def stylesInline = List("""
18+
.htmx-settling img {
19+
opacity: 0;
20+
}
21+
img {
22+
transition: opacity 300ms ease-in;
23+
width: 400px;
24+
}
25+
""")
26+
27+
val routes = Routes:
28+
case GET() -> Path() =>
29+
Response.withBody(IndexView)
30+
case GET() -> Path("graph") =>
31+
Thread.sleep(1000) // simulate slow, stonks
32+
val graph = img(src := "/img/tokyo.png")
33+
Response.withBody(graph)
34+
35+
Undertow.builder
36+
.addHttpListener(8181, "localhost")
37+
.setHandler(SharafHandler(routes))
38+
.build
39+
.start()
40+
41+
println(s"Server started at http://localhost:8181")
Loading

0 commit comments

Comments
 (0)