Interactive interface for browsing global, full-resolution satellite imagery
WV-2396: Dateline tooltip hover delay (#3976)
Ryan Weiler803d0b0adc9e
diff --git a/web/js/components/dateline/line.js b/web/js/components/dateline/line.jsindex dcf92747f..f85f49d3e 100644--- a/web/js/components/dateline/line.js+++ b/web/js/components/dateline/line.js@@ -1,8 +1,9 @@-import React from 'react';+import React, { useState, useEffect, useRef } from 'react';import PropTypes from 'prop-types';import OlOverlay from 'ol/Overlay';import util from '../../util/util';import LineText from './text';+import usePrevious from '../../util/customHooks';const lineStyles = {dashArray: '5, 10',@@ -15,136 +16,126 @@ const lineStyles = {},};-class Line extends React.Component {- constructor(props) {- super(props);- this.state = {- overlay: '',- hovered: false,- textActive: props.alwaysShow,- };- this.nodeRef = React.createRef();- }+export default function Line (props) {+ const {+ id,+ alwaysShow,+ date,+ lineX,+ lineY,+ isCompareActive,+ style,+ map,+ height,+ setTextCoords,+ textCoords,+ hideText,+ } = props;- componentDidMount() {- const { lineX, map } = this.props;- const overlay = new OlOverlay({- element: this.nodeRef.current,+ const [overlay, setOverlay] = useState('');+ const [hovered, toggleHovered] = useState(false);+ const [textActive, toggleTextActive] = useState(alwaysShow);+ const prevAlwaysShow = usePrevious(alwaysShow);++ const nodeRef = useRef();+ const timerRef = useRef();++ useEffect(() => {+ const newOverlay = new OlOverlay({+ element: nodeRef.current,stopEvent: false,});- overlay.setPosition([lineX, 90]);- map.addOverlay(overlay);- this.setState({ overlay });- }+ newOverlay.setPosition([lineX, 90]);+ map.addOverlay(newOverlay);+ setOverlay(newOverlay);+ }, []);- componentDidUpdate(prevProps) {- const { lineX, lineY, alwaysShow } = this.props;- const { overlay } = this.state;- overlay.setPosition([lineX, lineY]);- if (!alwaysShow && alwaysShow !== prevProps.alwaysShow) {- // eslint-disable-next-line react/no-did-update-set-state- this.setState({ textActive: false });+ useEffect(() => {+ if (overlay !== '') return overlay.setPosition([lineX, lineY]);+ if (!alwaysShow && alwaysShow !== prevAlwaysShow) {+ toggleTextActive(true);}- }-- mouseOver = () => {- this.setState({- hovered: true,- });- }+ });- mouseOut = () => {- this.setState({- hovered: false,- });- }+ const mouseOver = () => {+ toggleHovered(true);+ };- mouseOverHidden = (e) => {- const { map, setTextCoords } = this.props;- setTextCoords(map.getCoordinateFromPixel([e.clientX, e.clientY]));- this.setState({- textActive: true,- });- }+ const mouseOut = () => {+ toggleHovered(false);+ };- mouseOutHidden = () => {- this.setState({ textActive: false });- }+ const mouseOverHidden = (e) => {+ const coords = map.getCoordinateFromPixel([e.clientX, e.clientY]);+ timerRef.current = setTimeout(() => {+ setTextCoords(coords);+ toggleTextActive(true);+ }, 300);+ };- render() {- const {- id,- alwaysShow,- date,- lineX,- isCompareActive,- style,- map,- height,- textCoords,- hideText,- } = this.props;- const { hovered, textActive } = this.state;+ const mouseLeaveHidden = () => {+ clearTimeout(timerRef.current);+ toggleTextActive(false);+ };- const {- strokeWidth, dashArray, color, svgStyle, width,- } = lineStyles;- const useOpacity = alwaysShow || util.browser.mobileAndTabletDevice || hovered- ? lineStyles.opacity- : '0';+ const {+ strokeWidth, dashArray, color, svgStyle, width,+ } = lineStyles;+ const useOpacity = alwaysShow || util.browser.mobileAndTabletDevice || hovered+ ? lineStyles.opacity+ : '0';- return (- <>- <svg- id={id}- ref={this.nodeRef}- className="dateline-case"- style={svgStyle}- height={height}- width={width}- onMouseOver={this.mouseOver}- onMouseOut={this.mouseOut}- >- <line- strokeWidth={strokeWidth}- stroke={color}- opacity={useOpacity}- x1={strokeWidth / 2}- x2={strokeWidth / 2}- strokeDasharray={dashArray}- y1="0"- y2={height}- />- <line- className="dateline-hidden"- onMouseOver={this.mouseOverHidden}- onMouseMove={this.mouseOverHidden}- onMouseOut={this.mouseOutHidden}- style={style}- opacity="0"- x1={strokeWidth / 2}- x2={strokeWidth / 2}- strokeWidth={strokeWidth}- stroke={color}- y1="0"- y2={height}- />- </svg>- <LineText- active={!hideText && (alwaysShow || textActive)}- map={map}- date={date}- x={lineX}- y={90}- isCompareActive={isCompareActive}- isLeft={lineX < 0}- textCoords={textCoords}+ return (+ <>+ <svg+ id={id}+ ref={nodeRef}+ className="dateline-case"+ style={svgStyle}+ height={height}+ width={width}+ onMouseOver={mouseOver}+ onMouseOut={mouseOut}+ >+ <line+ strokeWidth={strokeWidth}+ stroke={color}+ opacity={useOpacity}+ x1={strokeWidth / 2}+ x2={strokeWidth / 2}+ strokeDasharray={dashArray}+ y1="0"+ y2={height}/>- </>- );- }+ <line+ className="dateline-hidden"+ onMouseOver={mouseOverHidden}+ onMouseLeave={mouseLeaveHidden}+ style={style}+ opacity="0"+ x1={strokeWidth / 2}+ x2={strokeWidth / 2}+ strokeWidth={strokeWidth}+ stroke={color}+ y1="0"+ y2={height}+ />+ </svg>+ <LineText+ active={!hideText && (alwaysShow || textActive)}+ map={map}+ date={date}+ x={lineX}+ y={90}+ isCompareActive={isCompareActive}+ isLeft={lineX < 0}+ textCoords={textCoords}+ />+ </>+ );}+Line.propTypes = {alwaysShow: PropTypes.bool,date: PropTypes.object,@@ -160,4 +151,3 @@ Line.propTypes = {hideText: PropTypes.bool,};-export default Line;diff --git a/web/js/util/customHooks.js b/web/js/util/customHooks.jsnew file mode 100644index 000000000..f29398ea7--- /dev/null+++ b/web/js/util/customHooks.js@@ -0,0 +1,10 @@+import { useEffect, useRef } from 'react';++// customHook to get previous value of a prop... see prevProps for class components+export default function usePrevious (value) {+ const ref = useRef();+ useEffect(() => {+ ref.current = value;+ }, [value]);+ return ref.current;+}