Skip to content

Repository files navigation

Cart

Headless cart management library

npm i cart

All Contributors: 2Codecov Test CoverageContributor CovenantLicense: MITStyle: PrettierTypeScript: Strict

⚠️ Expect some breaking changes, Use at your own risk

🛒 Demo

📦 Requirements

  • React or Nextjs Project ⚛️

✨ Installation

  • Install using the below command (with your package manager of choice)
# npm
npm install cart --save
# yarn
yarn add cart
#pnpm
pnpm add cart
# bun
bun install cart

💡 Usage Example

importReactfrom"react";import{useCart}from"cart";constitem={productId: "123",name: "Product 1",quantity: 1,price: 10,};functionCart(){const{
addToCart,
cartItems,
clearCart,
decreaseItem,
toggleCart,
isCartOpen,}=useCart();return(<div><p>{isCartOpen ? "Open" : "Closed"}</p><buttononClick={()=>toggleCart()}>Toggle</button><buttononClick={()=>addToCart(item)}>Add</button><buttononClick={()=>clearCart()}>Clear</button><buttononClick={()=>decreaseItem("123",1)}>Decrease</button><p>{JSON.stringify(cartItems)}</p></div>);}exportdefaultCart;

💡 SSR Example

import{useCart,withSSR}from"cart";importReactfrom"react";constitem={productId: "123",name: "Product 1",quantity: 1,price: 10,};functionMyCart(){constcart=withSSR(useCart,(state)=>state);consthandleToggle=()=>{cart?.toggleCart();};constitemadd=()=>{cart?.addToCart(item);};return(<div><p>{cart?.isCartOpen ? "Open" : "Closed"}</p><buttononClick={()=>handleToggle()}>Toggle</button><buttononClick={()=>itemadd()}>Add</button><buttononClick={()=>cart?.clearCart()}>Clear</button><buttononClick={()=>cart?.decreaseItem("123",1)}>Decrease</button><p>{JSON.stringify(cart?.cartItems)}</p></div>);}exportdefaultMyCart;

API Reference

NameTypeDefault ValueDescriptionExample
isCartOpenbooleanfalseIndicates whether the cart is open or not.isCartOpen ? "Yes" : "No"
toggleCartfunction-Toggles the visibility of the shopping cart.toggleCart();
openCartfunction-Sets the cart open state to true.openCart();
closeCartfunction-Sets the cart open state to false.closeCart();
cartItemsarray[]An array of items in the cart.cartItems.map((item) => ( <div key={item.productId}> <p>{item.name}</p> <p>Quantity: {item.quantity}</p> <p>Price: ${item.price}</p> </div> ))
addToCartfunction-Adds an item to the shopping cart or updates its quantity if already in the cart.addToCart({ productId: 'product1', name: 'Product 1', quantity: 2, price: 20 });
decreaseItemfunction-Decreases the quantity of an item in the shopping cart or removes it if the quantity becomes zero.decreaseItem('product1', 1);
removeFromCartfunction-Removes an item from the shopping cart.removeFromCart('product1');
clearCartfunction-Clears all items from the shopping cart.clearCart();

🙏 Credits

Huge thanks to Peter Krumins for the package name cart. Please checkout Browserling - Online cross-browser testing platform.

(Btw, This is not a sponsored message, Just my way of saying thank you)

Contributors

Josh Goldberg ✨
Josh Goldberg ✨

🔧
MC Naveen
MC Naveen

💻🖋📖🤔🚇🚧📆🔧

💚 Message

I hope you find this useful. If you have any questions, please create an issue.


💙 This package is based on @JoshuaKGoldberg's create-typescript-app.

About

🛒 An Open Source Headless cart management library that does all the heavy lifting of managing the cart state, storing them in local storage, and even more.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

19 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages