Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 943b7e8

Browse files
Fixed #287: Added Nav.openUrl to open in a new tab
1 parent 8d883be commit 943b7e8

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

sandbox-zio/src/main/scala/example/SandboxZIO.scala

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ import scala.scalajs.js.annotation.*
1212
@JSExportTopLevel("TyrianApp")
1313
object SandboxZIO extends TyrianZIOApp[Msg, Model]:
1414

15-
def router: Location => Msg = Routing.externalOnly(Msg.NoOp, Msg.FollowLink(_))
15+
def router: Location => Msg =
16+
Routing.externalOnly(
17+
Msg.NoOp,
18+
_ match {
19+
case url if url.contains("ultra") =>
20+
Msg.FollowLink(url, true)
21+
22+
case url =>
23+
Msg.FollowLink(url, false)
24+
}
25+
)
1626

1727
def init(flags: Map[String, String]): (Model, Cmd[Task, Msg]) =
1828
(Model.init, Cmd.None)
@@ -21,7 +31,10 @@ object SandboxZIO extends TyrianZIOApp[Msg, Model]:
2131
case Msg.NoOp =>
2232
(model, Cmd.None)
2333

24-
case Msg.FollowLink(href) =>
34+
case Msg.FollowLink(href, openInNewTab) if openInNewTab =>
35+
(model, Nav.openUrl(href))
36+
37+
case Msg.FollowLink(href, openInNewTab) =>
2538
(model, Nav.loadUrl(href))
2639

2740
case Msg.NewRandomInt(i) =>
@@ -64,9 +77,11 @@ object SandboxZIO extends TyrianZIOApp[Msg, Model]:
6477
div(
6578
div(hidden(false))("Random number: " + model.randomNumber.toString),
6679
div(
67-
a(href := "/another-page")("Internal link (will be ignored)"),
80+
a(href := "/another-page")(s"Internal${_nbsp_}link (will be ignored)"),
81+
br,
82+
a(href := "http://tyrian.indigoengine.io/")("Tyrian website (opens in current window)"),
6883
br,
69-
a(href := "http://tyrian.indigoengine.io/")("Tyrian website")
84+
a(href := "http://ultraviolet.indigoengine.io/")(text("Tyrian website"), _nbsp_, text("(opens in new tab)"))
7085
),
7186
div(
7287
input(placeholder := "Text to reverse", onInput(s => Msg.NewContent(s)), myStyle),
@@ -164,7 +179,7 @@ enum Msg:
164179
case Remove
165180
case Modify(i: Int, msg: Counter.Msg)
166181
case NewRandomInt(i: Int)
167-
case FollowLink(href: String)
182+
case FollowLink(href: String, newTab: Boolean)
168183
case NoOp
169184

170185
object Counter:

tyrian/src/main/scala/tyrian/Nav.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ object Nav:
3030
window.location.href = href
3131
}
3232

33+
/** Open's the link in a new tab / window, using the `_blank` target.
34+
*/
35+
def openUrl[F[_]: Async](href: String): Cmd[F, Nothing] =
36+
Cmd.SideEffect {
37+
window.open(href, "_blank")
38+
}
39+
3340
/** Update the address bar location with a new url. Should be used in conjunction with Tyrian's frontend routing so
3441
* that when your model decides to change pages, you can update the browser accordingly.
3542
*/

0 commit comments

Comments
 (0)