question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to add onClick event to line wrap?

See original GitHub issue

I’m looking to add event to each line through an onClick property.

Essentially, I wanna highlight a row on click and create a popup. I’m looking into the highlight’s wrapLinesInSpan function, but I am not sure how to make it work. https://github.com/conorhastings/react-syntax-highlighter/blob/master/src/highlight.js#L78

I am very impressed with this project and really hope that someone has already ran into a similar issue.

My code is below, mostly pulled from examples. As you can see I’m struggling with how to hit handleLineClicked from each <span> element.

import React, { Component } from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { monokaiSublime } from 'react-syntax-highlighter/styles/hljs';
import './App.css';

const CODE = 
`  const woah = fun => fun + 1;
  const dude = woah(2) + 3;
  function thisIsAFunction() {
    return [1,2,3].map(n => n + 1).filter(n !== 3);
  }
  console.log('making up fake code is really hard');

  function itIs() {
    return 'no seriously really it is';
  }
`;

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      lineToHighlight: null
    };
  }

  handleLineClicked(e) {
    // const lineNum = e.Something;
    // this.setState({lineToHighlight: lineNum});
    console.log('something has been clicked:', e);
  }
  render() {
    return (
      <div className="App">
        <div style={{paddingTop: 20, display: 'flex'}}>
          <div style={{flex: 1, width: '100%', flexDirection: 'column'}}>
            <SyntaxHighlighter 
              style={monokaiSublime} 
              wrapLines={true}
              showLineNumbers={true}
              lineStyle={lineNumber => {
                let style = { display: 'block' };
                if (this.state.lineToHighlight === lineNumber) {
                  style.backgroundColor = 'rgb(41, 62, 98)';
                }
                return style;
              }}
            >
              {CODE}
            </SyntaxHighlighter>
          </div>
        </div>
      </div>
    );
  }
}

export default App;

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
conorhastingscommented, Jan 24, 2018

@clevernyyyy cool, already started working this morning on what will likely be a major version release, allowing for arbitrary props to through to the lines, which will allow click events, so please leave open, thanks!

0reactions
clevernyyyycommented, Jan 26, 2018

@conorhastings - you’re a legend mate, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to add onClick event to line wrap? #101 - GitHub
I'm looking to add event to each line through an onClick property. Essentially, I wanna highlight a row on click and create a...
Read more >
Adding an onclick event to a text without breaking the line
Use span <span>See our <span onclick="openWindow()" style="cursor: pointer; color:blue">support page</span> and other things...</span>.
Read more >
Wrap text in a cell - Microsoft Support
In a worksheet, select the cells that you want to format. On the Home tab, in the Alignment group, click Wrap Text. (On...
Read more >
HTML: A good basis for accessibility - Learn web development
Anchor tags are often abused with the onclick event to create pseudo-buttons by setting href to "#" or "javascript:void(0)" to prevent the page ......
Read more >
How to wrap text in Excel automatically and manually - Ablebits
The fastest way is to select the cell(s) and click the Wrap Text ... For other ways to insert a line break in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found