Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeTerminalModule.ts

65 lines1.8 KB

1import type { ComponentType } from "react";
2import type { NativeSyntheticEvent, ViewProps } from "react-native";
3import { requireNativeView } from "expo";
4​
5const NATIVE_TERMINAL_MODULE_NAME = "T3TerminalSurface";
6​
7interface ExpoGlobalWithViewConfig {
8 readonly expo?: {
9 getViewConfig?: (moduleName: string, viewName?: string) => unknown;
10 };
12​
13interface TerminalInputEvent {
14 readonly data: string;
16​
17interface TerminalResizeEvent {
18 readonly cols: number;
19 readonly rows: number;
21​
22export interface NativeTerminalSurfaceProps extends ViewProps {
23 readonly appearanceScheme?: "light" | "dark";
24 readonly themeConfig?: string;
25 readonly backgroundColor?: string;
26 readonly foregroundColor?: string;
27 readonly mutedForegroundColor?: string;
28 readonly terminalKey: string;
29 readonly initialBuffer: string;
30 readonly fontSize: number;
31 readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
32 readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
34​
35let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
36​
37function getExpoViewConfig(moduleName: string) {
38 return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
39 moduleName,
40 );
42​
43export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null {
44 if (cachedNativeTerminalSurfaceView) {
45 return cachedNativeTerminalSurfaceView;
46 }
47​
48 if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) {
49 return null;
50 }
51​
52 try {
53 cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>(
54 NATIVE_TERMINAL_MODULE_NAME,
55 );
56 } catch {
57 return null;
58 }
59​
60 return cachedNativeTerminalSurfaceView ?? null;
62​
63export function hasNativeTerminalSurface() {
64 return resolveNativeTerminalSurfaceView() !== null;