Skip to content
Shaken UI
Sign in

Suggestion

Save

Short prompt suggestions as hairline pills that send in one click, in a sideways scrolling row with edge fades or a wrapping layout.

Category
AI elements
Version
0.1.0
Frameworks
Next.js, Vite + React
Styling
Tailwind CSS v4

Preview

Suggestion preview

One line, scrolls sideways

Wrapping, with icons

Pick a suggestion to send it.

Installation

npx @shakenui/cli add suggestion

Review the plan first. A dry run lists every file and package without changing anything:

npx @shakenui/cli add suggestion --dry-run

Usage

suggestion-example.tsx
"use client";

import { useState } from "react";
import { Suggestion, Suggestions } from "@/components/shaken/agent/suggestion";

const prompts = [
  "Summarize this pull request",
  "Find flaky tests",
  "Explain the failing build",
  "Draft release notes",
  "Which files changed most this week?",
];

export default function SuggestionExample() {
  const [sent, setSent] = useState<string | null>(null);
  return (
    <div className="grid w-full max-w-md gap-3">
      <Suggestions>
        {prompts.map((prompt) => (
          <Suggestion key={prompt} suggestion={prompt} onSelect={setSent} />
        ))}
      </Suggestions>
      <p aria-live="polite" className="text-xs text-zinc-500">
        {sent ? `Sent: ${sent}` : "Pick a suggestion to send it."}
      </p>
    </div>
  );
}

API

PropTypeDefaultDescription
Suggestions variant"scroll" | "wrap""scroll"scroll keeps one line that scrolls sideways and fades the edges with more pills behind them; wrap flows onto more lines.
Suggestions aria-labelstring"Suggestions"Names the group.
Suggestion suggestionstring—The prompt it sends, and the visible text unless children are passed.
Suggestion onSelect(suggestion) => void—Called with the prompt on press.
Suggestion iconReactNode—A 16px icon before the text.

Accessibility

  • Pills are native buttons in a labeled group, reachable with Tab; the scrolling row scrolls focused pills into view.
  • The scroller keeps padding inside its edges so focus rings are not clipped. The scrollbar is hidden, but touch, trackpad, and keyboard scrolling work.
  • With reduced motion the press scale and the entry fade are off.

Files

Installed into src/components/shaken, or components/shaken when the project has no src directory.

  • src/components/shaken/agent/suggestion.tsxsuggestion
  • npm packages: noneReact and Tailwind CSS v4 required

Source

The exact source the CLI installs, from /r/items/react/suggestion/0.1.0.json.

agent/suggestion.tsxShow
agent/suggestion.tsx
"use client";

import { type ComponentProps, type CSSProperties, type ReactNode, useEffect, useRef, useState } from "react";

function cx(...classes: Array<string | false | null | undefined>) {
  return classes.filter(Boolean).join(" ");
}

export interface SuggestionsProps extends Omit<ComponentProps<"div">, "role"> {
  /**
   * scroll keeps the pills on one line that scrolls sideways, with soft fades on the edges that
   * have more pills behind them. wrap lets them flow onto as many lines as they need.
   * @default "scroll"
   */
  variant?: "scroll" | "wrap";
  /** Names the group for screen readers. @default "Suggestions" */
  "aria-label"?: string;
}

/** A row of prompt suggestions. Put `Suggestion` pills inside it. */
export function Suggestions({ variant = "scroll", "aria-label": label = "Suggestions", className, style, children, ...props }: SuggestionsProps) {
  const ref = useRef<HTMLDivElement>(null);
  const [edges, setEdges] = useState({ start: false, end: false });

  useEffect(() => {
    const node = ref.current;
    if (!node || variant !== "scroll") return;
    const update = () => {
      const max = node.scrollWidth - node.clientWidth;
      // scrollLeft is negative in right-to-left layouts.
      const left = Math.abs(node.scrollLeft);
      setEdges({ start: left > 1, end: left < max - 1 });
    };
    update();
    node.addEventListener("scroll", update, { passive: true });
    const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update);
    observer?.observe(node);
    for (const child of Array.from(node.children)) observer?.observe(child);
    return () => {
      node.removeEventListener("scroll", update);
      observer?.disconnect();
    };
  }, [variant, children]);

  const fade = {
    "--fade-start": edges.start ? "2rem" : "0px",
    "--fade-end": edges.end ? "2rem" : "0px",
  } as CSSProperties;

  return (
    <div
      ref={ref}
      role="group"
      aria-label={label}
      data-variant={variant}
      style={variant === "scroll" ? { ...fade, ...style } : style}
      className={cx(
        "flex gap-2",
        variant === "scroll"
          ? cx(
              // Padding leaves room for focus rings inside the scroller; the negative margin keeps alignment.
              "-m-1 overflow-x-auto overscroll-x-contain p-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
              "[mask-image:linear-gradient(to_right,transparent,black_var(--fade-start),black_calc(100%_-_var(--fade-end)),transparent)]",
            )
          : "flex-wrap",
        className,
      )}
      {...props}
    >
      {children}
    </div>
  );
}

export interface SuggestionProps extends Omit<ComponentProps<"button">, "onSelect" | "value"> {
  /** The prompt this pill sends. Also the visible text unless you pass children. */
  suggestion: string;
  /** Called with the prompt when the pill is pressed. Send it straight away; one click is the point. */
  onSelect?: (suggestion: string) => void;
  /** A 16px icon before the text. */
  icon?: ReactNode;
}

/** One prompt suggestion: a hairline pill that sends its prompt in one click. */
export function Suggestion({ suggestion, onSelect, icon, className, children, onClick, type = "button", ...props }: SuggestionProps) {
  return (
    <button
      type={type}
      onClick={(event) => {
        onClick?.(event);
        if (!event.defaultPrevented) onSelect?.(suggestion);
      }}
      className={cx(
        "inline-flex h-8 shrink-0 cursor-pointer items-center gap-1.5 rounded-full border px-3.5 text-sm whitespace-nowrap select-none",
        "border-zinc-950/10 bg-white text-zinc-700 shadow-[0_1px_2px_rgb(0_0_0/0.04)]",
        "dark:border-white/10 dark:bg-black dark:text-zinc-300 dark:shadow-none",
        "transition-[background-color,border-color,color,scale,opacity,translate] duration-150 ease-out active:duration-100 motion-safe:active:scale-[0.97] motion-reduce:transition-none",
        "hover:border-zinc-950/20 hover:bg-zinc-950/[0.03] hover:text-zinc-950 dark:hover:border-white/20 dark:hover:bg-white/[0.06] dark:hover:text-white",
        "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-500",
        "disabled:pointer-events-none disabled:opacity-45",
        // A new set of suggestions settles in from just below, once.
        "starting:translate-y-1 starting:opacity-0",
        "[&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:text-zinc-400 dark:[&_svg]:text-zinc-500",
        icon ? "pl-3" : "",
        className,
      )}
      {...props}
    >
      {icon}
      {children ?? suggestion}
    </button>
  );
}