Cancel subscription

Cancelling a plan on a raised page, in one narrow column: what happens and what is lost, an optional reason, one alternative that fits it, and the only red button; then the result in place, with the way to undo it.

Open full screen

Inset · Sidebar. <AppShell direction="inset">, the default: the navigation on the grey app frame, the page a raised white card inside it, scrolling within.

npx wideset add block-cancel-subscription

Import the default export from @/blocks/wideset/cancel-subscription. The files below are relative to your project's source directory.

Installed with this block
  • app-shell
  • avatar
  • block-styles
  • breadcrumbs
  • button
  • divider
  • dropdown-menu
  • form-field
  • icon-button
  • list-item
  • radio-button
  • sidebar
  • text-input
  • textarea
  • tile
  • toast
  • utils
blocks/wideset/cancel-subscription/index.tsx
'use client';

import '@/styles/wideset-blocks.css';

import { RiSideBarLine } from '@remixicon/react';
import { AppShell } from '@/components/ui/wideset/app-shell/AppShell';
import { Breadcrumbs } from '@/components/ui/wideset/breadcrumbs/Breadcrumbs';
import { IconButton } from '@/components/ui/wideset/button/IconButton';
import { ToastProvider, useToast } from '@/components/ui/wideset/toast/Toast.context';
import { useEffect, useId, useRef, useState } from 'react';

import { AlternativeOffer } from './alternative-offer';
import { ConfirmCancel } from './confirm-cancel';
import {
  alternatives,
  monthlyPrice,
  type OutcomeId,
  outcomes,
  type ReasonId,
  ROUTES,
  subscription,
} from './data';
import { formatUsd } from './format';
import { LeavingReason } from './leaving-reason';
import { OutcomeSummary } from './outcome-summary';
import { CancelSubscriptionSidebar } from './sidebar';
import { WhatHappens } from './what-happens';

/**
 * The page: four steps in one narrow column, read top to bottom, then the result in their place.
 * It holds the reason and the note, so undoing a cancellation brings the steps back as they were
 * left.
 */
function CancelSubscriptionPage() {
  const { showToast } = useToast();
  const [reason, setReason] = useState<ReasonId>();
  const [note, setNote] = useState('');
  const [saving, setSaving] = useState<OutcomeId | null>(null);
  const [outcomeId, setOutcomeId] = useState<OutcomeId | null>(null);
  const outcome = outcomeId && outcomes[outcomeId];

  // The result replaces the steps in place, so focus moves to the title that names it.
  const title = useRef<HTMLHeadingElement>(null);
  const shown = useRef(outcomeId);
  useEffect(() => {
    if (shown.current !== outcomeId) title.current?.focus();
    shown.current = outcomeId;
  }, [outcomeId]);

  // Cleared on unmount, so the stand-in request never lands on a closed screen.
  const timer = useRef<number>(undefined);
  useEffect(() => () => window.clearTimeout(timer.current), []);

  // A timeout stands in for the request; a real screen awaits it and, when it fails, stays on the
  // steps and says so in an InlineAlert above the buttons.
  const decide = (id: OutcomeId) => {
    setSaving(id);
    timer.current = window.setTimeout(() => {
      setSaving(null);
      setOutcomeId(id);
    }, 800);
  };

  const header = outcome ?? {
    title: `Cancel the ${subscription.plan} plan`,
    line: `${formatUsd(monthlyPrice)} a month for ${subscription.seats} seats. Nothing changes until you confirm below.`,
  };

  return (
    // The page is read, not scanned: one column of 40rem, centred on the card.
    <div className="px-ws-14 gap-ws-14 pt-ws-14 pb-ws-16 mx-auto flex w-full max-w-[44rem] flex-col">
      <header className="gap-ws-2 flex flex-col">
        <h1
          ref={title}
          tabIndex={-1}
          className="typography-title-md text-ws-static-strong outline-none"
        >
          {header.title}
        </h1>
        <p className="typography-body-md text-ws-static-subtle tabular-nums">{header.line}</p>
      </header>

      {outcome ? (
        <OutcomeSummary
          outcome={outcome}
          onUndo={() => {
            setOutcomeId(null);
            showToast({ content: outcome.undone });
          }}
        />
      ) : (
        <>
          <WhatHappens />
          {/* The offer answers the reason, so the two are one step. */}
          <div>
            <LeavingReason
              reason={reason}
              onReasonChange={setReason}
              note={note}
              onNoteChange={setNote}
              disabled={saving !== null}
            />
            <AlternativeOffer
              alternative={reason && alternatives[reason]}
              busy={saving !== null}
              onAccept={(alternative) => decide(alternative.outcome)}
            />
          </div>
          <ConfirmCancel
            cancelling={saving === 'cancelled'}
            busy={saving !== null}
            onCancel={() => decide('cancelled')}
          />
        </>
      )}
    </div>
  );
}

/**
 * Cancel subscription (Inset direction): cancelling the Scale plan as one focused page on the
 * raised card beside the workspace sidebar. It says what happens and what is lost, asks why
 * without insisting, offers one alternative that fits the reason, and ends in the only red thing
 * on the page; then the page becomes the result, with the way to undo it.
 */
export default function CancelSubscription() {
  const sidebarId = useId();
  const [collapsed, setCollapsed] = useState(false);

  return (
    <ToastProvider>
      <AppShell className="ws-block-utility" direction="inset">
        <CancelSubscriptionSidebar
          id={sidebarId}
          collapsed={collapsed}
          onCollapsedChange={setCollapsed}
        />
        <AppShell.Main>
          <div className="h-ws-18 border-ws-static-base flex shrink-0 items-center border-b">
            {/* The bar belongs to the surround: it runs edge to edge. The sidebar's toggle is
                pulled left so its glyph, not its box, sits 20px from the edge; the crumbs follow
                it. */}
            <div className="gap-ws-5 px-ws-11 flex w-full items-center">
              <IconButton
                icon={RiSideBarLine}
                variant="secondary"
                preset="ghost"
                size="sm"
                iconClassName="text-ws-static-subtle size-ws-9"
                aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
                aria-expanded={!collapsed}
                aria-controls={sidebarId}
                onClick={() => setCollapsed((collapsed) => !collapsed)}
                className="-ml-ws-5"
              />
              <Breadcrumbs separator="arrow">
                <Breadcrumbs.Item asChild>
                  <a href="#settings">Settings</a>
                </Breadcrumbs.Item>
                <Breadcrumbs.Item asChild>
                  <a href={ROUTES.billing}>Billing</a>
                </Breadcrumbs.Item>
                <Breadcrumbs.Item>Cancel plan</Breadcrumbs.Item>
              </Breadcrumbs>
            </div>
          </div>

          {/* The page scrolls inside the card, so the card's corners stay put. */}
          <div className="ws-scrollbar min-h-0 flex-1 overflow-y-auto">
            <CancelSubscriptionPage />
          </div>
        </AppShell.Main>
      </AppShell>
    </ToastProvider>
  );
}