Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [CustomerCenter] Introduce CompatibilityLabeledContent #4659

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// CompatibilityLabeledContent.swift
//
//
// Created by Facundo Menzella on 14/1/25.
//

import SwiftUI

#if os(iOS)

/// A SwiftUI view for displaying labeled content
@available(iOS 15.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
struct CompatibilityLabeledContent<Label: View, Content: View>: View {

let label: () -> Label
let content: (() -> Content)?

var body: some View {
if #available(iOS 16.0, *) {
LabeledContent {
if let content {
content()
}
} label: {
label()
}
} else {
HStack {
label()

if let content {
Spacer()
content()
}
}
}
}
}

@available(iOS 15.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
extension CompatibilityLabeledContent where Label == Text {
init(_ label: String, @ViewBuilder content: @escaping () -> Content) {
self.label = { Text(label) }
self.content = content
}
}

@available(iOS 15.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
extension CompatibilityLabeledContent where Label == Text, Content == EmptyView {
init(_ label: String) {
self.label = { Text(label) }
self.content = nil
}
}

#endif