Skip to content

Commit 6c9178c

Browse files
committed
Documentation
1 parent 8efc015 commit 6c9178c

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SwiftWebUI Router
1+
# [SwiftWebUI](https://github.com/carson-katri/SwiftWebUI) Router
22

33
A simple Router for SwiftWebUI:
44

@@ -39,3 +39,42 @@ module.exports = {
3939
};
4040
```
4141
Now the server will always serve your index.html, no matter the route.
42+
43+
# Docs
44+
45+
## `Router`
46+
A container for `Routes`.
47+
Uses `location.pathname` to resolve which `Route` to render:
48+
```swift
49+
Router {
50+
...
51+
}
52+
```
53+
54+
## `Route`
55+
A map from a path to a View
56+
Path can contains arguments, such as `/artists/:artistId/song/:songId`:
57+
```swift
58+
Route(to: "/artists/:artistId/song/:songId") { args in
59+
VStack {
60+
Text("Artist: \(args["artistId"])")
61+
Text("Song: \(args["songId"])")
62+
}
63+
}
64+
```
65+
66+
## Navigator
67+
A way to navigate without using Views:
68+
```swift
69+
Navigator.back()
70+
Navigator.navigateTo(["artists", "5"])
71+
Navigator.navigateTo("/artists/5")
72+
```
73+
74+
## RouterLink
75+
A button that navigates to the specified path
76+
```swift
77+
RouterLink(to: "/artists") {
78+
Text("Back")
79+
}
80+
```

Sources/Router/Navigator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
import JavaScriptKit
88

9+
/// A way to navigate without using Views
910
public struct Navigator {
1011
public static func back() {
1112
_ = JSObjectRef.global.history.object?.back?()

Sources/Router/Route.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import SwiftWebUI
88
import JavaScriptKit
99

10+
/// A map from a path to a View
11+
/// Path can contains arguments, such as `/artists/:artistId/song/:songId`
1012
public struct Route : View {
1113
let path: [String]
1214
let content: AnyView

Sources/Router/Router.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import SwiftWebUI
99
import JavaScriptKit
1010

11+
/// A container for `Route`s.
12+
/// Uses `location.pathname` to resolve which `Route` to render
1113
public struct Router : View {
1214
let path: [String]
1315
let routes: [Route]

Sources/Router/RouterLink.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import SwiftWebUI
99

10+
/// A button that navigates to the specified path
1011
public struct RouterLink<Content: View>: View {
1112
let destination: String
1213
let content: Content

0 commit comments

Comments
 (0)