UNPKG

5.63 kBTypeScriptView Raw
1import { IDisposable } from '@lumino/disposable';
2import { Message } from '@lumino/messaging';
3import { ISignal, Signal } from '@lumino/signaling';
4import { Widget } from '@lumino/widgets';
5import * as React from 'react';
6type ReactRenderElement = Array<React.ReactElement<any>> | React.ReactElement<any>;
7/**
8 * An abstract class for a Lumino widget which renders a React component.
9 */
10export declare abstract class ReactWidget extends Widget {
11 constructor();
12 /**
13 * Creates a new `ReactWidget` that renders a constant element.
14 * @param element React element to render.
15 */
16 static create(element: ReactRenderElement): ReactWidget;
17 /**
18 * Render the content of this widget using the virtual DOM.
19 *
20 * This method will be called anytime the widget needs to be rendered, which
21 * includes layout triggered rendering.
22 *
23 * Subclasses should define this method and return the root React nodes here.
24 */
25 protected abstract render(): ReactRenderElement | null;
26 /**
27 * Called to update the state of the widget.
28 *
29 * The default implementation of this method triggers
30 * VDOM based rendering by calling the `renderDOM` method.
31 */
32 protected onUpdateRequest(msg: Message): void;
33 /**
34 * Called after the widget is attached to the DOM
35 */
36 protected onAfterAttach(msg: Message): void;
37 /**
38 * Called before the widget is detached from the DOM.
39 */
40 protected onBeforeDetach(msg: Message): void;
41 /**
42 * Render the React nodes to the DOM.
43 *
44 * @returns a promise that resolves when the rendering is done.
45 */
46 private renderDOM;
47 renderPromise?: Promise<void>;
48 private _rootDOM;
49}
50/**
51 * An abstract ReactWidget with a model.
52 */
53export declare abstract class VDomRenderer<T extends VDomRenderer.IModel | null = null> extends ReactWidget {
54 /**
55 * Create a new VDomRenderer
56 */
57 constructor(model?: T);
58 /**
59 * A signal emitted when the model changes.
60 */
61 get modelChanged(): ISignal<this, void>;
62 /**
63 * Set the model and fire changed signals.
64 */
65 set model(newValue: T);
66 /**
67 * Get the current model.
68 */
69 get model(): T;
70 /**
71 * Dispose this widget.
72 */
73 dispose(): void;
74 private _model;
75 private _modelChanged;
76}
77/**
78 * Props for the UseSignal component
79 */
80export interface IUseSignalProps<SENDER, ARGS> {
81 /**
82 * Phosphor signal to connect to.
83 */
84 signal: ISignal<SENDER, ARGS>;
85 /**
86 * Initial value to use for the sender, used before the signal emits a value.
87 * If not provided, initial sender will be undefined
88 */
89 initialSender?: SENDER;
90 /**
91 * Initial value to use for the args, used before the signal emits a value.
92 * If not provided, initial args will be undefined.
93 */
94 initialArgs?: ARGS;
95 /**
96 * Function mapping the last signal value or initial values to an element to render.
97 *
98 * Note: returns `React.ReactNode` as per
99 * https://github.com/sw-yx/react-typescript-cheatsheet#higher-order-componentsrender-props
100 */
101 children: (sender?: SENDER, args?: ARGS) => React.ReactNode;
102 /**
103 * Given the last signal value, should return whether to update the state or not.
104 *
105 * The default unconditionally returns `true`, so you only have to override if you want
106 * to skip some updates.
107 */
108 shouldUpdate?: (sender: SENDER, args: ARGS) => boolean;
109}
110/**
111 * State for the UseSignal component
112 */
113export interface IUseSignalState<SENDER, ARGS> {
114 value: [SENDER?, ARGS?];
115}
116/**
117 * UseSignal provides a way to hook up a Lumino signal to a React element,
118 * so that the element is re-rendered every time the signal fires.
119 *
120 * It is implemented through the "render props" technique, using the `children`
121 * prop as a function to render, so that it can be used either as a prop or as a child
122 * of this element
123 * https://reactjs.org/docs/render-props.html
124 *
125 *
126 * Example as child:
127 *
128 * ```
129 * function LiveButton(isActiveSignal: ISignal<any, boolean>) {
130 * return (
131 * <UseSignal signal={isActiveSignal} initialArgs={true}>
132 * {(_, isActive) => <Button isActive={isActive}>}
133 * </UseSignal>
134 * )
135 * }
136 * ```
137 *
138 * Example as prop:
139 *
140 * ```
141 * function LiveButton(isActiveSignal: ISignal<any, boolean>) {
142 * return (
143 * <UseSignal
144 * signal={isActiveSignal}
145 * initialArgs={true}
146 * children={(_, isActive) => <Button isActive={isActive}>}
147 * />
148 * )
149 * }
150 * ```
151 */
152export declare class UseSignal<SENDER, ARGS> extends React.Component<IUseSignalProps<SENDER, ARGS>, IUseSignalState<SENDER, ARGS>> {
153 constructor(props: IUseSignalProps<SENDER, ARGS>);
154 componentDidMount(): void;
155 componentWillUnmount(): void;
156 private slot;
157 render(): React.ReactNode;
158}
159/**
160 * The namespace for VDomRenderer statics.
161 */
162export declare namespace VDomRenderer {
163 /**
164 * An interface for a model to be used with vdom rendering.
165 */
166 interface IModel extends IDisposable {
167 /**
168 * A signal emitted when any model state changes.
169 */
170 readonly stateChanged: ISignal<this, void>;
171 }
172}
173/**
174 * Concrete implementation of VDomRenderer model.
175 */
176export declare class VDomModel implements VDomRenderer.IModel {
177 /**
178 * A signal emitted when any model state changes.
179 */
180 readonly stateChanged: Signal<this, void>;
181 /**
182 * Test whether the model is disposed.
183 */
184 get isDisposed(): boolean;
185 /**
186 * Dispose the model.
187 */
188 dispose(): void;
189 private _isDisposed;
190}
191export {};