Skip to content

Commit

Permalink
🔨 (tooltip) dismiss mobile tooltips in faceted charts
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Nov 14, 2024
1 parent a7918bc commit 1a76b25
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
20 changes: 14 additions & 6 deletions packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ import {
HorizontalNumericColorLegend,
} from "../horizontalColorLegend/HorizontalColorLegends"

const LINE_CHART_CLASS_NAME = "LineChart"

// line color
const BLUR_LINE_COLOR = "#eee"
const DEFAULT_LINE_COLOR = "#000"
Expand Down Expand Up @@ -786,10 +788,16 @@ export class LineChart

@action.bound onDocumentClick(e: MouseEvent): void {
// only dismiss the tooltip if the click is outside of the chart area
if (
!this.base.current ||
isTargetOutsideElement(e.target as Node, this.base.current)
) {
// and outside of the chart areas of neighbouring facets
const chartContainer = this.manager.base?.current
if (!chartContainer) return
const chartAreas = chartContainer.getElementsByClassName(
LINE_CHART_CLASS_NAME
)
const isTargetOutsideChartAreas = Array.from(chartAreas).every(
(chartArea) => isTargetOutsideElement(e.target!, chartArea)
)
if (isTargetOutsideChartAreas) {
this.dismissTooltip()
}
}
Expand Down Expand Up @@ -962,7 +970,7 @@ export class LineChart
return (
<g
ref={this.base}
className="LineChart"
className={LINE_CHART_CLASS_NAME}
onMouseLeave={this.onCursorLeave}
onTouchEnd={this.onCursorLeave}
onTouchCancel={this.onCursorLeave}
Expand Down Expand Up @@ -995,7 +1003,7 @@ export class LineChart

if (this.failMessage)
return (
<g className="LineChart">
<g>
{this.renderDualAxis()}
<NoDataModal
manager={manager}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ interface AreasProps extends React.SVGAttributes<SVGGElement> {
onAreaMouseLeave?: () => void
}

const STACKED_AREA_CHART_CLASS_NAME = "StackedArea"

const BLUR_COLOR = "#ddd"

@observer
Expand Down Expand Up @@ -592,10 +594,16 @@ export class StackedAreaChart

@action.bound onDocumentClick(e: MouseEvent): void {
// only dismiss the tooltip if the click is outside of the chart area
if (
!this.base.current ||
isTargetOutsideElement(e.target as Node, this.base.current)
) {
// and outside of the chart areas of neighbouring facets
const chartContainer = this.manager.base?.current
if (!chartContainer) return
const chartAreas = chartContainer.getElementsByClassName(
STACKED_AREA_CHART_CLASS_NAME
)
const isTargetOutsideChartAreas = Array.from(chartAreas).every(
(chartArea) => isTargetOutsideElement(e.target!, chartArea)
)
if (isTargetOutsideChartAreas) {
this.dismissTooltip()
}
}
Expand Down Expand Up @@ -661,7 +669,7 @@ export class StackedAreaChart
return (
<g
ref={this.base}
className="StackedArea"
className={STACKED_AREA_CHART_CLASS_NAME}
onMouseLeave={this.onCursorLeave}
onTouchEnd={this.onCursorLeave}
onTouchCancel={this.onCursorLeave}
Expand Down Expand Up @@ -696,7 +704,7 @@ export class StackedAreaChart
render(): React.ReactElement {
if (this.failMessage)
return (
<g className="StackedArea">
<g>
{this.renderAxis()}
<NoDataModal
manager={this.manager}
Expand Down

0 comments on commit 1a76b25

Please sign in to comment.