-
Notifications
You must be signed in to change notification settings - Fork 35
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 #1937 from Skyscanner/bellagio/horizontal_navigation
Add BPKHorizontalNavigation component
- Loading branch information
Showing
22 changed files
with
386 additions
and
9 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
Backpack-SwiftUI/HorizontalNavigation/Classes/BPKHorizontalNavigation.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,151 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2018-2023 Skyscanner 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 BPKHorizontalNavigation { | ||
enum Size { | ||
case `default` | ||
case small | ||
|
||
var titleStyle: BPKFontStyle { | ||
switch self { | ||
case .default: return .label1 | ||
case .small: return .label2 | ||
} | ||
} | ||
|
||
var verticalPadding: CGFloat { | ||
switch self { | ||
case .default: return 12 | ||
case .small: return BPKSpacing.md.value | ||
} | ||
} | ||
} | ||
|
||
struct Tab { | ||
let title: String | ||
let icon: BPKIcon? | ||
|
||
public init(title: String, icon: BPKIcon? = nil) { | ||
self.title = title | ||
self.icon = icon | ||
} | ||
} | ||
} | ||
|
||
public struct BPKHorizontalNavigation: View { | ||
let tabs: [Tab] | ||
let size: Size | ||
@Binding var selectedTab: Int | ||
|
||
public init(tabs: [Tab], size: Size = .default, selectedTab: Binding<Int>) { | ||
self.tabs = tabs | ||
self.size = size | ||
_selectedTab = selectedTab | ||
} | ||
|
||
public var body: some View { | ||
ZStack(alignment: .bottom) { | ||
HStack(spacing: BPKSpacing.none) { | ||
ForEach(0..<tabs.count, id: \.self) { index in | ||
Button { | ||
withAnimation { | ||
selectedTab = index | ||
} | ||
} label: { | ||
TabCellView( | ||
tab: tabs[index], | ||
isSelected: selectedTab == index, | ||
size: size | ||
) | ||
} | ||
} | ||
} | ||
.padding(.vertical, size.verticalPadding) | ||
|
||
GeometryReader { proxy in | ||
let width = tabsWidth(for: proxy) | ||
Color(.coreAccentColor) | ||
.frame(width: width) | ||
.offset(x: width * CGFloat(selectedTab)) | ||
} | ||
.frame(height: 2) | ||
} | ||
.background(.surfaceDefaultColor) | ||
} | ||
|
||
private func tabsWidth(for proxy: GeometryProxy) -> CGFloat { | ||
proxy.size.width / CGFloat(tabs.count) | ||
} | ||
|
||
struct TabCellView: View { | ||
let tab: Tab | ||
let isSelected: Bool | ||
let size: Size | ||
|
||
var body: some View { | ||
HStack(spacing: .md) { | ||
if let icon = tab.icon { | ||
BPKIconView(icon) | ||
.foregroundColor(foregroundColor) | ||
} | ||
BPKText(tab.title, style: size.titleStyle) | ||
.foregroundColor(foregroundColor) | ||
} | ||
.frame(maxWidth: .infinity) | ||
} | ||
|
||
private var foregroundColor: BPKColor { | ||
isSelected ? .coreAccentColor : .textPrimaryColor | ||
} | ||
} | ||
} | ||
|
||
struct BPKHorizontalNavigation_Previews: PreviewProvider { | ||
static var previews: some View { | ||
VStack { | ||
BPKHorizontalNavigation( | ||
tabs: [.init(title: "One"), .init(title: "Two"), .init(title: "Three")], | ||
selectedTab: .constant(1) | ||
) | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "One", icon: .flight), | ||
.init(title: "Two", icon: .flight), | ||
.init(title: "Three", icon: .flight) | ||
], | ||
selectedTab: .constant(1) | ||
) | ||
BPKHorizontalNavigation( | ||
tabs: [.init(title: "One"), .init(title: "Two"), .init(title: "Three")], | ||
size: .small, | ||
selectedTab: .constant(2) | ||
) | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "One", icon: .flight), | ||
.init(title: "Two", icon: .flight), | ||
.init(title: "Three", icon: .flight) | ||
], | ||
size: .small, | ||
selectedTab: .constant(0) | ||
) | ||
} | ||
} | ||
} |
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,56 @@ | ||
# HorizontalNavigation | ||
|
||
[![Cocoapods](https://img.shields.io/cocoapods/v/Backpack-SwiftUI.svg?style=flat)](hhttps://cocoapods.org/pods/Backpack-SwiftUI) | ||
[![class reference](https://img.shields.io/badge/Class%20reference-iOS-blue)](https://backpack.github.io/ios/versions/latest/swiftui/Structs/BPKBadge.html) | ||
[![view on Github](https://img.shields.io/badge/Source%20code-GitHub-lightgrey)](https://github.com/Skyscanner/backpack-ios/tree/main/Backpack-SwiftUI/Badge) | ||
|
||
## Default | ||
|
||
| Day | Night | | ||
| --- | --- | | ||
| <img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_horizontal-navigation___default_lm.png" alt="" width="375" /> | <img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_horizontal-navigation___default_dm.png" alt="" width="375" /> | | ||
|
||
## Usage | ||
|
||
```swift | ||
@State var selectedTab: Int = 0 | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "Flights", icon: .flight), | ||
.init(title: "Hotels", icon: .hotel), | ||
.init(title: "Cars", icon: .car) | ||
], | ||
selectedTab: $selectedTab | ||
) | ||
``` | ||
|
||
### Setting the Size | ||
|
||
BPkHorizontalNavigation supports both a `default` and a `small` size. | ||
|
||
```swift | ||
@State var selectedTab: Int = 0 | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "Flights", icon: .flight), | ||
.init(title: "Hotels", icon: .hotel), | ||
.init(title: "Cars", icon: .car) | ||
], | ||
size: .small, | ||
selectedTab: $selectedTab | ||
) | ||
``` | ||
|
||
### Tabs withou icons | ||
|
||
```swift | ||
@State var selectedTab: Int = 0 | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "Flights"), | ||
.init(title: "Hotels"), | ||
.init(title: "Cars") | ||
], | ||
selectedTab: $selectedTab | ||
) | ||
``` |
86 changes: 86 additions & 0 deletions
86
Backpack-SwiftUI/Tests/HorizontalNavigation/BPKHorizontalNavigationTests.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,86 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2018 Skyscanner 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 XCTest | ||
import SwiftUI | ||
@testable import Backpack_SwiftUI | ||
|
||
class BPKHorizontalNavigationTests: XCTestCase { | ||
func test_allTabsWithoutIcon_defaultSize() { | ||
assertSnapshot( | ||
BPKHorizontalNavigation( | ||
tabs: [.init(title: "One"), .init(title: "Two"), .init(title: "Three")], | ||
selectedTab: .constant(1) | ||
) | ||
.frame(width: 400) | ||
) | ||
} | ||
|
||
func test_allTabsWithoutIcon_smallSize() { | ||
assertSnapshot( | ||
BPKHorizontalNavigation( | ||
tabs: [.init(title: "One"), .init(title: "Two"), .init(title: "Three")], | ||
size: .small, | ||
selectedTab: .constant(1) | ||
) | ||
.frame(width: 400) | ||
) | ||
} | ||
|
||
func test_allTabsWithIcon_defaultSize() { | ||
assertSnapshot( | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "One", icon: .flight), | ||
.init(title: "Two", icon: .flight), | ||
.init(title: "Three", icon: .flight) | ||
], | ||
selectedTab: .constant(1) | ||
) | ||
.frame(width: 400) | ||
) | ||
} | ||
|
||
func test_allTabsWithIcon_smallSize() { | ||
assertSnapshot( | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "One", icon: .flight), | ||
.init(title: "Two", icon: .flight), | ||
.init(title: "Three", icon: .flight) | ||
], | ||
size: .small, | ||
selectedTab: .constant(1) | ||
) | ||
.frame(width: 400) | ||
) | ||
} | ||
|
||
func test_accessibility() { | ||
assertA11ySnapshot( | ||
BPKHorizontalNavigation( | ||
tabs: [ | ||
.init(title: "One", icon: .flight), | ||
.init(title: "Two", icon: .flight), | ||
.init(title: "Three", icon: .flight) | ||
], | ||
selectedTab: .constant(1) | ||
) | ||
) | ||
} | ||
} |
Binary file added
BIN
+34.1 KB
...vigation/__Snapshots__/BPKHorizontalNavigationTests/test_accessibility.a11y.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.16 KB
...s__/BPKHorizontalNavigationTests/test_allTabsWithIcon_defaultSize.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.51 KB
...__/BPKHorizontalNavigationTests/test_allTabsWithIcon_defaultSize.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.57 KB
...apshots__/BPKHorizontalNavigationTests/test_allTabsWithIcon_defaultSize.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.83 KB
...ots__/BPKHorizontalNavigationTests/test_allTabsWithIcon_smallSize.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.34 KB
...ts__/BPKHorizontalNavigationTests/test_allTabsWithIcon_smallSize.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.34 KB
...Snapshots__/BPKHorizontalNavigationTests/test_allTabsWithIcon_smallSize.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.22 KB
.../BPKHorizontalNavigationTests/test_allTabsWithoutIcon_defaultSize.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.73 KB
...BPKHorizontalNavigationTests/test_allTabsWithoutIcon_defaultSize.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.71 KB
...hots__/BPKHorizontalNavigationTests/test_allTabsWithoutIcon_defaultSize.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.82 KB
...__/BPKHorizontalNavigationTests/test_allTabsWithoutIcon_smallSize.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.52 KB
..._/BPKHorizontalNavigationTests/test_allTabsWithoutIcon_smallSize.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.52 KB
...pshots__/BPKHorizontalNavigationTests/test_allTabsWithoutIcon_smallSize.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.