Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcnaveen/cart
🛒 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.
https://github.com/mcnaveen/cart
cart cart-functionality cart-items cart-management hacktoberfest headless headless-cart react-cart
Last synced: 12 days ago
JSON representation
🛒 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.
- Host: GitHub
- URL: https://github.com/mcnaveen/cart
- Owner: mcnaveen
- License: mit
- Created: 2023-09-22T17:29:04.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-27T06:32:28.000Z (about 1 year ago)
- Last Synced: 2024-09-19T14:19:09.607Z (2 months ago)
- Topics: cart, cart-functionality, cart-items, cart-management, hacktoberfest, headless, headless-cart, react-cart
- Language: TypeScript
- Homepage: https://cart.js.org
- Size: 361 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Cart
Headless cart management library
![npm i cart](image/cover.png)
> ⚠️ Expect some breaking changes, Use at your own risk
### 🛒 Demo
- [View React Demo](https://stackblitz-starters-dt9zzc.stackblitz.io/)
- [Nextjs App Directory Demo](https://stackblitzstarterskcruvd-uufb--3000--d6c42aca.local-credentialless.webcontainer.io/)### :package: Requirements
- React or Nextjs Project ⚛️
### :sparkles: Installation
- Install using the below command (with your package manager of choice)
```bash
# npm
npm install cart --save# yarn
yarn add cart#pnpm
pnpm add cart# bun
bun install cart```
---
### :bulb: Usage Example
```jsx
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 (
{isCartOpen ? "Open" : "Closed"}
toggleCart()}>Toggle
addToCart(item)}>AddclearCart()}>Clear
decreaseItem("123", 1)}>Decrease
{JSON.stringify(cartItems)}
);
}export default Cart;
```### :bulb: SSR Example
```jsx
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 (
{cart?.isCartOpen ? "Open" : "Closed"}
handleToggle()}>Toggle
itemadd()}>Addcart?.clearCart()}>Clear
cart?.decreaseItem("123", 1)}>Decrease
{JSON.stringify(cart?.cartItems)}
);
}export default MyCart;
```---
### API Reference
| Name | Type | Default Value | Description | Example |
| ---------------- | ---------- | ------------- | -------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `isCartOpen` | `boolean` | `false` | Indicates whether the cart is open or not. | `isCartOpen ? "Yes" : "No"` |
| `toggleCart` | `function` | - | Toggles the visibility of the shopping cart. | `toggleCart();` |
| `openCart` | `function` | - | Sets the cart open state to `true`. | `openCart();` |
| `closeCart` | `function` | - | Sets the cart open state to `false`. | `closeCart();` |
| `cartItems` | `array` | `[]` | An array of items in the cart. | `cartItems.map((item) => ())` |{item.name}
Quantity: {item.quantity}
Price: ${item.price}
| `addToCart` | `function` | - | 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 });` |
| `decreaseItem` | `function` | - | Decreases the quantity of an item in the shopping cart or removes it if the quantity becomes zero. | `decreaseItem('product1', 1);` |
| `removeFromCart` | `function` | - | Removes an item from the shopping cart. | `removeFromCart('product1');` |
| `clearCart` | `function` | - | Clears all items from the shopping cart. | `clearCart();` |#### :pray: Credits
Huge thanks to [Peter Krumins](https://github.com/pkrumins) for the package name `cart`.
Please checkout [Browserling](https://www.browserling.com/) - Online cross-browser testing platform.(Btw, This is not a sponsored message, Just my way of saying thank you)
#### Contributors
Josh Goldberg ✨
🔧
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](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).