Files
pezkuwi-telemetry/frontend/src/components/Tooltip.tsx
T
Maciej Hirsz ebb01c1a7d Turbo Render (#298)
* More responsive React updates on scroll

* `Icon`s now use shadow dom

* Faster Sparkline

* Recycle table rows

* Separate Header from Chain to avoid vdom diffing

* Separate THead from Row.HEADER to avoid vdom diffing

* Throttle rendering updates on chain tabs, also styles

* Minor tweaks and fixes

* Created components for all columns

* Wrapping up Column refactor

* Rename Row--td to Column

* Lazy `Ago`

* Update styles for faster layouting

* Minor cleanup

* Fix Connection

* Use shadow DOM in `PolkadotIcon`

* Comments and tweaks for the List component

* Faster Tooltip and Truncate

* Minor tweaks

* Tooltiped columns can now be copied

* Future-proof Connection

* Remove the <div> wrapper from Icon

* Fix dash on missing graph data

* Clean up some SVGs

* Cleanup and comments

* Localize the use of `previousKeys` to `recalculateKeys`

* Custom appState disjoint from React component state

* Make appState and appUpdate refs readonly

* Cleanup
2020-11-11 13:41:01 +01:00

111 lines
2.3 KiB
TypeScript

import * as React from 'react';
import { Maybe } from '../common';
import './Tooltip.css';
export namespace Tooltip {
export interface Props {
text: string;
copy?: (cb: CopyCallback) => void;
className?: string;
position?: 'left' | 'right' | 'center';
onInit?: (update: UpdateCallback) => void;
}
export interface State {
copied: boolean;
}
export type UpdateCallback = (text: string) => void;
export type CopyCallback = Maybe<() => void>;
}
function copyToClipboard(text: string) {
const el = document.createElement('textarea');
el.value = text;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
export class Tooltip extends React.Component<Tooltip.Props, Tooltip.State> {
public state = { copied: false };
private el: HTMLDivElement;
private timer: NodeJS.Timer | null = null;
public componentDidMount() {
if (this.props.onInit) {
this.props.onInit(this.update);
}
if (this.props.copy) {
this.props.copy(this.onClick);
}
}
public componentWillUnmount() {
if (this.timer) {
clearTimeout(this.timer);
}
if (this.props.copy) {
this.props.copy(null);
}
}
public shouldComponentUpdate(
nextProps: Tooltip.Props,
nextState: Tooltip.State
) {
return (
this.props.text !== nextProps.text ||
this.state.copied !== nextState.copied
);
}
public render() {
const { text, className, position } = this.props;
const { copied } = this.state;
let tooltipClass = 'Tooltip';
if (position && position !== 'center') {
tooltipClass += ` Tooltip-${position}`;
}
if (copied) {
tooltipClass += ' Tooltip-copied';
}
return (
<div className={tooltipClass} ref={this.onRef}>
{copied ? 'Copied to clipboard!' : text}
</div>
);
}
private onRef = (el: HTMLDivElement) => {
this.el = el;
};
private update = (text: string) => {
this.el.textContent = text;
};
private onClick = () => {
copyToClipboard(this.props.text);
if (this.timer) {
clearTimeout(this.timer);
}
this.setState({ copied: true });
this.timer = setTimeout(this.restore, 2000);
};
private restore = () => {
this.setState({ copied: false });
this.timer = null;
};
}