Skip to content

Commit

Permalink
Merge pull request #18 from juandahurt/accordion
Browse files Browse the repository at this point in the history
feat: add accordion view
  • Loading branch information
juandahurt authored Jun 14, 2022
2 parents 05e98e7 + 98786d5 commit 2f5efce
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions PuraceDemo/PuraceDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
2F1F7D7528318EEE00AA30DB /* ButtonExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F1F7D7428318EEE00AA30DB /* ButtonExample.swift */; };
2F27BC9D284814B600B5AC8D /* TabExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F27BC9C284814B600B5AC8D /* TabExample.swift */; };
2F5EFD3C284D23E3005D130A /* HorizontalGridExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F5EFD3B284D23E2005D130A /* HorizontalGridExample.swift */; };
2F6267AE2857B6960063A630 /* AccordionExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6267AD2857B6960063A630 /* AccordionExample.swift */; };
2F77A234283C4AC700F143FB /* ImageExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F77A233283C4AC700F143FB /* ImageExample.swift */; };
2F9321B3282EE49E003DA929 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F9321B2282EE49E003DA929 /* AppDelegate.swift */; };
2F9321B5282EE49E003DA929 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F9321B4282EE49E003DA929 /* SceneDelegate.swift */; };
Expand All @@ -33,6 +34,7 @@
2F1F7D7428318EEE00AA30DB /* ButtonExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonExample.swift; sourceTree = "<group>"; };
2F27BC9C284814B600B5AC8D /* TabExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabExample.swift; sourceTree = "<group>"; };
2F5EFD3B284D23E2005D130A /* HorizontalGridExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HorizontalGridExample.swift; sourceTree = "<group>"; };
2F6267AD2857B6960063A630 /* AccordionExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccordionExample.swift; sourceTree = "<group>"; };
2F77A233283C4AC700F143FB /* ImageExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageExample.swift; sourceTree = "<group>"; };
2F9321AF282EE49E003DA929 /* PuraceDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PuraceDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2F9321B2282EE49E003DA929 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -75,6 +77,7 @@
2F1F7D7428318EEE00AA30DB /* ButtonExample.swift */,
2F072906283C00080098C5AF /* LoaderExample.swift */,
2F77A233283C4AC700F143FB /* ImageExample.swift */,
2F6267AD2857B6960063A630 /* AccordionExample.swift */,
);
path = Basic;
sourceTree = "<group>";
Expand Down Expand Up @@ -212,6 +215,7 @@
2F1F7D7328318D9900AA30DB /* TextExample.swift in Sources */,
2F9321B7282EE49E003DA929 /* MenuView.swift in Sources */,
2F9321B3282EE49E003DA929 /* AppDelegate.swift in Sources */,
2F6267AE2857B6960063A630 /* AccordionExample.swift in Sources */,
2F77A234283C4AC700F143FB /* ImageExample.swift in Sources */,
2F5EFD3C284D23E3005D130A /* HorizontalGridExample.swift in Sources */,
2FA9867B28373DD3000DB409 /* SnackbarExample.swift in Sources */,
Expand Down
23 changes: 23 additions & 0 deletions PuraceDemo/PuraceDemo/Examples/Basic/AccordionExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// AccordionExample.swift
// PuraceDemo
//
// Created by Juan Hurtado on 13/06/22.
//

import Foundation
import SwiftUI
import Purace

struct AccordionExample: View {
var body: some View {
VStack {
PuraceAccordionView(title: "Biografía") {
PuraceTextView("Fue un político, militar y estadista colombiano, miembro del Partido Liberal Colombiano. A lo largo de su carrera ocupó varios cargos importantes en varios gobiernos, y también destacó como militar laureado, participando en algunas guerras civiles colombianas, como la de 1854, 1875 y 1885.")
.multilineTextAlignment(.leading)
.padding(10)
}
Spacer()
}
}
}
3 changes: 3 additions & 0 deletions PuraceDemo/PuraceDemo/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct MenuView: View {
NavigationLink("Image") {
ImageExample()
}
NavigationLink("Accordion") {
AccordionExample()
}
}
Section(header: Text("Complex")) {
NavigationLink("Collection card") {
Expand Down
1 change: 1 addition & 0 deletions Sources/Purace/Styles/PuraceStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct PuraceStyle {
public static let N3 = SwiftUI.Color(hex: "636363") ?? .black
public static let N4 = SwiftUI.Color(hex: "808080") ?? .black
public static let N5 = SwiftUI.Color(hex: "8D8D8D") ?? .black
public static let N8 = SwiftUI.Color(hex: "EEEEEE") ?? .black

public static let B1 = SwiftUI.Color(hex: "4E82DD") ?? .blue
public static let B2 = SwiftUI.Color(hex: "6491DF") ?? .blue
Expand Down
62 changes: 62 additions & 0 deletions Sources/Purace/Views/Basic/Accordion/PuraceAccordionView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// PuraceAccordionView.swift
//
//
// Created by Juan Hurtado on 13/06/22.
//

import Foundation
import SwiftUI

public struct PuraceAccordionView<Content: View>: View {
private let closeColor = Color.white
private let openColor = PuraceStyle.Color.G8.opacity(0.3)

private let openForegroundColor = PuraceStyle.Color.N1
private let closeForegroundColor = PuraceStyle.Color.G1

@State var isOpen = false
@State var rotation: CGFloat = 270

let title: String
let content: Content

public init(title: String, _ content: () -> Content) {
self.title = title
self.content = content()
}

var header: some View {
HStack {
PuraceTextView(title, fontSize: 14, textColor: isOpen ? openForegroundColor : closeForegroundColor, weight: .medium)
Spacer()
Image(systemName: "chevron.left")
.foregroundColor(isOpen ? openForegroundColor : closeForegroundColor)
.rotationEffect(.degrees(rotation))
}
.padding(.vertical)
.padding(.horizontal, 16)
.background(
ZStack(alignment: .bottom) {
isOpen ? openColor : closeColor
Divider()
}
)
.onTapGesture {
withAnimation(.linear(duration: 0.15)) {
isOpen = !isOpen
rotation = isOpen ? 90 : 270
}
}
}

public var body: some View {
VStack(spacing: 0) {
header
content
.frame(maxWidth: .greatestFiniteMagnitude, maxHeight: !isOpen ? 0 : .none)
.clipped()
.animation(.easeOut(duration: 0.2))
}
}
}

0 comments on commit 2f5efce

Please sign in to comment.