forked from NASA-AMMOS/MMGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/timeline plugin #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sandesh-sp
wants to merge
2
commits into
feat/timeline-core
Choose a base branch
from
feat/timeline-plugin
base: feat/timeline-core
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Feat/timeline plugin #113
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/essence/Ancillary/FloatingPopover/FloatingPopover.tsx
This file contains hidden or 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,140 @@ | ||
| import React, { useLayoutEffect, useState, useRef, useEffect } from 'react' | ||
| import { createPortal } from 'react-dom' | ||
|
|
||
| export interface FloatingPopoverProps { | ||
| anchorRef: React.RefObject<HTMLElement | null> | ||
| isOpen: boolean | ||
| onClose?: () => void | ||
| placement?: 'top' | 'bottom' | 'left' | 'right' | ||
| offset?: number | ||
| className?: string | ||
| children: React.ReactNode | ||
| } | ||
|
|
||
| export const FloatingPopover: React.FC<FloatingPopoverProps> = ({ | ||
| anchorRef, | ||
| isOpen, | ||
| onClose, | ||
| placement = 'bottom', | ||
| offset = 8, | ||
| className = '', | ||
| children | ||
| }) => { | ||
| const popupRef = useRef<HTMLDivElement>(null) | ||
| const [pos, setPos] = useState({ top: 0, left: 0 }) | ||
|
|
||
| // Close on outside click | ||
| useEffect(() => { | ||
| if (!isOpen || !onClose) return | ||
|
|
||
| const handleClickOutside = (e: MouseEvent) => { | ||
| const target = e.target as HTMLElement | ||
| // If the anchor exists and the click is inside it, ignore (the button toggle will handle it) | ||
| const clickedInsideAnchor = anchorRef.current && anchorRef.current.contains(target) | ||
| // If the click is inside the popup itself, ignore | ||
| const clickedInsidePopup = popupRef.current && popupRef.current.contains(target) | ||
|
|
||
| if (!clickedInsideAnchor && !clickedInsidePopup) { | ||
| onClose() | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('mousedown', handleClickOutside) | ||
| return () => document.removeEventListener('mousedown', handleClickOutside) | ||
| }, [isOpen, onClose, anchorRef]) | ||
|
|
||
| // Update position | ||
| useLayoutEffect(() => { | ||
| if (!isOpen) return | ||
|
|
||
| const updatePosition = () => { | ||
| if (!anchorRef.current || !popupRef.current) return | ||
|
|
||
| const anchorRect = anchorRef.current.getBoundingClientRect() | ||
| const popupRect = popupRef.current.getBoundingClientRect() | ||
|
|
||
| let top = 0 | ||
| let left = 0 | ||
|
|
||
| switch (placement) { | ||
| case 'top': | ||
| top = anchorRect.top - popupRect.height - offset | ||
| left = anchorRect.left + (anchorRect.width / 2) - (popupRect.width / 2) | ||
| break | ||
| case 'bottom': | ||
| top = anchorRect.bottom + offset | ||
| left = anchorRect.left + (anchorRect.width / 2) - (popupRect.width / 2) | ||
| break | ||
| case 'left': | ||
| top = anchorRect.top + (anchorRect.height / 2) - (popupRect.height / 2) | ||
| left = anchorRect.left - popupRect.width - offset | ||
| break | ||
| case 'right': | ||
| top = anchorRect.top + (anchorRect.height / 2) - (popupRect.height / 2) | ||
| left = anchorRect.right + offset | ||
| break | ||
| } | ||
|
|
||
| // Simple viewport bounds checking | ||
| if (left < 8) left = 8 | ||
| if (top < 8) { | ||
| if (placement === 'top') { | ||
| top = anchorRect.bottom + offset | ||
| } else { | ||
| top = 8 | ||
| } | ||
| } | ||
| if (left + popupRect.width > window.innerWidth - 8) { | ||
| left = window.innerWidth - popupRect.width - 8 | ||
| } | ||
| if (top + popupRect.height > window.innerHeight - 8) { | ||
| if (placement === 'bottom') { | ||
| top = anchorRect.top - popupRect.height - offset | ||
| } else { | ||
| top = window.innerHeight - popupRect.height - 8 | ||
| } | ||
| } | ||
|
|
||
| // Prevent React state updates if the position hasn't changed to avoid infinite loops | ||
| setPos(prev => { | ||
| if (Math.abs(prev.top - top) < 1 && Math.abs(prev.left - left) < 1) { | ||
| return prev | ||
| } | ||
| return { top, left } | ||
| }) | ||
| } | ||
|
|
||
| updatePosition() | ||
| window.addEventListener('resize', updatePosition) | ||
| window.addEventListener('scroll', updatePosition, true) | ||
|
|
||
| // Wait a tick and update again in case children render changed dimensions | ||
| const timeout = setTimeout(updatePosition, 0) | ||
|
|
||
| return () => { | ||
| clearTimeout(timeout) | ||
| window.removeEventListener('resize', updatePosition) | ||
| window.removeEventListener('scroll', updatePosition, true) | ||
| } | ||
| }, [isOpen, placement, offset, anchorRef]) | ||
|
|
||
| if (!isOpen) return null | ||
|
|
||
| return createPortal( | ||
| <div | ||
| ref={popupRef} | ||
| className={`floating-popover-portal ${className}`} | ||
| style={{ | ||
| position: 'fixed', | ||
| zIndex: 999999, | ||
| top: `${pos.top}px`, | ||
| left: `${pos.left}px`, | ||
| // visibility hidden on first render if pos is 0,0 to prevent flicker in top left | ||
| visibility: pos.top === 0 && pos.left === 0 ? 'hidden' : 'visible' | ||
| }} | ||
| > | ||
| {children} | ||
| </div>, | ||
| document.body | ||
| ) | ||
| } |
This file contains hidden or 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 @@ | ||
| export * from './FloatingPopover' |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The components should be self-sufficient and shouldn't be importing anything that's not within its own folder