Skip to content

Commit

Permalink
🪄 Implement LivePreview support
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
MihaelIsaev committed Dec 18, 2022
1 parent 14cb926 commit 0f50412
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "Autolayout", type: .static, targets: ["Autolayout"])
],
dependencies: [
.package(url: "https://github.com/swifweb/web", from: "1.0.0-beta.1.22.0")
.package(url: "https://github.com/swifweb/web", from: "1.0.0-beta.1.22.2")
],
targets: [
.target(name: "Autolayout", dependencies: [
Expand Down
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Add package to your SwifWeb app's `Package.swift`
In dependencies section
```swift
dependencies: [
.package(url: "https://github.com/swifweb/autolayout", from: "1.0.0")
.package(url: "https://github.com/swifweb/autolayout", from: "1.0.1")
]
```
In target section
Expand Down Expand Up @@ -778,3 +778,40 @@ Sets the opacity level for an element
// will set opacity to 0.5 only for extra-small, small and medium screens
.opacity(0.5, breakpoints: .xs, .s, m)
```

# Live preview

To make it work with live preview you need to specify either all styles or exact autolayout's one

#### With all app styles included
```swift
class Welcome_Preview: WebPreview {
override class var title: String { "Initial page" } // optional
override class var width: UInt { 440 } // optional
override class var height: UInt { 480 } // optional

@Preview override class var content: Preview.Content {
// add styles if needed
AppStyles.all
// add here as many elements as needed
WelcomeViewController()
}
}
```

#### With exact app styles including autoalyout's one
```swift
class Welcome_Preview: WebPreview {
override class var title: String { "Initial page" } // optional
override class var width: UInt { 440 } // optional
override class var height: UInt { 480 } // optional

@Preview override class var content: Preview.Content {
// add styles if needed
AppStyles.id(.mainStyle)
AppStyles.id(.autolayoutStyles)
// add here as many elements as needed
WelcomeViewController()
}
}
```'
9 changes: 8 additions & 1 deletion Sources/Autolayout/Autolayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ import ResizeObserverAPI

let _autolayout = Autolayout()

extension Id {
public static var autolayoutStyles: Id { "autolayout_styles" }
}

public class Autolayout {
let stylesheet = Stylesheet().id("autolayout_styles")
let stylesheet = Stylesheet().id(.autolayoutStyles)
var ruleIndexCache: [String: Int] = [:]

fileprivate init () {
#if WEBPREVIEW
WebApp.shared.stylesheets.append(stylesheet)
#endif
WebApp.shared.document.head.appendChild(stylesheet)
}

Expand Down

0 comments on commit 0f50412

Please sign in to comment.