Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SF Symbols support to left/right top bar buttons #7318

Merged
merged 6 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ios/RNNButtonBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ - (RNNUIBarButtonItem *)build:(RNNButtonOptions *)button
return [[RNNUIBarButtonItem alloc] initCustomIcon:button
iconCreator:_iconCreator
onPress:onPress];
} else if (button.icon.hasValue) {
} else if (button.sfSymbol.hasValue) {
return [[RNNUIBarButtonItem alloc] initWithSFSymbol:button onPress:onPress];
} else if (button.icon.hasValue) {
return [[RNNUIBarButtonItem alloc] initWithIcon:button onPress:onPress];
} else if (button.text.hasValue) {
return [[RNNUIBarButtonItem alloc] initWithTitle:button onPress:onPress];
Expand Down
1 change: 1 addition & 0 deletions lib/ios/RNNButtonOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@property(nonatomic, strong) Color *color;
@property(nonatomic, strong) Color *disabledColor;
@property(nonatomic, strong) Image *icon;
@property(nonatomic, strong) Text *sfSymbol;
@property(nonatomic, strong) Bool *enabled;
@property(nonatomic, strong) RNNInsetsOptions *iconInsets;
@property(nonatomic, strong) Bool *selectTabOnPress;
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/RNNButtonOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
self.fontWeight = [TextParser parse:dict key:@"fontWeight"];
self.fontSize = [NumberParser parse:dict key:@"fontSize"];
self.text = [TextParser parse:dict key:@"text"];
self.sfSymbol = [TextParser parse:dict key:@"sfSymbol"];
self.testID = [TextParser parse:dict key:@"testID"];
self.accessibilityLabel = [TextParser parse:dict key:@"accessibilityLabel"];
self.color = [ColorParser parse:dict key:@"color"];
Expand Down Expand Up @@ -39,6 +40,7 @@ - (RNNButtonOptions *)copy {
newOptions.color = self.color.copy;
newOptions.disabledColor = self.disabledColor.copy;
newOptions.icon = self.icon.copy;
newOptions.sfSymbol = self.sfSymbol.copy;
newOptions.iconInsets = self.iconInsets.copy;
newOptions.enabled = self.enabled.copy;
newOptions.selectTabOnPress = self.selectTabOnPress.copy;
Expand Down Expand Up @@ -71,6 +73,8 @@ - (void)mergeOptions:(RNNButtonOptions *)options {
self.disabledColor = options.disabledColor;
if (options.icon.hasValue)
self.icon = options.icon;
if (options.sfSymbol.hasValue)
self.sfSymbol = options.sfSymbol;
if (options.enabled.hasValue) {
self.enabled = options.enabled;
[self.iconBackground setEnabled:self.enabled];
Expand Down
2 changes: 2 additions & 0 deletions lib/ios/RNNUIBarButtonItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ typedef void (^RNNButtonPressCallback)(NSString *buttonId);
- (instancetype)initCustomIcon:(RNNButtonOptions *)buttonOptions
iconCreator:(RNNIconCreator *)iconCreator
onPress:(RNNButtonPressCallback)onPress;
- (instancetype)initWithSFSymbol:(RNNButtonOptions *)buttonOptions
onPress:(RNNButtonPressCallback)onPress;
- (instancetype)initWithIcon:(RNNButtonOptions *)buttonOptions
onPress:(RNNButtonPressCallback)onPress;
- (instancetype)initWithTitle:(RNNButtonOptions *)buttonOptions
Expand Down
17 changes: 17 additions & 0 deletions lib/ios/RNNUIBarButtonItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ - (instancetype)init {
return self;
}

- (instancetype)initWithSFSymbol:(RNNButtonOptions *)buttonOptions
onPress:(RNNButtonPressCallback)onPress {
UIImage *iconImage = [UIImage alloc];

if (@available(iOS 13.0, *)) {
iconImage = [UIImage systemImageNamed:[buttonOptions.sfSymbol withDefault:nil]];
}

self = [super initWithImage:iconImage
style:UIBarButtonItemStylePlain
target:self
action:@selector(onButtonPressed:)];
[self applyOptions:buttonOptions];
self.onPress = onPress;
return self;
}

- (instancetype)initWithIcon:(RNNButtonOptions *)buttonOptions
onPress:(RNNButtonPressCallback)onPress {
UIImage *iconImage = buttonOptions.icon.get;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/interfaces/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ export interface OptionsTopBarButton {
* Set the button icon
*/
icon?: ImageResource;
/**
* Set the SF symbol as icon (will be used primarily)
* #### (iOS 13+ specific)
*/
sfSymbol?: string;
/**
* Set the button icon insets
*/
Expand Down
8 changes: 8 additions & 0 deletions website/docs/api/options-button.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ Button icon. If the button is pushed to the overflow menu, the button [text](#te
| ------------------------------------------ | -------- | -------- |
| [ImageResource](options-imageResource.mdx) | No | Both |

### `sfSymbol`

SF Symbol to show as the back button

| Type | Required | Platform |
| ------ | -------- | -------- |
| string | No | iOS 13+ |

### `text`

Button text. Ignored if an icon is specified, unless the button is displayed in the overflow menu.
Expand Down