Skip to content

Commit 29207cf

Browse files
committed
fix: improve copy tooltip perf
1 parent a3cbb14 commit 29207cf

File tree

5 files changed

+85
-35
lines changed

5 files changed

+85
-35
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"@types/react-dom": "^16.0.0",
4040
"@types/react-hot-loader": "^3.0.3",
4141
"@types/react-tabs": "^1.0.2",
42-
"@types/react-tooltip": "^3.3.3",
4342
"@types/webpack": "^3.0.5",
4443
"@types/webpack-env": "^1.13.0",
4544
"awesome-typescript-loader": "^3.2.2",
@@ -95,7 +94,7 @@
9594
"react-dropdown": "^1.3.0",
9695
"react-hot-loader": "3.1.3",
9796
"react-tabs": "^2.0.0",
98-
"react-tooltip": "^3.4.0",
97+
"react-tippy": "^1.2.2",
9998
"remarkable": "^1.7.1",
10099
"slugify": "^1.2.1",
101100
"stickyfill": "^1.1.1",
Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as React from 'react';
2-
import * as ReactTooltip from 'react-tooltip';
2+
import { Tooltip } from 'react-tippy';
3+
import { injectGlobal } from '../styled-components';
4+
5+
import styles from 'react-tippy/dist/tippy.css';
6+
7+
injectGlobal`${styles}`;
38

49
import { ClipboardService } from '../services/ClipboardService';
510

@@ -12,7 +17,17 @@ export interface CopyButtonWrapperProps {
1217
) => React.ReactNode;
1318
}
1419

15-
export class CopyButtonWrapper extends React.PureComponent<CopyButtonWrapperProps> {
20+
export class CopyButtonWrapper extends React.PureComponent<
21+
CopyButtonWrapperProps,
22+
{ tooltipShown: boolean }
23+
> {
24+
constructor(props) {
25+
super(props);
26+
this.state = {
27+
tooltipShown: false,
28+
};
29+
}
30+
1631
render() {
1732
return this.props.children({ renderCopyButton: this.renderCopyButton });
1833
}
@@ -23,33 +38,37 @@ export class CopyButtonWrapper extends React.PureComponent<CopyButtonWrapperProp
2338
? this.props.data
2439
: JSON.stringify(this.props.data, null, 2);
2540
ClipboardService.copyCustom(content);
41+
this.showTooltip();
2642
};
2743

2844
renderCopyButton = () => {
2945
return (
30-
<>
31-
<span
32-
onClick={this.copy}
33-
data-tip={true}
34-
data-for="copy_tooltip"
35-
data-event="click"
36-
data-event-off="mouseleave"
46+
<span onClick={this.copy}>
47+
<Tooltip
48+
// options
49+
title={ClipboardService.isSupported() ? 'Copied' : 'Not supported in your browser'}
50+
position="top"
51+
open={this.state.tooltipShown}
52+
arrow="true"
53+
duration={150}
54+
trigger="manual"
55+
theme="light"
3756
>
3857
Copy
39-
</span>
40-
<ReactTooltip
41-
isCapture={true}
42-
id="copy_tooltip"
43-
place="top"
44-
getContent={this.getTooltipContent}
45-
type="light"
46-
effect="solid"
47-
/>
48-
</>
58+
</Tooltip>
59+
</span>
4960
);
5061
};
5162

52-
getTooltipContent() {
53-
return ClipboardService.isSupported() ? 'Copied' : 'Not supported in your browser';
63+
showTooltip() {
64+
this.setState({
65+
tooltipShown: true,
66+
});
67+
68+
setTimeout(() => {
69+
this.setState({
70+
tooltipShown: false,
71+
});
72+
}, 1500);
5473
}
5574
}

src/components/Redoc/elements.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const RedocWrap = styled.div`
3737
export const ApiContent = styled.div`
3838
z-index: 10;
3939
position: relative;
40+
overflow: hidden;
4041
width: calc(100% - ${props => props.theme.menu.width});
4142
${media.lessThan('small')`
4243
width: 100%;

src/utils/decorators.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const throttle = function(func, wait) {
2+
var context, args, result;
3+
var timeout: any = null;
4+
var previous = 0;
5+
var later = function() {
6+
(previous = new Date().getTime()), (timeout = null);
7+
result = func.apply(context, args);
8+
if (!timeout) context = args = null;
9+
};
10+
return function() {
11+
var now = new Date().getTime();
12+
var remaining = wait - (now - previous);
13+
context = this;
14+
args = arguments;
15+
if (remaining <= 0 || remaining > wait) {
16+
if (timeout) {
17+
clearTimeout(timeout);
18+
timeout = null;
19+
}
20+
previous = now;
21+
result = func.apply(context, args);
22+
if (!timeout) context = args = null;
23+
} else if (!timeout) {
24+
timeout = setTimeout(later, remaining);
25+
}
26+
return result;
27+
};
28+
};
29+
30+
export function Throttle(delay: number) {
31+
return (_, _2, desc: PropertyDescriptor) => {
32+
desc.value = throttle(desc.value, delay);
33+
};
34+
}

yarn.lock

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@
134134
dependencies:
135135
"@types/react" "*"
136136

137-
"@types/react-tooltip@^3.3.3":
138-
version "3.3.3"
139-
resolved "https://registry.yarnpkg.com/@types/react-tooltip/-/react-tooltip-3.3.3.tgz#3b6dbb278fc8317ad04f0ce1972a0972f5450aa7"
140-
dependencies:
141-
"@types/react" "*"
142-
143137
"@types/react@*", "@types/react@^16.0.30":
144138
version "16.0.35"
145139
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.0.35.tgz#7ce8a83cad9690fd965551fc513217a74fc9e079"
@@ -1168,7 +1162,7 @@ class-utils@^0.3.5:
11681162
isobject "^3.0.0"
11691163
static-extend "^0.1.1"
11701164

1171-
classnames@^2.2.0, classnames@^2.2.3, classnames@^2.2.5:
1165+
classnames@^2.2.0, classnames@^2.2.3:
11721166
version "2.2.5"
11731167
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.5.tgz#fb3801d453467649ef3603c7d61a02bd129bde6d"
11741168

@@ -5385,6 +5379,10 @@ pn@^1.1.0:
53855379
version "1.1.0"
53865380
resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"
53875381

5382+
popper.js@^1.11.1:
5383+
version "1.12.9"
5384+
resolved "https://registry.yarnpkg.com/popper.js/-/popper.js-1.12.9.tgz#0dfbc2dff96c451bb332edcfcfaaf566d331d5b3"
5385+
53885386
portfinder@^1.0.9:
53895387
version "1.0.13"
53905388
resolved "https://registry.yarnpkg.com/portfinder/-/portfinder-1.0.13.tgz#bb32ecd87c27104ae6ee44b5a3ccbf0ebb1aede9"
@@ -5972,12 +5970,11 @@ react-test-renderer@^16.0.0-0:
59725970
object-assign "^4.1.1"
59735971
prop-types "^15.6.0"
59745972

5975-
react-tooltip@^3.4.0:
5976-
version "3.4.0"
5977-
resolved "https://registry.yarnpkg.com/react-tooltip/-/react-tooltip-3.4.0.tgz#037f38f797c3e6b1b58d2534ccc8c2c76af4f52d"
5973+
react-tippy@^1.2.2:
5974+
version "1.2.2"
5975+
resolved "https://registry.yarnpkg.com/react-tippy/-/react-tippy-1.2.2.tgz#061467d34d29e7a5a9421822d125c451d6bb5153"
59785976
dependencies:
5979-
classnames "^2.2.5"
5980-
prop-types "^15.6.0"
5977+
popper.js "^1.11.1"
59815978

59825979
react@^16.2.0:
59835980
version "16.2.0"

0 commit comments

Comments
 (0)