@skyastrall/mentions-core
v0.5.0
Published
Framework-agnostic engine for trigger detection, markup parsing, and mention state management. Zero dependencies.
Maintainers
Readme
@skyastrall/mentions-core
Framework-agnostic engine for trigger detection, markup parsing, and mention state management. Zero dependencies. Runs in Node.js, browser, or any JS runtime.
Docs · Playground · GitHub
Most users should install a framework adapter instead:
@skyastrall/mentions-react,@skyastrall/mentions-vue,@skyastrall/mentions-svelte, or@skyastrall/mentions-angular. This package is for building custom adapters, server-side usage (parsing markup, extracting mentions), or any non-framework context.
Install
npm install @skyastrall/mentions-coreWhat's inside
MentionController—subscribe/getStatestore (TanStack-style) that drives the mention lifecycle: debounce, abort, fetch staleness, and callbacks.- State machine — pure reducer for the mention lifecycle (
idle → suggesting → navigating → loading) with state-scoped transitions. - Markup parser —
parseMarkup(),extractMentions(),markupToPlainText(),plainIndexToMarkupIndex(),applyChange(). - Trigger detection —
detectTrigger(), backward scan, multi-character triggers sorted longest-first. - ARIA props —
connect(state, id)generates exact-shapeInputAriaProps/ListAriaProps/ItemAriaProps. WAI-ARIA 1.2 combobox. - DOM utilities (browser-only) —
buildMentionHTML(),performMentionInsertion(),getCaretRect(),getCursorOffset(),getMarkupFromDOM(),getPlainTextFromDOM(),restoreCursor(),insertTextAtCursor(),isExtensionNode(). - Filter —
filterItems()(default case-insensitive prefix-then-includes). Replaceable viafilterFn. - CSS variables —
--mention-bg,--mention-color,--mention-radiusfor theming<mark data-mention>nodes. - Optional effects stylesheet —
import "@skyastrall/mentions-core/effects.css"for prebuilt focus/animation classes (mentions-gradient-border,mentions-shimmer,mentions-glow,mentions-animate) and their--mentions-*variables. - ~6 KB minified + gzipped, zero runtime dependencies.
Server-side usage
The parser and markup utilities run anywhere — Node, edge runtimes, Bun, Deno. No DOM, no framework.
import { extractMentions, markupToPlainText } from "@skyastrall/mentions-core";
const markup = "@[Alice](1) said #[urgent](t1)";
const triggers = [
{ char: "@", data: [] },
{ char: "#", data: [] },
];
const mentions = extractMentions(markup, triggers);
// → [{ id: "1", label: "Alice" }, { id: "t1", label: "urgent" }]
const plain = markupToPlainText(markup, triggers);
// → "@Alice said #urgent"Building a custom adapter
Every shipped adapter (react, vue, svelte, angular) follows the same recipe against this package:
new MentionController({ triggers, initialMarkup, callbacks })- Subscribe with your framework's reactivity primitive (
useSyncExternalStore,shallowRef,$state,signal) - Render
connect(state, id)to spread ARIA props onto the editor / listbox / options - Wire
input,keydown,compositionstart,compositionend,blur,paste,dropto controller methods - Call
performMentionInsertion(...)thencontroller.handleInsertComplete(...)on select controller.destroy()on unmount
packages/react/src/use-mentions.ts is the shortest reference. Vue, Svelte, and Angular follow the same shape.
License
MIT — Built by SkyAstrall
