Skip to content

Commit c7dc438

Browse files
authored
Merge pull request #69 from EnCiv/long-press-button-#66
Long press button #66
2 parents 316e495 + 10653b9 commit c7dc438

File tree

3 files changed

+1479
-968
lines changed

3 files changed

+1479
-968
lines changed

app/components/button.jsx

+53-12
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
/**
@@ -24,23 +25,63 @@ function Button(props) {
2425
} = props;
2526

2627
const [isDisabled, setIsDisabled] = useState(disabled);
28+
const [isPortalOpen, setIsPortalOpen] = useState(false);
29+
const timeRef = useRef(null);
2730

2831
const classes = buttonStyles();
2932
const combinedClassName = cx(classes.buttonBase, className);
3033

34+
const handleMouseDown = () => {
35+
timeRef.current = setTimeout(() => {
36+
setIsPortalOpen(true);
37+
}, 500);
38+
}
39+
40+
const handleMouseUp = () => {
41+
clearTimeout(timeRef.current);
42+
setTimeout(() => setIsPortalOpen(false), displayTime);
43+
}
44+
45+
useEffect(() => {
46+
if (isPortalOpen) {
47+
const displayTime = Math.max(8, 0.1 * title.length) * 1000;
48+
const timeout = setTimeout(() => {
49+
setIsPortalOpen(false);
50+
}, displayTime);
51+
return () => clearTimeout(timeout);
52+
}
53+
}, [isPortalOpen, title.length]);
54+
55+
3156
return (
32-
<button
33-
className={combinedClassName}
34-
title={title}
35-
disabled={isDisabled}
36-
onClick={() => {
37-
if (onDone) onDone();
38-
if (disableOnClick) setIsDisabled(true);
39-
}}
40-
{...otherProps}
57+
<span
58+
onMouseDown={handleMouseDown}
59+
onMouseUp={handleMouseUp}
60+
onMouseLeave={handleMouseUp}
4161
>
42-
{children}
43-
</button>
62+
<PositioningPortal
63+
isOpen={isPortalOpen}
64+
portalContent={
65+
<span>
66+
{title}
67+
</span>
68+
}>
69+
<button
70+
className={combinedClassName}
71+
title={title}
72+
disabled={isDisabled}
73+
onClick={() => {
74+
if (onDone) onDone();
75+
if (disableOnClick) setIsDisabled(true);
76+
}}
77+
{...otherProps}
78+
>
79+
{children}
80+
</button>
81+
</PositioningPortal>
82+
83+
</span>
84+
4485
)
4586
}
4687

0 commit comments

Comments
 (0)