Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Stop segments tooltip cutoff (#5541)

Artur Pata73c46c6f4c08
diff --git a/assets/js/dashboard/nav-menu/filter-menu.tsx b/assets/js/dashboard/nav-menu/filter-menu.tsx
index 1120e7e074..12fa7c9997 100644
--- a/assets/js/dashboard/nav-menu/filter-menu.tsx
+++ b/assets/js/dashboard/nav-menu/filter-menu.tsx
@@ -48,6 +48,7 @@ const FilterMenuItems = ({ closeDropdown }: { closeDropdown: () => void }) => {
const site = useSiteContext()
const columns = useMemo(() => getFilterListItems(site), [site])
const buttonRef = useRef<HTMLButtonElement>(null)
+ const panelRef = useRef<HTMLDivElement>(null)
return (
<>
@@ -74,6 +75,7 @@ const FilterMenuItems = ({ closeDropdown }: { closeDropdown: () => void }) => {
)}
>
<Popover.Panel
+ ref={panelRef}
className={classNames(popover.panel.classNames.roundedSheet)}
data-testid="filtermenu"
>
@@ -105,7 +107,10 @@ const FilterMenuItems = ({ closeDropdown }: { closeDropdown: () => void }) => {
</div>
))}
</div>
- <SearchableSegmentsSection closeList={closeDropdown} />
+ <SearchableSegmentsSection
+ closeList={closeDropdown}
+ tooltipContainerRef={panelRef}
+ />
</Popover.Panel>
</Transition>
</>
diff --git a/assets/js/dashboard/nav-menu/segments/searchable-segments-section.tsx b/assets/js/dashboard/nav-menu/segments/searchable-segments-section.tsx
index 7ede78def5..02a3aee997 100644
--- a/assets/js/dashboard/nav-menu/segments/searchable-segments-section.tsx
+++ b/assets/js/dashboard/nav-menu/segments/searchable-segments-section.tsx
@@ -1,4 +1,4 @@
-import React from 'react'
+import React, { RefObject } from 'react'
import { useQueryContext } from '../../query-context'
import { useSiteContext } from '../../site-context'
import {
@@ -32,9 +32,11 @@ const linkClassName = classNames(
const INITIAL_SEGMENTS_SHOWN = 5
export const SearchableSegmentsSection = ({
- closeList
+ closeList,
+ tooltipContainerRef
}: {
closeList: () => void
+ tooltipContainerRef: RefObject<HTMLElement>
}) => {
const site = useSiteContext()
const segmentsContext = useSegmentsContext()
@@ -93,6 +95,7 @@ export const SearchableSegmentsSection = ({
{showableData.map((segment) => {
return (
<Tooltip
+ containerRef={tooltipContainerRef}
className="group"
key={segment.id}
info={
@@ -122,7 +125,11 @@ export const SearchableSegmentsSection = ({
)
})}
{countOfMoreToShow > 0 && (
- <Tooltip className="group" info={null}>
+ <Tooltip
+ className="group"
+ info={null}
+ containerRef={tooltipContainerRef}
+ >
<button
className={classNames(
linkClassName,
@@ -137,7 +144,11 @@ export const SearchableSegmentsSection = ({
)}
</div>
{searching && !filteredData.length && (
- <Tooltip className="group" info={null}>
+ <Tooltip
+ className="group"
+ info={null}
+ containerRef={tooltipContainerRef}
+ >
<button
className={classNames(
linkClassName,
diff --git a/assets/js/dashboard/util/tooltip.tsx b/assets/js/dashboard/util/tooltip.tsx
index 497eef4db8..64f8217607 100644
--- a/assets/js/dashboard/util/tooltip.tsx
+++ b/assets/js/dashboard/util/tooltip.tsx
@@ -1,19 +1,24 @@
-import React, { ReactNode, useState } from 'react'
+import React, { CSSProperties, ReactNode, RefObject, useState } from 'react'
import { usePopper } from 'react-popper'
import classNames from 'classnames'
+import { createPortal } from 'react-dom'
export function Tooltip({
children,
info,
className,
onClick,
- boundary
+ boundary,
+ containerRef
}: {
info: ReactNode
children: ReactNode
className?: string
onClick?: () => void
- boundary?: HTMLElement
+ /** if provided, the tooltip is confined to the particular element */
+ boundary?: HTMLElement | null
+ /** if defined, the tooltip is rendered in a portal to this element */
+ containerRef?: RefObject<HTMLElement>
}) {
const [visible, setVisible] = useState(false)
const [referenceElement, setReferenceElement] =
@@ -33,13 +38,17 @@ export function Tooltip({
offset: [0, 4]
}
},
- boundary && {
- name: 'preventOverflow',
- options: {
- boundary: boundary
- }
- }
- ].filter((x) => !!x)
+ ...(boundary
+ ? [
+ {
+ name: 'preventOverflow',
+ options: {
+ boundary: boundary
+ }
+ }
+ ]
+ : [])
+ ]
})
return (
@@ -53,21 +62,61 @@ export function Tooltip({
{children}
</div>
{info && visible && (
- <div
- ref={setPopperElement}
- style={styles.popper}
- {...attributes.popper}
- className="z-50 p-2 rounded text-sm text-gray-100 font-bold popper-tooltip"
- role="tooltip"
+ <TooltipMessage
+ containerRef={containerRef}
+ popperStyle={styles.popper}
+ popperAttributes={attributes.popper}
+ setPopperElement={setPopperElement}
+ setArrowElement={setArrowElement}
+ arrowStyle={styles.arrow}
>
{info}
- <div
- ref={setArrowElement}
- style={styles.arrow}
- className="tooltip-arrow"
- ></div>
- </div>
+ </TooltipMessage>
)}
</div>
)
}
+
+function TooltipMessage({
+ containerRef,
+ popperStyle,
+ popperAttributes,
+ setPopperElement,
+ setArrowElement,
+ arrowStyle,
+ children
+}: {
+ containerRef?: RefObject<HTMLElement>
+ popperStyle: CSSProperties
+ arrowStyle: CSSProperties
+ popperAttributes?: Record<string, string>
+ setPopperElement: (element: HTMLDivElement) => void
+ setArrowElement: (element: HTMLDivElement) => void
+ children: ReactNode
+}) {
+ const messageElement = (
+ <div
+ ref={setPopperElement}
+ style={popperStyle}
+ {...popperAttributes}
+ className="z-50 p-2 rounded text-sm text-gray-100 font-bold popper-tooltip"
+ role="tooltip"
+ >
+ {children}
+ <div
+ ref={setArrowElement}
+ style={arrowStyle}
+ className="tooltip-arrow"
+ ></div>
+ </div>
+ )
+ if (containerRef) {
+ if (containerRef.current) {
+ return createPortal(messageElement, containerRef.current)
+ } else {
+ return null
+ }
+ }
+
+ return messageElement
+}