Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@thoughtindustries/cart

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thoughtindustries/cart

A base component for cart

  • 1.2.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@thoughtindustries/cart

Cart components relate to the merchandise that a customer intends to purchase.

Table of contents:

  • Cart
  • CartButton
  • Core cart components
  • Core cart hooks

Cart

The Cart component takes a checkout url to handle the checkout redirection, and optional properties to customize price formatting. It provides access to both UI behavior (like: to toggle cart modal) and callback functions to manage a local cart object.

Example code

import { Cart } from '@thoughtindustries/cart';

export function MyComponent() {
  // ...

  return (
    <Cart checkoutUrl="/checkout">{/* Your JSX */}</Cart>
  );
}

Props

NameRequiredTypeDescription
childrenYesReactNodeAny ReactNode elements.
checkoutUrlYesstringThe URL for the checkout for this cart.
priceFormatNo(priceInCents: number) => stringA callback that is invoked to format monetary value with currency. It takes a number value for the price in cent unit and return the formatted value. Default value uses Intl.NumberFormat with props companyDefaultLocale and/or currencyCode to enable locale-specific currency formatting.
companyDefaultLocaleNostringA locale value to format price when prop priceFormat is not specified. Used to speficy the locale in Intl.NumberFormat. Default to en-US.
currencyCodeNostringA currency code value to format price when prop priceFormat is not specified. Used to speficy the currency code in Intl.NumberFormat. Default to USD.

CartButton

The CartButton component is a client component that displays the current item count in the cart, and handle click event to toggle the cart modal. It must be a descendent of the Cart component.

Example code

import { Cart, CartButton } from '@thoughtindustries/cart';

export function MyComponent() {
  // ...

  return (
    <Cart checkoutUrl="/checkout">
      <CartButton />
    </Cart>
  );
}

Core cart components

Core cart components are objects that contain all of business logic for the cart concept that they represent. They're used to parse and process data.

AddToCartButton

The AddToCartButton component renders a button that adds a purchaseable item to the cart when pressed. With additional props, it will follow up with step to either open the cart modal or take the user directly to the checkout flow. It must be a descendent of the CartUIProvider component.

Example code
import { CartUIProvider, AddToCartButton, EcommItemType } from '@thoughtindustries/cart';

export function MyComponent() {
  // ...

  return (
    <CartUIProvider checkoutUrl="/checkout">
      <AddToCartButton
        purchasableType={EcommItemType.Product}
        purchasable={{id:'product-uuid', priceInCents:1000}}
      >
        Add to Cart
      </AddToCartButton>
    </CartUIProvider>
  );
}
Props
NameRequiredTypeDescription
childrenYesReactNodeAny ReactNode elements.
shouldOpenCartNobooleanOption to open cart modal as a follow-up action.
purchasableTypeYesEcommItemTypeType of purchaseable item (one of the ecommerce item type).
purchasableYesPurchaseableItemObject of the purchaseable item.
couponNoCouponOptional coupon object.
intervalNoCartItemIntervalOptional payment interval (one of the interval type).
quantityNonumberOptional quantity (a default value will be applied when not sepcified).
buttonRefNoRef<HTMLButtonElement> A reference to the underlying button.

CartCheckoutButton

The CartCheckoutButton component renders a button that redirects to the checkout URL for the cart. It must be a descendent of a CartProvider component.

Example code
import { CartCheckoutButton, CartProvider } from '@thoughtindustries/cart';

export function MyComponent() {
  return (
    <CartProvider checkoutUrl="/checkout">
      <CartCheckoutButton>Checkout</CartCheckoutButton>
    </CartProvider>
  )
}
Props
NameRequiredTypeDescription
childrenYesReactNodeA ReactNode element.
buttonRefNoRef<HTMLButtonElement> A reference to the underlying button.

CartProvider

The CartProvider component creates a context for using a cart. It creates a cart object and callbacks that can be accessed by any descendent component using the useCart hook and related hooks.

You must use this component if you want to use the useCart hook or related hooks, or if you would like to use the AddToCartButton component.

Example code
import { CartProvider } from '@thoughtindustries/cart';

export function App() {
  return <CartProvider>{/* Your JSX */}</CartProvider>;
}
Props
NameRequiredTypeDescription
childrenYesReact.ReactNodeAny ReactNode elements.
checkoutUrlYesstringThe URL for the checkout for this cart.

CartUIProvider

The CartUIProvider component defines the behavior that occurs when a user is interacting with a cart (for example, opening or closing it), it also creates a cart object and provides callbacks that can be accessed by any descendent component using the useCartUI hook, the useCart hook, and related hooks. This component renders the CartProvider to provides any of its descendant access to the context of CartProvider.

You must use this component if you want to use the useCartUI hook, the useCart hook or related hooks, or if you would like to use the AddToCartButton component.

Example code
import { CartUIProvider } from '@thoughtindustries/cart';

export function App() {
  return <CartProvider>{/* Your JSX */}</CartProvider>;
}
Props
NameRequiredTypeDescription
childrenYesReact.ReactNodeAny ReactNode elements.
checkoutUrlYesstringThe URL for the checkout for this cart.

Core cart hooks

Core cart hooks are functions that allow you to use state and other methods inside cart components.

useCartCheckout

The useCartCheckout hook provides access to the cart checkout process. It must be a descendent of a CartProvider component.

Example code
import { CartProvider, useCartCheckout } from '@thoughtindustries/cart';

export function MyComponent() {
  return (
    <CartProvider checkoutUrl="/checkout">
      <CartCheckoutButton />
    </CartProvider>
  );
}

export function CartCheckoutButton() {
  const { isCheckoutRequested, startCheckout } = useCartCheckout();

  return (
    <button disabled={isCheckoutRequested} onClick={startCheckout}>
      {/* Your JSX */}
    </button>
  );
}
Return value

The useCartCheckout hook returns an object with the following keys:

NameRequiredDescription
isCheckoutRequestedYesThis indicates if the cart checkout process has already requested.
startCheckoutYesA callback that starts the cart checkout process.

useCartUI

The useCartUI hook provides access to the cart UI context. It must be a descendent of a CartUIProvider component.

Example code
import { CartUIProvider, useCartUI } from '@thoughtindustries/cart';

export function MyComponent() {
  return (
    <CartUIProvider checkoutUrl="/checkout">
      <CartToggle />
    </CartUIProvider>
  );
}

export function CartToggle() {
  const { isCartOpen, openCart } = useCartUI();

  return (
    <button disabled={isCartOpen} onClick={openCart}>
      {/* Your JSX */}
    </button>
  );
}
Return value

The useCartUI hook returns an object with the following keys:

NameRequiredDescription
isCartOpenYesBoolean value indicating if the cart (modal) is open.
openCartYesA callback to open cart.
closeCartYesA callback to close cart.
toggleCartYesA callback to toggle cart to open or to close.

useCart

The useCart hook provides access to the cart object. It must be a descendent of a CartProvider component.

Example code
import { CartProvider, useCart } from '@thoughtindustries/cart';

export function MyComponent() {
  return (
    <CartProvider checkoutUrl="/checkout">
      <CartTotalQuantity />
    </CartProvider>
  );
}

export function CartTotalQuantity() {
  const { totalQuantity } = useCart();

  return (
    <>
      {totalQuantity}
    </>
  );
}
Return value

The useCart hook returns an object with the following keys:

NameRequiredDescription
itemsYesThe cart items.
checkoutUrlYesThe URL for the checkout for this cart.
isInitializedYesThis indicates if the cart is initialized. The initialization process will trigger once the CartProvider component is mounted.
addPurchaseableItemYesA callback that adds purchaseable item to the cart. Expects the AddPurchaseableItemPayload input.
removeItemYesA callback that removes item from the cart. Expects the CartItem input.
toggleItemInstructorAccessYesA callback that updates item variation label for instructor access in the cart. Expects the CartItem.
totalQuantityYesThe total number of items in the cart, across all items. If there are no items, then the value is 0.

FAQs

Package last updated on 01 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc