Tutorials
Integrating with React
Terminosaurus provides a native integration allowing you to use React components in your terminal applications. This is useful if you want to create complex user interfaces or if you're already familiar with React.
Basic usage
Import the terminosaurus/react
entry point instead of the default one, and call render
instead of run
:
import {renderfunction render(element: React.ReactNode): Promise<void> (+1 overload)
Render a React element into the terminal. Return a promise that resolves when the screen is closed.} from 'terminosaurus/react';
renderfunction render(element: React.ReactNode): Promise<void> (+1 overload)
Render a React element into the terminal. Return a promise that resolves when the screen is closed.(<termJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
:divJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
>Hello, world!</termJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
:divJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
>);
CSS styling
The React integration provides more natural bindings for CSS properties:
- Enums are referenced by their name instead of their fully-qualified names (ie you should use
"hidden"
instead ofStyleValues.Overflow.Hidden
). - CSS props are top-level rather than aggregated in a separate
style
object (ie you should useoverflow="hidden"
instead ofstyle={{overflow: "hidden"}}
). - Numeric units are interpreted as pixel units (ie
width={100}
is equivalent towidth="100px"
). String units suffixed by%
are treated as relative sizes.
import {renderfunction render(element: React.ReactNode): Promise<void> (+1 overload)
Render a React element into the terminal. Return a promise that resolves when the screen is closed.} from 'terminosaurus/react';
renderfunction render(element: React.ReactNode): Promise<void> (+1 overload)
Render a React element into the terminal. Return a promise that resolves when the screen is closed.(
<termJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
:divJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
widthwidth?: string | number | undefined
={`100%`} heightheight?: string | number | undefined
={`100%`} borderborder?: "inherit" | "simple" | "strong" | "modern" | {} | [[string | null], [string | null, string | null], [string | null, string | null, string | null, string | null], [...], [...]] | "double" | "block" | "rounded" | undefined
={`modern`} backgroundColorbackgroundColor?: string | undefined
={`blue`} colorcolor?: string | null | undefined
={`white`}>
Hello, world!
</termJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
:divJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
>
);
Event handling
You can attach event handlers to React components using the on
prefix:
import {useStatefunction useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] (+1 overload)
Returns a stateful value, and a function to update it.} from 'react';
import {renderfunction render(element: React.ReactNode): Promise<void> (+1 overload)
Render a React element into the terminal. Return a promise that resolves when the screen is closed.} from 'terminosaurus/react';
function Counterfunction Counter(): React.JSX.Element
() {
const [countconst count: number
, setCountconst setCount: React.Dispatch<React.SetStateAction<number>>
] = useStateuseState<number>(initialState: number | (() => number)): [number, React.Dispatch<React.SetStateAction<number>>] (+1 overload)
Returns a stateful value, and a function to update it.(0);
return (
<termJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
:divJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
onClickonClick?: ((e: Event<TermElement, {
mouse: Mouse;
worldCoordinates: Point;
contentCoordinates: Point;
}>) => void) | undefined
={() => setCountconst setCount: (value: React.SetStateAction<number>) => void
(countconst count: number
+ 1)}>
Clicked {countconst count: number
} times
</termJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
:divJSX.IntrinsicElements['term:div']: {
key?: string | undefined;
ref?: React.Ref<TermElement> | undefined;
} & Partial<AllPropertiesInputs> & {
decorated?: boolean | undefined;
} & AllReactEventsFor<...> & HasChildren
>
);
}
renderfunction render(element: React.ReactNode): Promise<void> (+1 overload)
Render a React element into the terminal. Return a promise that resolves when the screen is closed.(
<Counterfunction Counter(): React.JSX.Element
/>
);