-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aec45d3
commit ebe5a36
Showing
10 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -25,3 +25,7 @@ | |
background-blend-mode: overlay; | ||
} | ||
} | ||
|
||
:global(.react-tooltip) { | ||
z-index: 1000; | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/settings-renderer/components/widgets/Popover.module.scss
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,38 @@ | ||
////////////////////////////////////////////////////////////////////////////////////////// | ||
// _ _ ____ _ _ ___ ____ // | ||
// |_/ |__| |\ | | \ | | This file belongs to Kando, the cross-platform // | ||
// | \_ | | | \| |__/ |__| pie menu. Read more on github.com/kando-menu/kando // | ||
// // | ||
////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
// SPDX-FileCopyrightText: Simon Schneegans <code@simonschneegans.de> | ||
// SPDX-License-Identifier: MIT | ||
|
||
@import '../../variables.scss'; | ||
|
||
.popover { | ||
position: absolute; | ||
padding: 5px; | ||
border-radius: 6px; | ||
background-color: black; | ||
|
||
transition: opacity 200ms; | ||
opacity: 0; | ||
} | ||
|
||
.popoverTriangle { | ||
position: absolute; | ||
pointer-events: none; | ||
width: 0; | ||
height: 0; | ||
border-left: 10px solid transparent; | ||
border-right: 10px solid transparent; | ||
border-top: 10px solid black; | ||
} | ||
|
||
:global(.popover-enter), | ||
:global(.popover-enter-done) { | ||
&.popover { | ||
opacity: 1; | ||
} | ||
} |
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,106 @@ | ||
////////////////////////////////////////////////////////////////////////////////////////// | ||
// _ _ ____ _ _ ___ ____ // | ||
// |_/ |__| |\ | | \ | | This file belongs to Kando, the cross-platform // | ||
// | \_ | | | \| |__/ |__| pie menu. Read more on github.com/kando-menu/kando // | ||
// // | ||
////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
// SPDX-FileCopyrightText: Simon Schneegans <code@simonschneegans.de> | ||
// SPDX-License-Identifier: MIT | ||
|
||
import React from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { CSSTransition } from 'react-transition-group'; | ||
|
||
import * as classes from './Popover.module.scss'; | ||
|
||
interface IProps { | ||
/** Whether the modal is visible. */ | ||
visible: boolean; | ||
|
||
/** Called when the user clicks outside the popover area. */ | ||
onClickOutside: () => void; | ||
|
||
/** Content to display inside the popover. */ | ||
content: React.ReactNode; | ||
|
||
/** The popover target. It will be used to position the popover relative to it. */ | ||
children: React.ReactNode; | ||
} | ||
|
||
/** | ||
* A simple popover component. When props.visible becomes true, the popover will be faded | ||
* in with a CSS transition. When it becomes false, the popover will be faded out and its | ||
* content will be unmounted. The popover will be mounted to the body element and | ||
* positioned relative to the children of the Popover component. | ||
* | ||
* @param props - The properties for the modal component. | ||
* @returns A popover element. | ||
*/ | ||
export default (props: IProps) => { | ||
const popoverContent = React.useRef(null); | ||
const popoverTriangle = React.useRef(null); | ||
const popoverTarget = React.useRef(null); | ||
|
||
React.useEffect(() => { | ||
if (!props.visible) { | ||
return; | ||
} | ||
|
||
// Position the popover above the target element. | ||
const triangleSize = 10; | ||
const targetRect = popoverTarget.current.getBoundingClientRect(); | ||
const contentRect = popoverContent.current.getBoundingClientRect(); | ||
const yDiff = targetRect.top - contentRect.height - triangleSize; | ||
const xDiff = targetRect.left - contentRect.width / 2 + targetRect.width / 2; | ||
|
||
// Clamp to window bounds left and right. Vertical position is not clamped yet, | ||
// ideally we would position the popover below the target element if there is not | ||
// // enough space above it. | ||
const windowPadding = 10; | ||
const clampedXDiff = Math.max( | ||
windowPadding, | ||
Math.min(window.innerWidth - contentRect.width - windowPadding, xDiff) | ||
); | ||
|
||
popoverContent.current.style.top = `${yDiff}px`; | ||
popoverContent.current.style.left = `${clampedXDiff}px`; | ||
popoverTriangle.current.style.top = `${contentRect.height - 1}px`; | ||
popoverTriangle.current.style.left = `${contentRect.width / 2 - triangleSize + xDiff - clampedXDiff}px`; | ||
|
||
// Dismiss the popover when the user clicks outside of it. | ||
const handleClick = (event: MouseEvent) => { | ||
if ( | ||
popoverContent.current && | ||
!popoverContent.current.contains(event.target as Node) | ||
) { | ||
props.onClickOutside(); | ||
} | ||
}; | ||
|
||
document.addEventListener('mousedown', handleClick); | ||
return () => document.removeEventListener('mousedown', handleClick); | ||
}, [props.onClickOutside, props.visible]); | ||
|
||
return ( | ||
<> | ||
<div ref={popoverTarget}>{props.children}</div> | ||
{createPortal( | ||
<CSSTransition | ||
in={props.visible} | ||
nodeRef={popoverContent} | ||
// The modal CSS class uses a 200ms transition when fading in and out, so we set the | ||
// timeout to 200ms to match this. | ||
timeout={200} | ||
classNames="popover" | ||
unmountOnExit> | ||
<div ref={popoverContent} className={classes.popover}> | ||
<div ref={popoverTriangle} className={classes.popoverTriangle} /> | ||
{props.content} | ||
</div> | ||
</CSSTransition>, | ||
document.body | ||
)} | ||
</> | ||
); | ||
}; |
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