A secure, performant React component library for rendering sanitized HTML with DOMPurify integration
π Documentation β’ π Quick Start β’ π Security β’ π€ Contributing β’ π License
- π Security First: Built with DOMPurify, the industry standard for HTML sanitization
- β‘ Performance: Optimized bundle size (~2.5KB gzipped) with tree-shaking support
- π¨ Developer Experience: Full TypeScript support with excellent IntelliSense
- π Universal: Works in React 18+, Next.js, Remix, and all modern React frameworks
- π‘οΈ XSS Protection: Comprehensive protection against malicious HTML content
- π± SSR Ready: Server-side rendering support with fallbacks
# npm
npm install react-secure-html
# pnpm (recommended)
pnpm add react-secure-html
# yarn
yarn add react-secure-html{
"react": "^18 || ^19",
"react-dom": "^18 || ^19"
}import { SafeHTML } from 'react-secure-html';
function App() {
return <SafeHTML content="<p>Hello <strong>World</strong>!</p>" allowHTML />;
}<SafeHTML content={userContent} allowHTML tag="article" className="prose prose-lg max-w-none" />import { useSanitize } from 'react-secure-html';
function MyComponent() {
const { sanitize, sanitizeHTML, escape } = useSanitize();
const safeText = sanitize(userInput, false); // Escapes HTML
const safeHTML = sanitizeHTML(userHTML); // Sanitizes with DOMPurify
return <div dangerouslySetInnerHTML={{ __html: safeHTML }} />;
}| Prop | Type | Default | Description |
|---|---|---|---|
content |
string |
- | Raw HTML or text content to render |
allowHTML |
boolean |
false |
Whether to allow HTML tags (sanitized) |
className |
string |
- | CSS class name to apply |
tag |
keyof React.JSX.IntrinsicElements |
'div' |
HTML tag to render as |
Returns an object with sanitization utilities:
const {
sanitize, // (input: string, allowHTML?: boolean) => string
sanitizeHTML, // (html: string, options?: Config) => string
escape, // (text: string) => string
sanitizeUrl, // (url: string) => string
} = useSanitize();import { sanitizeHTML, escapeHTML, sanitizeInput, sanitizeURL } from 'react-secure-html';
// Sanitize HTML with DOMPurify
const cleanHTML = sanitizeHTML('<p>Safe content</p>');
// Escape HTML entities
const escapedText = escapeHTML('<script>alert("xss")</script>');
// Sanitize user input
const safeInput = sanitizeInput(userInput, false);
// Sanitize URLs
const safeUrl = sanitizeURL('https://example.com');This library provides multiple layers of security:
- DOMPurify Integration: Uses the industry-standard DOMPurify library
- HTML Entity Escaping: Converts dangerous characters to safe entities
- URL Sanitization: Prevents
javascript:anddata:URL schemes - Tag Filtering: Only allows safe HTML tags by default
// By default, HTML is escaped (safe)
<SafeHTML content="<script>alert('xss')</script>" />
// Renders: <script>alert('xss')</script>
// Only when explicitly allowed, HTML is sanitized
<SafeHTML content="<script>alert('xss')</script>" allowHTML />
// Renders: (script tag removed)b,i,em,strong- Text formattingp,br,span- Structureclass,idattributes only
function BlogPost({ content }: { content: string }) {
return (
<SafeHTML content={content} allowHTML tag="article" className="prose prose-lg max-w-none" />
);
}function Comment({ text }: { text: string }) {
return <SafeHTML content={text} allowHTML tag="div" className="comment-content" />;
}function RichTextPreview({ content }: { content: string }) {
const { sanitizeHTML } = useSanitize();
return (
<div
className="preview"
dangerouslySetInnerHTML={{
__html: sanitizeHTML(content, {
ALLOWED_TAGS: ['p', 'br', 'strong', 'em', 'a'],
ALLOWED_ATTR: ['href', 'class'],
}),
}}
/>
);
}function EmailTemplate({ html }: { html: string }) {
return <SafeHTML content={html} allowHTML tag="div" className="email-template" />;
}- Node.js 18+
- pnpm (recommended) or npm
# Clone the repository
git clone https://github.com/ramonxm/react-secure-html.git
cd sanitize-html
# Install dependencies
pnpm install
# Start development
pnpm devpnpm dev # Watch mode for development
pnpm build # Build for production
pnpm test # Run tests
pnpm test:watch # Run tests in watch mode
pnpm test:ui # Run tests with UI
pnpm lint # Lint code
pnpm lint:fix # Fix linting issues
pnpm format # Format code
pnpm type-check # TypeScript type checking# Run all tests
pnpm test
# Run tests with coverage
pnpm test --coverage
# Run tests in watch mode
pnpm test:watch- Bundle Size: ~2.5KB gzipped
- Tree Shaking: Full support for unused code elimination
- Runtime Performance: Optimized with React.useMemo and useCallback
- Memory Efficient: No memory leaks, proper cleanup
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Run tests:
pnpm test - Commit:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- DOMPurify - For providing excellent HTML sanitization
- React - For the amazing framework
- TypeScript - For type safety
- π Documentation
- π Report Bug
- π‘ Request Feature
- π¬ Discussions
Made with β€οΈ by Ramon Xavier
β Star this repo β’ π Report Bug β’ π‘ Request Feature