npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cart

v1.1.2

Published

Headless cart management library

Downloads

160

Readme

npm i cart

⚠️ 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

| 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) => ( <div key={item.productId}> <p>{item.name}</p> <p>Quantity: {item.quantity}</p> <p>Price: ${item.price}</p> </div> )) | | 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 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

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