Skip to content

Commit

Permalink
Check minWidth/Height == nil (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri authored Jul 9, 2021
1 parent 2efa80a commit 30f55d9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Sources/TokamakCore/Modifiers/FlexFrameLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public struct _FlexFrameLayout: ViewModifier {
// These are special cases in SwiftUI, where the child
// will request the entire width/height of the parent.
public var fillWidth: Bool {
minWidth == 0 && maxWidth == .infinity
(minWidth == 0 || minWidth == nil) && maxWidth == .infinity
}

public var fillHeight: Bool {
minHeight == 0 && maxHeight == .infinity
(minHeight == 0 || minHeight == nil) && maxHeight == .infinity
}

init(
Expand Down
14 changes: 8 additions & 6 deletions Sources/TokamakCore/Views/Containers/List/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ public struct List<SelectionValue, Content>: View
@_spi(TokamakCore)
public var body: some View {
if let style = style as? ListStyleDeferredToRenderer {
style.listBody(ScrollView {
HStack { Spacer() }
listStack
.environment(\._outlineGroupStyle, _ListOutlineGroupStyle())
})
.frame(minHeight: 0, maxHeight: .infinity)
ScrollView {
style.listBody(Group {
HStack { Spacer() }
listStack
.environment(\._outlineGroupStyle, _ListOutlineGroupStyle())
})
}
.frame(maxHeight: .infinity, alignment: .topLeading)
} else {
ScrollView {
HStack { Spacer() }
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakCore/Views/Containers/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension Section: View, SectionView where Parent: View, Content: View, Footer:
sectionContent(style)
footerView(style)
}
.frame(minWidth: 0, maxWidth: .infinity)
.frame(maxWidth: .infinity, alignment: .leading)
)
}
}
Expand Down
28 changes: 28 additions & 0 deletions Sources/TokamakStaticHTML/Modifiers/LayoutModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,28 @@ private extension DOMViewModifier {
}
}

private extension VerticalAlignment {
var flexAlignment: String {
switch self {
case .top: return "flex-start"
case .center: return "center"
case .bottom: return "flex-end"
}
}
}

private extension HorizontalAlignment {
var flexAlignment: String {
switch self {
case .leading: return "flex-start"
case .center: return "center"
case .trailing: return "flex-end"
}
}
}

extension _FrameLayout: DOMViewModifier {
public var isOrderDependent: Bool { true }
public var attributes: [HTMLAttribute: String] {
["style": """
\(unwrapToStyle(\.width, property: "width"))
Expand All @@ -43,11 +64,15 @@ extension _FrameLayout: DOMViewModifier {
white-space: nowrap;
flex-grow: 0;
flex-shrink: 0;
display: flex;
align-items: \(alignment.vertical.flexAlignment);
justify-content: \(alignment.horizontal.flexAlignment);
"""]
}
}

extension _FlexFrameLayout: DOMViewModifier {
public var isOrderDependent: Bool { true }
public var attributes: [HTMLAttribute: String] {
["style": """
\(unwrapToStyle(\.minWidth, property: "min-width"))
Expand All @@ -61,6 +86,9 @@ extension _FlexFrameLayout: DOMViewModifier {
white-space: nowrap;
flex-grow: 0;
flex-shrink: 0;
display: flex;
align-items: \(alignment.vertical.flexAlignment);
justify-content: \(alignment.horizontal.flexAlignment);
"""]
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/TokamakStaticHTML/Views/Containers/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ extension InsetGroupedListStyle: ListStyleDeferredToRenderer {
.font(.caption)
.padding([.top, .leading])
.padding(.leading)
.frame(minWidth: 0, maxWidth: .infinity)
.frame(maxWidth: .infinity, alignment: .leading)
)
}

Expand All @@ -144,7 +144,7 @@ extension InsetGroupedListStyle: ListStyleDeferredToRenderer {
.background(Color.listGroupBackground)
.cornerRadius(10)
.padding([.horizontal, .top])
.frame(minWidth: 0, maxWidth: .infinity)
.frame(maxWidth: .infinity, alignment: .leading)
)
}

Expand Down Expand Up @@ -180,7 +180,7 @@ extension SidebarListStyle: ListStyleDeferredToRenderer {
}

public func listRow<Row>(_ row: Row) -> AnyView where Row: View {
AnyView(row.frame(minWidth: 0, maxWidth: .infinity))
AnyView(row.frame(maxWidth: .infinity, alignment: .leading))
}

public func listBody<ListBody>(_ content: ListBody) -> AnyView where ListBody: View {
Expand Down
10 changes: 10 additions & 0 deletions Tests/TokamakStaticHTMLTests/RenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ final class RenderingTests: XCTestCase {
timeout: defaultSnapshotTimeout
)
}

func testFrames() {
assertSnapshot(
matching: Color.red
.frame(width: 20, height: 20)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing),
as: .image(size: .init(width: 50, height: 50)),
timeout: defaultSnapshotTimeout
)
}
}

#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 30f55d9

Please # to comment.