I wish to automatically select some text in a span element so the user can easily copy it.
I have tried using .select()
however, this appears to only work for <input>
& <textarea>
elements; my text is within a <span>
and I don't want to change it because I handle all text within my application with another component that takes care of the styling (to answer @dandavis because comment wasn't working for me).
My text is rendered in a popup so I want to show selected for the user.
Here is what I tried:
import React from "react";
const PropTypes = React.PropTypes;
class CopyText extends React.Component {
constructor(props) {
super(props);
this.handleRef = this.handleRef.bind(this);
}
componentDidUpdate() {
this.copyText.select();
}
handleRef(component) {
this.copyText = component;
}
render() {
return (
<span ref={this.handleRef}>
{this.props.text}
</span>
);
}
}
CopyText.propTypes = {
text: PropTypes.string
};
export default CopyText;
Would anyone be able to help me create a custom auto-select text function for a span element? Thanks very much for your advice.