Skip to content

Commit 9ba39c6

Browse files
authored
improved example (#5)
1 parent 3028f30 commit 9ba39c6

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

Examples/Embedded/Public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
<head>
44
<title>Swiftle</title>
55
<script src="https://cdn.tailwindcss.com"></script>
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
67
</head>
78

8-
<body>
9+
<body style="background-color: black">
910
<script src="./index.js" type="module"></script>
1011
</body>
1112
</html>

Examples/Embedded/Sources/EmbeddedApp/Views.swift

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ struct GameView: View {
66
var onRestart: () -> Void
77

88
var content: some View {
9-
main(.class("flex flex-col items-center h-screen bg-black text-white")) {
9+
main(.class("flex flex-col gap-5 items-center h-screen bg-black text-white")) {
1010
div(.class("flex gap-4 items-center pt-5")) {
1111
SwiftLogo()
1212
h1(.class("text-2xl uppercase tracking-wider font-serif")) { "Swiftle" }
1313
SwiftLogo()
1414
}
1515

16-
div(.class("flex flex-col gap-1 font-mono py-5 relative")) {
16+
div(.class("flex flex-col gap-1 font-mono relative")) {
1717
for guess in game.guesses {
1818
GuessView(guess: guess)
1919
}
@@ -22,6 +22,19 @@ struct GameView: View {
2222
}
2323

2424
KeyboardView(keyboard: game.keyboard, onKeyPressed: onKeyPressed)
25+
26+
footer {
27+
p(.class("text-xs text-gray-400 text-center")) {
28+
"This is a proof of concept demo of an Embedded Swift Wasm app."
29+
br()
30+
"Find the source code in the "
31+
a(.href("https://github.com/sliemeobn/elementary-dom"),
32+
.class("text-orange-600 hover:underline"))
33+
{
34+
"elementary-dom github repository."
35+
}
36+
}
37+
}
2538
}
2639
}
2740
}
@@ -98,10 +111,11 @@ struct KeyboardLetterView: View {
98111
guess.letter.value
99112
}
100113
}
101-
.attributes(.class("bg-gray-400"), when: guess.status == .unknown)
102-
.attributes(.class("bg-green-600"), when: guess.status == .correctPosition)
103-
.attributes(.class("bg-yellow-600"), when: guess.status == .inWord)
104-
.attributes(.class("bg-gray-600"), when: guess.status == .notInWord)
114+
.enabledMobileActive()
115+
.attributes(.class("bg-gray-400 active:bg-gray-300"), when: guess.status == .unknown)
116+
.attributes(.class("bg-gray-600 active:bg-gray-500"), when: guess.status == .notInWord)
117+
.attributes(.class("bg-yellow-600 active:bg-yellow-500"), when: guess.status == .inWord)
118+
.attributes(.class("bg-green-600 active:bg-green-500"), when: guess.status == .correctPosition)
105119
.onClick { _ in
106120
onKeyPressed(.letter(guess.letter))
107121
}
@@ -115,7 +129,8 @@ struct EnterKeyView: View {
115129
button(.class("flex justify-center items-center w-12 h-10 p-2 rounded-sm")) {
116130
img(.src("enter.svg"))
117131
}
118-
.attributes(.class("bg-gray-400"))
132+
.enabledMobileActive()
133+
.attributes(.class("bg-gray-400 active:bg-gray-300"))
119134
.onClick { _ in
120135
onKeyPressed(.enter)
121136
}
@@ -129,7 +144,8 @@ struct BackspaceKeyView: View {
129144
button(.class("flex justify-center items-center w-12 h-10 p-1 rounded-sm")) {
130145
img(.src("backspace.svg"))
131146
}
132-
.attributes(.class("bg-gray-400"))
147+
.enabledMobileActive()
148+
.attributes(.class("bg-gray-400 active:bg-gray-300"))
133149
.onClick { _ in
134150
onKeyPressed(.backspace)
135151
}
@@ -143,7 +159,7 @@ struct GameEndOverlay: View {
143159
var content: some View {
144160
if game.state != .playing {
145161
div(.class("absolute inset-0 bg-black bg-opacity-50 flex items-center justify-center")) {
146-
div(.class("bg-gray-600 p-5 rounded-md flex flex-col gap-4 items-center w-full mx-2")) {
162+
div(.class("bg-gray-600 p-5 rounded-md flex flex-col gap-4 items-center w-full mx-2 shadow-lg")) {
147163
h1(.class("text-xl uppercase tracking-wider")) {
148164
game.state == .won ? "Nice job!" : "Oh no!"
149165
}
@@ -157,3 +173,9 @@ struct GameEndOverlay: View {
157173
}
158174
}
159175
}
176+
177+
extension View where Tag == HTMLTag.button {
178+
func enabledMobileActive() -> _AttributedElement<Self> {
179+
attributes(.custom(name: "ontouchstart", value: ""))
180+
}
181+
}

0 commit comments

Comments
 (0)