@gechiui/components
Version:
UI components for GeChiUI.
29 lines (22 loc) • 553 B
JavaScript
/**
* External dependencies
*/
import { escapeRegExp } from 'lodash';
/**
* GeChiUI dependencies
*/
import { createInterpolateElement } from '@gechiui/element';
const TextHighlight = ( { text = '', highlight = '' } ) => {
const trimmedHighlightText = highlight.trim();
if ( ! trimmedHighlightText ) {
return text;
}
const regex = new RegExp(
`(${ escapeRegExp( trimmedHighlightText ) })`,
'gi'
);
return createInterpolateElement( text.replace( regex, '<mark>$&</mark>' ), {
mark: <mark />,
} );
};
export default TextHighlight;