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

cart

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cart

Headless cart management library

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35
decreased by-7.89%
Maintainers
1
Weekly downloads
 
Created
Source

Cart

Headless cart management library

npm i cart

All Contributors: 2 Codecov Test Coverage Contributor Covenant License: MIT Style: Prettier TypeScript: Strict

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

🛒 Demo

:package: Requirements

  • React or Nextjs Project ⚛️

:sparkles: 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


:bulb: Usage Example

import React from "react";
import { useCart } from "cart";

const item = {
  productId: "123",
  name: "Product 1",
  quantity: 1,
  price: 10,
};

function Cart() {
  const {
    addToCart,
    cartItems,
    clearCart,
    decreaseItem,
    toggleCart,
    isCartOpen,
  } = useCart();

  return (
    <div>
      <p>{isCartOpen ? "Open" : "Closed"}</p>
      <button onClick={() => toggleCart()}>Toggle</button>
      <button onClick={() => addToCart(item)}>Add</button>

      <button onClick={() => clearCart()}>Clear</button>
      <button onClick={() => decreaseItem("123", 1)}>Decrease</button>

      <p>{JSON.stringify(cartItems)}</p>
    </div>
  );
}

export default Cart;

:bulb: SSR Example

import { useCart, withSSR } from "cart";
import React from "react";

const item = {
  productId: "123",
  name: "Product 1",
  quantity: 1,
  price: 10,
};

function MyCart() {
  const cart = withSSR(useCart, (state) => state);

  const handleToggle = () => {
    cart?.toggleCart();
  };

  const itemadd = () => {
    cart?.addToCart(item);
  };

  return (
    <div>
      <p>{cart?.isCartOpen ? "Open" : "Closed"}</p>
      <button onClick={() => handleToggle()}>Toggle</button>
      <button onClick={() => itemadd()}>Add</button>

      <button onClick={() => cart?.clearCart()}>Clear</button>
      <button onClick={() => cart?.decreaseItem("123", 1)}>Decrease</button>

      <p>{JSON.stringify(cart?.cartItems)}</p>
    </div>
  );
}

export default MyCart;

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();
:pray: 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

💻 🖋 📖 🤔 🚇 🚧 📆 🔧
:green_heart: 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.

Keywords

FAQs

Package last updated on 27 Oct 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