Skip to content

Instantly share code, notes, and snippets.

@apatronl
Last active February 24, 2022 20:15
Show Gist options
  • Save apatronl/fcf4cfe49f4ab5293c997d5c4ca78388 to your computer and use it in GitHub Desktop.
Save apatronl/fcf4cfe49f4ab5293c997d5c4ca78388 to your computer and use it in GitHub Desktop.
struct ExpandableButtonPanel: View {
let primaryItem: ExpandableButtonItem
let secondaryItems: [ExpandableButtonItem]
private let noop: () -> Void = {}
private let size: CGFloat = 70
private var cornerRadius: CGFloat {
get { size / 2 }
}
private let shadowColor = Color.black.opacity(0.4)
private let shadowPosition: (x: CGFloat, y: CGFloat) = (x: 2, y: 2)
private let shadowRadius: CGFloat = 3
@State private var isExpanded = false
var body: some View {
VStack {
ForEach(secondaryItems) { item in
Button(item.label, action: item.action ?? self.noop)
.frame(
width: self.isExpanded ? self.size : 0,
height: self.isExpanded ? self.size : 0)
}
Button(primaryItem.label, action: {
withAnimation {
self.isExpanded.toggle()
}
self.primaryItem.action?()
})
.frame(width: size, height: size)
}
.background(Color(UIColor.systemPurple))
.cornerRadius(cornerRadius)
.font(.title)
.shadow(
color: shadowColor,
radius: shadowRadius,
x: shadowPosition.x,
y: shadowPosition.y
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment