-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from juandahurt/accordion
feat: add accordion view
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
PuraceDemo/PuraceDemo/Examples/Basic/AccordionExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Sources/Purace/Views/Basic/Accordion/PuraceAccordionView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |