-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Creating new `FluentButtonToggleStyle` * Simplify corner radius calculation for buttons * Typos, GlobalTokens, and renaming Presssed -> On
- Loading branch information
1 parent
e7dd82f
commit 585aa32
Showing
5 changed files
with
114 additions
and
28 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// `ToggleStyle` which configures the `Toggle` according to its state and design tokens. | ||
public struct FluentButtonToggleStyle: ToggleStyle { | ||
public init() {} | ||
|
||
@Environment(\.fluentTheme) private var fluentTheme: FluentTheme | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
SwiftUI.Button(action: { | ||
configuration.isOn.toggle() | ||
}, label: { | ||
configuration.label | ||
}) | ||
.buttonStyle(configuration.isOn ? buttonStyleOn : buttonStyle) | ||
} | ||
|
||
private var buttonStyle: FluentButtonStyle { | ||
var style = FluentButtonStyle(style: .transparentNeutral) | ||
style.overrideTokens(buttonTokens) | ||
return style | ||
} | ||
|
||
private var buttonStyleOn: FluentButtonStyle { | ||
var style = FluentButtonStyle(style: .subtle) | ||
style.overrideTokens(buttonOnTokens) | ||
return style | ||
} | ||
|
||
private var buttonTokens: [ButtonToken: ControlTokenValue] { | ||
[ | ||
.cornerRadius: .float { GlobalTokens.corner(.radius40) } | ||
] | ||
} | ||
|
||
private var buttonOnTokens: [ButtonToken: ControlTokenValue] { | ||
let backgroundColor = fluentTheme.color(.brandBackgroundTint) | ||
return buttonTokens.merging([ | ||
.backgroundColor: .uiColor { backgroundColor }, | ||
.backgroundPressedColor: .uiColor { backgroundColor }, | ||
.backgroundFocusedColor: .uiColor { backgroundColor } | ||
]) { (_, new) in new } | ||
} | ||
} |