Skip to content

Commit 5772d4a

Browse files
committed
Improve popover positioining
- fallback to centered position for Safari due to foreign object issues - add space around popover to allow for hitting viewport boundaries
1 parent 07f41ee commit 5772d4a

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

projects/js-packages/charts/src/components/line-chart/line-chart-annotation-label-popover.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Gridicon } from '@automattic/jetpack-components';
22
import clsx from 'clsx';
33
import { useEffect, useId, useRef, useState } from 'react';
4+
import { isSafari } from '../shared/utils';
45
import styles from './line-chart.module.scss';
56
import type { FC } from 'react';
67

@@ -31,9 +32,13 @@ const LineChartAnnotationLabelWithPopover: FC< LineChartAnnotationLabelWithPopov
3132
if ( ! button || ! popover ) return;
3233

3334
const positionPopover = () => {
34-
const buttonRect = button.getBoundingClientRect();
35-
popover.style.left = `${ buttonRect.right + 10 }px`;
36-
popover.style.top = `${ buttonRect.top }px`;
35+
// Popover positioning in Safari is complicated due to issues with SVG foreign objects, so let it be positioned in the centre of the viewport.
36+
if ( ! isSafari ) {
37+
const buttonRect = button.getBoundingClientRect();
38+
popover.style.left = `${ buttonRect.right }px`;
39+
popover.style.top = `${ buttonRect.top }px`;
40+
}
41+
3742
setIsPositioned( true );
3843
};
3944

@@ -74,7 +79,8 @@ const LineChartAnnotationLabelWithPopover: FC< LineChartAnnotationLabelWithPopov
7479
{ ...( { popover: 'auto' } as any ) }
7580
className={ clsx(
7681
styles[ 'line-chart__annotation-label-popover' ],
77-
! isPositioned && styles[ 'line-chart__annotation-label-popover--hidden' ]
82+
! isPositioned && styles[ 'line-chart__annotation-label-popover--hidden' ],
83+
isSafari && styles[ 'line-chart__annotation-label-popover--safari' ]
7884
) }
7985
>
8086
<div className={ styles[ 'line-chart__annotation-label-popover-header' ] }>

projects/js-packages/charts/src/components/line-chart/line-chart-annotation.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DataContext } from '@visx/xychart';
1010
import { merge } from 'lodash';
1111
import { useContext, useRef, useEffect, useState, useMemo } from 'react';
1212
import { useChartTheme } from '../../providers/theme/theme-provider';
13+
import { isSafari } from '../shared/utils';
1314
import LineChartAnnotationLabelWithPopover, {
1415
POPOVER_BUTTON_SIZE,
1516
} from './line-chart-annotation-label-popover';
@@ -21,8 +22,6 @@ import type { LineSubjectProps } from '@visx/annotation/lib/components/LineSubje
2122
import type { TextProps } from '@visx/text';
2223
import type { FC } from 'react';
2324

24-
const isSafari = /^((?!chrome|android).)*safari/i.test( navigator.userAgent );
25-
2625
export type AnnotationStyles = {
2726
circleSubject?: Omit< CircleSubjectProps, 'x' | 'y' > & { fill?: string };
2827
lineSubject?: Omit< LineSubjectProps, 'x' | 'y' >;

projects/js-packages/charts/src/components/line-chart/line-chart.module.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,20 @@
5656
min-width: 125px;
5757
background: #fff;
5858
border: none;
59-
position: fixed;
60-
margin: 0;
61-
visibility: visible;
6259
border-radius: 4px;
6360
font-size: 14px;
6461
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
62+
position: fixed;
63+
margin: 0.5rem 1rem;
6564

6665
&--hidden {
6766
visibility: hidden;
6867
}
68+
69+
&--safari {
70+
position: initial;
71+
margin: auto;
72+
}
6973
}
7074

7175
&__annotation-label-popover-header {

projects/js-packages/charts/src/components/line-chart/stories/annotation.stories.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ const customTopAnnotationArgs: Partial< LineChartAnnotationProps > = {
173173
</span>
174174
),
175175
renderLabelPopover: () => (
176-
<div style={ { display: 'flex', flexDirection: 'column', gap: '10px' } }>
177-
<h4
176+
<div style={ { display: 'flex', flexDirection: 'column', gap: '0.5rem' } }>
177+
<div
178178
style={ {
179179
margin: 0,
180180
display: 'flex',
@@ -184,8 +184,8 @@ const customTopAnnotationArgs: Partial< LineChartAnnotationProps > = {
184184
} }
185185
>
186186
<DeployedIcon />
187-
<span>Deploy finished</span>
188-
</h4>
187+
<strong>Deploy finished</strong>
188+
</div>
189189
<p style={ { margin: 0 } }>Thu. Apr 24, 2025. 09:57:23 UTC</p>
190190
</div>
191191
),
@@ -224,8 +224,8 @@ const customBottomAnnotationArgs: Partial< LineChartAnnotationProps > = {
224224
title: 'Alert',
225225
renderLabel: () => <AlertIcon />,
226226
renderLabelPopover: () => (
227-
<div style={ { display: 'flex', flexDirection: 'column', gap: '10px' } }>
228-
<h4
227+
<div style={ { display: 'flex', flexDirection: 'column', gap: '0.5rem' } }>
228+
<div
229229
style={ {
230230
margin: 0,
231231
display: 'flex',
@@ -235,8 +235,8 @@ const customBottomAnnotationArgs: Partial< LineChartAnnotationProps > = {
235235
} }
236236
>
237237
<AlertIcon />
238-
Origin HTTP 5xx Response Codes Rate Anomaly [Beta]
239-
</h4>
238+
<strong>Origin HTTP 5xx Response Codes Rate Anomaly [Beta]</strong>
239+
</div>
240240
<p style={ { margin: 0 } }>
241241
Unusually high number of HTTP 5xx response codes detected on Origin
242242
</p>

projects/js-packages/charts/src/components/shared/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const getLongestTickWidth = < T extends AnyD3Scale >(
2323

2424
return getStringWidth( longestTick, labelStyle );
2525
};
26+
27+
export const isSafari = /^((?!chrome|android).)*safari/i.test( navigator.userAgent );

0 commit comments

Comments
 (0)