|
| 1 | +import React, {useEffect, useMemo, useState} from 'react'; |
| 2 | +import {Pressable, ScrollView, Text, View} from 'react-native'; |
| 3 | +import NativeScript, {defineUIKitView} from '@nativescript/react-native'; |
| 4 | + |
| 5 | +NativeScript.init(); |
| 6 | + |
| 7 | +type BadgeProps = { |
| 8 | + title: string; |
| 9 | + tone: 'blue' | 'green'; |
| 10 | +}; |
| 11 | + |
| 12 | +const NativeBadge = defineUIKitView<BadgeProps, UIView>({ |
| 13 | + displayName: 'NativeBadge', |
| 14 | + create() { |
| 15 | + const view = UIView.alloc().initWithFrame(CGRectZero); |
| 16 | + view.clipsToBounds = true; |
| 17 | + |
| 18 | + const label = UILabel.alloc().initWithFrame(CGRectZero); |
| 19 | + label.tag = 1; |
| 20 | + label.textAlignment = NSTextAlignment.Center; |
| 21 | + label.textColor = UIColor.whiteColor; |
| 22 | + label.autoresizingMask = |
| 23 | + UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; |
| 24 | + view.addSubview(label); |
| 25 | + |
| 26 | + return view; |
| 27 | + }, |
| 28 | + mounted(view) { |
| 29 | + view.layer.cornerRadius = 14; |
| 30 | + }, |
| 31 | + update(view, props) { |
| 32 | + view.backgroundColor = |
| 33 | + props.tone === 'green' ? UIColor.systemGreenColor : UIColor.systemBlueColor; |
| 34 | + const label = view.viewWithTag(1) as UILabel; |
| 35 | + label.text = props.title; |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +async function readNativeSummary() { |
| 40 | + const api = (globalThis as any).__nativeScriptNativeApi; |
| 41 | + let ranOnMainThread = false; |
| 42 | + |
| 43 | + await NativeScript.runOnUI(() => { |
| 44 | + ranOnMainThread = NSThread.isMainThread === true; |
| 45 | + UIApplication.sharedApplication.keyWindow.tintColor = |
| 46 | + UIColor.systemPinkColor; |
| 47 | + }); |
| 48 | + |
| 49 | + return { |
| 50 | + backend: api?.backend, |
| 51 | + classes: api?.metadata?.classes ?? 0, |
| 52 | + constants: api?.metadata?.constants ?? 0, |
| 53 | + enums: api?.metadata?.enums ?? 0, |
| 54 | + ranOnMainThread, |
| 55 | + timeoutConstant: NSURLErrorTimedOut, |
| 56 | + darkStyle: UIUserInterfaceStyle.Dark, |
| 57 | + }; |
| 58 | +} |
| 59 | +export default function App() { |
| 60 | + const [tone, setTone] = useState<'blue' | 'green'>('blue'); |
| 61 | + const [summary, setSummary] = useState('Loading NativeScript...'); |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + readNativeSummary() |
| 65 | + .then((value) => setSummary(JSON.stringify(value, null, 2))) |
| 66 | + .catch((error) => { |
| 67 | + setSummary(error instanceof Error ? error.message : String(error)); |
| 68 | + }); |
| 69 | + }, []); |
| 70 | + |
| 71 | + const title = useMemo( |
| 72 | + () => (tone === 'blue' ? 'UIKit view from Expo' : 'Updated from React state'), |
| 73 | + [tone], |
| 74 | + ); |
| 75 | + |
| 76 | + return ( |
| 77 | + <ScrollView |
| 78 | + contentInsetAdjustmentBehavior="automatic" |
| 79 | + contentContainerStyle={{gap: 18, padding: 24}}> |
| 80 | + <View style={{gap: 8}}> |
| 81 | + <Text style={{fontSize: 28, fontWeight: '800'}}> |
| 82 | + NativeScript Expo |
| 83 | + </Text> |
| 84 | + <Text selectable style={{fontSize: 16, color: '#394150'}}> |
| 85 | + Define UIKit views directly in JavaScript and mount them in an Expo |
| 86 | + development build. |
| 87 | + </Text> |
| 88 | + </View> |
| 89 | + |
| 90 | + <NativeBadge title={title} tone={tone} style={{height: 56}} /> |
| 91 | + |
| 92 | + <Pressable |
| 93 | + onPress={() => setTone((value) => (value === 'blue' ? 'green' : 'blue'))} |
| 94 | + style={{ |
| 95 | + alignItems: 'center', |
| 96 | + borderRadius: 8, |
| 97 | + backgroundColor: '#111827', |
| 98 | + minHeight: 48, |
| 99 | + justifyContent: 'center', |
| 100 | + paddingHorizontal: 16, |
| 101 | + }}> |
| 102 | + <Text style={{color: 'white', fontSize: 16, fontWeight: '700'}}> |
| 103 | + Toggle Native Badge |
| 104 | + </Text> |
| 105 | + </Pressable> |
| 106 | + |
| 107 | + <Text |
| 108 | + selectable |
| 109 | + style={{ |
| 110 | + borderRadius: 8, |
| 111 | + backgroundColor: '#f3f4f6', |
| 112 | + color: '#111827', |
| 113 | + fontFamily: 'Menlo', |
| 114 | + fontSize: 13, |
| 115 | + lineHeight: 18, |
| 116 | + padding: 14, |
| 117 | + }}> |
| 118 | + {summary} |
| 119 | + </Text> |
| 120 | + </ScrollView> |
| 121 | + ); |
| 122 | +} |
0 commit comments