1
1
// https://github.com/EnCiv/civil-pursuit/issues/43
2
- import React , { useState , useRef } from 'react' ;
2
+ import React , { useState , useRef , useEffect } from 'react' ;
3
3
import { createUseStyles } from 'react-jss' ;
4
+ import { PositioningPortal } from '@codastic/react-positioning-portal' ;
4
5
import cx from 'classnames' ;
5
6
6
7
/**
@@ -25,25 +26,60 @@ function Button(props) {
25
26
26
27
const [ isDisabled , setIsDisabled ] = useState ( disabled ) ;
27
28
const [ parentIsHovered , setParentIsHovered ] = useState ( false ) ;
29
+ const [ isPortalOpen , setIsPortalOpen ] = useState ( false ) ;
30
+ const timeRef = useRef ( null ) ;
28
31
29
32
const classes = buttonStyles ( ) ;
30
33
const combinedClassName = cx ( classes . buttonBase , className , { 'hover' : parentIsHovered } ) ;
31
34
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
+ } , [ ] ) ;
33
53
34
54
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 }
44
59
>
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
+
47
83
)
48
84
}
49
85
0 commit comments