This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/components/Highlight/Highlight.js

35 lines
763 B
JavaScript

// @flow
import React from 'react';
import replace from 'string-replace-to-array';
import styled from 'styled-components';
import { color } from 'shared/styles/constants';
type Props = {
highlight: ?string,
text: string,
caseSensitive?: boolean,
};
function Highlight({ highlight, caseSensitive, text, ...rest }: Props) {
return (
<span {...rest}>
{highlight
? replace(
text,
new RegExp(
(highlight || '').replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'),
caseSensitive ? 'g' : 'gi'
),
(tag, index) => <Mark key={index}>{tag}</Mark>
)
: text}
</span>
);
}
const Mark = styled.mark`
background: ${color.yellow};
`;
export default Highlight;