klyv-ui

Inline Completion

Component

About

A text input or textarea that suggests the rest of a sentence as faint ghost text after the caret, which the user can accept with Tab or Ctrl/Cmd+Right Arrow, like inline code-completion in an editor.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Inline Completion to your project and fits it to your theme.

Code

How Inline Completion is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

import { InlineCompletion } from "@/components/ui/inline-completion";
import { useState } from "react";

// Synthetic, local completions resolved after a simulated 400ms delay — no
// network/AI call. The AbortSignal cancels the pending timer when the text
// changes before it settles, matching the component's own contract.
const COMMIT_SUGGESTIONS: Array<[string, string]> = [
  ["fix(", "auth): guard against an expired refresh token"],
  ["feat(", "editor): add inline completion for commit messages"],
  ["chore(", "deps): bump the build toolchain"],
];

function matchCommit(text: string): string | null {
  const hit = COMMIT_SUGGESTIONS.find(([prefix]) => text === prefix);
  return hit ? hit[1] : null;
}

export default function InlineCompletionAsyncDemo() {
  const [value, setValue] = useState("");
  const [stats, setStats] = useState({ asked: 0, cancelled: 0 });

  const getSuggestion = (text: string, signal: AbortSignal) =>
    new Promise<string | null>((resolve) => {
      setStats((s) => ({ ...s, asked: s.asked + 1 }));
      const timer = setTimeout(() => resolve(matchCommit(text)), 400);
      signal.addEventListener("abort", () => {
        clearTimeout(timer);
        setStats((s) => ({ ...s, cancelled: s.cancelled + 1 }));
        resolve(null);
      });
    });

  return (
    <div className="flex w-full max-w-sm flex-col gap-2 bg-background p-6">
      <label
        htmlFor="commit-field"
        className="text-sm font-medium text-foreground"
      >
        Commit message
      </label>
      <InlineCompletion
        id="commit-field"
        label="Commit message"
        placeholder="fix(..."
        multiline
        rows={3}
        debounce={400}
        value={value}
        onValueChange={setValue}
        getSuggestion={getSuggestion}
      />
      <p className="text-xs text-muted-foreground">
        Try “fix(” or “feat(”. Suggestions arrive after 400 ms — keep typing and
        the pending request is cancelled.
      </p>
      <p className="text-xs text-muted-foreground">
        requests: {stats.asked} asked, {stats.cancelled} cancelled
      </p>
    </div>
  );
}

More by klyv-ui

View all
  • Gantt Chart

    Gantt Chart

    106
  • Shader Canvas

    Shader Canvas

    69
  • Klyv Animated Beam

    Klyv Animated Beam

    68
  • Pareto Chart

    Pareto Chart

    60
  • Fluid Canvas

    Fluid Canvas

    44
  • Sunburst Chart

    Sunburst Chart

    35
  • Time Slot Picker

    Time Slot Picker

    32
  • Morphing Text

    Morphing Text

    32