-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHighlighter.jsx
155 lines (139 loc) · 4.07 KB
/
Highlighter.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* TEAM: frontend_infra
* WATCHERS: ctan
*
* @flow
*/
import * as React from "react";
import {isEqual} from "lodash";
import {StyleSheet, css} from "aphrodite";
import colors, {type Color} from "./colors";
export type Option = {|
+label: string,
+value: string,
+disabled?: boolean,
|};
export type HighlighterProps = {|
/** any React.Node, Highlighter will add a highlight background alongside it's children */
+children: React.Node,
/** color of the highlight */
+color: Color,
/** Ref (`React.createRef`) attached to whichever element is currently highlighted, does not have to be a direct descendent */
+selectionRef: {current: null | HTMLElement},
/** default size and location of the highlighter, defaults to 0 for everything */
+defaultSizeAndLocation?: {|
left: number,
top: number,
height: number,
width: number,
|},
/** if true, renders the highlight before there is a selected element, defaults to false */
+initializeWithoutSelection?: boolean,
|};
/**
* @short Highlighter adds a background fill to a selected element
* @brandStatus V3
* @status Stable
* @category Interaction */
const Highlighter = React.memo<HighlighterProps>(function Highlighter({
children,
color,
selectionRef,
defaultSizeAndLocation = {
left: 0,
top: 0,
height: 0,
width: 0,
},
initializeWithoutSelection = false,
}: HighlighterProps) {
const [sizeAndLocation, setSizeAndLocation] = React.useState(
defaultSizeAndLocation
);
const [oocssWaitComplete, setOocssWaitComplete] = React.useState(false);
const [isHighlightVisible, setIsHighlightVisible] = React.useState(false);
const highlightTimeoutId = React.useRef();
const showHighlight = () => {
highlightTimeoutId.current = setTimeout(() => {
setIsHighlightVisible(true);
}, 0);
};
React.useEffect(() => {
let oocssTimeoutId;
if (!isHighlightVisible && initializeWithoutSelection)
setIsHighlightVisible(true);
// fixes bug where the background position is calculated before aphrodite styles are applied to the DOM
if (!oocssWaitComplete) {
oocssTimeoutId = setTimeout(setOocssWaitComplete, 0, true);
}
// On unmount, clear any timeouts
return () => {
clearTimeout(highlightTimeoutId.current);
clearTimeout(oocssTimeoutId);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
React.useLayoutEffect(() => {
if (!oocssWaitComplete) return;
const elementToHighlight = selectionRef.current;
const newSizeAndLocation = elementToHighlight
? {
left: elementToHighlight.offsetLeft,
top: elementToHighlight.offsetTop,
height: elementToHighlight.offsetHeight,
width: elementToHighlight.offsetWidth,
}
: {
left: 0,
top: 0,
height: 0,
width: 0,
};
if (!isEqual(sizeAndLocation, newSizeAndLocation)) {
setSizeAndLocation(newSizeAndLocation);
if (
!isHighlightVisible &&
!initializeWithoutSelection &&
newSizeAndLocation.width > 0 &&
newSizeAndLocation.height > 0
)
showHighlight();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [oocssWaitComplete, children]);
return (
<>
{isHighlightVisible && (
<div
aria-hidden="true"
className={css(styles.highlight)}
style={{
backgroundColor: colors[color],
height: sizeAndLocation.height,
width: sizeAndLocation.width,
transform: `translate(${sizeAndLocation.left}px, ${
sizeAndLocation.top
}px)`,
}}
/>
)}
{children}
</>
);
});
export default Highlighter;
const styles = StyleSheet.create({
highlight: {
position: "absolute",
left: 0,
top: 0,
transition: "300ms ease",
transitionProperty:
"width, height, transform, borderRadius, backgroundColor",
transformStyle: "flat",
animation: "3s linear 1s",
overflow: "hidden",
pointerEvents: "none",
userSelect: "none",
},
});