SwepUI is an experimental project that intend to mimic SwiftUI functionality on the web using SwiftWasm. As of now it only has the reusability and encapsulation of the SwiftUI Views. But it missing a lot of features that SwiftUI has. For example: Modifiers, Layout, etc. However, you have full control over the css styling.
A live example of SwepUI is available at Github Pages
import SwepUI
struct MyApp: App {
var body: some Scene {
WindowGroup {
MyView()
.display(.flex)
.alignItems(.center)
.justifyContent(.center)
.height(.vh(100))
.width(.vw(100))
.minWidth(100%)
.minHeight(100%)
}
}
}
struct MyView: View {
@State var counter: Int = 0
@State var darkMode: Bool = false
var body: some View {
Div("counter = \(counter)")
.color(darkMode ? .white : .tomato)
.backgroundColor(darkMode ? .tomato : .white)
.fontSize(.px(100))
.fontFamily("Helvetica")
.cursor(.pointer)
.onClick { _ in
counter += 1
darkMode.toggle()
print(counter)
}
}
}
// Run the webapp
MyApp.main()- Mimic SwiftUI Encapsulation
- Being able to interact with the CSS style from outside its
bodyas following:MyCustomView().backgroundColor(.red) - Mimic SwiftUI Modifiers
- Mimic SwiftUI Layout
In order to use SwepUI, you need to have the following:
SwiftWasminstalledCartoninstalled
NOTE: Installing
Cartonwill also installSwiftWasmout of the box. Read more about the installation process here.
As mentioned earlier SwepUI require carton. You'll install carton via Homebrew. Assuming you already have Homebrew installed, you can create a new SwepUI app by following these steps:
- Install
carton:
brew install swiftwasm/tap/cartonIf you had carton installed before this, make sure you have version 0.16.0 or greater:
carton --version- Create a directory for your project and make it current:
mkdir MySwepUIApp && cd MySwepUIApp- Initialize the project from a template with
carton:
carton init --template basic- Add
SwepUIas dependency package to your project:
dependencies: [
.package(url: "https://github.com/alja7dali/swift-web-page-ui.git", from: "0.0.2")
]- Add
SwepUIas dependency target toMySwepUIApp:
targets: [
.target(
name: "MySwepUIApp",
dependencies: [
.product(name: "SwepUI", package: "swift-web-page-ui"),
]
),
]- Build the project and start the development server,
carton devcan be kept running during development:
carton dev-
Open http://127.0.0.1:8080/ in your browser to see the app running. You can edit the app source code in your favorite editor and save it,
cartonwill immediately rebuild the app and reload all browser tabs that have the app open. -
After you are done with the development, you can bundle the app for production use by running:
carton bundleNOTE: You can also clone this repository and run
carton dev --product SwepUIExamplein its root directory. This will build the demo app that shows almost all of the currently implemented APIs. Or check out the live example based onSwepUIExampleis available at Github Pages
Special thanks to the team and contributors behind SwiftWasm & Carton
All modules are released under the MIT license. See LICENSE for details.