-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from stoplightio/feat/SL-1331/controlled-mode-…
…popup [SL-1331] Popup controlled mode
- Loading branch information
Showing
7 changed files
with
244 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import * as React from 'react'; | ||
|
||
import { useWindowResize } from '../hooks/useWindowResize'; | ||
import { useTheme } from '../theme'; | ||
import { PopupContent } from './PopupContent'; | ||
import { IPopupDefaultProps, IPopupProps } from './types'; | ||
import { calculateStyles, getDefaultStyle } from './utils'; | ||
|
||
export { IPopupProps, IPopupDefaultProps }; | ||
|
||
export interface IPopup extends IPopupProps {} | ||
|
||
export const Popup: React.FunctionComponent<IPopup> = props => { | ||
const { hideDelay, width, offset, posX, posY, show = false } = props; | ||
|
||
const theme = useTheme(); | ||
|
||
const controlled = 'show' in props; | ||
const triggerRef = React.useRef<HTMLElement>(null); | ||
const contentRef = React.useRef<HTMLDivElement>(null); | ||
const [visibility, setVisibility] = React.useState<boolean>(false); | ||
const isVisible = controlled ? show : visibility; | ||
const lastResizeTimestamp = useWindowResize(); | ||
const [style, setStyle] = React.useState<React.CSSProperties>({}); | ||
let isOverTrigger: boolean = false; | ||
let isOverContent: boolean = false; | ||
let willHide: NodeJS.Timer | number | null = null; | ||
|
||
const repaint = React.useCallback( | ||
() => { | ||
if (isVisible) { | ||
setStyle({ | ||
...getDefaultStyle(props), | ||
...calculateStyles(triggerRef, contentRef, props), | ||
}); | ||
} | ||
}, | ||
[triggerRef.current, contentRef.current, width, offset, posX, posY, isVisible] | ||
); | ||
|
||
if (typeof window !== 'undefined') { | ||
React.useEffect(repaint, [lastResizeTimestamp, contentRef.current]); | ||
} | ||
|
||
const showPopup = React.useCallback( | ||
() => { | ||
if (controlled) return; | ||
if (willHide !== null) { | ||
clearTimeout(willHide as number); | ||
willHide = null; | ||
} | ||
|
||
setVisibility(true); | ||
}, | ||
[willHide, isVisible, controlled] | ||
); | ||
|
||
const hidePopup = React.useCallback( | ||
() => { | ||
if (willHide !== null || controlled) { | ||
return; | ||
} | ||
|
||
willHide = setTimeout(() => { | ||
isOverTrigger = false; | ||
isOverContent = false; | ||
setVisibility(false); | ||
}, hideDelay); | ||
}, | ||
[willHide, isVisible, controlled] | ||
); | ||
|
||
const { renderTrigger, renderContent } = props; | ||
|
||
const funcs = { | ||
isVisible, | ||
showPopup, | ||
hidePopup, | ||
}; | ||
|
||
const handleMouseEnter = React.useCallback<React.MouseEventHandler<HTMLElement>>( | ||
({ target }) => { | ||
if (target === triggerRef.current) { | ||
isOverTrigger = true; | ||
} else if (target === contentRef.current) { | ||
isOverContent = true; | ||
} | ||
|
||
showPopup(); | ||
}, | ||
[triggerRef.current, contentRef.current, isVisible] | ||
); | ||
|
||
const handleMouseLeave = React.useCallback<React.MouseEventHandler<HTMLElement>>( | ||
({ target }) => { | ||
if (target === triggerRef.current) { | ||
isOverTrigger = false; | ||
} else if (target === contentRef.current) { | ||
isOverContent = false; | ||
} | ||
|
||
if (isVisible && !isOverTrigger && !isOverContent) { | ||
hidePopup(); | ||
} | ||
}, | ||
[triggerRef.current, contentRef.current, isVisible] | ||
); | ||
|
||
return ( | ||
<> | ||
{React.cloneElement( | ||
renderTrigger({ | ||
...funcs, | ||
isOver: isOverTrigger, | ||
}), | ||
{ | ||
ref: triggerRef, | ||
onMouseEnter: handleMouseEnter, | ||
onMouseLeave: handleMouseLeave, | ||
} | ||
)} | ||
{isVisible && ( | ||
<PopupContent | ||
ref={contentRef} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
style={style} | ||
repaint={repaint} | ||
> | ||
{renderContent({ | ||
...funcs, | ||
isOver: isOverContent, | ||
theme, | ||
})} | ||
</PopupContent> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
Popup.defaultProps = { | ||
padding: 15, | ||
hideDelay: 200, | ||
posX: 'left', | ||
posY: 'top', | ||
} as IPopupDefaultProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1 @@ | ||
import * as React from 'react'; | ||
|
||
import { useWindowResize } from '../hooks/useWindowResize'; | ||
import { useTheme } from '../theme'; | ||
import { PopupContent } from './PopupContent'; | ||
import { IPopupDefaultProps, IPopupProps } from './types'; | ||
import { calculateStyles, getDefaultStyle } from './utils'; | ||
|
||
export { IPopupProps, IPopupDefaultProps }; | ||
|
||
export interface IPopup extends IPopupProps {} | ||
|
||
export const Popup: React.FunctionComponent<IPopup> = props => { | ||
const { hideDelay, width, offset, posX, posY } = props; | ||
|
||
const theme = useTheme(); | ||
|
||
const triggerRef = React.useRef<HTMLElement>(null); | ||
const contentRef = React.createRef<HTMLDivElement>(); | ||
const [isVisible, setVisibility] = React.useState<boolean>(false); | ||
const lastResizeTimestamp = useWindowResize(); | ||
let lastRepaintTimestamp = 0; | ||
const [style, setStyle] = React.useState<React.CSSProperties>({}); | ||
let isOverTrigger: boolean = false; | ||
let isOverContent: boolean = false; | ||
let willHide: NodeJS.Timer | number | null = null; | ||
|
||
const repaint = React.useCallback( | ||
() => { | ||
if (isVisible) { | ||
lastRepaintTimestamp = Date.now(); | ||
setStyle({ | ||
...getDefaultStyle(props), | ||
...calculateStyles(triggerRef, contentRef, props), | ||
}); | ||
} | ||
}, | ||
[width, offset, posX, posY, contentRef, lastRepaintTimestamp] | ||
); | ||
|
||
if (typeof window !== 'undefined') { | ||
React.useEffect(repaint, [lastResizeTimestamp]); | ||
} | ||
|
||
const showPopup = () => { | ||
if (willHide !== null) { | ||
clearTimeout(willHide as number); | ||
willHide = null; | ||
} | ||
|
||
setVisibility(true); | ||
}; | ||
|
||
const hidePopup = () => { | ||
if (willHide !== null) { | ||
return; | ||
} | ||
|
||
willHide = setTimeout(() => { | ||
isOverTrigger = false; | ||
isOverContent = false; | ||
setVisibility(false); | ||
}, hideDelay); | ||
}; | ||
|
||
const { renderTrigger, renderContent } = props; | ||
|
||
const funcs = { | ||
isVisible, | ||
showPopup, | ||
hidePopup, | ||
}; | ||
|
||
const handleMouseEnter = ({ target }: React.SyntheticEvent<HTMLElement>) => { | ||
if (target === triggerRef.current) { | ||
isOverTrigger = true; | ||
} else if (target === contentRef.current) { | ||
isOverContent = true; | ||
} | ||
|
||
showPopup(); | ||
}; | ||
|
||
const handleMouseLeave = ({ target }: React.SyntheticEvent<HTMLElement>) => { | ||
if (target === triggerRef.current) { | ||
isOverTrigger = false; | ||
} else if (target === contentRef.current) { | ||
isOverContent = false; | ||
} | ||
|
||
if (isVisible && !isOverTrigger && !isOverContent) { | ||
hidePopup(); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{React.cloneElement( | ||
renderTrigger({ | ||
...funcs, | ||
isOver: isOverTrigger, | ||
}), | ||
{ | ||
ref: triggerRef, | ||
onMouseEnter: handleMouseEnter, | ||
onMouseLeave: handleMouseLeave, | ||
} | ||
)} | ||
{isVisible && ( | ||
<PopupContent | ||
ref={contentRef} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
style={style} | ||
repaint={repaint} | ||
> | ||
{renderContent({ | ||
...funcs, | ||
isOver: isOverContent, | ||
theme, | ||
})} | ||
</PopupContent> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
Popup.defaultProps = { | ||
padding: 15, | ||
hideDelay: 200, | ||
posX: 'left', | ||
posY: 'top', | ||
} as IPopupDefaultProps; | ||
export * from './Popup'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.