Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long press button #66 #69

Merged
merged 7 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions app/components/button.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// https://github.com/EnCiv/civil-pursuit/issues/43
import React, {useState, useRef} from 'react';
import React, {useState, useRef, useEffect} from 'react';
import { createUseStyles } from 'react-jss';
import { PositioningPortal } from '@codastic/react-positioning-portal';
import cx from 'classnames';

/**
Expand All @@ -24,23 +25,63 @@ function Button(props) {
} = props;

const [isDisabled, setIsDisabled] = useState(disabled);
const [isPortalOpen, setIsPortalOpen] = useState(false);
const timeRef = useRef(null);

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

const handleMouseDown = () => {
timeRef.current = setTimeout(() => {
setIsPortalOpen(true);
}, 500);
}

const handleMouseUp = () => {
clearTimeout(timeRef.current);
setIsPortalOpen(false);
}

useEffect(() => {
if (isPortalOpen) {
const displayTime = Math.min(8, 0.1 * title.length) * 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a difference. When I long click, the text appears. When I let go of the click, the text goes away immediately. The idea was that the text would remain visible so it could be read after moving your finger out of the way.

Also, I know I said min - but it may need to be Math.max - we want the larger number. But, it's not even holding for 8 secconds.

const timeout = setTimeout(() => {
setIsPortalOpen(false);
}, displayTime);
return () => clearTimeout(timeout);
}
}, [isPortalOpen, title.length]);


return (
<button
className={combinedClassName}
title={title}
disabled={isDisabled}
onClick={() => {
if (onDone) onDone();
if (disableOnClick) setIsDisabled(true);
}}
{...otherProps}
<span
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
>
{children}
</button>
<PositioningPortal
isOpen={isPortalOpen}
portalContent={
<span>
{title}
</span>
}>
<button
className={combinedClassName}
title={title}
disabled={isDisabled}
onClick={() => {
if (onDone) onDone();
if (disableOnClick) setIsDisabled(true);
}}
{...otherProps}
>
{children}
</button>
</PositioningPortal>

</span>

)
}

Expand Down
Loading