Skip to content

Commit 1cc6514

Browse files
committed
Show the title after pressing for 500ms
1 parent 189ad66 commit 1cc6514

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

app/components/button.jsx

+49-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// https://github.com/EnCiv/civil-pursuit/issues/43
2-
import React, {useState, useRef} from 'react';
2+
import React, {useState, useRef, useEffect} from 'react';
33
import { createUseStyles } from 'react-jss';
4+
import { PositioningPortal } from '@codastic/react-positioning-portal';
45
import cx from 'classnames';
56

67
/**
@@ -25,25 +26,60 @@ function Button(props) {
2526

2627
const [isDisabled, setIsDisabled] = useState(disabled);
2728
const [parentIsHovered, setParentIsHovered] = useState(false);
29+
const [isPortalOpen, setIsPortalOpen] = useState(false);
30+
const timeRef = useRef(null);
2831

2932
const classes = buttonStyles();
3033
const combinedClassName = cx(classes.buttonBase, className, { 'hover': parentIsHovered });
3134

32-
console.log(combinedClassName)
35+
const handleMouseDown = () => {
36+
timeRef.current = setTimeout(() => {
37+
setIsPortalOpen(true);
38+
}, 500);
39+
}
40+
41+
const handleMouseUp = () => {
42+
clearTimeout(timeRef.current);
43+
setIsPortalOpen(false);
44+
}
45+
46+
useEffect(() => {
47+
return () => {
48+
if (timeRef.current) {
49+
clearTimeout(timeRef.current);
50+
}
51+
};
52+
}, []);
3353

3454
return (
35-
<button
36-
className={combinedClassName}
37-
title={title}
38-
disabled={isDisabled}
39-
onClick={() => {
40-
if (onDone) onDone();
41-
if (disableOnClick) setIsDisabled(true);
42-
}}
43-
{...otherProps}
55+
<div
56+
onMouseDown={handleMouseDown}
57+
onMouseUp={handleMouseUp}
58+
onMouseLeave={handleMouseUp}
4459
>
45-
{children}
46-
</button>
60+
<PositioningPortal
61+
isOpen={isPortalOpen}
62+
portalContent={
63+
<div>
64+
{title}
65+
</div>
66+
}>
67+
<button
68+
className={combinedClassName}
69+
title={title}
70+
disabled={isDisabled}
71+
onClick={() => {
72+
if (onDone) onDone();
73+
if (disableOnClick) setIsDisabled(true);
74+
}}
75+
{...otherProps}
76+
>
77+
{children}
78+
</button>
79+
</PositioningPortal>
80+
81+
</div>
82+
4783
)
4884
}
4985

0 commit comments

Comments
 (0)