phation 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/LICENSE.md +15 -0
  2. package/README.md +142 -2
  3. package/animate/core/feels.js +11 -0
  4. package/animate/core/recipes/index.js +84 -0
  5. package/animate/core/recipes/resolve.js +96 -0
  6. package/animate/detect.mjs +201 -0
  7. package/animate/index.mjs +266 -0
  8. package/animate/testset/eval.mjs +24 -0
  9. package/animate/testset/expected.json +23 -0
  10. package/animate/testset/holdout/commerce-delete-item-button.tsx +38 -0
  11. package/animate/testset/holdout/commerce-gallery.tsx +96 -0
  12. package/animate/testset/holdout/commerce-tile.tsx +50 -0
  13. package/animate/testset/holdout/shadcn-button-group-demo.tsx +114 -0
  14. package/animate/testset/holdout/shadcn-card-demo.tsx +63 -0
  15. package/animate/testset/holdout/shadcn-command-dialog.tsx +87 -0
  16. package/animate/testset/holdout/shadcn-drawer-dialog.tsx +90 -0
  17. package/animate/testset/holdout/shadcn-field-switch.tsx +24 -0
  18. package/animate/testset/holdout/shadcn-hover-card-demo.tsx +40 -0
  19. package/animate/testset/holdout/shadcn-mode-toggle.tsx +40 -0
  20. package/animate/testset/holdout/shadcn-toggle-group-demo.tsx +22 -0
  21. package/animate/testset/holdout.json +14 -0
  22. package/animate/testset/holdout2/chatbot-artifact-close-button.tsx +30 -0
  23. package/animate/testset/holdout2/chatbot-message-actions.tsx +211 -0
  24. package/animate/testset/holdout2/chatbot-sidebar-history-item.tsx +132 -0
  25. package/animate/testset/holdout2/chatbot-sidebar-toggle.tsx +35 -0
  26. package/animate/testset/holdout2/chatbot-suggested-actions.tsx +82 -0
  27. package/animate/testset/holdout2/chatbot-weather.tsx +434 -0
  28. package/animate/testset/holdout2/site-code-tabs.tsx +26 -0
  29. package/animate/testset/holdout2/site-copy-button.tsx +121 -0
  30. package/animate/testset/holdout2/site-github-link.tsx +38 -0
  31. package/animate/testset/holdout2/site-mobile-nav.tsx +220 -0
  32. package/animate/testset/holdout2/site-mode-switcher.tsx +97 -0
  33. package/animate/testset/holdout2/site-page-nav.tsx +15 -0
  34. package/animate/testset/holdout2.json +15 -0
  35. package/animate/testset/screens/commerce-cart-add-to-cart.tsx +94 -0
  36. package/animate/testset/screens/commerce-cart-modal.tsx +256 -0
  37. package/animate/testset/screens/commerce-navbar-mobile-menu.tsx +104 -0
  38. package/animate/testset/screens/commerce-product-variant-selector.tsx +106 -0
  39. package/animate/testset/screens/shadcn-accordion-demo.tsx +61 -0
  40. package/animate/testset/screens/shadcn-alert-dialog-demo.tsx +35 -0
  41. package/animate/testset/screens/shadcn-card-with-form.tsx +59 -0
  42. package/animate/testset/screens/shadcn-collapsible-demo.tsx +46 -0
  43. package/animate/testset/screens/shadcn-dialog-demo.tsx +50 -0
  44. package/animate/testset/screens/shadcn-drawer-demo.tsx +136 -0
  45. package/animate/testset/screens/shadcn-login-form.tsx +71 -0
  46. package/animate/testset/screens/shadcn-nav-main.tsx +73 -0
  47. package/animate/testset/screens/shadcn-section-cards.tsx +102 -0
  48. package/animate/testset/screens/shadcn-sheet-demo.tsx +47 -0
  49. package/animate/testset/screens/shadcn-switch-demo.tsx +11 -0
  50. package/animate/testset/screens/shadcn-tabs-demo.tsx +80 -0
  51. package/animate/testset/screens/taxonomy-main-nav.tsx +61 -0
  52. package/animate/testset/screens/taxonomy-post-item.tsx +42 -0
  53. package/animate/testset/screens/taxonomy-pricing.tsx +69 -0
  54. package/animate/testset/screens/taxonomy-user-account-nav.tsx +65 -0
  55. package/audit.mjs +456 -0
  56. package/bin/cli.mjs +113 -0
  57. package/bin/server.mjs +147 -0
  58. package/data/entries.json +864 -0
  59. package/data/standard/CORE.md +21 -0
  60. package/data/standard/STANDARD.md +185 -0
  61. package/data/standard/claude/CLAUDE.block.md +5 -0
  62. package/data/standard/claude/SKILL.md +26 -0
  63. package/data/standard/codex/AGENTS.block.md +25 -0
  64. package/data/standard/cursor/phation.mdc +27 -0
  65. package/data/standard/meta.json +4 -0
  66. package/data/standard/vscode/phation.instructions.md +25 -0
  67. package/data/standard/windsurf/phation.md +26 -0
  68. package/lib.mjs +214 -0
  69. package/package.json +41 -4
  70. package/share.mjs +72 -0
@@ -0,0 +1,26 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+
5
+ import { useConfig } from "@/hooks/use-config"
6
+ import { Tabs } from "@/registry/new-york-v4/ui/tabs"
7
+
8
+ export function CodeTabs({ children }: React.ComponentProps<typeof Tabs>) {
9
+ const [config, setConfig] = useConfig()
10
+
11
+ const installationType = React.useMemo(() => {
12
+ return config.installationType || "cli"
13
+ }, [config])
14
+
15
+ return (
16
+ <Tabs
17
+ value={installationType}
18
+ onValueChange={(value) =>
19
+ setConfig({ ...config, installationType: value as "cli" | "manual" })
20
+ }
21
+ className="relative mt-6 w-full *:data-[slot=tabs-list]:gap-6"
22
+ >
23
+ {children}
24
+ </Tabs>
25
+ )
26
+ }
@@ -0,0 +1,121 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { IconCheck, IconCopy } from "@tabler/icons-react"
5
+ import { cn } from "cn"
6
+
7
+ import { trackEvent, type Event } from "@/lib/events"
8
+ import { Button } from "@/registry/new-york-v4/ui/button"
9
+
10
+ function legacyCopyToClipboard(value: string) {
11
+ const textArea = document.createElement("textarea")
12
+ textArea.value = value
13
+ textArea.setAttribute("readonly", "")
14
+ textArea.style.position = "fixed"
15
+ textArea.style.opacity = "0"
16
+ textArea.style.pointerEvents = "none"
17
+
18
+ document.body.appendChild(textArea)
19
+ textArea.focus()
20
+ textArea.select()
21
+ textArea.setSelectionRange(0, value.length)
22
+
23
+ let hasCopied = false
24
+ try {
25
+ hasCopied = document.execCommand("copy")
26
+ } catch {
27
+ hasCopied = false
28
+ }
29
+
30
+ document.body.removeChild(textArea)
31
+ return hasCopied
32
+ }
33
+
34
+ export async function copyToClipboardWithMeta(value: string, event?: Event) {
35
+ if (typeof window === "undefined") {
36
+ return false
37
+ }
38
+
39
+ if (!value) {
40
+ return false
41
+ }
42
+
43
+ let hasCopied = false
44
+
45
+ if (navigator.clipboard?.writeText) {
46
+ try {
47
+ await navigator.clipboard.writeText(value)
48
+ hasCopied = true
49
+ } catch {
50
+ hasCopied = legacyCopyToClipboard(value)
51
+ }
52
+ } else {
53
+ hasCopied = legacyCopyToClipboard(value)
54
+ }
55
+
56
+ if (!hasCopied) {
57
+ return false
58
+ }
59
+
60
+ if (event) {
61
+ trackEvent(event)
62
+ }
63
+
64
+ return true
65
+ }
66
+
67
+ export function CopyButton({
68
+ value,
69
+ className,
70
+ variant = "ghost",
71
+ event,
72
+ ...props
73
+ }: React.ComponentProps<typeof Button> & {
74
+ value: string
75
+ src?: string
76
+ event?: Event["name"]
77
+ tooltip?: string
78
+ }) {
79
+ const [hasCopied, setHasCopied] = React.useState(false)
80
+
81
+ React.useEffect(() => {
82
+ if (hasCopied) {
83
+ const timer = setTimeout(() => setHasCopied(false), 2000)
84
+ return () => clearTimeout(timer)
85
+ }
86
+ }, [hasCopied])
87
+
88
+ return (
89
+ <Button
90
+ data-slot="copy-button"
91
+ data-copied={hasCopied}
92
+ size="icon"
93
+ variant={variant}
94
+ className={cn(
95
+ "absolute top-3 right-2 z-10 size-7 bg-code hover:opacity-100 focus-visible:opacity-100",
96
+ className
97
+ )}
98
+ onClick={async () => {
99
+ const hasCopied = await copyToClipboardWithMeta(
100
+ value,
101
+ event
102
+ ? {
103
+ name: event,
104
+ properties: {
105
+ code: value,
106
+ },
107
+ }
108
+ : undefined
109
+ )
110
+
111
+ if (hasCopied) {
112
+ setHasCopied(true)
113
+ }
114
+ }}
115
+ {...props}
116
+ >
117
+ <span className="sr-only">Copy</span>
118
+ {hasCopied ? <IconCheck /> : <IconCopy />}
119
+ </Button>
120
+ )
121
+ }
@@ -0,0 +1,38 @@
1
+ import * as React from "react"
2
+ import Link from "next/link"
3
+
4
+ import { siteConfig } from "@/lib/config"
5
+ import { Icons } from "@/components/icons"
6
+ import { Button } from "@/registry/new-york-v4/ui/button"
7
+ import { Skeleton } from "@/registry/new-york-v4/ui/skeleton"
8
+
9
+ export function GitHubLink() {
10
+ return (
11
+ <Button asChild size="sm" variant="ghost" className="h-8 shadow-none">
12
+ <Link href={siteConfig.links.github} target="_blank" rel="noreferrer">
13
+ <Icons.gitHub />
14
+ <React.Suspense fallback={<Skeleton className="h-4 w-[42px]" />}>
15
+ <StarsCount />
16
+ </React.Suspense>
17
+ </Link>
18
+ </Button>
19
+ )
20
+ }
21
+
22
+ export async function StarsCount() {
23
+ const data = await fetch("https://api.github.com/repos/shadcn-ui/ui", {
24
+ next: { revalidate: 86400 },
25
+ })
26
+ const json = await data.json()
27
+
28
+ const formattedCount =
29
+ json.stargazers_count >= 1000
30
+ ? `${Math.round(json.stargazers_count / 1000)}k`
31
+ : json.stargazers_count?.toLocaleString()
32
+
33
+ return (
34
+ <span className="w-fit text-xs text-muted-foreground tabular-nums">
35
+ {formattedCount}
36
+ </span>
37
+ )
38
+ }
@@ -0,0 +1,220 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import Link, { type LinkProps } from "next/link"
5
+ import { usePathname, useRouter } from "next/navigation"
6
+ import { cn } from "cn"
7
+
8
+ import { PAGES_NEW } from "@/lib/docs"
9
+ import { showMcpDocs } from "@/lib/flags"
10
+ import { getCurrentBase, getPagesFromFolder } from "@/lib/page-tree"
11
+ import { type source } from "@/lib/source"
12
+ import { Button } from "@/registry/new-york-v4/ui/button"
13
+ import {
14
+ Popover,
15
+ PopoverContent,
16
+ PopoverTrigger,
17
+ } from "@/registry/new-york-v4/ui/popover"
18
+
19
+ const TOP_LEVEL_SECTIONS = [
20
+ { name: "Introduction", href: "/docs" },
21
+ {
22
+ name: "Components",
23
+ href: "/docs/components",
24
+ },
25
+ {
26
+ name: "AI Elements",
27
+ href: "/docs/ai-elements",
28
+ },
29
+ {
30
+ name: "Installation",
31
+ href: "/docs/installation",
32
+ },
33
+ {
34
+ name: "Theming",
35
+ href: "/docs/theming",
36
+ },
37
+ {
38
+ name: "CLI",
39
+ href: "/docs/cli",
40
+ },
41
+ {
42
+ name: "RTL",
43
+ href: "/docs/rtl",
44
+ },
45
+ {
46
+ name: "Skills",
47
+ href: "/docs/skills",
48
+ },
49
+ {
50
+ name: "MCP Server",
51
+ href: "/docs/mcp",
52
+ },
53
+ {
54
+ name: "Registry",
55
+ href: "/docs/registry",
56
+ },
57
+ {
58
+ name: "Forms",
59
+ href: "/docs/forms",
60
+ },
61
+ {
62
+ name: "Changelog",
63
+ href: "/docs/changelog",
64
+ },
65
+ ]
66
+
67
+ export function MobileNav({
68
+ tree,
69
+ items,
70
+ className,
71
+ }: {
72
+ tree: typeof source.pageTree
73
+ items: { href: string; label: string }[]
74
+ className?: string
75
+ }) {
76
+ const [open, setOpen] = React.useState(false)
77
+ const pathname = usePathname()
78
+ const currentBase = getCurrentBase(pathname)
79
+
80
+ return (
81
+ <Popover open={open} onOpenChange={setOpen}>
82
+ <PopoverTrigger asChild>
83
+ <Button
84
+ variant="ghost"
85
+ className={cn(
86
+ "extend-touch-target h-8 touch-manipulation items-center justify-start gap-2.5 p-0! hover:bg-transparent focus-visible:bg-transparent focus-visible:ring-0 active:bg-transparent dark:hover:bg-transparent",
87
+ className
88
+ )}
89
+ >
90
+ <div className="relative flex h-8 w-4 items-center justify-center">
91
+ <div className="relative size-4">
92
+ <span
93
+ className={cn(
94
+ "absolute left-0 block h-0.5 w-4 bg-foreground transition-all duration-100",
95
+ open ? "top-[0.4rem] -rotate-45" : "top-1"
96
+ )}
97
+ />
98
+ <span
99
+ className={cn(
100
+ "absolute left-0 block h-0.5 w-4 bg-foreground transition-all duration-100",
101
+ open ? "top-[0.4rem] rotate-45" : "top-2.5"
102
+ )}
103
+ />
104
+ </div>
105
+ <span className="sr-only">Toggle Menu</span>
106
+ </div>
107
+ <span className="flex h-8 items-center text-lg leading-none font-medium">
108
+ Menu
109
+ </span>
110
+ </Button>
111
+ </PopoverTrigger>
112
+ <PopoverContent
113
+ className="no-scrollbar h-(--radix-popper-available-height) w-(--radix-popper-available-width) overflow-y-auto rounded-none border-none bg-background/90 p-0 shadow-none backdrop-blur duration-100 data-open:animate-none!"
114
+ align="start"
115
+ side="bottom"
116
+ alignOffset={-16}
117
+ sideOffset={14}
118
+ >
119
+ <div className="flex flex-col gap-12 overflow-auto px-6 py-6">
120
+ <div className="flex flex-col gap-4">
121
+ <div className="text-sm font-medium text-muted-foreground">
122
+ Menu
123
+ </div>
124
+ <div className="flex flex-col gap-3">
125
+ {items.map((item, index) => (
126
+ <MobileLink key={index} href={item.href} onOpenChange={setOpen}>
127
+ {item.label}
128
+ </MobileLink>
129
+ ))}
130
+ </div>
131
+ </div>
132
+ <div className="flex flex-col gap-4">
133
+ <div className="text-sm font-medium text-muted-foreground">
134
+ Sections
135
+ </div>
136
+ <div className="flex flex-col gap-3">
137
+ {TOP_LEVEL_SECTIONS.map(({ name, href }) => {
138
+ if (!showMcpDocs && href.includes("/mcp")) {
139
+ return null
140
+ }
141
+ return (
142
+ <MobileLink key={name} href={href} onOpenChange={setOpen}>
143
+ {name}
144
+ {PAGES_NEW.includes(href) && (
145
+ <span
146
+ className="flex size-2 rounded-full bg-blue-500"
147
+ title="New"
148
+ />
149
+ )}
150
+ </MobileLink>
151
+ )
152
+ })}
153
+ </div>
154
+ </div>
155
+ <div className="flex flex-col gap-8">
156
+ {tree?.children?.map((group, index) => {
157
+ if (group.type === "folder") {
158
+ const pages = getPagesFromFolder(group, currentBase)
159
+ return (
160
+ <div key={index} className="flex flex-col gap-4">
161
+ <div className="text-sm font-medium text-muted-foreground">
162
+ {group.name}
163
+ </div>
164
+ <div className="flex flex-col gap-3">
165
+ {pages.map((item) => {
166
+ if (!showMcpDocs && item.url.includes("/mcp")) {
167
+ return null
168
+ }
169
+ return (
170
+ <MobileLink
171
+ key={`${item.url}-${index}`}
172
+ href={item.url}
173
+ onOpenChange={setOpen}
174
+ className="flex items-center gap-2"
175
+ >
176
+ {item.name}{" "}
177
+ {PAGES_NEW.includes(item.url) && (
178
+ <span className="flex size-2 rounded-full bg-blue-500" />
179
+ )}
180
+ </MobileLink>
181
+ )
182
+ })}
183
+ </div>
184
+ </div>
185
+ )
186
+ }
187
+ })}
188
+ </div>
189
+ </div>
190
+ </PopoverContent>
191
+ </Popover>
192
+ )
193
+ }
194
+
195
+ function MobileLink({
196
+ href,
197
+ onOpenChange,
198
+ className,
199
+ children,
200
+ ...props
201
+ }: LinkProps & {
202
+ onOpenChange?: (open: boolean) => void
203
+ children: React.ReactNode
204
+ className?: string
205
+ }) {
206
+ const router = useRouter()
207
+ return (
208
+ <Link
209
+ href={href}
210
+ onClick={() => {
211
+ router.push(href.toString())
212
+ onOpenChange?.(false)
213
+ }}
214
+ className={cn("flex items-center gap-2 text-2xl font-medium", className)}
215
+ {...props}
216
+ >
217
+ {children}
218
+ </Link>
219
+ )
220
+ }
@@ -0,0 +1,97 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import Script from "next/script"
5
+ import { cn } from "cn"
6
+ import { useTheme } from "next-themes"
7
+
8
+ import { useMetaColor } from "@/hooks/use-meta-color"
9
+ import { Button } from "@/registry/new-york-v4/ui/button"
10
+
11
+ export const DARK_MODE_FORWARD_TYPE = "dark-mode-forward"
12
+
13
+ export function ModeSwitcher({
14
+ variant = "ghost",
15
+ className,
16
+ }: {
17
+ variant?: React.ComponentProps<typeof Button>["variant"]
18
+ className?: React.ComponentProps<typeof Button>["className"]
19
+ }) {
20
+ const { setTheme, resolvedTheme } = useTheme()
21
+ const { setMetaColor, metaColor } = useMetaColor()
22
+
23
+ React.useEffect(() => {
24
+ setMetaColor(metaColor)
25
+ }, [metaColor, setMetaColor])
26
+
27
+ const toggleTheme = React.useCallback(() => {
28
+ setTheme(resolvedTheme === "dark" ? "light" : "dark")
29
+ }, [resolvedTheme, setTheme])
30
+
31
+ return (
32
+ <Button
33
+ variant={variant}
34
+ size="icon"
35
+ className={cn("group/toggle extend-touch-target size-8", className)}
36
+ onClick={toggleTheme}
37
+ >
38
+ <svg
39
+ xmlns="http://www.w3.org/2000/svg"
40
+ width="24"
41
+ height="24"
42
+ viewBox="0 0 24 24"
43
+ fill="none"
44
+ stroke="currentColor"
45
+ strokeWidth="2"
46
+ strokeLinecap="round"
47
+ strokeLinejoin="round"
48
+ className="size-4.5"
49
+ >
50
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
51
+ <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
52
+ <path d="M12 3l0 18" />
53
+ <path d="M12 9l4.65 -4.65" />
54
+ <path d="M12 14.3l7.37 -7.37" />
55
+ <path d="M12 19.6l8.85 -8.85" />
56
+ </svg>
57
+ <span className="sr-only">Toggle theme</span>
58
+ </Button>
59
+ )
60
+ }
61
+
62
+ export function DarkModeScript() {
63
+ return (
64
+ // eslint-disable-next-line @next/next/no-before-interactive-script-outside-document
65
+ <Script
66
+ id="dark-mode-listener"
67
+ strategy="beforeInteractive"
68
+ dangerouslySetInnerHTML={{
69
+ __html: `
70
+ (function() {
71
+ // Forward D key
72
+ document.addEventListener('keydown', function(e) {
73
+ if ((e.key === 'd' || e.key === 'D') && !e.metaKey && !e.ctrlKey && !e.altKey) {
74
+ if (
75
+ (e.target instanceof HTMLElement && e.target.isContentEditable) ||
76
+ e.target instanceof HTMLInputElement ||
77
+ e.target instanceof HTMLTextAreaElement ||
78
+ e.target instanceof HTMLSelectElement
79
+ ) {
80
+ return;
81
+ }
82
+ e.preventDefault();
83
+ if (window.parent && window.parent !== window) {
84
+ window.parent.postMessage({
85
+ type: '${DARK_MODE_FORWARD_TYPE}',
86
+ key: e.key
87
+ }, '*');
88
+ }
89
+ }
90
+ });
91
+
92
+ })();
93
+ `,
94
+ }}
95
+ />
96
+ )
97
+ }
@@ -0,0 +1,15 @@
1
+ import { cn } from "cn"
2
+
3
+ export function PageNav({
4
+ children,
5
+ className,
6
+ ...props
7
+ }: React.ComponentProps<"div">) {
8
+ return (
9
+ <div className={cn("container-wrapper scroll-mt-24", className)} {...props}>
10
+ <div className="container flex items-center justify-between gap-4 py-4">
11
+ {children}
12
+ </div>
13
+ </div>
14
+ )
15
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "_about": "Holdout 2: 12 screens from vercel/ai-chatbot and the shadcn site, labelled BEFORE the detector ran on them and never tuned on (2026-11-18). This is the honest score.",
3
+ "chatbot-artifact-close-button.tsx": { "19": ["icon-button"] },
4
+ "chatbot-message-actions.tsx": { "145": ["icon-button"], "154": ["icon-button"], "168": ["icon-button"], "176": ["icon-button"], "186": ["icon-button"] },
5
+ "chatbot-sidebar-history-item.tsx": { "62": ["button"], "74": ["icon-button"] },
6
+ "chatbot-sidebar-toggle.tsx": { "20": ["icon-button"] },
7
+ "chatbot-suggested-actions.tsx": { "57": ["button"] },
8
+ "chatbot-weather.tsx": { "331": ["card"], "364": ["number"], "373": ["number"], "375": ["number"], "390": ["list"], "413": ["number"] },
9
+ "site-code-tabs.tsx": {},
10
+ "site-copy-button.tsx": { "89": ["icon-button"] },
11
+ "site-github-link.tsx": { "11": ["button"] },
12
+ "site-mobile-nav.tsx": { "83": ["button"], "126": ["list"], "142": ["list"], "170": ["list"] },
13
+ "site-mode-switcher.tsx": { "32": ["icon-button"] },
14
+ "site-page-nav.tsx": {}
15
+ }
@@ -0,0 +1,94 @@
1
+ "use client";
2
+
3
+ import { PlusIcon } from "@heroicons/react/24/outline";
4
+ import clsx from "clsx";
5
+ import { addItem } from "components/cart/actions";
6
+ import { Product, ProductVariant } from "lib/shopify/types";
7
+ import { useSearchParams } from "next/navigation";
8
+ import { useActionState } from "react";
9
+ import { useCart } from "./cart-context";
10
+
11
+ function SubmitButton({
12
+ availableForSale,
13
+ selectedVariantId,
14
+ }: {
15
+ availableForSale: boolean;
16
+ selectedVariantId: string | undefined;
17
+ }) {
18
+ const buttonClasses =
19
+ "relative flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white";
20
+ const disabledClasses = "cursor-not-allowed opacity-60 hover:opacity-60";
21
+
22
+ if (!availableForSale) {
23
+ return (
24
+ <button disabled className={clsx(buttonClasses, disabledClasses)}>
25
+ Out Of Stock
26
+ </button>
27
+ );
28
+ }
29
+
30
+ if (!selectedVariantId) {
31
+ return (
32
+ <button
33
+ aria-label="Please select an option"
34
+ disabled
35
+ className={clsx(buttonClasses, disabledClasses)}
36
+ >
37
+ <div className="absolute left-0 ml-4">
38
+ <PlusIcon className="h-5" />
39
+ </div>
40
+ Add To Cart
41
+ </button>
42
+ );
43
+ }
44
+
45
+ return (
46
+ <button
47
+ aria-label="Add to cart"
48
+ className={clsx(buttonClasses, {
49
+ "hover:opacity-90": true,
50
+ })}
51
+ >
52
+ <div className="absolute left-0 ml-4">
53
+ <PlusIcon className="h-5" />
54
+ </div>
55
+ Add To Cart
56
+ </button>
57
+ );
58
+ }
59
+
60
+ export function AddToCart({ product }: { product: Product }) {
61
+ const { variants, availableForSale } = product;
62
+ const { addCartItem } = useCart();
63
+ const searchParams = useSearchParams();
64
+ const [message, formAction] = useActionState(addItem, null);
65
+
66
+ const variant = variants.find((variant: ProductVariant) =>
67
+ variant.selectedOptions.every(
68
+ (option) => option.value === searchParams.get(option.name.toLowerCase()),
69
+ ),
70
+ );
71
+ const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined;
72
+ const selectedVariantId = variant?.id || defaultVariantId;
73
+ const addItemAction = formAction.bind(null, selectedVariantId);
74
+ const finalVariant = variants.find(
75
+ (variant) => variant.id === selectedVariantId,
76
+ )!;
77
+
78
+ return (
79
+ <form
80
+ action={async () => {
81
+ addCartItem(finalVariant, product);
82
+ addItemAction();
83
+ }}
84
+ >
85
+ <SubmitButton
86
+ availableForSale={availableForSale}
87
+ selectedVariantId={selectedVariantId}
88
+ />
89
+ <p aria-live="polite" className="sr-only" role="status">
90
+ {message}
91
+ </p>
92
+ </form>
93
+ );
94
+ }