Open History to see the last commit
1import type { LinkOptions } from "@tanstack/react-router";
2import { Check, Play } from "lucide-react";
3import { createContext, useContext } from "react";
4import type { ReactNode } from "react";
5
6import { useIsSectionDone } from "./guide-store";
7
8/**
9 * Opens the app on a section's screen. Provided where the app is out of view
10 * (the small-screen layout, where it lives in a sheet); without a provider the
11 * guide renders no "Try it" buttons, because the app is already on screen.
12 */
13export type TryItHandler = (sectionId: string, target: LinkOptions) => void;
14
15const TryItContext = createContext<TryItHandler | null>(null);
16
17export function TryItProvider({
18 onTryIt,
19 children,
20}: {
21 onTryIt: TryItHandler;
22 children: ReactNode;
23}) {
24 return <TryItContext value={onTryIt}>{children}</TryItContext>;
25}
26
27export function TryItButton({ sectionId, target }: { sectionId: string; target: LinkOptions }) {
28 const onTryIt = useContext(TryItContext);
29 const done = useIsSectionDone(sectionId);
30 if (!onTryIt) return null;
31
32 return (
33 <div className="mt-5 flex items-center gap-3">
34 <button
35 type="button"
36 onClick={(event) => {
37 // Safari doesn't focus buttons on tap; focus it so closing the app returns here.
38 event.currentTarget.focus({ preventScroll: true });
39 onTryIt(sectionId, target);
40 }}
41 className="inline-flex h-10 items-center gap-2 rounded-full bg-notes-accent pr-4.5 pl-4 text-[15px] font-semibold text-black shadow-sm transition-transform outline-none focus-visible:ring-3 focus-visible:ring-notes-accent/40 active:scale-95"
42 >
43 <Play className="size-4 fill-current" aria-hidden />
44 Try it
45 </button>
46 {done && (
47 <span className="inline-flex items-center gap-1 text-sm font-medium text-emerald-700 dark:text-emerald-400">
48 <Check className="size-4" aria-hidden />
49 You did it
50 </span>
51 )}
52 </div>
53 );
54}