All files / owid-grapher/grapher/text Text.tsx

100% Statements 37/37
100% Branches 1/1
100% Functions 1/1
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 1x 1x
import * as React from "react"
import { Bounds } from "../../clientUtils/Bounds"
import { omit } from "../../clientUtils/Util"
 
// The default SVG text behavior is to put the text on *top* of the specified y coordinate
// Nothing else we do works like that though, so this wraps it to use the same spatial behavior
// as other components
 
interface TextProps extends React.SVGProps<SVGTextElement> {
    x: number
    y: number
    fontSize: number
    children: string
}
 
export class Text extends React.Component<TextProps> {
    render(): JSX.Element {
        const bounds = Bounds.forText(this.props.children, {
            fontSize: this.props.fontSize,
            fontFamily: this.props["fontFamily"],
        })
        const y = this.props.y + bounds.height - bounds.height * 0.2
 
        return (
            <text
                {...omit(this.props, ["children"])}
                y={y}
                dangerouslySetInnerHTML={{ __html: this.props.children }}
            />
        )
    }
}