Skip to content
awesome-tour

Type to search every page.

    DSurveyContents

    Survey

    An NPS / CSAT / CES survey card with conditional follow-ups.

    Surveys
    Loopworknight-drive.lwp
    001.1.1

    Mixer

    4 channels
    Drums
    80%
    Bass
    35%
    Keys
    60%
    Vox
    50%

    Bounce

    8 bars · 0:21
    Renders the loop to one file.
    Hit Bounce. Rate it 3 or lower to get the follow-up question.
    SurveyDemo.tsx
    import { TourProvider, useTourManager } from '@awesome-tour/react';
    import { createSurvey, type SurveyAnswers } from '@awesome-tour/react/surfaces';
    import { LoopworkApp, useCreateLoopwork, useLoopwork } from '@loopwork';
    import { useEffect, useState } from 'react';
    import { SurveyCard } from '@/components/awesome-tour/survey';
    import { DemoFrame } from './ui';
    let report: (answers: SurveyAnswers) => void = () => {};
    const survey = createSurvey({
    id: 'docs-csat',
    questions: [
    { id: 'sound', type: 'csat', prompt: 'How did the bounce sound?' },
    {
    id: 'off',
    type: 'text',
    prompt: 'What sounded off?',
    placeholder: 'Too quiet, clipping, muddy low end…',
    optional: true,
    when: (answers) => Number(answers.sound) <= 3,
    },
    ],
    onSubmit: (answers) => report(answers),
    });
    /** Asks right after the moment it's about: the first finished bounce. */
    function AskAfterBounce() {
    const manager = useTourManager();
    const status = useLoopwork((s) => s.export.status);
    useEffect(() => {
    if (status === 'done') void manager.start(survey.id, { force: true });
    }, [status, manager]);
    return null;
    }
    export function SurveyDemo() {
    const [answers, setAnswers] = useState<SurveyAnswers | null>(null);
    report = setAnswers;
    const app = useCreateLoopwork({ route: 'mix' });
    return (
    <TourProvider tours={[survey]}>
    <DemoFrame
    title="Surveys"
    footer={
    <span aria-live="polite">
    {answers
    ? `onSubmit(${JSON.stringify(answers)})`
    : 'Hit Bounce. Rate it 3 or lower to get the follow-up question.'}
    </span>
    }
    >
    <LoopworkApp scope="feedback" app={app} layout="routed">
    <AskAfterBounce />
    </LoopworkApp>
    </DemoFrame>
    <SurveyCard survey={survey} lowLabel="Rough" highLabel="Release-ready" />
    </TourProvider>
    );
    }

    Installation

    Terminal window
    pnpm dlx shadcn@latest add @awesome-tour/survey
    Terminal window
    npx shadcn@latest add @awesome-tour/survey

    Built on the Survey primitives from @awesome-tour/react/surfaces.

    Source

    components/awesome-tour/survey.tsx
    'use client';
    import { useTourMessages } from '@awesome-tour/react';
    import { Survey, type SurveyInstance } from '@awesome-tour/react/surfaces';
    import { ArrowLeft, ArrowRight, Check, X } from 'lucide-react';
    import { Button } from '@/components/ui/button';
    import { cn } from '@/lib/utils';
    export interface SurveyCardProps {
    survey: SurveyInstance;
    /** Label of the last question's button. Default: "Submit". */
    submitLabel?: string;
    /** Low / high ends shown under the scale (the question's own labels win). */
    lowLabel?: string;
    highLabel?: string;
    /** Go to the next question as soon as a score is picked. Default: false. */
    autoAdvance?: boolean;
    className?: string;
    }
    /**
    * A corner card for NPS / CSAT / CES surveys with optional follow-up text questions.
    * Ambient by default (`variant: 'slideout'`): it never blocks the page.
    */
    function SurveyCard({
    survey,
    submitLabel = 'Submit',
    lowLabel,
    highLabel,
    autoAdvance = false,
    className,
    }: SurveyCardProps) {
    const { dismiss } = useTourMessages();
    return (
    <Survey.Root survey={survey}>
    <Survey.Portal>
    <Survey.Content
    data-slot="survey-card"
    className={cn(
    'fixed right-4 bottom-4 z-50 flex w-[min(26rem,calc(100vw-2rem))] flex-col gap-4 rounded-xl border bg-popover p-5 text-sm text-popover-foreground shadow-xl',
    'data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:slide-in-from-bottom-4',
    'data-[state=closed]:animate-out data-[state=closed]:fade-out-0',
    className,
    )}
    >
    <div className="flex items-start gap-3">
    <Survey.Prompt className="flex-1 text-base leading-snug font-semibold" />
    <Survey.Dismiss asChild>
    <Button
    variant="ghost"
    size="icon-sm"
    aria-label={dismiss}
    className="-mt-1 -mr-2 text-muted-foreground"
    >
    <X />
    </Button>
    </Survey.Dismiss>
    </div>
    <Survey.When type={['nps', 'csat', 'ces']}>
    <div className="flex flex-col gap-2">
    <Survey.Scale
    autoAdvance={autoAdvance}
    className="flex gap-1 [&>button]:h-9 [&>button]:min-w-0 [&>button]:flex-1 [&>button]:cursor-pointer [&>button]:rounded-md [&>button]:border [&>button]:bg-background [&>button]:text-sm [&>button]:font-medium [&>button]:tabular-nums [&>button]:transition-colors [&>button:hover]:bg-accent [&>button:focus-visible]:ring-[3px] [&>button:focus-visible]:ring-ring/50 [&>button:focus-visible]:outline-none [&>button[data-state=checked]]:border-primary [&>button[data-state=checked]]:bg-primary [&>button[data-state=checked]]:text-primary-foreground"
    />
    {(lowLabel || highLabel) && (
    <div className="flex justify-between text-xs text-muted-foreground">
    <span>{lowLabel}</span>
    <span>{highLabel}</span>
    </div>
    )}
    </div>
    </Survey.When>
    <Survey.When type="text">
    <Survey.Comment
    rows={3}
    className="min-h-20 w-full resize-y rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-xs outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50"
    />
    </Survey.When>
    <div className="flex items-center justify-between gap-2">
    <Survey.Progress className="text-xs text-muted-foreground tabular-nums">
    {({ current, total }) => (total > 1 ? `${current} of ${total}` : null)}
    </Survey.Progress>
    <div className="flex gap-1">
    <Survey.Back asChild>
    <Button variant="ghost" size="sm" className="data-disabled:hidden">
    <ArrowLeft />
    Back
    </Button>
    </Survey.Back>
    <SurveyNext submitLabel={submitLabel} />
    </div>
    </div>
    </Survey.Content>
    </Survey.Portal>
    </Survey.Root>
    );
    }
    function SurveyNext({ submitLabel }: { submitLabel: string }) {
    return (
    <Survey.Next asChild>
    <Button size="sm" className="group/next">
    <span className="group-data-last/next:hidden">Next</span>
    <ArrowRight className="group-data-last/next:hidden" />
    <span className="hidden group-data-last/next:inline">{submitLabel}</span>
    <Check className="hidden group-data-last/next:block" />
    </Button>
    </Survey.Next>
    );
    }
    export { SurveyCard };