An open API service indexing awesome lists of open source software.

https://github.com/shiyangzhaoa/react-text-maker

A React component for text highlighting and annotation
https://github.com/shiyangzhaoa/react-text-maker

annotate component highlight react

Last synced: 28 days ago
JSON representation

A React component for text highlighting and annotation

Awesome Lists containing this project

README

          

# React Text Marker

English | [็ฎ€ไฝ“ไธญๆ–‡](README.zh-CN.md)

A React component for text highlighting and annotation with customizable themes and interactive features.


Tailwind Tool



Total Downloads
Latest Release
License

## Features

- ๐ŸŽจ Customizable highlight colors and themes
- ๐Ÿ–ฑ๏ธ Interactive text selection and highlighting
- โŒจ๏ธ Keyboard shortcuts support (Delete/Backspace to remove highlights)
- ๐ŸŽฏ Multiple highlight support with nested ranges
- ๐Ÿ“ฑ Responsive design
- ๐Ÿงช Comprehensive test coverage
- ๐Ÿ” Customizable hint display
- ๐ŸŽญ Theme-based highlighting
- โšก Real-time highlight updates

## Installation

```bash
# Using npm
npm install react-text-maker

# Using yarn
yarn add react-text-maker

# Using pnpm
pnpm add react-text-maker
```

## Usage

```tsx
import { ReactTextMaker } from 'react-text-maker';
import 'react-text-maker/dist/style.css';

function App() {
const [highlights, setHighlights] = useState([]);

return (
console.log('Clicked highlights:', ids)}
onMarkAdd={(item) => console.log('Added highlight:', item)}
onMarkRemove={(items) => console.log('Removed highlights:', items)}
/>
);
}
```

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| text | string | required | The text content to be highlighted |
| hint | string | required | The hint text to be displayed with highlights |
| value | HighlightItem[] | [] | Array of highlight items |
| onChange | (items: HighlightItem[]) => void | - | Callback when highlights change |
| onMarkClick | (ids: string[]) => void | - | Callback when a highlight is clicked |
| onMarkAdd | (item: HighlightItem) => void | - | Callback when a highlight is added |
| onMarkRemove | (items: HighlightItem[]) => void \| boolean | - | Callback when highlights are removed |
| disabled | boolean | false | Whether the component is disabled |
| className | string | - | Additional CSS class name |
| style | React.CSSProperties | - | Additional inline styles |
| theme | (string \| { hint: string; color?: string })[] | DEFAULT_THEME | Array of color codes or theme objects for highlights |
| maxCount | number | - | Maximum number of highlights allowed |
| hintStyle | React.CSSProperties | - | Custom styles for hint text |

## HighlightItem Type

```typescript
interface HighlightItem {
id: string; // Unique identifier for the highlight
text: string; // The highlighted text content
hint: string; // The hint text to display
range: [number, number]; // Start and end indices of the highlight
color: [number, number, number]; // rgb
}
```

## Development

```bash
# Install dependencies
pnpm install

# Start development server
pnpm dev

# Run tests
pnpm test

# Run tests with coverage
pnpm test:coverage

# Run tests with UI
pnpm test:ui

# Build
pnpm build

# Lint
pnpm lint

# Lint with auto-fix
pnpm lint:fix
```

## Testing

The project uses Vitest and React Testing Library for testing. Run the following commands:

```bash
# Run tests
pnpm test

# Run tests with coverage
pnpm test:coverage

# Run tests with UI
pnpm test:ui
```

## Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

MIT