-
Notifications
You must be signed in to change notification settings - Fork 5
Tooltip component #304
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
Open
jav1anpry5ce
wants to merge
5
commits into
main
Choose a base branch
from
tooltip-component
base: main
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.
Open
Tooltip component #304
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6678dd8
create question mark icon
jav1anpry5ce 6e32126
create reusable question mark component
jav1anpry5ce 50daadf
remove iconClass prop
jav1anpry5ce aa85172
use SolidJS builtin Show component to conditionally render QuestionTo…
jav1anpry5ce 92e9b9b
refactor for readability
jav1anpry5ce 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
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,18 @@ | ||
| export default function QuestionMarkIcon(props: { class?: string }) { | ||
| return ( | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/Url" | ||
| fill="none" | ||
| viewBox="0 0 24 24" | ||
| strokeWidth={1.5} | ||
| stroke="currentColor" | ||
| class={props.class || "size-6"} | ||
| > | ||
| <path | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| d="M9.879 7.519c1.171-1.025 3.071-1.025 4.242 0 1.172 1.025 1.172 2.687 0 3.712-.203.179-.43.326-.67.442-.745.361-1.45.999-1.45 1.827v.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9 5.25h.008v.008H12v-.008Z" | ||
| /> | ||
| </svg> | ||
| ); | ||
| } |
99 changes: 99 additions & 0 deletions
99
builder-frontend/src/components/shared/QuestionTooltip.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,99 @@ | ||
| import { createSignal, JSX, Show } from "solid-js"; | ||
| import QuestionMarkIcon from "../icon/QuestionMarkIcon"; | ||
|
|
||
| export enum TooltipAlignment { | ||
| Center = "center", | ||
| Left = "left", | ||
| Right = "right", | ||
| } | ||
|
|
||
| interface TooltipProps { | ||
| text: string; | ||
| children?: JSX.Element; | ||
| } | ||
|
|
||
| export default function QuestionTooltip(props: TooltipProps) { | ||
| const [isVisible, setIsVisible] = createSignal(false); | ||
| const [position, setPosition] = createSignal<{ | ||
| x: number; | ||
| align: TooltipAlignment; | ||
| }>({ x: 0, align: TooltipAlignment.Center }); | ||
|
|
||
| let containerRef: HTMLDivElement | undefined; | ||
|
|
||
| const determineAlignment = (rect: DOMRect): TooltipAlignment => { | ||
| const tooltipWidth = 256; // w-64 is 16rem = 256px | ||
|
|
||
| // Calculate potential left and right bounds if centered | ||
| const centerLeft = rect.left + rect.width / 2 - tooltipWidth / 2; | ||
| const centerRight = centerLeft + tooltipWidth; | ||
|
|
||
| const viewportWidth = window.innerWidth; | ||
| const padding = 16; // 16px safety padding from screen edges | ||
|
|
||
| if (centerLeft < padding) { | ||
| return TooltipAlignment.Left; | ||
| } else if (centerRight > viewportWidth - padding) { | ||
| return TooltipAlignment.Right; | ||
| } else { | ||
| return TooltipAlignment.Center; | ||
| } | ||
| }; | ||
|
|
||
| const handleMouseEnter = () => { | ||
| if (containerRef) { | ||
| const rect = containerRef.getBoundingClientRect(); | ||
| const align = determineAlignment(rect); | ||
| setPosition({ x: 0, align }); | ||
| } | ||
| setIsVisible(true); | ||
| }; | ||
|
|
||
| const handleMouseLeave = () => setIsVisible(false); | ||
|
|
||
| return ( | ||
| <div | ||
| ref={containerRef} | ||
| class="relative inline-flex items-center justify-center cursor-help" | ||
| onMouseEnter={handleMouseEnter} | ||
| onMouseLeave={handleMouseLeave} | ||
| onFocus={handleMouseEnter} | ||
| onBlur={handleMouseLeave} | ||
| tabIndex={0} | ||
| aria-label="More information" | ||
| > | ||
| <Show | ||
| when={props.children} | ||
| fallback={ | ||
| <QuestionMarkIcon class="size-5 text-gray-500 hover:text-gray-700 transition-colors" /> | ||
| } | ||
| > | ||
| {props.children} | ||
| </Show> | ||
|
|
||
| <Show when={isVisible()}> | ||
| <div | ||
| class={`absolute z-50 w-64 p-3 mt-2 text-sm text-gray-800 bg-white border border-gray-200 rounded-lg shadow-lg top-full pointer-events-none fade-in ${ | ||
| position().align === TooltipAlignment.Center | ||
| ? "left-1/2 -translate-x-1/2" | ||
| : position().align === TooltipAlignment.Left | ||
| ? "left-0" | ||
| : "right-0" | ||
| }`} | ||
| > | ||
| {props.text} | ||
| {/* Decorative arrow pointing up */} | ||
| <div | ||
| class={`absolute w-3 h-3 bg-white border-t border-l border-gray-200 rotate-45 -top-[7px] ${ | ||
| position().align === TooltipAlignment.Center | ||
| ? "left-1/2 -translate-x-1/2" | ||
| : position().align === TooltipAlignment.Left | ||
| ? "left-3" | ||
| : "right-3" | ||
| }`} | ||
| /> | ||
| </div> | ||
| </Show> | ||
| </div> | ||
| ); | ||
| } | ||
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.
For readability, I have a few small nitpicks here:
'center' | 'left' | 'right'.handleMouseEnter(...)function call another function for determining alignment that returns aTooltipAlignment, and then settingsetPosition(...);based on the return value.This would make it easier at-a-glance to know what
handleMouseEnter(...)does. This is purely stylistic so feel free to ignore.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.
That makes sense. It would make the code more readable and maintainable.