Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a new highlighter component #543

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions __registry__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,30 @@ export const Index: Record<string, any> = {
}),
meta: undefined,
},
highlighter: {
name: "highlighter",
description:
"A text highlighter that mimics the effect of a human-drawn marker stroke.",
type: "registry:ui",
registryDependencies: undefined,
files: [
{
path: "registry/magicui/highlighter.tsx",
type: "registry:ui",
target: "components/magicui/highlighter.tsx",
},
],
component: React.lazy(async () => {
const mod = await import("@/registry/magicui/highlighter.tsx");
const exportName =
Object.keys(mod).find(
(key) =>
typeof mod[key] === "function" || typeof mod[key] === "object",
) || item.name;
return { default: mod.default || mod[exportName] };
}),
meta: undefined,
},
"magic-card-demo": {
name: "magic-card-demo",
description:
Expand Down Expand Up @@ -4710,6 +4734,34 @@ export const Index: Record<string, any> = {
}),
meta: undefined,
},
"highlighter-demo": {
name: "highlighter-demo",
description: "Example showing the demo of a Highlighter",
type: "registry:example",
registryDependencies: ["https://magicui.design/r/highlighter"],
files: [
{
path: "registry/example/highlighter-demo.tsx",
type: "registry:example",
target: "components/highlighter-demo.tsx",
},
{
path: "registry/magicui/highlighter.tsx",
type: "registry:ui",
target: "components/magicui/highlighter.tsx",
},
],
component: React.lazy(async () => {
const mod = await import("@/registry/example/highlighter-demo.tsx");
const exportName =
Object.keys(mod).find(
(key) =>
typeof mod[key] === "function" || typeof mod[key] === "object",
) || item.name;
return { default: mod.default || mod[exportName] };
}),
meta: undefined,
},
utils: {
name: "utils",
description: "",
Expand Down
6 changes: 6 additions & 0 deletions config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ export const docsConfig: DocsConfig = {
items: [],
label: "",
},
{
title: "Highlighter",
href: "/docs/components/highlighter",
items: [],
label: "New",
},
],
},
{
Expand Down
50 changes: 50 additions & 0 deletions content/docs/components/highlighter.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Highlighter
date: 2025-02-11
description: A text highlighter that mimics the effect of a human-drawn marker stroke.
author: pratiyank
published: true
---

<ComponentPreview name="highlighter-demo" />

## Installation

<Tabs defaultValue="cli">

<TabsList>
<TabsTrigger value="cli">CLI</TabsTrigger>
<TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
npx shadcn@latest add "https://magicui.design/r/highlighter"
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="highlighter" />

</Steps>

</TabsContent>

</Tabs>

## Props

| Prop | Type | Default | Description |
| -------- | ----------------------- | ------------- | -------------------------- |
| `color` | `String` | `"pink"` | The color of the highlight |
| `action` | "highlight" or "circle" | `"highlight"` | Action of the highlighter |

## Credits

- Credit to [@pratiyank](https://github.com/Pratiyankkumar) for this component!
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"react-dom": "18.3.1",
"react-hook-form": "^7.50.1",
"react-tweet": "^3.2.1",
"rough-notation": "^0.5.1",
"schema-dts": "^1.1.2",
"shadcn": "2.3.0",
"sonner": "^1.5.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions public/r/highlighter-demo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
"name": "highlighter-demo",
"type": "registry:example",
"title": "Highlighter Demo",
"description": "Example showing the demo of a Highlighter",
"dependencies": [
"rough-notation"
],
"registryDependencies": [
"https://magicui.design/r/highlighter"
],
"files": [
{
"path": "registry/example/highlighter-demo.tsx",
"content": "import Highlighter from \"@/components/magicui/highlighter\";\n\nexport default function HighlighterDemo() {\n return (\n <div className=\"relative justify-center\">\n <p className=\"w-full text-center\">\n This is <Highlighter color=\"#FF9800\"> Magic UI Highlighter</Highlighter>{\" \"}\n component, designed to make important text stand out effortlessly.\n Customize colors, actions, and styles to fit your needs.{\" \"}\n <Highlighter color=\"#FFC107\" action=\"circle\">\n Try it out now!\n </Highlighter>\n </p>\n </div>\n );\n}\n",
"type": "registry:example",
"target": "components/highlighter-demo.tsx"
},
{
"path": "registry/magicui/highlighter.tsx",
"content": "\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport { annotate } from \"rough-notation\";\nimport type { RoughAnnotation } from \"rough-notation/lib/model\";\nimport type React from \"react\";\n\ninterface HighlighterProps {\n children: React.ReactNode;\n action?: \"highlight\" | \"circle\";\n color?: string;\n}\n\nexport default function Highlighter({\n children,\n action = \"highlight\",\n color = \"#ffd1dc\", // Default pink color\n}: HighlighterProps) {\n const elementRef = useRef<HTMLSpanElement>(null);\n const annotationRef = useRef<RoughAnnotation | null>(null);\n\n useEffect(() => {\n if (elementRef.current) {\n const annotation = annotate(elementRef.current, {\n type: action === \"circle\" ? \"circle\" : \"highlight\",\n color: color,\n multiline: true,\n padding: action === \"circle\" ? 8 : 2,\n iterations: 2, // More iterations for a natural effect\n animationDuration: 500,\n });\n\n annotationRef.current = annotation;\n annotation.show();\n }\n\n return () => {\n annotationRef.current?.remove();\n };\n }, [action, color, elementRef.current]); // Added elementRef.current dependency\n\n return (\n <span ref={elementRef} className=\"relative inline-block bg-transparent\">\n {children}\n </span>\n );\n}\n",
"type": "registry:ui",
"target": "components/magicui/highlighter.tsx"
}
]
}
18 changes: 18 additions & 0 deletions public/r/highlighter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
"name": "highlighter",
"type": "registry:ui",
"title": "Highlighter",
"description": "A text highlighter that mimics the effect of a human-drawn marker stroke.",
"dependencies": [
"rough-notation"
],
"files": [
{
"path": "registry/magicui/highlighter.tsx",
"content": "\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport { annotate } from \"rough-notation\";\nimport type { RoughAnnotation } from \"rough-notation/lib/model\";\nimport type React from \"react\";\n\ninterface HighlighterProps {\n children: React.ReactNode;\n action?: \"highlight\" | \"circle\";\n color?: string;\n}\n\nexport default function Highlighter({\n children,\n action = \"highlight\",\n color = \"#ffd1dc\", // Default pink color\n}: HighlighterProps) {\n const elementRef = useRef<HTMLSpanElement>(null);\n const annotationRef = useRef<RoughAnnotation | null>(null);\n\n useEffect(() => {\n if (elementRef.current) {\n const annotation = annotate(elementRef.current, {\n type: action === \"circle\" ? \"circle\" : \"highlight\",\n color: color,\n multiline: true,\n padding: action === \"circle\" ? 8 : 2,\n iterations: 2, // More iterations for a natural effect\n animationDuration: 500,\n });\n\n annotationRef.current = annotation;\n annotation.show();\n }\n\n return () => {\n annotationRef.current?.remove();\n };\n }, [action, color, elementRef.current]); // Added elementRef.current dependency\n\n return (\n <span ref={elementRef} className=\"relative inline-block bg-transparent\">\n {children}\n </span>\n );\n}\n",
"type": "registry:ui",
"target": "components/magicui/highlighter.tsx"
}
]
}
Loading