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

Add the DesignKit package to the project. #64

Merged
merged 6 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -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)
}

Expand All @@ -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()
}
Expand Down
99 changes: 99 additions & 0 deletions DesignKit/Sources/Buttons/ElementGhostButtonStyle.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
82 changes: 0 additions & 82 deletions DesignKit/Sources/Buttons/SecondaryActionButtonStyle.swift

This file was deleted.

22 changes: 22 additions & 0 deletions DesignKit/Sources/Common/ElementControlSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// 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 Foundation

public enum ElementControlSize {
case regular
case xLarge
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

import SwiftUI

struct RoundedCornerShape: Shape {

public struct RoundedCornerShape: Shape {
let radius: CGFloat
let corners: UIRectCorner

func path(in rect: CGRect) -> Path {

public init(radius: CGFloat, corners: UIRectCorner) {
self.radius = radius
self.corners = corners
}

public func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
return Path(path.cgPath)
}
Expand Down
34 changes: 18 additions & 16 deletions DesignKit/Sources/TextFields/BorderedInputFieldStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,15 @@ public struct BorderedInputFieldStyle: TextFieldStyle {

public var isEditing: Bool
public var isError: Bool
public var returnKey: UIReturnKeyType?

private var borderColor: Color {
if isError {
return .element.alert
} else if isEditing {
return .element.accent
} else {
return .element.quinaryContent
}
guard !isError else { return .element.alert }
return isEditing ? .element.accent : .element.quinaryContent
}

private var accentColor: Color {
if isError {
return .element.alert
}
return .element.accent
isError ? .element.alert : .element.accent
}

private var textColor: Color {
Expand All @@ -62,16 +55,22 @@ public struct BorderedInputFieldStyle: TextFieldStyle {
}

private var placeholderColor: Color {
return .element.tertiaryContent
.element.tertiaryContent
}

private var borderWidth: CGFloat {
return isEditing || isError ? 2.0 : 1.5
isEditing || isError ? 2.0 : 1.5
}

public init(isEditing: Bool = false, isError: Bool = false) {
/// Creates the text field style configured as required.
/// - Parameters:
/// - isEditing: Whether or not the text field is currently being edited.
/// - isError: Whether or not the text field is currently in the error state.
/// - returnKey: The return key to be used. Pass `nil` for iOS 15+ and use `.submitLabel` instead.
public init(isEditing: Bool = false, isError: Bool = false, returnKey: UIReturnKeyType? = .done) {
self.isEditing = isEditing
self.isError = isError
self.returnKey = returnKey
}

public func _body(configuration: TextField<_Label>) -> some View {
Expand All @@ -80,13 +79,16 @@ public struct BorderedInputFieldStyle: TextFieldStyle {
.font(.element.callout)
.foregroundColor(textColor)
.accentColor(accentColor)
.frame(height: 48.0)
.padding(.vertical, 12.0)
pixlwave marked this conversation as resolved.
Show resolved Hide resolved
.padding(.horizontal, 8.0)
.background(backgroundColor)
.clipShape(rect)
.overlay(rect.stroke(borderColor, lineWidth: borderWidth))
.introspectTextField { textField in
textField.returnKeyType = .done
if let returnKey = returnKey {
textField.returnKeyType = returnKey
}

textField.clearButtonMode = .whileEditing
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder ?? "",
attributes: [NSAttributedString.Key.foregroundColor: UIColor(placeholderColor)])
Expand Down
Loading