Files
iOS/Sources/Shared/DesignSystem/Components/CloseButton.swift
Bruno Pantaleão Gonçalves f93822c430 Fix close button style and sizes for macOS (#3609)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2025-05-21 12:09:17 +02:00

79 lines
2.0 KiB
Swift

import SwiftUI
public struct CloseButton: View {
public enum Size {
case small
case medium
case large
var size: CGFloat {
if Current.isCatalyst {
switch self {
case .small:
return 24
case .medium:
return 28
case .large:
return 32
}
} else {
switch self {
case .small:
return 20
case .medium:
return 24
case .large:
return 28
}
}
}
}
@Environment(\.dismiss) private var dismiss
private let alternativeAction: (() -> Void)?
private let tint: Color
private let size: Size
/// When alternative action is set, the button will execute this action instead of dismissing the view.
public init(
tint: Color = Color.gray,
size: Size = .small,
alternativeAction: (() -> Void)? = nil
) {
self.alternativeAction = alternativeAction
self.tint = tint
self.size = size
}
public var body: some View {
Button(action: {
if let alternativeAction {
alternativeAction()
} else {
dismiss()
}
}, label: {
Image(systemSymbol: .xmarkCircleFill)
.resizable()
.frame(width: size.size, height: size.size)
.foregroundStyle(tint)
})
.buttonStyle(.plain)
}
}
#Preview {
VStack {
CloseButton {}
.frame(maxWidth: .infinity, alignment: .trailing)
.padding()
CloseButton(size: .medium) {}
.frame(maxWidth: .infinity, alignment: .trailing)
.padding()
CloseButton(size: .large) {}
.frame(maxWidth: .infinity, alignment: .trailing)
.padding()
Spacer()
}
}