Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-svg-text

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-svg-text

## Features - Word-wrapping (when `width` prop is defined) - Vertical alignment (`verticalAnchor` prop) - Rotation (`angle` prop) - Scale-to-fit text (`scaleToFit` prop)

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Better SVG text component for React

Features

  • Word-wrapping (when width prop is defined)
  • Vertical alignment (verticalAnchor prop)
  • Rotation (angle prop)
  • Scale-to-fit text (scaleToFit prop)

Example

Simple demo to show off a useful feature. Since svg <text> itself does not support verticalAnchor, normally text rendered at 0,0 would be outside the viewport and not visible. By using <Text> with verticalAnchor="start" prop, the text will now be visible as you'd expect.

import React from 'react';
import { render } from 'react-dom';
import Text from 'react-svg-text';

const App = () => (
  <svg>
    <Text verticalAnchor="start">Hello world</Text>
  </svg>
);

render(<App />, document.getElementById('root'));

See demo to see additional props and how they work

Props

PropertyTypeDefaultDescription
xnumberx coordinate to use as a basis for positioning the text element
ynumbery coordinate to use as a basis for positioning the text element
dxnumberHorizontal shift from the x coordinate
dynumberVertical shift from the y coordinate
widthnumberWidth of text container. Used to implement word wrapping and for context when scaleToFit is true
scaleToFitboolfalseResize text to fit container width
anglenumberRotate text around origin (defined by textAnchor and verticalAnchor)
textAnchorstringstartHow the text is horizontally positioned relative to the given x and y coordinates. Options are start, middle, end, and inherit.
verticalAnchorstringendHow text is vertically positioned relative to the given x and y coordinates. Options are start, middle, end
lineHeightstring1emHow much space a single line of text should take up
capHeightstring0.71em (Magic number from d3)Defines a text metric for the font being used: the expected height of capital letters. This is necessary because of SVG, which (a) positions the bottom of the text at y, and (b) has no notion of line height. This prop should be given as a number of ems
additional propsAdditional props are passed down to underlying <text> component, including style and className

TODO

  • Fix tests (jsdom does not support getComputedTextLength() or getBoundingClientRect()) and setup CI

FAQs

Package last updated on 02 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc