Coder Social home page Coder Social logo

react-svg-text's Introduction

Better SVG text component for React

Note: Merged into vx and is now @vx/text

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

Property Type Default Description
x number x coordinate to use as a basis for positioning the text element
y number y coordinate to use as a basis for positioning the text element
dx number Horizontal shift from the x coordinate
dy number Vertical shift from the y coordinate
width number Width of text container. Used to implement word wrapping and for context when scaleToFit is true
scaleToFit bool false Resize text to fit container width
angle number Rotate text around origin (defined by textAnchor and verticalAnchor)
textAnchor string start How the text is horizontally positioned relative to the given x and y coordinates. Options are start, middle, end, and inherit.
verticalAnchor string end How text is vertically positioned relative to the given x and y coordinates. Options are start, middle, end
lineHeight string 1em How much space a single line of text should take up
capHeight string 0.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 props Additional 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

react-svg-text's People

Contributors

techniq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-svg-text's Issues

IE11 Text visible on screen

It appears when used inside IE11 the text element you use for measuring text size remains visible on the page, e.g.

image

My temporary fix is just setting the style, e.g.

#__react_svg_text_measurement_id{
  opacity: 0;
}

This element should be removed after measurement is complete?

Possibility to choose between a text or a tspan as main tag element

Wonderful package very useful, thank you :)

I am using it with react-vis to have nice axis tick labels. In this particular use case you need to render a tspan and not a text element because there is already a text element.

So I think it could be cool nice to have an option to choose which tag element you want to render so more people could directly use your package.

Anyway it works great! Thanks for sharing your solution to this tricky problem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.