{"version":3,"file":"AddToCartButton.mjs","names":[],"sources":["../../src/AddToCartButton.tsx"],"sourcesContent":["import {useCallback, useEffect, useState} from 'react';\nimport {useCart} from './CartProvider.js';\nimport {useProduct} from './ProductProvider.js';\nimport {\n  BaseButton,\n  type CustomBaseButtonProps,\n  type BaseButtonProps,\n} from './BaseButton.js';\nimport * as React from 'react';\nimport {CartLineParentInput} from './storefront-api-types.js';\n\nexport interface AddToCartButtonPropsBase {\n  /** An array of cart line attributes that belong to the item being added to the cart. */\n  attributes?: {\n    key: string;\n    value: string;\n  }[];\n  /** The ID of the variant. */\n  variantId?: string | null;\n  /** The item quantity. */\n  quantity?: number;\n  /** The text that is announced by the screen reader when the item is being added to the cart. Used for accessibility purposes only and not displayed on the page. */\n  accessibleAddingToCartLabel?: string;\n  /** The parent line item of the item being added to the cart. Used for nested cart lines. */\n  parent?: CartLineParentInput;\n  /** The selling plan ID of the subscription variant */\n  sellingPlanId?: string;\n}\n\nexport type AddToCartButtonProps<AsType extends React.ElementType = 'button'> =\n  AddToCartButtonPropsBase & BaseButtonProps<AsType>;\n\n/**\n * The `AddToCartButton` component renders a button that adds an item to the cart when pressed.\n * It must be a descendent of the `CartProvider` component.\n * @publicDocs\n */\nexport function AddToCartButton<AsType extends React.ElementType = 'button'>(\n  props: AddToCartButtonProps<AsType>,\n): JSX.Element {\n  const [addingItem, setAddingItem] = useState<boolean>(false);\n  const {\n    variantId: explicitVariantId,\n    quantity = 1,\n    attributes,\n    sellingPlanId,\n    onClick,\n    children,\n    accessibleAddingToCartLabel,\n    parent,\n    ...passthroughProps\n  } = props;\n  const {status, linesAdd} = useCart();\n  const {selectedVariant} = useProduct();\n  const variantId = explicitVariantId ?? selectedVariant?.id ?? '';\n  const disabled =\n    explicitVariantId === null ||\n    variantId === '' ||\n    selectedVariant === null ||\n    addingItem ||\n    // Only certain 'as' types such as 'button' contain `disabled`\n    (passthroughProps as {disabled?: boolean}).disabled;\n\n  useEffect(() => {\n    if (addingItem && status === 'idle') {\n      setAddingItem(false);\n    }\n  }, [status, addingItem]);\n\n  const handleAddItem = useCallback(() => {\n    setAddingItem(true);\n    linesAdd([\n      {\n        quantity,\n        merchandiseId: variantId || '',\n        attributes,\n        parent,\n        sellingPlanId,\n      },\n    ]);\n  }, [linesAdd, quantity, variantId, attributes, sellingPlanId, parent]);\n\n  return (\n    <>\n      <BaseButton\n        {...passthroughProps}\n        disabled={disabled}\n        onClick={onClick}\n        defaultOnClick={handleAddItem}\n      >\n        {children}\n      </BaseButton>\n      {accessibleAddingToCartLabel ? (\n        <p\n          style={{\n            position: 'absolute',\n            width: '1px',\n            height: '1px',\n            padding: '0',\n            margin: '-1px',\n            overflow: 'hidden',\n            clip: 'rect(0, 0, 0, 0)',\n            whiteSpace: 'nowrap',\n            borderWidth: '0',\n          }}\n          role=\"alert\"\n          aria-live=\"assertive\"\n        >\n          {addingItem ? accessibleAddingToCartLabel : null}\n        </p>\n      ) : null}\n    </>\n  );\n}\n\n// This is only for documentation purposes, and it is not used in the code.\n/** @publicDocs */\nexport interface AddToCartButtonPropsForDocs<\n  AsType extends React.ElementType = 'button',\n>\n  extends AddToCartButtonPropsBase, CustomBaseButtonProps<AsType> {}\n"],"mappings":";;;;;;;;;;;AAqCA,SAAgB,gBACd,OACa;CACb,MAAM,CAAC,YAAY,iBAAiB,SAAkB,MAAM;CAC5D,MAAM,EACJ,WAAW,mBACX,WAAW,GACX,YACA,eACA,SACA,UACA,6BACA,QACA,GAAG,qBACD;CACJ,MAAM,EAAC,QAAQ,aAAY,SAAS;CACpC,MAAM,EAAC,oBAAmB,YAAY;CACtC,MAAM,YAAY,qBAAqB,iBAAiB,MAAM;CAC9D,MAAM,WACJ,sBAAsB,QACtB,cAAc,MACd,oBAAoB,QACpB,cAEC,iBAA0C;AAE7C,iBAAgB;AACd,MAAI,cAAc,WAAW,OAC3B,eAAc,MAAM;IAErB,CAAC,QAAQ,WAAW,CAAC;CAExB,MAAM,gBAAgB,kBAAkB;AACtC,gBAAc,KAAK;AACnB,WAAS,CACP;GACE;GACA,eAAe,aAAa;GAC5B;GACA;GACA;GACD,CACF,CAAC;IACD;EAAC;EAAU;EAAU;EAAW;EAAY;EAAe;EAAO,CAAC;AAEtE,QACE,qBAAA,YAAA,EAAA,UAAA,CACE,oBAAC,YAAD;EACE,GAAI;EACM;EACD;EACT,gBAAgB;EAEf;EACU,CAAA,EACZ,8BACC,oBAAC,KAAD;EACE,OAAO;GACL,UAAU;GACV,OAAO;GACP,QAAQ;GACR,SAAS;GACT,QAAQ;GACR,UAAU;GACV,MAAM;GACN,YAAY;GACZ,aAAa;GACd;EACD,MAAK;EACL,aAAU;YAET,aAAa,8BAA8B;EAC1C,CAAA,GACF,KACH,EAAA,CAAA"}