Skip to content

Commit 7631bf8

Browse files
committed
Add ios code from old repo
1 parent adecee3 commit 7631bf8

14 files changed

+370
-62
lines changed

example/App.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
1-
/**
2-
* Sample React Native App
3-
*
4-
* adapted from App.js generated by the following command:
5-
*
6-
* react-native init example
7-
*
8-
* https://github.com/facebook/react-native
9-
*/
1+
import React, { useState } from 'react';
2+
import { SafeAreaView, View, StyleSheet } from 'react-native';
3+
import ContextMenu from 'react-native-context-menu';
104

11-
import React, { Component } from 'react';
12-
import { Platform, StyleSheet, Text, View } from 'react-native';
13-
import ReactNativeContextMenu from 'react-native-context-menu';
5+
const App = () => {
6+
const [color, setColor] = useState('blue');
147

15-
export default class App extends Component<{}> {
16-
render() {
17-
return (
18-
<View style={styles.container}>
19-
<Text style={styles.welcome}>☆ReactNativeContextMenu example☆</Text>
20-
<Text style={styles.instructions}>STATUS: loaded</Text>
21-
<Text style={styles.welcome}>☆☆☆</Text>
22-
<ReactNativeContextMenu />
23-
</View>
24-
);
25-
}
8+
return (
9+
<SafeAreaView style={styles.container}>
10+
<ContextMenu title={'Set Color'} actions={[
11+
{
12+
title: 'blue',
13+
systemIcon: color == 'blue' ? 'paintbrush.fill' : 'paintbrush',
14+
},
15+
{
16+
title: 'red',
17+
systemIcon: color == 'red' ? 'paintbrush.fill' : 'paintbrush'
18+
}
19+
]} onPress={(event) => {
20+
setColor(event.nativeEvent.name);
21+
}} onCancel={() => { console.warn('CANCELLED') }} >
22+
<View style={[styles.rectangle, {backgroundColor: color }]} />
23+
</ContextMenu>
24+
</SafeAreaView>
25+
);
2626
}
2727

2828
const styles = StyleSheet.create({
2929
container: {
3030
flex: 1,
3131
justifyContent: 'center',
32-
alignItems: 'center',
33-
backgroundColor: '#F5FCFF',
34-
},
35-
welcome: {
36-
fontSize: 20,
37-
textAlign: 'center',
38-
margin: 10,
39-
},
40-
instructions: {
41-
textAlign: 'center',
42-
color: '#333333',
43-
marginBottom: 5,
32+
alignItems: 'center'
4433
},
34+
rectangle: {
35+
width: 200,
36+
height: 200,
37+
}
4538
});
39+
40+
export default App;

example/ios/example.xcodeproj/project.pbxproj

Lines changed: 141 additions & 0 deletions
Large diffs are not rendered by default.

example/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5408,7 +5408,7 @@ react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.8.6:
54085408
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
54095409
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
54105410

5411-
"react-native-context-menu@link:../":
5411+
"react-native-context-menu@link:..":
54125412
version "0.0.0"
54135413
uid ""
54145414

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { requireNativeComponent } from 'react-native';
22

3-
const ReactNativeContextMenu = requireNativeComponent('ReactNativeContextMenu', null);
3+
const ContextMenu = requireNativeComponent('ContextMenu', null);
44

5-
export default ReactNativeContextMenu;
5+
export default ContextMenu;

ios/ContextMenu.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <React/RCTViewManager.h>
2+
3+
@interface ContextMenu : RCTViewManager
4+
5+
@end

ios/ContextMenu.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#import "ContextMenu.h"
2+
#import "ContextMenuView.h"
3+
4+
@implementation ContextMenu
5+
6+
RCT_EXPORT_MODULE()
7+
8+
- (UIView *) view {
9+
return [[ContextMenuView alloc] init];
10+
}
11+
12+
RCT_EXPORT_VIEW_PROPERTY(title, NSString)
13+
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
14+
RCT_EXPORT_VIEW_PROPERTY(actions, NSArray<ContextMenuAction>)
15+
16+
@end

ios/ContextMenuAction.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// ContextMenuAction.h
3+
// reactnativeuimenu
4+
//
5+
// Created by Matthew Iannucci on 10/6/19.
6+
// Copyright © 2019 Facebook. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface ContextMenuAction : NSObject
12+
13+
@property (nonatomic, copy) NSString* title;
14+
@property (nonatomic, copy) NSString* systemIcon;
15+
16+
@end

ios/ContextMenuAction.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ContextMenuAction.m
3+
// reactnativeuimenu
4+
//
5+
// Created by Matthew Iannucci on 10/6/19.
6+
// Copyright © 2019 Facebook. All rights reserved.
7+
//
8+
9+
#import "ContextMenuAction.h"
10+
11+
@implementation ContextMenuAction
12+
13+
@end

ios/ContextMenuView.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// ContextMenu.h
3+
// reactnativeuimenu
4+
//
5+
// Created by Matthew Iannucci on 10/6/19.
6+
// Copyright © 2019 Facebook. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <React/RCTComponent.h>
11+
#import "ContextMenuAction.h"
12+
13+
@interface ContextMenuView : UIView<UIContextMenuInteractionDelegate>
14+
15+
@property (nonatomic, copy) NSString* title;
16+
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
17+
@property (nonatomic, copy) RCTBubblingEventBlock onCancel;
18+
@property (nonatomic, copy) NSArray<ContextMenuAction*>* actions;
19+
20+
21+
@end

ios/ContextMenuView.m

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// ContextMenu.m
3+
// reactnativeuimenu
4+
//
5+
// Created by Matthew Iannucci on 10/6/19.
6+
// Copyright © 2019 Facebook. All rights reserved.
7+
//
8+
9+
#import "ContextMenuView.h"
10+
#import <React/UIView+React.h>
11+
12+
@implementation ContextMenuView {
13+
BOOL cancelled;
14+
}
15+
16+
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
17+
{
18+
[super insertReactSubview:subview atIndex:atIndex];
19+
if (@available(iOS 13.0, *)) {
20+
UIContextMenuInteraction* contextInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
21+
22+
[subview addInteraction:contextInteraction];
23+
}
24+
}
25+
26+
- (void)removeReactSubview:(UIView *)subview
27+
{
28+
[super removeReactSubview:subview];
29+
}
30+
31+
- (void)didUpdateReactSubviews
32+
{
33+
[super didUpdateReactSubviews];
34+
}
35+
36+
- (void)layoutSubviews
37+
{
38+
[super layoutSubviews];
39+
}
40+
41+
- (nullable UIContextMenuConfiguration *)contextMenuInteraction:(nonnull UIContextMenuInteraction *)interaction configurationForMenuAtLocation:(CGPoint)location API_AVAILABLE(ios(13.0)){
42+
43+
return [UIContextMenuConfiguration configurationWithIdentifier:nil previewProvider:nil actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
44+
NSMutableArray* actions = [[NSMutableArray alloc] init];
45+
46+
for (ContextMenuAction* thisAction in self.actions) {
47+
UIAction* actionMenuItem = [UIAction actionWithTitle:thisAction.title.capitalizedString image:[UIImage systemImageNamed:thisAction.systemIcon] identifier:nil handler:^(__kindof UIAction * _Nonnull action) {
48+
if (self.onPress != nil) {
49+
self->cancelled = false;
50+
self.onPress(@{@"name": thisAction.title});
51+
}
52+
}];
53+
54+
[actions addObject:actionMenuItem];
55+
}
56+
57+
return [UIMenu menuWithTitle:self.title children:actions];
58+
}];
59+
}
60+
61+
- (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction willDisplayMenuForConfiguration:(UIContextMenuConfiguration *)configuration animator:(id<UIContextMenuInteractionAnimating>)animator API_AVAILABLE(ios(13.0)){
62+
cancelled = true;
63+
}
64+
65+
- (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction
66+
willEndForConfiguration:(UIContextMenuConfiguration *)configuration
67+
animator:(id<UIContextMenuInteractionAnimating>)animator API_AVAILABLE(ios(13.0)) API_AVAILABLE(ios(13.0)){
68+
69+
if (cancelled && self.onCancel) {
70+
self.onCancel(@{});
71+
}
72+
73+
}
74+
75+
@end

0 commit comments

Comments
 (0)