mirror of
https://github.com/outline/outline.git
synced 2026-01-04 10:09:52 -06:00
34 lines
749 B
TypeScript
34 lines
749 B
TypeScript
import isEqual from "lodash/isEqual";
|
|
import { action, computed, observable } from "mobx";
|
|
import type { FunctionComponent } from "react";
|
|
import { createPortal } from "react-dom";
|
|
|
|
export class NodeViewRenderer<T extends object> {
|
|
@observable public props: T;
|
|
|
|
public constructor(
|
|
public element: HTMLElement,
|
|
private Component: FunctionComponent,
|
|
props: T
|
|
) {
|
|
this.props = props;
|
|
}
|
|
|
|
@computed
|
|
public get content() {
|
|
return createPortal(<this.Component {...this.props} />, this.element);
|
|
}
|
|
|
|
@action
|
|
public updateProps(props: T) {
|
|
if (!isEqual(props, this.props)) {
|
|
this.props = props;
|
|
}
|
|
}
|
|
|
|
@action
|
|
public setProp<K extends keyof T>(key: K, value: T[K]) {
|
|
this.props[key] = value;
|
|
}
|
|
}
|