@teaui/core
Version:
A high-level terminal UI library for Node
144 lines (143 loc) • 5.03 kB
TypeScript
import type { Viewport } from './Viewport.js';
import type { Screen } from './Screen.js';
import type { Purpose } from './Palette.js';
import { Palette } from './Palette.js';
import { Container } from './Container.js';
import { System } from './System.js';
import { type KeyEvent, type MouseEvent } from './events/index.js';
import { Size } from './geometry.js';
import { Color } from './Color.js';
import { type Edges, type LegendItem } from './types.js';
export type Dimension = number | 'fill' | 'shrink' | 'natural';
export type FlexSize = 'natural' | number;
export type FlexShorthand = FlexSize | `flex${number}`;
export declare function parseFlexShorthand(flex: FlexShorthand): FlexSize;
export type Pin = 'horizontal' | 'vertical';
export interface Props {
purpose?: Palette | Purpose;
/**
* A heading for this view. Container views (Box, Page, Alert, Drawer, etc.)
* can read this from their children to display a section heading without
* requiring a wrapper component.
*/
heading?: string;
x?: number;
y?: number;
width?: Dimension;
height?: Dimension;
minWidth?: number;
minHeight?: number;
maxWidth?: number;
maxHeight?: number;
padding?: number | Partial<Edges>;
paddingTop?: number;
paddingRight?: number;
paddingBottom?: number;
paddingLeft?: number;
isVisible?: boolean;
background?: Color;
flex?: FlexShorthand;
/**
* Pin this view's size to the visible rect in the given direction. Only
* meaningful inside a scrollable Stack — a pinned view will not scroll in the
* pinned direction and its size in that dimension will match the visible area
* rather than the full content area.
*
* - `'horizontal'` — pin width to visible width (don't scroll horizontally)
* - `'vertical'` — pin height to visible height (don't scroll vertically)
*/
pin?: Pin;
debug?: boolean;
}
export declare abstract class View {
#private;
parent: Container | undefined;
debug: boolean;
padding: Edges | undefined;
flex: FlexSize;
pin: Pin | undefined;
constructor(props?: Props);
update(props: Props): void;
get purpose(): Palette;
set purpose(value: Palette | Purpose | undefined);
childPalette(_view: View): Palette;
get heading(): string | undefined;
set heading(value: string | undefined);
get isVisible(): boolean;
set isVisible(value: boolean);
get background(): Color | undefined;
set background(value: Color | undefined);
get screen(): Screen | undefined;
get children(): View[];
get contentSize(): Size;
get isHover(): boolean;
get isPressed(): boolean;
get hasFocus(): boolean;
get width(): Dimension | undefined;
get height(): Dimension | undefined;
abstract naturalSize(available: Size): Size;
abstract render(viewport: Viewport): void;
/**
* Called from a view when a property change could affect naturalSize
*/
invalidateSize(): void;
/**
* Indicates that a rerender is needed (but size is not affected)
*/
invalidateRender(): void;
/**
* Called before being added to the parent View
*/
willMoveTo(_parent: View): void;
/**
* Called after being removed from the parent View
*/
didMoveFrom(_parent: View): void;
/**
* Called after being added to a Screen
*/
didMount(_screen: Screen): void;
/**
* Called after being removed from a Screen (even when about to be moved to a new
* screen).
*/
didUnmount(_screen: Screen): void;
/**
* Called when this view gains keyboard focus. Only called on views that call
* `viewport.registerFocus()` in their render method.
*/
didFocus(): void;
/**
* Called when this view loses keyboard focus. Only called on views that previously
* had focus (i.e. `didFocus()` was called).
*/
didBlur(): void;
/**
* Returns keyboard shortcut items for this view, shown by AutoLegend
* when this view has focus. Override in subclasses to advertise shortcuts.
*/
legendItems(): LegendItem[];
removeFromParent(): void;
moveToScreen(screen: Screen | undefined): void;
/**
* To register for this event, call `viewport.registerFocus()`, which returns `true`
* if the current view has the keyboard focus.
*/
receiveKey(_event: KeyEvent): void;
/**
* Called when text is pasted from the clipboard (bracketed paste).
* Only dispatched to the currently focused view.
*/
receivePaste(_text: string): void;
/**
* To register for this event, call `viewport.registerMouse()`
*/
receiveMouse(event: MouseEvent, _system: System): void;
/**
* Receives the time-delta between previous and current render. Return 'true' if
* this function causes the view to need a rerender.
*
* To register for this event, call `viewport.registerTick()`
*/
receiveTick(_dt: number): boolean;
}