-
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 #12 from stoplightio/SL-142/popup
SL-142/popup
- Loading branch information
Showing
20 changed files
with
788 additions
and
12 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 |
---|---|---|
|
@@ -39,6 +39,7 @@ TODO | |
- Link | ||
- List | ||
- ListScroller | ||
- Popup | ||
- Portal | ||
- Mark | ||
- Menu | ||
|
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,4 @@ | ||
const _ = require.requireActual('lodash'); | ||
|
||
module.exports = _; | ||
module.exports.debounce = jest.fn(_.debounce); |
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
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,28 @@ | ||
import * as React from 'react'; | ||
|
||
import { Portal } from '../Portal'; | ||
import { IPopupContentProps } from './types'; | ||
|
||
export const PopupContent = React.forwardRef<HTMLDivElement, IPopupContentProps>((props, ref) => { | ||
const { onMouseEnter, onMouseLeave, repaint, style } = props; | ||
const lastChildRef = React.useRef<HTMLElement>(null); | ||
|
||
React.useEffect(repaint, [lastChildRef.current]); | ||
|
||
const count = React.Children.count(props.children); | ||
|
||
return ( | ||
<Portal> | ||
<div onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} ref={ref} style={style}> | ||
{React.Children.map(props.children, (child, i) => { | ||
if (i !== count - 1) { | ||
return child; | ||
} | ||
|
||
// the purpose of doing this is to get rid of that forced re-render | ||
return React.cloneElement(child as React.ReactElement<any>, { ref: lastChildRef }); | ||
})} | ||
</div> | ||
</Portal> | ||
); | ||
}); |
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,126 @@ | ||
import * as React from 'react'; | ||
|
||
import { useWindowResize } from '../hooks/useWindowResize'; | ||
import { PopupContent } from './PopupContent'; | ||
import { IPopupDefaultProps, IPopupProps } from './types'; | ||
import { calculateStyles, getDefaultStyle } from './utils'; | ||
|
||
export { IPopupProps, IPopupDefaultProps }; | ||
|
||
export const Popup = (props: IPopupProps) => { | ||
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 | undefined>(undefined); | ||
let isOverTrigger: boolean = false; | ||
let isOverContent: boolean = false; | ||
// Number could be set here, but unfortunately Node returns Timeout which is not exported | ||
let willHide: any; | ||
|
||
const repaint = React.useCallback( | ||
() => { | ||
if (isVisible) { | ||
lastRepaintTimestamp = Date.now(); | ||
setStyle({ | ||
...getDefaultStyle(props), | ||
...calculateStyles(triggerRef, contentRef, props), | ||
}); | ||
} | ||
}, | ||
[props.width, props.offset, props.posX, props.posY, contentRef, lastRepaintTimestamp] | ||
); | ||
|
||
if (typeof window !== 'undefined') { | ||
React.useEffect(repaint, [lastResizeTimestamp]); | ||
} | ||
|
||
const showPopup = () => { | ||
if (willHide !== undefined) { | ||
clearTimeout(willHide); | ||
willHide = undefined; | ||
} | ||
|
||
setVisibility(true); | ||
}; | ||
|
||
const hidePopup = () => { | ||
if (willHide !== undefined) { | ||
return; | ||
} | ||
|
||
willHide = setTimeout(() => { | ||
isOverTrigger = false; | ||
isOverContent = false; | ||
setVisibility(false); | ||
}, props.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, | ||
}), | ||
{ | ||
onMouseEnter: handleMouseEnter, | ||
onMouseLeave: handleMouseLeave, | ||
ref: triggerRef, | ||
} | ||
)} | ||
{isVisible && ( | ||
<PopupContent | ||
ref={contentRef} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
style={style} | ||
repaint={repaint} | ||
> | ||
{renderContent({ | ||
...funcs, | ||
isOver: isOverContent, | ||
})} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as React from 'react'; | ||
|
||
export type PopupTriggerRenderer = ( | ||
props: { | ||
isVisible: boolean; | ||
isOver: boolean; | ||
showPopup: () => void; | ||
hidePopup: () => void; | ||
} | ||
) => any; | ||
|
||
export type PopupContentRenderer = ( | ||
props: { | ||
isVisible: boolean; | ||
isOver: boolean; | ||
showPopup: () => void; | ||
hidePopup: () => void; | ||
} | ||
) => any; | ||
|
||
export interface IPopupPosition { | ||
width?: number; // force a width | ||
padding: number; // transparent space around the popup | ||
offset?: { | ||
top?: number; | ||
bottom?: number; | ||
left?: number; | ||
right?: number; | ||
}; | ||
posX: 'left' | 'center' | 'right'; | ||
posY: 'top' | 'center' | 'bottom'; | ||
} | ||
|
||
export interface IPopupProps extends IPopupPosition { | ||
hideDelay: number; // how long popup will show for after user mouses out | ||
renderTrigger: PopupTriggerRenderer; | ||
renderContent: PopupContentRenderer; | ||
} | ||
|
||
export interface IPopupDefaultProps { | ||
padding: 15; | ||
hideDelay: 200; | ||
posX: 'left'; | ||
posY: 'top'; | ||
} | ||
|
||
export interface IPopupContentProps { | ||
children: React.ReactChildren; | ||
onMouseEnter: (e: React.SyntheticEvent<HTMLElement>) => any; | ||
onMouseLeave: (e: React.SyntheticEvent<HTMLElement>) => any; | ||
repaint: () => any; | ||
style?: React.CSSProperties; | ||
} |
Oops, something went wrong.