-
Notifications
You must be signed in to change notification settings - Fork 37
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 #2141 from Skyscanner/donburi/DON-1029-segmented-c…
…ontrol-custom DON-1029 segmented control custom
- Loading branch information
Showing
13 changed files
with
317 additions
and
18 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
Backpack-SwiftUI/SegmentedControl/Classes/BPKSegmentedControl.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,133 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright © 2025 Skyscanner Ltd. All rights reserved. | ||
* | ||
* 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 | ||
/// A linear set of mutually exclusive buttons. | ||
/// | ||
/// You create a segmented control by providing a list of `items` that will serve as the segments' titles | ||
/// and a `selectedIndex` binding. Bind `selectedIndex` | ||
/// to an Int property that determines which segment is selected. | ||
/// The colours can be customised using the`style` parameter. | ||
public struct BPKSegmentedControl: View { | ||
private var items: [String] | ||
private var accessibilityLabel: String | ||
@Binding private var selectedIndex: Int | ||
private let style: Style | ||
private let segmentHeight: CGFloat = 32 | ||
@Namespace var namespace | ||
|
||
/// Creates a Segment Control that generates its labels from a list of Strings | ||
/// | ||
/// - Parameters: | ||
/// - items: An Array of Strings that describes the purpose of each segment. Limited to 4 items. | ||
/// - selectedIndex: A binding to an Int that indicates which segment is selected | ||
/// - accessibilityLabel: A string used to identify the component and each segment | ||
/// - style: A style defining the customised colours of the component | ||
public init( | ||
items: [String], | ||
selectedIndex: Binding<Int>, | ||
accessibilityLabel: String, | ||
style: Style = .defaultStyle | ||
) { | ||
// Limit the number of items to 4 | ||
self.items = Array(items.prefix(4)) | ||
self.accessibilityLabel = accessibilityLabel | ||
self.style = style | ||
_selectedIndex = selectedIndex | ||
} | ||
|
||
public var body: some View { | ||
ZStack { | ||
HStack(spacing: 0) { | ||
ForEach(items.indices, id: \.self) { index in | ||
segmentButton(for: index) | ||
.matchedGeometryEffect(id: index, in: namespace, isSource: true) | ||
} | ||
} | ||
.background { | ||
Rectangle() | ||
.fill(Color(style.selectedBgColor)) | ||
.matchedGeometryEffect(id: selectedIndex, in: namespace, isSource: false) | ||
} | ||
.background(Color(style.bgColor)) | ||
.animation(.spring(duration: 0.25), value: selectedIndex) | ||
.cornerRadius(8) | ||
.padding() | ||
.accessibilityIdentifier(accessibilityLabel) | ||
.frame(height: segmentHeight) | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private func segmentButton(for index: Int) -> some View { | ||
HStack(spacing: 0) { | ||
Button( | ||
action: { withAnimation { selectedIndex = index } }, | ||
label: { | ||
Text(items[index]) | ||
.font(style: .label2) | ||
.frame(maxWidth: .infinity, minHeight: segmentHeight) | ||
.foregroundColor(selectedIndex == index ? style.selectedTextColor : style.textColor) | ||
.accessibilityIdentifier("\(accessibilityLabel)_\(index)") | ||
.accessibilityAddTraits(selectedIndex == index ? .isSelected : []) | ||
.sizeCategory(.large) | ||
|
||
} | ||
) | ||
.frame(maxHeight: .infinity) | ||
|
||
if index < items.count - 1 && selectedIndex != index && selectedIndex != index + 1 { | ||
separator | ||
} | ||
} | ||
} | ||
|
||
private var separator: some View { | ||
Rectangle() | ||
.fill(Color(.lineColor)) | ||
.frame(width: 1, height: segmentHeight) | ||
} | ||
|
||
public struct Style { | ||
let bgColor: BPKColor | ||
let textColor: BPKColor | ||
let selectedBgColor: BPKColor | ||
let selectedTextColor: BPKColor | ||
|
||
public init(bgColor: BPKColor, textColor: BPKColor, selectedBgColor: BPKColor, selectedTextColor: BPKColor) { | ||
self.bgColor = bgColor | ||
self.textColor = textColor | ||
self.selectedBgColor = selectedBgColor | ||
self.selectedTextColor = selectedTextColor | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Default component style | ||
public extension BPKSegmentedControl.Style { | ||
static let defaultStyle = Self( | ||
bgColor: .segmentedControlCanvasDefaultColor, | ||
textColor: .textPrimaryColor, | ||
selectedBgColor: .corePrimaryColor, | ||
selectedTextColor: .textOnDarkColor | ||
) | ||
} | ||
|
||
#Preview { | ||
BPKSegmentedControl(items: ["1", "2", "3", "4"], selectedIndex: .constant(3), accessibilityLabel: "example_label") | ||
} |
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,50 @@ | ||
# Backpack-SwiftUI/SegmentedControl | ||
|
||
[](hhttps://cocoapods.org/pods/Backpack-SwiftUI) | ||
[](https://backpack.github.io/ios/versions/latest/swiftui/Structs/BPKSegmentedControl.html) | ||
[](https://github.com/Skyscanner/backpack-ios/tree/main/Backpack-SwiftUI/SegmentedControl) | ||
|
||
## Default | ||
|
||
| Day | Night | | ||
| --- | --- | | ||
| <img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_segmented-control___default_lm.png" alt="" width="375" /> |<img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_segmented-control___default_dm.png" alt="" width="375" /> | | ||
|
||
## Usage | ||
|
||
Use an array of strings to set the texts shown by the segmented control as a `BPKText` label. | ||
The colours can be customised using the`style` parameter. | ||
|
||
### Default style | ||
|
||
```swift | ||
import Backpack_SwiftUI | ||
@State var selectedIndex = 0 | ||
private let accessibilityLabel = "bpk_segmented_control_example" | ||
private let items: [String] = ["1", "2", "3"] | ||
|
||
BPKSegmentedControl( | ||
items: items, | ||
selectedIndex: $selectedIndex, | ||
accessibilityLabel: accessibilityLabel, | ||
style: .defaultStyle | ||
) | ||
``` | ||
|
||
### Custom style | ||
|
||
```swift | ||
import Backpack_SwiftUI | ||
@State var selectedIndex = 0 | ||
private let accessibilityLabel = "bpk_segmented_control_example" | ||
private let items: [String] = ["1", "2", "3"] | ||
|
||
BPKSegmentedControl( | ||
items: items, | ||
selectedIndex: $selectedIndex, | ||
accessibilityLabel: accessibilityLabel, | ||
style: .init( | ||
bgColor: .black, textColor: .white, selectedBgColor: .white, selectedTextColor: .black | ||
) | ||
) | ||
``` |
44 changes: 44 additions & 0 deletions
44
Backpack-SwiftUI/Tests/SegmentedControl/BPKSegmentedControlTests.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,44 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright © 2025 Skyscanner Ltd. All rights reserved. | ||
* | ||
* 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 BPKSegmentedControlTests: XCTestCase { | ||
func test_firstSegmentSelected() { | ||
assertSnapshot( | ||
BPKSegmentedControl( | ||
items: ["One", "Two", "Three"], | ||
selectedIndex: .constant(0), | ||
accessibilityLabel: "bpk_segmented_control_example", | ||
style: .defaultStyle) | ||
.frame(width: 200) | ||
) | ||
} | ||
|
||
func test_accessibility() { | ||
let segmentedControl = BPKSegmentedControl( | ||
items: ["One", "Two", "Three"], | ||
selectedIndex: .constant(0), | ||
accessibilityLabel: "bpk_segmented_control_example" | ||
) | ||
.frame(width: 200) | ||
assertA11ySnapshot(segmentedControl) | ||
} | ||
} |
Binary file added
BIN
+24.3 KB
...entedControl/__Snapshots__/BPKSegmentedControlTests/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
+5.33 KB
.../__Snapshots__/BPKSegmentedControlTests/test_firstSegmentSelected.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.33 KB
...__Snapshots__/BPKSegmentedControlTests/test_firstSegmentSelected.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.29 KB
...ontrol/__Snapshots__/BPKSegmentedControlTests/test_firstSegmentSelected.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
56 changes: 56 additions & 0 deletions
56
Example/Backpack/SwiftUI/Components/SegmentedControl/SegmentedControlExampleView.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,56 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright © 2025 Skyscanner Ltd. All rights reserved. | ||
* | ||
* 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 | ||
import Backpack_SwiftUI | ||
|
||
struct SegmentedControlExampleView: View { | ||
@State var selectedIndex = 0 | ||
private let accessibilityLabel = "bpk_segmented_control_example" | ||
private let items: [String] = ["1", "2", "3", "4"] | ||
private let longerItems: [String] = ["Item 1", "Item 2", "Very long item 3", "Item 4"] | ||
|
||
var body: some View { | ||
VStack(spacing: 10) { | ||
Text("Default style") | ||
|
||
BPKSegmentedControl( | ||
items: longerItems, | ||
selectedIndex: $selectedIndex, | ||
accessibilityLabel: accessibilityLabel, | ||
style: .defaultStyle | ||
) | ||
|
||
Text("Custom style") | ||
BPKSegmentedControl( | ||
items: items, | ||
selectedIndex: $selectedIndex, | ||
accessibilityLabel: accessibilityLabel, | ||
style: .init( | ||
bgColor: .black, textColor: .white, selectedBgColor: .white, selectedTextColor: .black | ||
) | ||
) | ||
|
||
Text("Selected index: \(selectedIndex)") | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
SegmentedControlExampleView() | ||
} |
Oops, something went wrong.