-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Introduce
LuminareToggleCompose
& LuminareButtonCompose
- Loading branch information
Showing
18 changed files
with
282 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
Sources/Luminare/Components/Compose/LuminareButtonCompose.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// | ||
// LuminareButtonCompose.swift | ||
// Luminare | ||
// | ||
// Created by KrLite on 2024/12/17. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// MARK: - Button Compose | ||
|
||
struct LuminareButtonCompose<Label, Content>: View where Label: View, Content: View { | ||
// MARK: Fields | ||
|
||
private let role: ButtonRole? | ||
@ViewBuilder private var label: () -> Label | ||
@ViewBuilder private var content: () -> Content | ||
private let action: () -> () | ||
|
||
// MARK: Initializers | ||
|
||
public init( | ||
role: ButtonRole? = nil, | ||
@ViewBuilder label: @escaping () -> Label, | ||
@ViewBuilder content: @escaping () -> Content, | ||
action: @escaping () -> () | ||
) { | ||
self.role = role | ||
self.label = label | ||
self.content = content | ||
self.action = action | ||
} | ||
|
||
public init( | ||
_ titleKey: LocalizedStringKey, | ||
role: ButtonRole? = nil, | ||
@ViewBuilder content: @escaping () -> Content, | ||
action: @escaping () -> () | ||
) where Label == Text { | ||
self.init( | ||
role: role | ||
) { | ||
Text(titleKey) | ||
} content: { | ||
content() | ||
} action: { | ||
action() | ||
} | ||
} | ||
|
||
public init( | ||
_ contentKey: LocalizedStringKey, | ||
role: ButtonRole? = nil, | ||
@ViewBuilder label: @escaping () -> Label, | ||
action: @escaping () -> () | ||
) where Content == Text { | ||
self.init( | ||
role: role, | ||
label: label | ||
) { | ||
Text(contentKey) | ||
} action: { | ||
action() | ||
} | ||
} | ||
|
||
public init( | ||
_ titleKey: LocalizedStringKey, | ||
_ contentKey: LocalizedStringKey, | ||
role: ButtonRole? = nil, | ||
action: @escaping () -> () | ||
) where Label == Text, Content == Text { | ||
self.init( | ||
role: role | ||
) { | ||
Text(titleKey) | ||
} content: { | ||
Text(contentKey) | ||
} action: { | ||
action() | ||
} | ||
} | ||
|
||
// MARK: Body | ||
|
||
var body: some View { | ||
LuminareCompose(contentMaxWidth: nil) { | ||
Button(role: role) { | ||
action() | ||
} label: { | ||
label() | ||
} | ||
.buttonStyle(.luminareCompact) | ||
} label: { | ||
label() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Preview | ||
|
||
@available(macOS 15.0, *) | ||
#Preview( | ||
"LuminareButtonCompose", | ||
traits: .sizeThatFitsLayout | ||
) { | ||
LuminareSection { | ||
LuminareButtonCompose("Button", "Click Me!") { | ||
print(1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Sources/Luminare/Components/Compose/LuminareToggleCompose.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// LuminareToggleCompose.swift | ||
// Luminare | ||
// | ||
// Created by KrLite on 2024/12/17. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// MARK: - Toggle Compose | ||
|
||
struct LuminareToggleCompose<Label>: View where Label: View { | ||
// MARK: Environments | ||
|
||
@Environment(\.luminareComposeControlSize) private var controlSize | ||
|
||
// MARK: Fields | ||
|
||
@Binding private var value: Bool | ||
@ViewBuilder private var label: () -> Label | ||
|
||
// MARK: Initializers | ||
|
||
public init( | ||
isOn value: Binding<Bool>, | ||
@ViewBuilder label: @escaping () -> Label | ||
) { | ||
self._value = value | ||
self.label = label | ||
} | ||
|
||
public init( | ||
_ key: LocalizedStringKey, | ||
isOn value: Binding<Bool> | ||
) where Label == Text { | ||
self.init(isOn: value) { | ||
Text(key) | ||
} | ||
} | ||
|
||
// MARK: Body | ||
|
||
var body: some View { | ||
LuminareCompose(contentMaxWidth: nil) { | ||
Toggle("", isOn: $value) | ||
.labelsHidden() | ||
.toggleStyle(.switch) | ||
.controlSize(controlSize.proposal ?? .small) | ||
} label: { | ||
label() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Preview | ||
|
||
@available(macOS 15.0, *) | ||
#Preview( | ||
"LuminareToggleCompose", | ||
traits: .sizeThatFitsLayout | ||
) { | ||
@Previewable @State var value = false | ||
|
||
LuminareSection { | ||
LuminareToggleCompose("Toggle", isOn: $value) | ||
} | ||
} |
Oops, something went wrong.