Skip to content

Commit

Permalink
#43: Add DesignKit package
Browse files Browse the repository at this point in the history
* Begin implementing DesignKit package.
* Use element-design-tokens repo.
* Rename Fonts to align with Colours.
  • Loading branch information
pixlwave authored May 26, 2022
1 parent 8fec972 commit edf765b
Show file tree
Hide file tree
Showing 14 changed files with 812 additions and 1 deletion.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ xcuserdata/
*.dSYM.zip
*.dSYM

## SwiftPM
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
Expand All @@ -26,4 +35,4 @@ Tools/Scripts/element-android

## macOS Files
.DS_Store
._*
._*
83 changes: 83 additions & 0 deletions DesignKit/Sources/Buttons/PrimaryActionButtonStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// 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 PrimaryActionButtonStyle: ButtonStyle {
@Environment(\.isEnabled) private var isEnabled
@Environment(\.colorScheme) private var colorScheme

public var customColor: Color?

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 func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding(12.0)
.frame(maxWidth: .infinity)
.foregroundColor(fontColor)
.font(.element.body)
.background(backgroundColor.opacity(backgroundOpacity(when: configuration.isPressed)))
.cornerRadius(8.0)
}

private func backgroundOpacity(when isPressed: Bool) -> CGFloat {
guard isEnabled else { return 0.3 }
return isPressed ? 0.6 : 1.0
}
}

public struct PrimaryActionButtonStyle_Previews: PreviewProvider {
public static var states: some View {
VStack {
Button("Enabled") { /* preview */ }
.buttonStyle(PrimaryActionButtonStyle())

Button("Disabled") { /* preview */ }
.buttonStyle(PrimaryActionButtonStyle())
.disabled(true)

Button { /* preview */ } label: {
Text("Clear BG")
.foregroundColor(.element.alert)
}
.buttonStyle(PrimaryActionButtonStyle(customColor: .clear))

Button("Red BG") { /* preview */ }
.buttonStyle(PrimaryActionButtonStyle(customColor: .element.alert))
}
.padding()
}

public static var previews: some View {
states
.preferredColorScheme(.light)
states
.preferredColorScheme(.dark)
}
}
82 changes: 82 additions & 0 deletions DesignKit/Sources/Buttons/SecondaryActionButtonStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// 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()
}
}
69 changes: 69 additions & 0 deletions DesignKit/Sources/Fonts/ElementFonts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// 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

public extension Font {
/// The fonts used by Element as defined in https://www.figma.com/file/X4XTH9iS2KGJ2wFKDqkyed/Compound?node-id=1362%3A0
static let element = ElementFonts(values: ElementSharedFonts())
}

/// Struct for holding fonts for use in SwiftUI.
public struct ElementFonts: Fonts {
public let largeTitle: Font
public let largeTitleB: Font
public let title1: Font
public let title1B: Font
public let title2: Font
public let title2B: Font
public let title3: Font
public let title3SB: Font
public let headline: Font
public let subheadline: Font
public let body: Font
public let bodySB: Font
public let callout: Font
public let calloutSB: Font
public let footnote: Font
public let footnoteSB: Font
public let caption1: Font
public let caption1SB: Font
public let caption2: Font
public let caption2SB: Font

public init(values: ElementSharedFonts) {
largeTitle = values.largeTitle.font
largeTitleB = values.largeTitleB.font
title1 = values.title1.font
title1B = values.title1B.font
title2 = values.title2.font
title2B = values.title2B.font
title3 = values.title3.font
title3SB = values.title3SB.font
headline = values.headline.font
subheadline = values.subheadline.font
body = values.body.font
bodySB = values.bodySB.font
callout = values.callout.font
calloutSB = values.calloutSB.font
footnote = values.footnote.font
footnoteSB = values.footnoteSB.font
caption1 = values.caption1.font
caption1SB = values.caption1SB.font
caption2 = values.caption2.font
caption2SB = values.caption2SB.font
}
}
Loading

0 comments on commit edf765b

Please sign in to comment.