Skip to content

Commit

Permalink
Add FXIOS-7395 [v120] Link button (#16858)
Browse files Browse the repository at this point in the history
* Create link button in component library

* Add sample app link button example

* Make adjustment to link button config

* swiftlint
  • Loading branch information
lmarceau authored Oct 17, 2023
1 parent 56dd9da commit c05da74
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
44 changes: 44 additions & 0 deletions BrowserKit/Sources/ComponentLibrary/Buttons/LinkButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import Common
import UIKit

public class LinkButton: ResizableButton, ThemeApplicable {
private struct UX {
static let buttonCornerRadius: CGFloat = 13
static let buttonVerticalInset: CGFloat = 12
static let buttonHorizontalInset: CGFloat = 16
}

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .clear
titleLabel?.adjustsFontForContentSizeCategory = true
contentEdgeInsets = UIEdgeInsets(top: UX.buttonVerticalInset,
left: UX.buttonHorizontalInset,
bottom: UX.buttonVerticalInset,
right: UX.buttonHorizontalInset)
}

public func configure(viewModel: LinkButtonViewModel) {
accessibilityIdentifier = viewModel.a11yIdentifier
setTitle(viewModel.title, for: .normal)
titleLabel?.font = DefaultDynamicFontHelper.preferredFont(withTextStyle: .body,
size: viewModel.fontSize)
titleLabel?.textAlignment = viewModel.textAlignment
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: ThemeApplicable

public func applyTheme(theme: Theme) {
setTitleColor(theme.colors.textAccent, for: .normal)
setTitleColor(theme.colors.actionPrimaryHover, for: .highlighted)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import UIKit
import Foundation

/// The view model used to configure a `LinkButton`
public struct LinkButtonViewModel {
public let title: String
public let a11yIdentifier: String
public let fontSize: CGFloat
public let textAlignment: NSTextAlignment

public init(title: String,
a11yIdentifier: String,
fontSize: CGFloat = 16,
textAlignment: NSTextAlignment = .left) {
self.title = title
self.a11yIdentifier = a11yIdentifier
self.fontSize = fontSize
self.textAlignment = textAlignment
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ``ComponentLibrary/LinkButton``

The button which is used for actions that looks like a webview link.

## Overview

The `LinkButton` is a subclass of the `UIButton`. This means properties of the `UIButton` are accessible, but for easy conveniance it's recommended to configure the button title, font and accessibility identifier through it's' view model ``LinkButtonViewModel``. The colors and spacing of the button although accessible shouldn't be adjusted and should be used as is.

## Illustration

> This image is illustrative only. For precise examples of iOS implementation, please run the SampleApplication.
![The LinkButton on iOS]LinkButton

## Topics

### View Model

- ``LinkButtonViewModel``
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ButtonsViewController: UIViewController, Themeable {

private lazy var primaryButton: PrimaryRoundedButton = .build { _ in }
private lazy var secondaryButton: SecondaryRoundedButton = .build { _ in }
private lazy var linkButton: LinkButton = .build { _ in }

private lazy var buttonStackView: UIStackView = .build { stackView in
stackView.distribution = .fillEqually
Expand Down Expand Up @@ -46,14 +47,20 @@ class ButtonsViewController: UIViewController, Themeable {
a11yIdentifier: "a11ySecondary")
secondaryButton.configure(viewModel: secondaryViewModel)

let linkButtonViewModel = LinkButtonViewModel(title: "This is a link",
a11yIdentifier: "a11yLink")
linkButton.configure(viewModel: linkButtonViewModel)

primaryButton.applyTheme(theme: themeManager.currentTheme)
secondaryButton.applyTheme(theme: themeManager.currentTheme)
linkButton.applyTheme(theme: themeManager.currentTheme)
}

private func setupView() {
view.addSubview(buttonStackView)
buttonStackView.addArrangedSubview(primaryButton)
buttonStackView.addArrangedSubview(secondaryButton)
buttonStackView.addArrangedSubview(linkButton)

NSLayoutConstraint.activate([
buttonStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
Expand Down

0 comments on commit c05da74

Please sign in to comment.