mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 03:06:37 +00:00
#43: Add the DesignKit package to the project.
* Add ElementTextFieldStyle and static style properties. * Rename button styles, support sizes and simplify colour.
This commit is contained in:
+28
-16
@@ -17,32 +17,44 @@
|
||||
import SwiftUI
|
||||
import DesignTokens
|
||||
|
||||
public struct PrimaryActionButtonStyle: ButtonStyle {
|
||||
public extension ButtonStyle where Self == ElementActionButtonStyle {
|
||||
/// The CTA button style as defined in Compound.
|
||||
/// - Parameter size: The control size to use. Defaults to regular.
|
||||
/// - Parameter color: The color of the button's background. Defaults to the accent color.
|
||||
static func elementAction(_ size: ElementControlSize = .regular,
|
||||
color: Color = .element.accent) -> ElementActionButtonStyle {
|
||||
ElementActionButtonStyle(size: size, color: color)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ElementActionButtonStyle: ButtonStyle {
|
||||
@Environment(\.isEnabled) private var isEnabled
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
|
||||
public var customColor: Color?
|
||||
public var size: ElementControlSize
|
||||
public var color: Color
|
||||
|
||||
private var verticalPadding: CGFloat { size == .xLarge ? 12 : 4 }
|
||||
private var maxWidth: CGFloat? { size == .xLarge ? .infinity : nil }
|
||||
|
||||
private var fontColor: Color {
|
||||
// Always white unless disabled with a dark theme.
|
||||
.white.opacity(colorScheme == .dark && !isEnabled ? 0.3 : 1.0)
|
||||
}
|
||||
|
||||
private var backgroundColor: Color {
|
||||
customColor ?? .element.accent
|
||||
}
|
||||
|
||||
public init(customColor: Color? = nil) {
|
||||
self.customColor = customColor
|
||||
public init(size: ElementControlSize = .regular, color: Color = .element.accent) {
|
||||
self.size = size
|
||||
self.color = color
|
||||
}
|
||||
|
||||
public func makeBody(configuration: Self.Configuration) -> some View {
|
||||
configuration.label
|
||||
.padding(12.0)
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, verticalPadding)
|
||||
.frame(maxWidth: maxWidth)
|
||||
.foregroundColor(fontColor)
|
||||
.font(.element.body)
|
||||
.background(backgroundColor.opacity(backgroundOpacity(when: configuration.isPressed)))
|
||||
.background(color.opacity(backgroundOpacity(when: configuration.isPressed)))
|
||||
.cornerRadius(8.0)
|
||||
}
|
||||
|
||||
@@ -52,24 +64,24 @@ public struct PrimaryActionButtonStyle: ButtonStyle {
|
||||
}
|
||||
}
|
||||
|
||||
public struct PrimaryActionButtonStyle_Previews: PreviewProvider {
|
||||
public struct ElementActionButtonStyle_Previews: PreviewProvider {
|
||||
public static var states: some View {
|
||||
VStack {
|
||||
Button("Enabled") { /* preview */ }
|
||||
.buttonStyle(PrimaryActionButtonStyle())
|
||||
.buttonStyle(ElementActionButtonStyle())
|
||||
|
||||
Button("Disabled") { /* preview */ }
|
||||
.buttonStyle(PrimaryActionButtonStyle())
|
||||
.buttonStyle(ElementActionButtonStyle())
|
||||
.disabled(true)
|
||||
|
||||
Button { /* preview */ } label: {
|
||||
Text("Clear BG")
|
||||
.foregroundColor(.element.alert)
|
||||
}
|
||||
.buttonStyle(PrimaryActionButtonStyle(customColor: .clear))
|
||||
.buttonStyle(ElementActionButtonStyle(color: .clear))
|
||||
|
||||
Button("Red BG") { /* preview */ }
|
||||
.buttonStyle(PrimaryActionButtonStyle(customColor: .element.alert))
|
||||
.buttonStyle(ElementActionButtonStyle(color: .element.alert))
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import DesignTokens
|
||||
|
||||
public extension ButtonStyle where Self == ElementGhostButtonStyle {
|
||||
/// The Ghost button style as defined in Compound.
|
||||
/// - Parameter size: The control size to use. Defaults to `regular`.
|
||||
/// - Parameter color: The color of the label and border. Defaults to the accent color.
|
||||
static func elementGhost(_ size: ElementControlSize = .regular,
|
||||
color: Color = .element.accent) -> ElementGhostButtonStyle {
|
||||
ElementGhostButtonStyle(size: size, color: color)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ElementGhostButtonStyle: ButtonStyle {
|
||||
@Environment(\.isEnabled) private var isEnabled
|
||||
|
||||
public var size: ElementControlSize
|
||||
public var color: Color
|
||||
|
||||
private var verticalPadding: CGFloat { size == .xLarge ? 12 : 4 }
|
||||
private var maxWidth: CGFloat? { size == .xLarge ? .infinity : nil }
|
||||
|
||||
public init(size: ElementControlSize = .regular, color: Color = .element.accent) {
|
||||
self.size = size
|
||||
self.color = color
|
||||
}
|
||||
|
||||
public func makeBody(configuration: Self.Configuration) -> some View {
|
||||
configuration.label
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, verticalPadding)
|
||||
.frame(maxWidth: maxWidth)
|
||||
.foregroundColor(color)
|
||||
.font(.element.body)
|
||||
.background(border)
|
||||
.opacity(opacity(when: configuration.isPressed))
|
||||
}
|
||||
|
||||
private var border: some View {
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.strokeBorder()
|
||||
.foregroundColor(color)
|
||||
}
|
||||
|
||||
private func opacity(when isPressed: Bool) -> CGFloat {
|
||||
guard isEnabled else { return 0.6 }
|
||||
return isPressed ? 0.6 : 1.0
|
||||
}
|
||||
}
|
||||
|
||||
public struct ElementGhostButtonStyle_Previews: PreviewProvider {
|
||||
public static var previews: some View {
|
||||
Group {
|
||||
states
|
||||
}
|
||||
.preferredColorScheme(.light)
|
||||
Group {
|
||||
states
|
||||
}
|
||||
.preferredColorScheme(.dark)
|
||||
}
|
||||
|
||||
public static var states: some View {
|
||||
VStack {
|
||||
Button("Enabled") { /* preview */ }
|
||||
.buttonStyle(ElementGhostButtonStyle())
|
||||
|
||||
Button("Disabled") { /* preview */ }
|
||||
.buttonStyle(ElementGhostButtonStyle())
|
||||
.disabled(true)
|
||||
|
||||
Button("Red BG") { /* preview */ }
|
||||
.buttonStyle(ElementGhostButtonStyle(color: .element.alert))
|
||||
|
||||
Button { /* preview */ } label: {
|
||||
Text("Custom")
|
||||
.foregroundColor(.element.primaryContent)
|
||||
}
|
||||
.buttonStyle(ElementGhostButtonStyle(color: .element.quaternaryContent))
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import DesignTokens
|
||||
|
||||
public struct SecondaryActionButtonStyle: ButtonStyle {
|
||||
@Environment(\.isEnabled) private var isEnabled
|
||||
|
||||
public var customColor: Color?
|
||||
|
||||
public init(customColor: Color? = nil) {
|
||||
self.customColor = customColor
|
||||
}
|
||||
|
||||
public func makeBody(configuration: Self.Configuration) -> some View {
|
||||
configuration.label
|
||||
.padding(12.0)
|
||||
.frame(maxWidth: .infinity)
|
||||
.foregroundColor(strokeColor(configuration.isPressed))
|
||||
.font(.element.body)
|
||||
.background(RoundedRectangle(cornerRadius: 8)
|
||||
.strokeBorder()
|
||||
.foregroundColor(strokeColor(configuration.isPressed)))
|
||||
.opacity(isEnabled ? 1.0 : 0.6)
|
||||
}
|
||||
|
||||
private func strokeColor(_ isPressed: Bool) -> Color {
|
||||
if let customColor = customColor {
|
||||
return customColor
|
||||
}
|
||||
|
||||
return isPressed ? .element.accent.opacity(0.6) : .element.accent
|
||||
}
|
||||
}
|
||||
|
||||
public struct SecondaryActionButtonStyle_Previews: PreviewProvider {
|
||||
public static var previews: some View {
|
||||
Group {
|
||||
states
|
||||
}
|
||||
.preferredColorScheme(.light)
|
||||
Group {
|
||||
states
|
||||
}
|
||||
.preferredColorScheme(.dark)
|
||||
}
|
||||
|
||||
public static var states: some View {
|
||||
VStack {
|
||||
Button("Enabled") { /* preview */ }
|
||||
.buttonStyle(SecondaryActionButtonStyle())
|
||||
|
||||
Button("Disabled") { /* preview */ }
|
||||
.buttonStyle(SecondaryActionButtonStyle())
|
||||
.disabled(true)
|
||||
|
||||
Button { /* preview */ } label: {
|
||||
Text("Clear BG")
|
||||
.foregroundColor(.element.alert)
|
||||
}
|
||||
.buttonStyle(SecondaryActionButtonStyle(customColor: .clear))
|
||||
|
||||
Button("Red BG") { /* preview */ }
|
||||
.buttonStyle(SecondaryActionButtonStyle(customColor: .element.alert))
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user