Inline Code Bridge

A tone-tinted inline code reference. Drop it into prose to anchor a snippet to a side panel, figure, or any element with a stable id. Clicking smooth-scrolls the target into view and points focus there for keyboard users.

Preview

The cache check at line 3 guards against double work. Click the pill to scroll the panel below into view.

Customize
Tone
accent
Features

Installation

npx shadcn@latest add https://craftbits.dev/r/inline-code-bridge.json

Usage

import { InlineCodeBridge } from "@craft-bits/core";
 
<p>
  The cache check at{" "}
  <InlineCodeBridge targetId="panel-cache" tone="good">
    line 3
  </InlineCodeBridge>{" "}
  guards against double work.
</p>
 
<aside id="panel-cache">Cache hit shortcut...</aside>

Pair with a tone for code-review intent:

<InlineCodeBridge targetId="panel-off-by-one" tone="bad">
  i &lt;= nums.length
</InlineCodeBridge>

Understanding the component

  1. Anchor first. The pill renders as a real <a href="#targetId"> so the link works without JavaScript, surfaces in browser link lists, and ships keyboard activation for free. Click is intercepted only to add smooth-scroll polish and focus management.
  2. Focus follows the jump. On click the target element receives focus — if it lacks tabindex, the component adds tabindex="-1" for the duration of the focus move, then strips it on blur. The next Tab keypress continues from the panel, matching the user's mental model.
  3. Tones map to tokens. Five tones — neutral / accent / good / bad / warn — map to --cb-fg-subtle, --cb-accent, --cb-success, --cb-error, --cb-warning. Override the --cb-* vars at the app root to reskin without touching the component.
  4. Cross-page bridges. Pass an explicit href to point at another route — same-page anchors get the smooth-scroll treatment, cross-page hrefs fall through to native navigation.
  5. A11y wiring. The pill carries aria-controls and aria-describedby pointed at the target id, so screen readers announce the bridge as a controller for the named region. The leading dot is aria-hidden.

Variants

  • Plain link — pass showDot={false} for a quieter inline treatment.
  • Cross-pagehref="/docs/foo#bar" to bridge to a different route.
  • Tone mix — combine good / bad / warn pills inline to read like a code-review margin.

Props

PropTypeDefaultDescription
targetIdstringrequiredId of the element this pill bridges to.
childrenReactNoderequiredInline content — typically a code snippet or short label.
tone'neutral' | 'accent' | 'good' | 'bad' | 'warn''accent'Semantic tone.
hrefstring#targetIdOverride the anchor href — set for cross-page bridges.
showDotbooleantrueRender the leading tone dot.
smoothScrollbooleantrueSmooth-scroll the target into view on click.
classNamestringMerged onto the root <a>.

Accessibility

  • Renders as a native <a> — works with keyboard navigation out of the box.
  • Carries aria-controls and aria-describedby set to targetId, surfacing the bridge as a controller for the named region.
  • Focus follows the jump — the target element is focused after smooth-scroll so the next Tab continues from there.
  • The tone dot is decorative (aria-hidden="true"); the children carry the announced label.
  • Focus-visible outline keyed to the tone color (--cb-accent / --cb-success / etc.) for visible keyboard focus.

Credits

  • Extracted from: algoflashcards (src/lessons/primitives/chrome/InlineCodeBridge.tsx). The source was a multi-step interactive code-bridge with chunks, prediction gates, and per-line reveals. The library version distills the inline-anchor surface into a reusable primitive — strip the chunk reducer, generalise the tone palette to the cb token system, and expose the deep-link as the public API.