Skip to content

Instantly share code, notes, and snippets.

@yarnball
Created February 11, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yarnball/0600caaac71c3e0bc03cac60b1da9cf1 to your computer and use it in GitHub Desktop.
Save yarnball/0600caaac71c3e0bc03cac60b1da9cf1 to your computer and use it in GitHub Desktop.
import React from "react"
import { render } from "react-dom"
class App extends React.Component {
state = { highlight: "", pos: "" }
getText = e => {
this.setState({
highlight: window.getSelection().toString(),
pos: window.getSelection()
})
}
render() {
const { highlight, pos } = this.state
return (
<div>
<div onClick={this.getText}> I am a body of text</div>
<h4> Highlighted: </h4>
<div style={{ background: "yellow" }}>
TEXT: {highlight}
<br />
POSITION:
{pos.anchorOffset} to
{pos.extentOffset}
</div>
</div>
)
}
}
render(<App />, document.getElementById("root"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment