
Research
/Security News
60 Malicious Ruby Gems Used in Targeted Credential Theft Campaign
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
react-interactive-highlighter
Advanced tools
A React component to select and display an arbitrary number of highlighted subsections in a text body.
A React component to select and display an arbitrary number of highlighted subsections in a text body.
A lot of other highlighter components focus on display only, and expect logic outside of the component to determine which strings should be highlighted. While react-interactive-highlighter
can be used this way, it also allows creation of new highlights at runtime via text selection.
This is a standard npm
package, installable with:
npm i react-interactive-highligher
or:
yarn add react-interactive-highlighter
For the most basic usage, simply provide the text block to be highlighted, and a list of highlights, which are defined by their start position in the text block and their length. (If the highlight list is empty, nothing will be highlighted.)
.highlighted {
background-color: #d9d9d9
}
import React from 'react';
import ReactDOM from 'react-dom';
import { InteractiveHighlighter } from 'react-interactive-highlighter';
import './index.css';
class TextWithHighlights extends React.Component {
render() {
const text = "The sky above the port was the color of television, tuned to a dead channel."
const highlights = [
{ startIndex: 63, numChars: 12 }
]
return (
<InteractiveHighlighter
text={text}
highlights={highlights}
customClass='highlighted'
/>
)
}
}
ReactDOM.render(<TextWithHighlights />, document.getElementById('root'));
If a selectionHandler()
is provided, it will be called onMouseUp
to allow new highlights to be created:
import * as React from 'react';
import ReactDOM from 'react-dom';
import { InteractiveHighlighter } from 'react-interactive-highlighter';
import './index.css';
class TextWithHighlights extends React.Component {
constructor() {
super();
this.state = {
text: "I saw the best minds of my generation destroyed by madness, starving hysterical naked, dragging themselves...",
highlights: []
}
this.selectionHandler = this.selectionHandler.bind(this);
}
selectionHandler(selected, startIndex, numChars) {
this.setState({
text: this.state.text,
highights: this.state.highlights.push({
startIndex: startIndex,
numChars: numChars
})
})
}
render() {
return (
<InteractiveHighlighter
text={this.state.text}
highlights={this.state.highlights}
customClass='highlighted'
selectionHandler={this.selectionHandler}
/>
)
}
}
ReactDOM.render(<TextWithHighlights />, document.getElementById('root'));
Property | Type | Required? | Description |
---|---|---|---|
text | String | Yes | The text containing highlights. |
customClass | String | CSS class used for highlighted sections ("default" if not defined). Note: Not used if customClassFn is provided. | |
customClassFn | Function | Callback function that takes a list of highlight indexes and returns a className to use for a given segment of text. (This lets the parent component logic choose highlighting visuals based on the substance of the data.) | |
highlights | List of objects | Yes | List of highlights, each defined by an object with properties startIndex (from 0) and numChars |
selectionHandler | Function | Called whenever a new selection is made (via onMouseUp ) - will receive the selected string, its start index in the text param, and its length. | |
getSelectionFn | Function | Hook to provide a mock for unit testing purposes (see tests for usage example). |
The logic for computing new highlight offsets in the presence of an existing highlight got a helpful jump-start from Dheeraj Suthar's react-highlight-selection component.
MIT - feel free to use, modify, or distribute as you wish.
FAQs
A React component to select and display an arbitrary number of highlighted subsections in a text body.
The npm package react-interactive-highlighter receives a total of 554 weekly downloads. As such, react-interactive-highlighter popularity was classified as not popular.
We found that react-interactive-highlighter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.