mentionize 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrea Cantarutti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # Mentionize
2
+
3
+ A React library for building mention inputs with support for multiple triggers, async search, and full customization. It provides a transparent textarea overlaid on a highlighted div to display mentions, and a dropdown for suggestions. With zero dependencies.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install mentionize
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```tsx
14
+ import { useState } from "react";
15
+ import { MentionInput } from "mentionize";
16
+ import type { MentionTrigger } from "mentionize";
17
+
18
+ const users = [
19
+ { id: "1", name: "Alice" },
20
+ { id: "2", name: "Bob" },
21
+ ];
22
+
23
+ const userTrigger: MentionTrigger<{ id: string; name: string }> = {
24
+ trigger: "@",
25
+ displayText: (user) => user.name,
26
+ serialize: (user) => `@[${user.name}](user:${user.id})`,
27
+ pattern: /@\[([^\]]+)\]\(user:([^)]+)\)/g,
28
+ parseMatch: (match) => ({ displayText: match[1]!, key: match[2]! }),
29
+ options: users,
30
+ };
31
+
32
+ function App() {
33
+ const [value, setValue] = useState("");
34
+
35
+ return (
36
+ <MentionInput
37
+ triggers={[userTrigger]}
38
+ value={value}
39
+ onChange={setValue}
40
+ placeholder="Type @ to mention someone..."
41
+ />
42
+ );
43
+ }
44
+ ```
45
+
46
+ The `value` passed to `onChange` is the **serialized** form (e.g. `Hello @[Alice](user:1)`). The component handles converting between the serialized and visible representations automatically.
47
+
48
+ ## API
49
+
50
+ ### `MentionTrigger<T>`
51
+
52
+ Defines how a trigger character activates suggestions and how mentions are serialized/parsed.
53
+
54
+ | Property | Type | Description |
55
+ |---|---|---|
56
+ | `trigger` | `string` | Character(s) that activate the trigger (e.g. `"@"`, `"#"`) |
57
+ | `displayText` | `(item: T) => string` | Converts an item to its visible text |
58
+ | `serialize` | `(item: T) => string` | Converts an item to its serialized form in the raw value |
59
+ | `pattern` | `RegExp` | Regex to detect serialized mentions (must use global flag) |
60
+ | `parseMatch` | `(match: RegExpExecArray) => { displayText: string; key: string }` | Parses a regex match back into display text and key |
61
+ | `options?` | `T[]` | Static options array (client-side filtering) |
62
+ | `onSearch?` | `(query: string, page: number) => Promise<{ items: T[]; hasMore: boolean }>` | Async search with pagination |
63
+ | `renderOption?` | `(item: T, highlighted: boolean) => ReactNode` | Custom option rendering |
64
+ | `renderMention?` | `(displayText: string) => ReactNode` | Custom mention highlight rendering |
65
+ | `mentionClassName?` | `string` | CSS class for highlighted mentions in the overlay |
66
+
67
+ ### `MentionInputProps`
68
+
69
+ | Property | Type | Description |
70
+ |---|---|---|
71
+ | `triggers` | `MentionTrigger<any>[]` | Array of trigger configurations |
72
+ | `value?` | `string` | Controlled raw/serialized value |
73
+ | `defaultValue?` | `string` | Initial raw value (uncontrolled mode) |
74
+ | `onChange?` | `(raw: string) => void` | Called when the raw value changes |
75
+ | `onMentionsChange?` | `(mentions: ActiveMention[]) => void` | Called when active mentions change |
76
+ | `placeholder?` | `string` | Textarea placeholder |
77
+ | `disabled?` | `boolean` | Disable the input |
78
+ | `rows?` | `number` | Textarea rows (default: 4) |
79
+ | `className?` | `string` | Container className |
80
+ | `inputClassName?` | `string` | Textarea className |
81
+ | `highlighterClassName?` | `string` | Highlighter overlay className |
82
+ | `dropdownClassName?` | `string` | Dropdown className |
83
+ | `dropdownWidth?` | `number` | Dropdown width in pixels (default: 250) |
84
+ | `renderDropdown?` | `(props: DropdownRenderProps) => ReactNode` | Full custom dropdown rendering |
85
+
86
+ ## Multiple Triggers
87
+
88
+ Pass multiple trigger configs to support different mention types:
89
+
90
+ ```tsx
91
+ const userTrigger: MentionTrigger<User> = { trigger: "@", /* ... */ };
92
+ const tagTrigger: MentionTrigger<Tag> = { trigger: "#", /* ... */ };
93
+
94
+ <MentionInput triggers={[userTrigger, tagTrigger]} />
95
+ ```
96
+
97
+ ## Async Search with Pagination
98
+
99
+ Use `onSearch` instead of `options` for server-side search. The dropdown automatically loads more results when scrolled to the bottom.
100
+
101
+ ```tsx
102
+ const trigger: MentionTrigger<User> = {
103
+ trigger: "@",
104
+ displayText: (user) => user.name,
105
+ serialize: (user) => `@[${user.name}](user:${user.id})`,
106
+ pattern: /@\[([^\]]+)\]\(user:([^)]+)\)/g,
107
+ parseMatch: (match) => ({ displayText: match[1]!, key: match[2]! }),
108
+ onSearch: async (query, page) => {
109
+ const res = await fetch(`/api/users?q=${query}&page=${page}`);
110
+ return res.json(); // { items: User[], hasMore: boolean }
111
+ },
112
+ };
113
+ ```
114
+
115
+ ## Headless Usage
116
+
117
+ Use `useMentionEngine` directly for full control over rendering:
118
+
119
+ ```tsx
120
+ import { useMentionEngine } from "mentionize";
121
+
122
+ const engine = useMentionEngine({
123
+ triggers: [userTrigger],
124
+ value,
125
+ onChange: setValue,
126
+ });
127
+
128
+ // engine.visible - display text
129
+ // engine.mentions - active mentions with positions
130
+ // engine.activeTrigger - currently active trigger (or null)
131
+ // engine.filteredOptions - filtered suggestions
132
+ // engine.handleTextChange(text, caretPos)
133
+ // engine.handleKeyDown(event, textarea)
134
+ // engine.selectOption(item, textarea)
135
+ ```
136
+
137
+ ## Styling
138
+
139
+ Mentionize uses a transparent textarea overlaid on a highlighted div. Apply styles via className props:
140
+
141
+ ```tsx
142
+ <MentionInput
143
+ className="my-container"
144
+ inputClassName="my-textarea"
145
+ highlighterClassName="my-highlighter"
146
+ dropdownClassName="my-dropdown"
147
+ triggers={[trigger]}
148
+ />
149
+ ```
150
+
151
+ Per-trigger mention highlights can be styled via `mentionClassName`:
152
+
153
+ ```tsx
154
+ const trigger: MentionTrigger<User> = {
155
+ trigger: "@",
156
+ mentionClassName: "mention-user",
157
+ // ...
158
+ };
159
+ ```
160
+
161
+ ### Tailwind CSS
162
+
163
+ ```tsx
164
+ <MentionInput
165
+ className="relative rounded-lg border border-gray-300 bg-white focus-within:border-blue-500 focus-within:ring-2 focus-within:ring-blue-200"
166
+ inputClassName="w-full border-none outline-none bg-transparent text-sm leading-relaxed"
167
+ highlighterClassName="text-sm leading-relaxed text-gray-900"
168
+ dropdownClassName="bg-white border border-gray-200 rounded-lg shadow-lg"
169
+ triggers={[userTrigger, tagTrigger]}
170
+ />
171
+ ```
172
+
173
+ Style mention highlights with Tailwind by referencing a utility class in `mentionClassName`:
174
+
175
+ ```tsx
176
+ const userTrigger: MentionTrigger<User> = {
177
+ trigger: "@",
178
+ mentionClassName: "bg-blue-100 text-blue-700 rounded px-0.5",
179
+ // ...
180
+ };
181
+
182
+ const tagTrigger: MentionTrigger<Tag> = {
183
+ trigger: "#",
184
+ mentionClassName: "bg-green-100 text-green-700 rounded px-0.5",
185
+ // ...
186
+ };
187
+ ```
188
+
@@ -0,0 +1,16 @@
1
+ import React from "react";
2
+ import type { CaretPosition, MentionTrigger } from "./types.ts";
3
+ interface MentionDropdownProps {
4
+ items: unknown[];
5
+ trigger: MentionTrigger<any>;
6
+ highlightedIndex: number;
7
+ onHighlight: (index: number) => void;
8
+ onSelect: (item: unknown) => void;
9
+ onLoadMore?: () => void;
10
+ loading: boolean;
11
+ position: CaretPosition;
12
+ width: number;
13
+ className?: string;
14
+ }
15
+ export declare const MentionDropdown: React.FC<MentionDropdownProps>;
16
+ export {};
@@ -0,0 +1,12 @@
1
+ import React from "react";
2
+ import type { ActiveMention, MentionTrigger } from "./types.ts";
3
+ interface MentionHighlighterProps {
4
+ visible: string;
5
+ mentions: ActiveMention[];
6
+ triggers: MentionTrigger<any>[];
7
+ textareaRef: React.RefObject<HTMLTextAreaElement | null>;
8
+ className?: string;
9
+ style?: React.CSSProperties;
10
+ }
11
+ export declare const MentionHighlighter: React.FC<MentionHighlighterProps>;
12
+ export {};
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ import type { MentionInputProps } from "./types.ts";
3
+ export declare const MentionInput: React.ForwardRefExoticComponent<MentionInputProps & React.RefAttributes<HTMLTextAreaElement>>;
@@ -0,0 +1,6 @@
1
+ export { MentionInput } from "./MentionInput.tsx";
2
+ export { MentionHighlighter } from "./MentionHighlighter.tsx";
3
+ export { MentionDropdown } from "./MentionDropdown.tsx";
4
+ export { useMentionEngine } from "./useMentionEngine.ts";
5
+ export { useCaretPosition } from "./useCaretPosition.ts";
6
+ export type { MentionTrigger, MentionInputProps, ActiveMention, DropdownRenderProps, CaretPosition, } from "./types.ts";