-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use GitHub native popover to substitue ours (#795)
* refactor: a basic demo showing how to use native popover to present our content * chore: proper delay * fix: decrease leave time to avoid "It looks like the React-rendered..." issue * fix: popover container may not exist * refactor: extract code to make a new componenet called NativePopover * style: add some padding * refactor popover * refactor: NativePopover used in repo-header-labels * chore: remove react-tooltip package --------- Co-authored-by: Zi1l <[email protected]>
- Loading branch information
Showing
13 changed files
with
253 additions
and
260 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
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,90 @@ | ||
import React, { PropsWithChildren, useEffect } from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
import elementReady from 'element-ready'; | ||
import $ from 'jquery'; | ||
|
||
interface NativePopoverProps extends PropsWithChildren<any> { | ||
anchor: JQuery<HTMLElement>; | ||
width: number; | ||
// for now, only support top-middle | ||
arrowPosition: | ||
| 'top-left' | ||
| 'top-middle' | ||
| 'top-right' | ||
| 'bottom-left' | ||
| 'bottom-middle' | ||
| 'bottom-right'; | ||
} | ||
|
||
export const NativePopover = ({ | ||
anchor, | ||
width, | ||
arrowPosition, | ||
children, | ||
}: NativePopoverProps): JSX.Element => { | ||
useEffect(() => { | ||
(async () => { | ||
await elementReady('div.Popover'); | ||
await elementReady('div.Popover-message'); | ||
const $popoverContainer = $('div.Popover'); | ||
const $popoverContent = $('div.Popover-message'); | ||
let popoverTimer: NodeJS.Timeout | null = null; | ||
let leaveTimer: NodeJS.Timeout | null = null; | ||
|
||
const showPopover = () => { | ||
popoverTimer = setTimeout(() => { | ||
const anchorOffset = anchor.offset(); | ||
const anchorWidth = anchor.outerWidth(); | ||
const anchorHeight = anchor.outerHeight(); | ||
if (!anchorOffset || !anchorHeight || !anchorWidth) { | ||
return; | ||
} | ||
const { top, left } = anchorOffset; | ||
|
||
$popoverContent.css('padding', '10px 5px'); | ||
$popoverContent.css('width', width); | ||
$popoverContainer.css('top', `${top + anchorHeight + 10}px`); | ||
$popoverContainer.css( | ||
'left', | ||
`${left - (width - anchorWidth) / 2}px` | ||
); | ||
$popoverContent.attr( | ||
'class', | ||
`Popover-message Box color-shadow-large Popover-message--${arrowPosition}` | ||
); | ||
render(children, $popoverContent[0]); | ||
$popoverContainer.css('display', 'block'); | ||
}, 1000); | ||
}; | ||
|
||
const hidePopover = () => { | ||
popoverTimer && clearTimeout(popoverTimer); | ||
$popoverContent.addClass('Popover-message--large'); | ||
if ($popoverContent.children().length > 0) { | ||
unmountComponentAtNode($popoverContent[0]); | ||
} | ||
$popoverContainer.css('display', 'none'); | ||
}; | ||
|
||
anchor[0].addEventListener('mouseenter', () => { | ||
popoverTimer = null; | ||
leaveTimer && clearTimeout(leaveTimer); | ||
showPopover(); | ||
}); | ||
|
||
anchor[0].addEventListener('mouseleave', () => { | ||
leaveTimer = setTimeout(hidePopover, 200); | ||
}); | ||
|
||
$popoverContainer[0].addEventListener('mouseenter', () => { | ||
leaveTimer && clearTimeout(leaveTimer); | ||
}); | ||
|
||
$popoverContainer[0].addEventListener('mouseleave', () => { | ||
leaveTimer = setTimeout(hidePopover, 200); | ||
}); | ||
})(); | ||
}, []); | ||
|
||
return <></>; | ||
}; |
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
Oops, something went wrong.