Skip to content
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

✨ make text outline work in Figma #4538

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/@ourworldindata/components/src/Halo/Halo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface HaloProps {
id: string
children: React.ReactElement
show?: boolean
outlineWidth: number
outlineColor?: Color
style?: React.CSSProperties
}
Expand All @@ -14,7 +15,6 @@ const defaultHaloStyle: React.CSSProperties = {
stroke: "#fff",
strokeLinecap: "round",
strokeLinejoin: "round",
strokeWidth: ".25em",
userSelect: "none",
}

Expand All @@ -24,6 +24,7 @@ export function Halo(props: HaloProps): React.ReactElement {

const defaultStyle = {
...defaultHaloStyle,
strokeWidth: props.outlineWidth,
fill: props.outlineColor ?? defaultHaloStyle.fill,
stroke: props.outlineColor ?? defaultHaloStyle.stroke,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/grapher/src/core/GrapherConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const GRAPHER_AREA_OPACITY_DEFAULT = 0.8
export const GRAPHER_AREA_OPACITY_MUTE = GRAPHER_OPACITY_MUTE
export const GRAPHER_AREA_OPACITY_FOCUS = 1

export const GRAPHER_TEXT_OUTLINE_FACTOR = 0.25

export const BASE_FONT_SIZE = 16

export const GRAPHER_FONT_SCALE_9_6 = 9.6 / BASE_FONT_SIZE
Expand Down
14 changes: 13 additions & 1 deletion packages/@ourworldindata/grapher/src/lineLegend/LineLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
SeriesName,
VerticalAlign,
} from "@ourworldindata/types"
import { BASE_FONT_SIZE, GRAPHER_FONT_SCALE_12 } from "../core/GrapherConstants"
import {
BASE_FONT_SIZE,
GRAPHER_FONT_SCALE_12,
GRAPHER_TEXT_OUTLINE_FACTOR,
} from "../core/GrapherConstants"
import { darkenColorForText } from "../color/ColorUtils"
import { AxisConfig } from "../axis/AxisConfig.js"
import { GRAPHER_BACKGROUND_DEFAULT, GRAY_30 } from "../color/ColorConstants"
Expand Down Expand Up @@ -141,6 +145,10 @@ class LineLabels extends React.Component<{
)}
key={getSeriesKey(series, index)}
show={this.showTextOutline}
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR *
series.textWrapForRendering.fontSize
}
outlineColor={this.textOutlineColor}
>
{series.textWrapForRendering.renderSVG(
Expand Down Expand Up @@ -175,6 +183,10 @@ class LineLabels extends React.Component<{
id={series.seriesName}
key={getSeriesKey(series, index)}
show={this.showTextOutline}
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR *
series.annotationTextWrap.fontSize
}
outlineColor={this.textOutlineColor}
>
{series.annotationTextWrap.renderSVG(
Expand Down
16 changes: 13 additions & 3 deletions packages/@ourworldindata/grapher/src/noDataModal/NoDataModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BASE_FONT_SIZE,
DEFAULT_GRAPHER_ENTITY_TYPE,
DEFAULT_GRAPHER_ENTITY_TYPE_PLURAL,
GRAPHER_TEXT_OUTLINE_FACTOR,
} from "../core/GrapherConstants"
import { Halo } from "@ourworldindata/components"
import { GRAPHER_DARK_TEXT, GRAPHER_LIGHT_TEXT } from "../color/ColorConstants"
Expand Down Expand Up @@ -65,12 +66,16 @@ export class NoDataModal extends React.Component<{
const center = bounds.centerPos
const padding = 0.75 * this.fontSize
const showHelpText = !isStatic && !!helpText
const helpTextFontSize = 0.9 * this.fontSize

return (
<g className="no-data">
<rect {...bounds.toProps()} fill="#fff" opacity={0.6} />

<Halo id="no-data-message">
<Halo
id="no-data-message"
outlineWidth={GRAPHER_TEXT_OUTLINE_FACTOR * this.fontSize}
>
<text
x={center.x}
y={center.y}
Expand All @@ -89,13 +94,18 @@ export class NoDataModal extends React.Component<{
</Halo>

{showHelpText && (
<Halo id="no-data-help">
<Halo
id="no-data-help"
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR * helpTextFontSize
}
>
<text
x={center.x}
y={center.y + padding / 2}
textAnchor="middle"
dy={dyFromAlign(VerticalAlign.bottom)}
fontSize={0.9 * this.fontSize}
fontSize={helpTextFontSize}
fill={GRAPHER_LIGHT_TEXT}
>
{helpText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { DualAxis } from "../axis/Axis"
import { generateComparisonLinePoints } from "./ComparisonLineGenerator"
import { Halo } from "@ourworldindata/components"
import { Color, ComparisonLineConfig } from "@ourworldindata/types"
import { GRAPHER_FONT_SCALE_10_5 } from "../core/GrapherConstants"
import {
GRAPHER_FONT_SCALE_10_5,
GRAPHER_TEXT_OUTLINE_FACTOR,
} from "../core/GrapherConstants"

@observer
export class ComparisonLine extends React.Component<{
Expand Down Expand Up @@ -85,6 +88,8 @@ export class ComparisonLine extends React.Component<{
const { innerBounds } = this.props.dualAxis
const { linePath, renderUid, placedLabel } = this

const fontSize = GRAPHER_FONT_SCALE_10_5 * this.props.baseFontSize

return (
<g
id={makeIdForHumanConsumption("comparison-line")}
Expand Down Expand Up @@ -114,9 +119,7 @@ export class ComparisonLine extends React.Component<{
{placedLabel && (
<text
style={{
fontSize:
GRAPHER_FONT_SCALE_10_5 *
this.props.baseFontSize,
fontSize,
opacity: 0.9,
textAnchor: "end",
fill: "#999",
Expand All @@ -125,6 +128,9 @@ export class ComparisonLine extends React.Component<{
>
<Halo
id={`path-${renderUid}`}
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR * fontSize
}
outlineColor={this.props.backgroundColor}
>
<textPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from "./ScatterUtils"
import { Triangle } from "./Triangle"
import { ColorScale } from "../color/ColorScale"
import { GRAPHER_TEXT_OUTLINE_FACTOR } from "../core/GrapherConstants"

// This is the component that actually renders the points. The higher level ScatterPlot class renders points, legends, comparison lines, etc.
@observer
Expand Down Expand Up @@ -433,6 +434,9 @@ export class ScatterPointsWithLabels extends React.Component<ScatterPointsWithLa
"outline",
series.seriesName
)}
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR * label.fontSize
}
outlineColor={this.props.backgroundColor}
>
<text
Expand Down Expand Up @@ -580,6 +584,9 @@ export class ScatterPointsWithLabels extends React.Component<ScatterPointsWithLa
series.seriesName
)}
key={`${series.displayKey}-label-${index}`}
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR * label.fontSize
}
outlineColor={this.props.backgroundColor}
>
<text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BASE_FONT_SIZE,
GRAPHER_FONT_SCALE_10,
GRAPHER_FONT_SCALE_11,
GRAPHER_TEXT_OUTLINE_FACTOR,
} from "../core/GrapherConstants"
import { CoreColumn } from "@ourworldindata/core-table"
import {
Expand Down Expand Up @@ -293,11 +294,6 @@ const LegendItem = ({
outsideLabel?: boolean
backgroundColor?: Color
}): React.ReactElement => {
const style: React.CSSProperties = {
fontSize: labelFontSize,
fontWeight: labelFontWeight,
textAnchor: "middle",
}
return (
<g>
<circle
Expand All @@ -311,15 +307,17 @@ const LegendItem = ({
/>
<Halo
id={label}
outlineWidth={GRAPHER_TEXT_OUTLINE_FACTOR * labelFontSize}
outlineColor={backgroundColor}
style={{ ...style, strokeWidth: 3.5 }}
>
<text
x={cx}
y={cy - circleRadius}
dy={outsideLabel ? "-.32em" : ".47em"}
fill={labelFill}
style={style}
fontSize={labelFontSize}
fontWeight={labelFontWeight}
textAnchor="middle"
>
{label}
</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BASE_FONT_SIZE,
GRAPHER_FONT_SCALE_12,
GRAPHER_OPACITY_MUTE,
GRAPHER_TEXT_OUTLINE_FACTOR,
} from "../core/GrapherConstants"
import {
ScaleType,
Expand Down Expand Up @@ -1242,6 +1243,9 @@ export class SlopeChart
return (
<Halo
id="x-axis-zero-label"
outlineWidth={
GRAPHER_TEXT_OUTLINE_FACTOR * this.lineLegendFontSize
}
outlineColor={this.backgroundColor}
>
<text
Expand Down