cartly 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # cartly 🛒
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/cartly.svg)](https://www.npmjs.com/package/cartly)
4
+ [![license](https://img.shields.io/npm/l/cartly.svg)](https://github.com/sncorreia/cartly/blob/main/LICENSE)
5
+
3
6
  **cartly** is a lightweight, type-safe React Cart Context library built with modern hooks.
4
7
  It’s designed to be **generic**, **framework-agnostic**, and easy to integrate into any React application — from e‑commerce stores to internal tools.
5
8
 
@@ -115,6 +118,25 @@ Call this once per app, export the result, and reuse it everywhere.
115
118
 
116
119
  💡 Pass storage={null} to disable persistence (recommended for SSR/tests).
117
120
 
121
+ #### Custom Storage (SSR/Next.js)
122
+
123
+ For SSR frameworks like Next.js, you can create a custom no-op storage adapter:
124
+
125
+ ```TypeScript
126
+ import type { CartStorage } from "cartly";
127
+
128
+ const noopStorage: CartStorage = {
129
+ getItem: () => null,
130
+ setItem: () => {},
131
+ removeItem: () => {},
132
+ };
133
+
134
+ // Usage
135
+ <CartProvider storageKey="cart" storage={noopStorage}>
136
+ {children}
137
+ </CartProvider>
138
+ ```
139
+
118
140
  ### useCart()
119
141
 
120
142
  ```TypeScript
@@ -128,6 +150,17 @@ const {
128
150
  } = useCart();
129
151
  ```
130
152
 
153
+ #### Return Values
154
+
155
+ | Return | Type | Description |
156
+ | -------------------- | -------------------------------------------------- | ---------------------------------------- |
157
+ | `cartItems` | `CartItem<T>[]` | Current cart items |
158
+ | `addItemToCart` | `(item: T) => void` | Add item (increments quantity if exists) |
159
+ | `updateItemQuantity` | `(id: string \| number, quantity: number) => void` | Set item quantity |
160
+ | `removeItemFromCart` | `(id: string \| number) => void` | Remove item from cart |
161
+ | `clearCart` | `() => void` | Empty the cart |
162
+ | `cartCount` | `number` | Total quantity of all items |
163
+
131
164
  ### 🧾 Cart Item Shape
132
165
 
133
166
  ```TypeScript
@@ -159,6 +192,29 @@ cartly/
159
192
 
160
193
  MIT
161
194
 
195
+ ## ❓ FAQ
196
+
197
+ ### Why is my cart empty on page refresh?
198
+
199
+ Make sure you're passing the correct `storageKey` and `storage` props to `CartProvider`. If `storage` is `null` or not provided, persistence is disabled.
200
+
201
+ ### How do I use this with Next.js or other SSR frameworks?
202
+
203
+ Use `storage={null}` to disable persistence during SSR, or provide a custom storage adapter (see [Custom Storage](#custom-storage-ssrnextjs) section above).
204
+
205
+ ### Can I have multiple carts in the same app?
206
+
207
+ Yes! Call `createCartContext<T>()` multiple times with different types and use separate providers:
208
+
209
+ ```TypeScript
210
+ export const { CartProvider: ProductCartProvider, useCart: useProductCart } = createCartContext<Product>();
211
+ export const { CartProvider: WishlistProvider, useCart: useWishlist } = createCartContext<WishlistItem>();
212
+ ```
213
+
214
+ ### Why does my item quantity not update?
215
+
216
+ Ensure your item has a unique `id` property. The cart uses `id` to identify and update items.
217
+
162
218
  ## 🤝 Contributing
163
219
 
164
220
  Issues and PRs are welcome.
@@ -1,9 +1,26 @@
1
1
  import { CartContextType, CartStorage } from './types';
2
+ /** Props for the CartProvider component */
2
3
  type Props = {
4
+ /** Child components that will have access to the cart context */
3
5
  children: React.ReactNode;
6
+ /** Key used for persisting cart data in storage (default: "cart") */
4
7
  storageKey?: string;
8
+ /** Storage mechanism for cart persistence (default: localStorage) */
5
9
  storage?: CartStorage;
6
10
  };
11
+ /**
12
+ * Factory function that creates a typed cart context and its associated hooks/providers.
13
+ * This pattern allows for type-safe cart items with custom properties.
14
+ *
15
+ * @template T - The type of items in the cart, must have an `id` property
16
+ * @returns An object containing the CartProvider component and useCart hook
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * type Product = { id: string; name: string; price: number };
21
+ * const { CartProvider, useCart } = createCartContext<Product>();
22
+ * ```
23
+ */
7
24
  export declare function createCartContext<T extends {
8
25
  id: string | number;
9
26
  }>(): {
@@ -1,2 +1,7 @@
1
+ /**
2
+ * Cartly - Generic, type-safe Cart Context & hooks for React
3
+ *
4
+ * @packageDocumentation
5
+ */
1
6
  export { createCartContext } from './CartProvider';
2
7
  export type { CartContextType, CartItem, CartStorage } from './types';
@@ -1,16 +1,31 @@
1
+ /**
2
+ * Represents an item in the cart, extending the base item type with a quantity.
3
+ * @template T - The base item type, must have an `id` property
4
+ */
1
5
  export type CartItem<T extends {
2
6
  id: string | number;
3
7
  }> = T & {
8
+ /** Number of this item in the cart */
4
9
  quantity: number;
5
10
  };
11
+ /**
12
+ * The shape of the cart context, providing state and operations for cart management.
13
+ * @template T - The base item type, must have an `id` property
14
+ */
6
15
  export type CartContextType<T extends {
7
16
  id: string | number;
8
17
  }> = {
18
+ /** Array of all items currently in the cart */
9
19
  cartItems: CartItem<T>[];
20
+ /** Adds an item to the cart or increments quantity if it already exists */
10
21
  addItemToCart: (item: T, quantity?: number) => void;
22
+ /** Updates the quantity of a specific item by its id */
11
23
  updateItemQuantity: (id: string | number, quantity: number) => void;
24
+ /** Removes an item completely from the cart by its id */
12
25
  removeItemFromCart: (id: string | number) => void;
26
+ /** Clears all items from the cart */
13
27
  clearCart: () => void;
28
+ /** Total count of all items in the cart (sum of quantities) */
14
29
  cartCount: number;
15
30
  };
16
31
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cartly",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Generic, type-safe Cart Context & hooks for React",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",