@shopify/shop-minis-react 0.2.9 → 0.3.1

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.
@@ -1,62 +1,79 @@
1
- import { jsx as r, jsxs as A } from "react/jsx-runtime";
2
- import * as m from "react";
3
- import { useState as T, useCallback as v } from "react";
4
- import { cn as f } from "../../lib/utils.js";
5
- import { Button as x } from "../atoms/button.js";
6
- import { useShopCartActions as C } from "../../hooks/shop/useShopCartActions.js";
7
- import { useErrorToast as y } from "../../hooks/util/useErrorToast.js";
8
- import { AnimatePresence as N } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/components/AnimatePresence/index.js";
9
- import { motion as k } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/render/components/motion/proxy.js";
10
- import w from "../../shop-minis-react/node_modules/.pnpm/lucide-react@0.513.0_react@19.1.0/node_modules/lucide-react/dist/esm/icons/check.js";
11
- function P({
12
- disabled: o = !1,
13
- className: u,
14
- size: d = "default",
15
- productId: a,
16
- productVariantId: i,
17
- discountCodes: n
1
+ import { jsx as o, jsxs as g } from "react/jsx-runtime";
2
+ import * as p from "react";
3
+ import { useState as w, useCallback as y } from "react";
4
+ import { useShopCartActions as k } from "../../internal/useShopCartActions.js";
5
+ import { cn as h } from "../../lib/utils.js";
6
+ import { Button as j } from "../atoms/button.js";
7
+ import { useShopNavigation as E } from "../../hooks/navigation/useShopNavigation.js";
8
+ import { useErrorToast as R } from "../../hooks/util/useErrorToast.js";
9
+ import { AnimatePresence as S } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/components/AnimatePresence/index.js";
10
+ import { motion as B } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/render/components/motion/proxy.js";
11
+ import F from "../../shop-minis-react/node_modules/.pnpm/lucide-react@0.513.0_react@19.1.0/node_modules/lucide-react/dist/esm/icons/check.js";
12
+ function L({
13
+ disabled: i = !1,
14
+ className: T,
15
+ size: v = "default",
16
+ productVariantId: a,
17
+ discountCodes: l,
18
+ product: x
18
19
  }) {
19
- const { addToCart: c } = C(), [t, s] = T(!1), e = m.useRef(void 0), { showErrorToast: l } = y(), h = v(async () => {
20
- if (!(t || o))
21
- try {
22
- a && i && c({
23
- productId: a,
24
- productVariantId: i,
25
- quantity: 1,
26
- discountCodes: n
27
- }).then(() => {
28
- }).catch(() => {
29
- l({ message: "Failed to add to cart" });
30
- }), s(!0), e.current && clearTimeout(e.current), e.current = window.setTimeout(() => {
31
- s(!1);
32
- }, 2e3);
33
- } catch (p) {
34
- s(!1), console.error("Failed to add to cart:", p);
20
+ const { addToCart: d } = k(), { navigateToProduct: m } = E(), [t, n] = w(!1), e = p.useRef(void 0), { id: r, referral: s, variants: C } = x ?? {}, u = C?.find(
21
+ (c) => c.id === a
22
+ )?.image?.url, { showErrorToast: f } = R(), A = y(async () => {
23
+ if (!i) {
24
+ if (r && s) {
25
+ m({
26
+ productId: r
27
+ });
28
+ return;
35
29
  }
30
+ if (!t)
31
+ try {
32
+ r && a && d({
33
+ productId: r,
34
+ productVariantId: a,
35
+ quantity: 1,
36
+ discountCodes: l,
37
+ variantImageUrl: u
38
+ }).then(() => {
39
+ }).catch(() => {
40
+ f({ message: "Failed to add to cart" });
41
+ }), n(!0), e.current && clearTimeout(e.current), e.current = window.setTimeout(() => {
42
+ n(!1);
43
+ }, 2e3);
44
+ } catch (c) {
45
+ n(!1), console.error("Failed to add to cart:", c);
46
+ }
47
+ }
36
48
  }, [
49
+ i,
50
+ r,
51
+ s,
37
52
  t,
38
- o,
39
- c,
53
+ m,
40
54
  a,
41
- i,
42
- n,
43
- l
55
+ d,
56
+ l,
57
+ u,
58
+ f
44
59
  ]);
45
- return m.useEffect(() => () => {
60
+ p.useEffect(() => () => {
46
61
  e.current && clearTimeout(e.current);
47
- }, []), /* @__PURE__ */ r(
48
- x,
62
+ }, []);
63
+ const N = s ? "View product" : t ? "Added to cart" : "Add to cart";
64
+ return /* @__PURE__ */ o(
65
+ j,
49
66
  {
50
- onClick: h,
51
- disabled: o,
52
- className: f(
67
+ onClick: A,
68
+ disabled: i,
69
+ className: h(
53
70
  "relative overflow-hidden transition-all duration-300",
54
- u
71
+ T
55
72
  ),
56
- size: d,
57
- children: /* @__PURE__ */ A("div", { className: "relative flex items-center justify-center", children: [
58
- /* @__PURE__ */ r(N, { children: t && /* @__PURE__ */ r(
59
- k.div,
73
+ size: v,
74
+ children: /* @__PURE__ */ g("div", { className: "relative flex items-center justify-center", children: [
75
+ /* @__PURE__ */ o(S, { children: t && /* @__PURE__ */ o(
76
+ B.div,
60
77
  {
61
78
  initial: { scale: 0, rotate: -180 },
62
79
  animate: { scale: 1, rotate: 0 },
@@ -66,17 +83,17 @@ function P({
66
83
  ease: [0.175, 0.885, 0.32, 1.275]
67
84
  // bounce effect
68
85
  },
69
- className: "absolute left-0",
86
+ className: "absolute left-2",
70
87
  style: { x: -8 },
71
- children: /* @__PURE__ */ r(w, { className: "size-4" })
88
+ children: /* @__PURE__ */ o(F, { className: "size-4" })
72
89
  }
73
90
  ) }),
74
- /* @__PURE__ */ r("span", { className: f(t && "pl-5", "transition-all duration-300"), children: t ? "Added to cart" : "Add to cart" })
91
+ /* @__PURE__ */ o("span", { className: h(t && "pl-5", "transition-all duration-300"), children: N })
75
92
  ] })
76
93
  }
77
94
  );
78
95
  }
79
96
  export {
80
- P as AddToCartButton
97
+ L as AddToCartButton
81
98
  };
82
99
  //# sourceMappingURL=add-to-cart.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"add-to-cart.js","sources":["../../../src/components/commerce/add-to-cart.tsx"],"sourcesContent":["import * as React from 'react'\nimport {useState, useCallback} from 'react'\n\nimport {CheckIcon} from 'lucide-react'\nimport {motion, AnimatePresence} from 'motion/react'\n\nimport {useErrorToast, useShopCartActions} from '../../hooks'\nimport {cn} from '../../lib/utils'\nimport {Button} from '../atoms/button'\n\ninterface AddToCartButtonProps {\n disabled?: boolean\n className?: string\n size?: 'default' | 'sm' | 'lg'\n /**\n * The discount codes to apply to the cart.\n */\n discountCodes?: string[]\n /**\n * The GID of the product. E.g. `gid://shopify/Product/123`.\n */\n productId: string\n /**\n * The GID of the product variant. E.g. `gid://shopify/ProductVariant/456`.\n */\n productVariantId: string\n}\n\nexport function AddToCartButton({\n disabled = false,\n className,\n size = 'default',\n productId,\n productVariantId,\n discountCodes,\n}: AddToCartButtonProps) {\n const {addToCart} = useShopCartActions()\n const [isAdded, setIsAdded] = useState(false)\n const timeoutRef = React.useRef<number | undefined>(undefined)\n\n const {showErrorToast} = useErrorToast()\n\n const handleClick = useCallback(async () => {\n if (isAdded || disabled) return\n\n try {\n // Call the callback if provided\n if (productId && productVariantId) {\n // Optimistic update with error toast\n addToCart({\n productId,\n productVariantId,\n quantity: 1,\n discountCodes,\n })\n .then(() => {})\n .catch(() => {\n showErrorToast({message: 'Failed to add to cart'})\n })\n }\n\n // Show success state\n setIsAdded(true)\n\n // Clear any existing timeout\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n }\n\n // Reset to initial state after delay\n timeoutRef.current = window.setTimeout(() => {\n setIsAdded(false)\n }, 2000)\n } catch (error) {\n // Handle error - reset to initial state\n setIsAdded(false)\n console.error('Failed to add to cart:', error)\n }\n }, [\n isAdded,\n disabled,\n addToCart,\n productId,\n productVariantId,\n discountCodes,\n showErrorToast,\n ])\n\n // Cleanup timeout on unmount\n React.useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n }\n }\n }, [])\n\n return (\n <Button\n onClick={handleClick}\n disabled={disabled}\n className={cn(\n 'relative overflow-hidden transition-all duration-300',\n className\n )}\n size={size}\n >\n <div className=\"relative flex items-center justify-center\">\n <AnimatePresence>\n {isAdded && (\n <motion.div\n initial={{scale: 0, rotate: -180}}\n animate={{scale: 1, rotate: 0}}\n exit={{scale: 0, rotate: 180}}\n transition={{\n duration: 0.4,\n ease: [0.175, 0.885, 0.32, 1.275], // bounce effect\n }}\n className=\"absolute left-0\"\n style={{x: -8}}\n >\n <CheckIcon className=\"size-4\" />\n </motion.div>\n )}\n </AnimatePresence>\n <span className={cn(isAdded && 'pl-5', 'transition-all duration-300')}>\n {isAdded ? 'Added to cart' : 'Add to cart'}\n </span>\n </div>\n </Button>\n )\n}\n"],"names":["AddToCartButton","disabled","className","size","productId","productVariantId","discountCodes","addToCart","useShopCartActions","isAdded","setIsAdded","useState","timeoutRef","React","showErrorToast","useErrorToast","handleClick","useCallback","error","jsx","Button","cn","jsxs","AnimatePresence","motion","CheckIcon"],"mappings":";;;;;;;;;;AA4BO,SAASA,EAAgB;AAAA,EAC9B,UAAAC,IAAW;AAAA,EACX,WAAAC;AAAA,EACA,MAAAC,IAAO;AAAA,EACP,WAAAC;AAAA,EACA,kBAAAC;AAAA,EACA,eAAAC;AACF,GAAyB;AACjB,QAAA,EAAC,WAAAC,EAAS,IAAIC,EAAmB,GACjC,CAACC,GAASC,CAAU,IAAIC,EAAS,EAAK,GACtCC,IAAaC,EAAM,OAA2B,MAAS,GAEvD,EAAC,gBAAAC,EAAc,IAAIC,EAAc,GAEjCC,IAAcC,EAAY,YAAY;AAC1C,QAAI,EAAAR,KAAWR;AAEX,UAAA;AAEF,QAAIG,KAAaC,KAELE,EAAA;AAAA,UACR,WAAAH;AAAA,UACA,kBAAAC;AAAA,UACA,UAAU;AAAA,UACV,eAAAC;AAAA,QAAA,CACD,EACE,KAAK,MAAM;AAAA,QAAA,CAAE,EACb,MAAM,MAAM;AACI,UAAAQ,EAAA,EAAC,SAAS,yBAAwB;AAAA,QAAA,CAClD,GAILJ,EAAW,EAAI,GAGXE,EAAW,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ACF,CAAA;AAAA,IAAA;AAAA,EACF;AAEJ;"}
1
+ {"version":3,"file":"add-to-cart.js","sources":["../../../src/components/commerce/add-to-cart.tsx"],"sourcesContent":["import * as React from 'react'\nimport {useState, useCallback} from 'react'\n\nimport {Product} from '@shopify/shop-minis-platform'\nimport {CheckIcon} from 'lucide-react'\nimport {motion, AnimatePresence} from 'motion/react'\n\nimport {useErrorToast, useShopNavigation} from '../../hooks'\nimport {useShopCartActions} from '../../internal/useShopCartActions'\nimport {cn} from '../../lib/utils'\nimport {Button} from '../atoms/button'\n\ninterface AddToCartButtonProps {\n disabled?: boolean\n className?: string\n size?: 'default' | 'sm' | 'lg'\n /**\n * The discount codes to apply to the cart.\n */\n discountCodes?: string[]\n /**\n * The GID of the product variant. E.g. `gid://shopify/ProductVariant/456`.\n */\n productVariantId: string\n\n /**\n * The product to add to the cart.\n */\n product?: Product\n}\n\nexport function AddToCartButton({\n disabled = false,\n className,\n size = 'default',\n productVariantId,\n discountCodes,\n product,\n}: AddToCartButtonProps) {\n const {addToCart} = useShopCartActions()\n const {navigateToProduct} = useShopNavigation()\n const [isAdded, setIsAdded] = useState(false)\n const timeoutRef = React.useRef<number | undefined>(undefined)\n const {id, referral, variants} = product ?? {}\n\n const variantImageUrl = variants?.find(\n variant => variant.id === productVariantId\n )?.image?.url\n\n const {showErrorToast} = useErrorToast()\n\n const handleClick = useCallback(async () => {\n if (disabled) return\n\n if (id && referral) {\n navigateToProduct({\n productId: id,\n })\n\n return\n }\n\n if (isAdded) return\n\n try {\n if (id && productVariantId) {\n addToCart({\n productId: id,\n productVariantId,\n quantity: 1,\n discountCodes,\n variantImageUrl,\n })\n .then(() => {})\n .catch(() => {\n showErrorToast({message: 'Failed to add to cart'})\n })\n }\n\n // Show success state\n setIsAdded(true)\n\n // Clear any existing timeout\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n }\n\n // Reset to initial state after delay\n timeoutRef.current = window.setTimeout(() => {\n setIsAdded(false)\n }, 2000)\n } catch (error) {\n // Handle error - reset to initial state\n setIsAdded(false)\n console.error('Failed to add to cart:', error)\n }\n }, [\n disabled,\n id,\n referral,\n isAdded,\n navigateToProduct,\n productVariantId,\n addToCart,\n discountCodes,\n variantImageUrl,\n showErrorToast,\n ])\n\n // Cleanup timeout on unmount\n React.useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n }\n }\n }, [])\n\n const addToCartText = isAdded ? 'Added to cart' : 'Add to cart'\n const buttonText = referral ? 'View product' : addToCartText\n\n return (\n <Button\n onClick={handleClick}\n disabled={disabled}\n className={cn(\n 'relative overflow-hidden transition-all duration-300',\n className\n )}\n size={size}\n >\n <div className=\"relative flex items-center justify-center\">\n <AnimatePresence>\n {isAdded && (\n <motion.div\n initial={{scale: 0, rotate: -180}}\n animate={{scale: 1, rotate: 0}}\n exit={{scale: 0, rotate: 180}}\n transition={{\n duration: 0.4,\n ease: [0.175, 0.885, 0.32, 1.275], // bounce effect\n }}\n className=\"absolute left-2\"\n style={{x: -8}}\n >\n <CheckIcon className=\"size-4\" />\n </motion.div>\n )}\n </AnimatePresence>\n <span className={cn(isAdded && 'pl-5', 'transition-all duration-300')}>\n {buttonText}\n </span>\n </div>\n </Button>\n )\n}\n"],"names":["AddToCartButton","disabled","className","size","productVariantId","discountCodes","product","addToCart","useShopCartActions","navigateToProduct","useShopNavigation","isAdded","setIsAdded","useState","timeoutRef","React","id","referral","variants","variantImageUrl","variant","showErrorToast","useErrorToast","handleClick","useCallback","error","buttonText","jsx","Button","cn","jsxs","AnimatePresence","motion","CheckIcon"],"mappings":";;;;;;;;;;;AA+BO,SAASA,EAAgB;AAAA,EAC9B,UAAAC,IAAW;AAAA,EACX,WAAAC;AAAA,EACA,MAAAC,IAAO;AAAA,EACP,kBAAAC;AAAA,EACA,eAAAC;AAAA,EACA,SAAAC;AACF,GAAyB;AACjB,QAAA,EAAC,WAAAC,EAAS,IAAIC,EAAmB,GACjC,EAAC,mBAAAC,EAAiB,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AaT,IAAW,iBADRN,IAAU,kBAAkB;AAIhD,SAAA,gBAAAgB;AAAA,IAACC;AAAA,IAAA;AAAA,MACC,SAASL;AAAA,MACT,UAAAtB;AAAA,MACA,WAAW4B;AAAA,QACT;AAAA,QACA3B;AAAA,MACF;AAAA,MACA,MAAAC;AAAA,MAEA,UAAA,gBAAA2B,EAAC,OAAI,EAAA,WAAU,6CACb,UAAA;AAAA,QAAA,gBAAAH,EAACI,KACE,UACCpB,KAAA,gBAAAgB;AAAA,UAACK,EAAO;AAAA,UAAP;AAAA,YACC,SAAS,EAAC,OAAO,GAAG,QAAQ,KAAI;AAAA,YAChC,SAAS,EAAC,OAAO,GAAG,QAAQ,EAAC;AAAA,YAC7B,MAAM,EAAC,OAAO,GAAG,QAAQ,IAAG;AAAA,YAC5B,YAAY;AAAA,cACV,UAAU;AAAA,cACV,MAAM,CAAC,OAAO,OAAO,MAAM,KAAK;AAAA;AAAA,YAClC;AAAA,YACA,WAAU;AAAA,YACV,OAAO,EAAC,GAAG,GAAE;AAAA,YAEb,UAAA,gBAAAL,EAACM,GAAU,EAAA,WAAU,SAAS,CAAA;AAAA,UAAA;AAAA,QAAA,GAGpC;AAAA,QACA,gBAAAN,EAAC,UAAK,WAAWE,EAAGlB,KAAW,QAAQ,6BAA6B,GACjE,UACHe,EAAA,CAAA;AAAA,MAAA,EACF,CAAA;AAAA,IAAA;AAAA,EACF;AAEJ;"}
@@ -0,0 +1,75 @@
1
+ import { jsx as n } from "react/jsx-runtime";
2
+ import { useState as h, useCallback as d } from "react";
3
+ import { useShopCartActions as w } from "../../internal/useShopCartActions.js";
4
+ import { cn as g } from "../../lib/utils.js";
5
+ import { Button as v } from "../atoms/button.js";
6
+ import { AnimatePresence as P } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/components/AnimatePresence/index.js";
7
+ import { motion as x } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/render/components/motion/proxy.js";
8
+ import { useShopNavigation as B } from "../../hooks/navigation/useShopNavigation.js";
9
+ import { useErrorToast as k } from "../../hooks/util/useErrorToast.js";
10
+ function I({
11
+ disabled: r = !1,
12
+ className: f,
13
+ size: l = "default",
14
+ productVariantId: i,
15
+ discountCode: s,
16
+ product: p
17
+ }) {
18
+ const { buyProduct: c } = w(), { navigateToProduct: m } = B(), [o, e] = h(!1), { id: t, referral: a } = p ?? {}, { showErrorToast: u } = k(), y = d(async () => {
19
+ if (!r) {
20
+ if (t && a) {
21
+ m({
22
+ productId: t
23
+ });
24
+ return;
25
+ }
26
+ if (!o)
27
+ try {
28
+ t && i && (e(!0), await c({
29
+ productId: t,
30
+ productVariantId: i,
31
+ quantity: 1,
32
+ discountCode: s
33
+ }), e(!1));
34
+ } catch {
35
+ u({ message: "Failed to complete purchase" }), e(!1);
36
+ }
37
+ }
38
+ }, [
39
+ r,
40
+ t,
41
+ a,
42
+ o,
43
+ m,
44
+ i,
45
+ c,
46
+ s,
47
+ u
48
+ ]);
49
+ return /* @__PURE__ */ n(
50
+ v,
51
+ {
52
+ onClick: y,
53
+ disabled: r || o,
54
+ className: g("relative overflow-hidden", f),
55
+ size: l,
56
+ "aria-live": "polite",
57
+ "aria-busy": o,
58
+ children: /* @__PURE__ */ n(P, { mode: "wait", children: /* @__PURE__ */ n(
59
+ x.span,
60
+ {
61
+ initial: { opacity: 0, y: 10 },
62
+ animate: { opacity: 1, y: 0 },
63
+ exit: { opacity: 0, y: -10 },
64
+ transition: { duration: 0.2 },
65
+ children: a ? "View product" : "Buy now"
66
+ },
67
+ "text"
68
+ ) })
69
+ }
70
+ );
71
+ }
72
+ export {
73
+ I as BuyNowButton
74
+ };
75
+ //# sourceMappingURL=buy-now.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buy-now.js","sources":["../../../src/components/commerce/buy-now.tsx"],"sourcesContent":["import {useState, useCallback} from 'react'\n\nimport {Product} from '@shopify/shop-minis-platform'\nimport {motion, AnimatePresence} from 'motion/react'\n\nimport {useErrorToast, useShopNavigation} from '../../hooks'\nimport {useShopCartActions} from '../../internal/useShopCartActions'\nimport {cn} from '../../lib/utils'\nimport {Button} from '../atoms/button'\n\ninterface BuyNowButtonProps {\n disabled?: boolean\n className?: string\n size?: 'default' | 'sm' | 'lg'\n /**\n * The discount code to apply to the purchase.\n */\n discountCode?: string\n /**\n * The GID of the product variant. E.g. `gid://shopify/ProductVariant/456`.\n */\n productVariantId: string\n /**\n * The product to buy now.\n */\n product?: Product\n}\n\nexport function BuyNowButton({\n disabled = false,\n className,\n size = 'default',\n productVariantId,\n discountCode,\n product,\n}: BuyNowButtonProps) {\n const {buyProduct} = useShopCartActions()\n const {navigateToProduct} = useShopNavigation()\n const [isPurchasing, setIsPurchasing] = useState(false)\n const {id, referral} = product ?? {}\n\n const {showErrorToast} = useErrorToast()\n\n const handleClick = useCallback(async () => {\n if (disabled) return\n\n if (id && referral) {\n navigateToProduct({\n productId: id,\n })\n\n return\n }\n\n if (isPurchasing) return\n\n try {\n if (id && productVariantId) {\n setIsPurchasing(true)\n\n await buyProduct({\n productId: id,\n productVariantId,\n quantity: 1,\n discountCode,\n })\n\n setIsPurchasing(false)\n }\n } catch (error) {\n showErrorToast({message: 'Failed to complete purchase'})\n setIsPurchasing(false)\n }\n }, [\n disabled,\n id,\n referral,\n isPurchasing,\n navigateToProduct,\n productVariantId,\n buyProduct,\n discountCode,\n showErrorToast,\n ])\n\n return (\n <Button\n onClick={handleClick}\n disabled={disabled || isPurchasing}\n className={cn('relative overflow-hidden', className)}\n size={size}\n aria-live=\"polite\"\n aria-busy={isPurchasing}\n >\n <AnimatePresence mode=\"wait\">\n <motion.span\n key=\"text\"\n initial={{opacity: 0, y: 10}}\n animate={{opacity: 1, y: 0}}\n exit={{opacity: 0, y: -10}}\n transition={{duration: 0.2}}\n >\n {referral ? 'View product' : 'Buy now'}\n </motion.span>\n </AnimatePresence>\n </Button>\n )\n}\n"],"names":["BuyNowButton","disabled","className","size","productVariantId","discountCode","product","buyProduct","useShopCartActions","navigateToProduct","useShopNavigation","isPurchasing","setIsPurchasing","useState","id","referral","showErrorToast","useErrorToast","handleClick","useCallback","jsx","Button","cn","AnimatePresence","motion"],"mappings":";;;;;;;;;AA4BO,SAASA,EAAa;AAAA,EAC3B,UAAAC,IAAW;AAAA,EACX,WAAAC;AAAA,EACA,MAAAC,IAAO;AAAA,EACP,kBAAAC;AAAA,EACA,cAAAC;AAAA,EACA,SAAAC;AACF,GAAsB;AACd,QAAA,EAAC,YAAAC,EAAU,IAAIC,EAAmB,GAClC,EAAC,mBAAAC,EAAiB,IAAIC,EAAkB,GACxC,CAACC,GAAcC,CAAe,IAAIC,EAAS,EAAK,GAChD,EAAC,IAAAC,GAAI,UAAAC,EAAQ,IAAIT,KAAW,CAAC,GAE7B,EAAC,gBAAAU,EAAc,IAAIC,EAAc,GAEjCC,IAAcC,EAAY,YAAY;AAC1C,QAAI,CAAAlB,GAEJ;AAAA,UAAIa,KAAMC,GAAU;AACA,QAAAN,EAAA;AAAA,UAChB,WAAWK;AAAA,QAAA,CACZ;AAED;AAAA,MAAA;AAGF,UAAI,CAAAH;AAEA,YAAA;AACF,UAAIG,KAAMV,MACRQ,EAAgB,EAAI,GAEpB,MAAML,EAAW;AAAA,YACf,WAAWO;AAAA,YACX,kBAAAV;AAAA,YACA,UAAU;AAAA,YACV,cAAAC;AAAA,UAAA,CACD,GAEDO,EAAgB,EAAK;AAAA,gBAET;AACC,UAAAI,EAAA,EAAC,SAAS,+BAA8B,GACvDJ,EAAgB,EAAK;AAAA,QAAA;AAAA;AAAA,EACvB,GACC;AAAA,IACDX;AAAA,IACAa;AAAA,IACAC;AAAA,IACAJ;AAAA,IACAF;AAAA,IACAL;AAAA,IACAG;AAAA,IACAF;AAAA,IACAW;AAAA,EAAA,CACD;AAGC,SAAA,gBAAAI;AAAA,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