Skip to content

Commit

Permalink
refactor: support self-sizing on SwiftUI
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxtynSong committed May 1, 2024
1 parent 377d208 commit f3dbb8d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sources/KarrotListKit/SwiftUISupport/ComponentRepresented.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ struct ComponentRepresented<C: Component>: UIViewRepresentable {
func makeCoordinator() -> C.Coordinator {
component.makeCoordinator()
}

func _overrideSizeThatFits(
_ size: inout CGSize,
in proposedSize: _ProposedSize,
uiView: C.Content
) {
size = uiView.sizeThatFits(
CGSize(
width: proposedSize.width ?? UIView.noIntrinsicMetric,
height: proposedSize.height ?? UIView.noIntrinsicMetric
)
)
}

#if swift(>=5.7)
@available(iOS 16.0, *)
func sizeThatFits(
_ proposal: ProposedViewSize,
uiView: C.Content,
context: Context
) -> CGSize? {
uiView.sizeThatFits(
CGSize(
width: proposal.width ?? UIView.noIntrinsicMetric,
height: proposal.height ?? UIView.noIntrinsicMetric
)
)
}
#endif
}

private extension SwiftUI._ProposedSize {

var width: CGFloat? {
Mirror(reflecting: self).children.first(where: { $0.label == "width" })?.value as? CGFloat
}

var height: CGFloat? {
Mirror(reflecting: self).children.first(where: { $0.label == "height" })?.value as? CGFloat
}
}

extension Component {
Expand Down

0 comments on commit f3dbb8d

Please sign in to comment.