Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.
Draft
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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
31 changes: 31 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "OpenMaterialUI",
platforms: [
.iOS(.v16),
.macOS(.v13),
.tvOS(.v16),
.watchOS(.v9)
],
products: [
.library(
name: "OpenMaterialUI",
targets: ["OpenMaterialUI"]
),
],
dependencies: [],
targets: [
.target(
name: "OpenMaterialUI",
dependencies: []
),
.testTarget(
name: "OpenMaterialUITests",
dependencies: ["OpenMaterialUI"]
),
]
)
78 changes: 78 additions & 0 deletions Sources/OpenMaterialUI/Button/FilledButtonView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// FilledButtonView.swift
//
//
// Created by Ahmed Shendy on 03/10/2023.
//

import SwiftUI

/// # Filled Button Specs:
/// https://m3.material.io/components/buttons/specs#5a8a88b0-f6fe-4bab-a6d7-c246c78586cb
/// ## Enabled State:
/// # Container
/// shape: md.sys.shape.corner.full -> Circular
/// height: 40dp
/// elevation: md.sys.elevation.level0 -> 0
/// shadowColor: md.sys.color.shadow -> md.ref.palette.neutral0 -> #000000
/// Color: md.sys.color.primary -> md.ref.palette.primary40 -> #6750A4
///
///
/// # LabelText
/// font: md.sys.typescale.label-large.font -> md.ref.typeface.plain -> Roboto
/// lineHeight: md.sys.typescale.label-large.line-height -> 20pt
/// size: md.sys.typescale.label-large.size -> 14pt
/// weight: md.sys.typescale.label-large.weight -> /// md.ref.typeface.weight-medium -> 500
/// tracking: md.sys.typescale.label-large.tracking -> 0.1pt
/// style: Label text type style
/// color: md.sys.color.on-primary -> md.ref.palette.primary100 -> #FFFFFF
///
/// # Icon
/// size: 18dp
/// color: md.ref.palette.primary100 -> #FFFFFF
public struct FilledButtonView: View {
private let text: String
private let systemImage: String?

private let action: () -> Void

public init(
text: String, systemImage: String? = nil,
action: @escaping () -> Void
) {
self.text = text
self.systemImage = systemImage
self.action = action
}

@MainActor
public var body: some View {
MaterialButton {
action()
} label: {
if let systemImage = systemImage {
Label(text, systemImage: systemImage)
} else {
Text(text)
}
}
.padding(.horizontal, 24)
.labelText(
font: .custom(
.mdSysTypescaleLabelLargeFamily,
size: .mdSysTypescaleLabelLargeSize
),
color: .mdSysColorOnPrimary
)
.container(
height: 40,
color: .mdSysColorPrimary
)
}
}

#Preview {
FilledButtonView(text: "Hello World") {
print("filled button tapped")
}
}
94 changes: 94 additions & 0 deletions Sources/OpenMaterialUI/Button/MaterialButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// MaterialButton.swift
//
//
// Created by Ahmed Shendy on 20/06/2023.
//

import SwiftUI

/**
Specs - Default values (enabled state): https://m3.material.io/components/buttons/specs#a0952e51-6975-486e-8d14-c78283c29cae
*/

public struct MaterialButton<Label>: View where Label: View {
@Environment(\.colorScheme) var colorScheme

private let action: () -> Void
private let labelBuilder: () -> Label

public init(
action: @escaping () -> Void,
@ViewBuilder label labelBuilder: @escaping () -> Label
) {
self.action = action
self.labelBuilder = labelBuilder
}

@MainActor
public var body: some View {
Button {
action()
} label: {
labelBuilder()
}
}
}

struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
MaterialButton {

} label: {
Label("Hello", systemImage: "house")
}
.previewLayout(.sizeThatFits)
.preferredColorScheme(.dark)
}
}

//extension Color {
// static var windowScene: UIWindowScene {
// UIApplication.windowScene!
// }
// static var systemColorPrimary: Color {
// switch windowScene.screen.traitCollection.userInterfaceStyle {
// case .dark:
// return Color(hex: 0xD0BCFF)
// case .light:
// return Color(hex: 0x6750A4)
// default:
// return Color(hex: 0xD0BCFF)
// }
// }
//
// init(hex: UInt32, alpha: Double = 1) {
// self.init(
// .sRGB,
// red: Double((hex >> 16) & 0xFF) / 255,
// green: Double((hex >> 8) & 0xFF) / 255,
// blue: Double(hex & 0xFF) / 255,
// opacity: alpha
// )
// }
//
// init(hex: String, alpha: Double = 1) {
// guard let hexValue = UInt32(hex, radix: 16) else {
// fatalError("Invalid hex color string")
// }
//
// self.init(
// .sRGB,
// red: Double((hexValue >> 16) & 0xFF) / 255,
// green: Double((hexValue >> 8) & 0xFF) / 255,
// blue: Double(hexValue & 0xFF) / 255,
// opacity: alpha
// )
// }
//}
//
//extension UIApplication {
// static var windowScene: UIWindowScene? {
// return shared.connectedScenes.first as? UIWindowScene
// }
//}
50 changes: 50 additions & 0 deletions Sources/OpenMaterialUI/Button/Modifiers/ContainerModifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// ContainerModifiers.swift
//
//
// Created by Ahmed Shendy on 03/10/2023.
//

import SwiftUI

public extension View {
func container(height: CGFloat, color: Color) -> some View {
modifier(ContainerModifier(height: height, color: color))
}

func containerColor(_ color: Color) -> some View {
modifier(ContainerColorModifier(value: color))
}

func containerHeight(_ height: CGFloat) -> some View {
modifier(ContainerHeightModifier(value: height))
}
}

struct ContainerModifier: ViewModifier {
var height: CGFloat
var color: Color

func body(content: Content) -> some View {
content
.frame(height: height)
.background(color)
.clipShape(Capsule(style: .continuous))
}
}

struct ContainerColorModifier: ViewModifier {
var value: Color

func body(content: Content) -> some View {
content.background(value)
}
}

struct ContainerHeightModifier: ViewModifier {
var value: CGFloat

func body(content: Content) -> some View {
content.frame(height: value)
}
}
49 changes: 49 additions & 0 deletions Sources/OpenMaterialUI/Button/Modifiers/LabelTextModifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// File.swift
//
//
// Created by Ahmed Shendy on 03/10/2023.
//

import SwiftUI

public extension View {
func labelText(font: Font, color: Color) -> some View {
modifier(LabelTextModifier(font: font, color: color))
}

func labelTextColor(_ color: Color) -> some View {
modifier(LabelTextColorModifier(value: color))
}

func labelTextFont(_ font: Font) -> some View {
modifier(LabelTextFontModifier(value: font))
}
}

struct LabelTextModifier: ViewModifier {
var font: Font
var color: Color

func body(content: Content) -> some View {
content
.font(font)
.foregroundStyle(color)
}
}

struct LabelTextColorModifier: ViewModifier {
var value: Color

func body(content: Content) -> some View {
content.foregroundStyle(value)
}
}

struct LabelTextFontModifier: ViewModifier {
var value: Font

func body(content: Content) -> some View {
content.font(value)
}
}
Loading