@molecule/app-listing-card-react 3.0.0 → 4.1.0

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
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
3
  Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
4
  Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
5
  To change this document, edit the module-level JSDoc in src/index.ts.
6
- Generated: 2026-10-27T23:36:17.037Z
6
+ Generated: 2026-11-18T17:41:18.494Z
7
7
  -->
8
8
 
9
9
  # @molecule/app-listing-card-react
@@ -12,7 +12,7 @@ React product / property / listing card primitives.
12
12
 
13
13
  Exports:
14
14
 
15
- - `<ListingCard>` — outer Card shell. Props: `children`, `onClick?`,
15
+ - `<ListingCard>` — outer Card shell. Props: `children`, `toggle?`, `onClick?`,
16
16
  `className?`, `dataMolId?`.
17
17
  - `<ListingCardMedia>` — top image slot. Props: `src?`, `alt?`, `children?`
18
18
  (custom media node), `aspect?` (`'1/1' | '4/3' | '16/9' | '3/2'`, default
@@ -36,24 +36,27 @@ import {
36
36
  ListingCard,
37
37
  ListingCardMedia,
38
38
  ListingCardBody,
39
- ListingCardActions,
40
39
  ListingGrid,
41
40
  } from '@molecule/app-listing-card-react'
42
41
 
43
42
  ;<ListingGrid columns={3}>
44
- <ListingCard onClick={() => navigate(`/listings/${item.id}`)}>
43
+ <ListingCard
44
+ key={item.id}
45
+ onClick={() => navigate(`/listings/${item.id}`)}
46
+ toggle={{
47
+ value: { pressed: item.recommended, count: item.recommendations },
48
+ commit: async (desired) => {
49
+ const saved = await setRecommendation(item.id, desired)
50
+ return { pressed: saved.recommended, count: saved.recommendations }
51
+ },
52
+ activeLabel: t('listing.undoRecommendation', undefined, {
53
+ defaultValue: 'Undo recommendation',
54
+ }),
55
+ inactiveLabel: t('listing.recommend', undefined, { defaultValue: 'Recommend' }),
56
+ }}
57
+ >
45
58
  <ListingCardMedia src={item.imageUrl} aspect="4/3" alt={item.name} />
46
59
  <ListingCardBody title={item.name} subtitle={item.location} price={`$${item.price}/night`} />
47
- <ListingCardActions>
48
- <button
49
- onClick={(e) => {
50
- e.stopPropagation()
51
- saveListing(item.id)
52
- }}
53
- >
54
- Save
55
- </button>
56
- </ListingCardActions>
57
60
  </ListingCard>
58
61
  </ListingGrid>
59
62
  ```
@@ -73,6 +76,21 @@ npm install -D @types/react
73
76
 
74
77
  ### Interfaces
75
78
 
79
+ #### `ListingCardToggleProps`
80
+
81
+ A server-backed card action with the total number of people who selected it.
82
+
83
+ ```typescript
84
+ interface ListingCardToggleProps extends Omit<OptimisticToggleButtonProps, 'value' | 'commit'> {
85
+ /** The pressed state and count from the same loaded record. */
86
+ value: { pressed: boolean; count: number }
87
+ /** Saves the desired state and returns the server-confirmed state and count together. */
88
+ commit: (desired: boolean) => Promise<ListingCardToggleProps['value']>
89
+ /** Overrides the button's default `data-mol-id`. */
90
+ dataMolId?: string
91
+ }
92
+ ```
93
+
76
94
  #### `ListingCardProps`
77
95
 
78
96
  Props for {@link ListingCard}.
@@ -81,6 +99,8 @@ Props for {@link ListingCard}.
81
99
  interface ListingCardProps {
82
100
  /** Slots — usually `<ListingCardMedia>` + `<ListingCardBody>` + `<ListingCardActions>`. */
83
101
  children: ReactNode
102
+ /** Renders a counted native toggle after the content, outside any links in the content. */
103
+ toggle?: ListingCardToggleProps
84
104
  /**
85
105
  * Called on any click on the card — including clicks inside
86
106
  * `<ListingCardActions>`, which bubble into it unless the action handler
@@ -174,7 +194,13 @@ Use for product tiles, property cards, course cards, listings,
174
194
  and similar content-card layouts.
175
195
 
176
196
  ```typescript
177
- function ListingCard({ children, onClick, className, dataMolId }: ListingCardProps): JSX.Element
197
+ function ListingCard({
198
+ children,
199
+ toggle,
200
+ onClick,
201
+ className,
202
+ dataMolId,
203
+ }: ListingCardProps): JSX.Element
178
204
  ```
179
205
 
180
206
  - `props` — Component props (see {@link ListingCardProps}).
@@ -250,9 +276,9 @@ function ListingGrid({
250
276
 
251
277
  Peer dependencies:
252
278
 
253
- - `@molecule/app-react` ^3.0.0
279
+ - `@molecule/app-react` ^4.0.0
254
280
  - `@molecule/app-ui` ^1.0.1
255
- - `@molecule/app-ui-react` ^3.0.0
281
+ - `@molecule/app-ui-react` ^4.0.0
256
282
  - `react` ^18.0.0 || ^19.0.0
257
283
 
258
284
  ### Runtime Dependencies
@@ -264,7 +290,13 @@ Peer dependencies:
264
290
 
265
291
  - `ListingCard.onClick` is attached to the WHOLE card — clicks on buttons inside
266
292
  `<ListingCardActions>` bubble into it. Call `e.stopPropagation()` in every
267
- action handler (as in the example) or the card navigation fires too.
293
+ custom action handler or the card navigation fires too. The built-in
294
+ `toggle` handles that itself.
295
+ - Pass `toggle` for a server-backed action with a count, such as a vote or
296
+ endorsement. The card renders the button and count together, including zero.
297
+ Pass the loaded `{ pressed, count }` and return both saved fields from
298
+ `commit(desired)`. Keep the card mounted and keyed by record ID. Omit
299
+ `toggle` for cards without that action.
268
300
  - `<ListingGrid columns={n}>` renders a responsive n-column grid (via `cm.grid`):
269
301
  1 column on phones, stepping up to `n` at larger breakpoints, so listings
270
302
  don't overflow on mobile. Override `className` for a fixed grid.
@@ -1,8 +1,23 @@
1
1
  import type { JSX, ReactNode } from 'react';
2
+ import { type OptimisticToggleButtonProps } from '@molecule/app-ui-react';
3
+ /** A server-backed card action with the total number of people who selected it. */
4
+ export interface ListingCardToggleProps extends Omit<OptimisticToggleButtonProps, 'value' | 'commit'> {
5
+ /** The pressed state and count from the same loaded record. */
6
+ value: {
7
+ pressed: boolean;
8
+ count: number;
9
+ };
10
+ /** Saves the desired state and returns the server-confirmed state and count together. */
11
+ commit: (desired: boolean) => Promise<ListingCardToggleProps['value']>;
12
+ /** Overrides the button's default `data-mol-id`. */
13
+ dataMolId?: string;
14
+ }
2
15
  /** Props for {@link ListingCard}. */
3
16
  export interface ListingCardProps {
4
17
  /** Slots — usually `<ListingCardMedia>` + `<ListingCardBody>` + `<ListingCardActions>`. */
5
18
  children: ReactNode;
19
+ /** Renders a counted native toggle after the content, outside any links in the content. */
20
+ toggle?: ListingCardToggleProps;
6
21
  /**
7
22
  * Called on any click on the card — including clicks inside
8
23
  * `<ListingCardActions>`, which bubble into it unless the action handler
@@ -39,5 +54,5 @@ export interface ListingCardProps {
39
54
  * </ListingCard>
40
55
  * ```
41
56
  */
42
- export declare function ListingCard({ children, onClick, className, dataMolId, }: ListingCardProps): JSX.Element;
57
+ export declare function ListingCard({ children, toggle, onClick, className, dataMolId, }: ListingCardProps): JSX.Element;
43
58
  //# sourceMappingURL=ListingCard.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ListingCard.d.ts","sourceRoot":"","sources":["../src/ListingCard.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAK3C,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,2FAA2F;IAC3F,QAAQ,EAAE,SAAS,CAAA;IACnB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,uCAAuC;IACvC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,4CAA4C;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,CAAC,EAC1B,QAAQ,EACR,OAAO,EACP,SAAS,EACT,SAAS,GACV,EAAE,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAWhC"}
1
+ {"version":3,"file":"ListingCard.d.ts","sourceRoot":"","sources":["../src/ListingCard.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAI3C,OAAO,EAAgB,KAAK,2BAA2B,EAAE,MAAM,wBAAwB,CAAA;AAIvF,mFAAmF;AACnF,MAAM,WAAW,sBAAuB,SAAQ,IAAI,CAClD,2BAA2B,EAC3B,OAAO,GAAG,QAAQ,CACnB;IACC,+DAA+D;IAC/D,KAAK,EAAE;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;IAC1C,yFAAyF;IACzF,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,sBAAsB,CAAC,OAAO,CAAC,CAAC,CAAA;IACtE,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,2FAA2F;IAC3F,QAAQ,EAAE,SAAS,CAAA;IACnB,2FAA2F;IAC3F,MAAM,CAAC,EAAE,sBAAsB,CAAA;IAC/B;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,uCAAuC;IACvC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,4CAA4C;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAgDD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,CAAC,EAC1B,QAAQ,EACR,MAAM,EACN,OAAO,EACP,SAAS,EACT,SAAS,GACV,EAAE,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAchC"}
@@ -1,6 +1,29 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useOptimisticValue } from '@molecule/app-react';
2
3
  import { getClassMap } from '@molecule/app-ui';
3
- import { Card } from '@molecule/app-ui-react';
4
+ import { Button, Card } from '@molecule/app-ui-react';
5
+ import { ListingCardActions } from './ListingCardActions.js';
6
+ /**
7
+ * Shows both fields of a card action and saves the person's latest selection.
8
+ *
9
+ * @param props - Loaded value, save function, labels and button props.
10
+ * @returns The card action row.
11
+ */
12
+ function ListingCardToggle({ value, commit, onError, activeLabel, inactiveLabel, dataMolId = 'listing-card-toggle', variant, ...buttonProps }) {
13
+ const toggle = useOptimisticValue({
14
+ value,
15
+ commit: (next) => commit(next.pressed),
16
+ onError: (error) => onError?.(error),
17
+ });
18
+ const { pressed, count } = toggle.value;
19
+ return (_jsx(ListingCardActions, { children: _jsxs(Button, { "data-mol-id": dataMolId, ...buttonProps, type: "button", variant: variant ?? (pressed ? 'solid' : 'outline'), "aria-pressed": pressed, "aria-busy": toggle.pending, onClick: (event) => {
20
+ event.stopPropagation();
21
+ toggle.update((current) => ({
22
+ pressed: !current.pressed,
23
+ count: Math.max(0, current.count + (current.pressed ? -1 : 1)),
24
+ }));
25
+ }, children: [pressed ? activeLabel : inactiveLabel, " ", _jsx("span", { children: count })] }) }));
26
+ }
4
27
  /**
5
28
  * Product / property / listing card shell. Just a `<Card>` with vertical
6
29
  * stacking of its children — typically `<ListingCardMedia>` +
@@ -26,7 +49,7 @@ import { Card } from '@molecule/app-ui-react';
26
49
  * </ListingCard>
27
50
  * ```
28
51
  */
29
- export function ListingCard({ children, onClick, className, dataMolId, }) {
52
+ export function ListingCard({ children, toggle, onClick, className, dataMolId, }) {
30
53
  const cm = getClassMap();
31
- return (_jsx(Card, { "data-mol-id": dataMolId, onClick: onClick, className: cm.cn(onClick ? cm.cursorPointer : undefined, className), children: _jsx("div", { className: cm.stack(0), children: children }) }));
54
+ return (_jsx(Card, { "data-mol-id": dataMolId, onClick: onClick, className: cm.cn(onClick ? cm.cursorPointer : undefined, className), children: _jsxs("div", { className: cm.stack(0), children: [children, toggle && _jsx(ListingCardToggle, { ...toggle })] }) }));
32
55
  }
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * React product / property / listing card primitives.
3
3
  *
4
4
  * Exports:
5
- * - `<ListingCard>` — outer Card shell. Props: `children`, `onClick?`,
5
+ * - `<ListingCard>` — outer Card shell. Props: `children`, `toggle?`, `onClick?`,
6
6
  * `className?`, `dataMolId?`.
7
7
  * - `<ListingCardMedia>` — top image slot. Props: `src?`, `alt?`, `children?`
8
8
  * (custom media node), `aspect?` (`'1/1' | '4/3' | '16/9' | '3/2'`, default
@@ -18,7 +18,13 @@
18
18
  * @remarks
19
19
  * - `ListingCard.onClick` is attached to the WHOLE card — clicks on buttons inside
20
20
  * `<ListingCardActions>` bubble into it. Call `e.stopPropagation()` in every
21
- * action handler (as in the example) or the card navigation fires too.
21
+ * custom action handler or the card navigation fires too. The built-in
22
+ * `toggle` handles that itself.
23
+ * - Pass `toggle` for a server-backed action with a count, such as a vote or
24
+ * endorsement. The card renders the button and count together, including zero.
25
+ * Pass the loaded `{ pressed, count }` and return both saved fields from
26
+ * `commit(desired)`. Keep the card mounted and keyed by record ID. Omit
27
+ * `toggle` for cards without that action.
22
28
  * - `<ListingGrid columns={n}>` renders a responsive n-column grid (via `cm.grid`):
23
29
  * 1 column on phones, stepping up to `n` at larger breakpoints, so listings
24
30
  * don't overflow on mobile. Override `className` for a fixed grid.
@@ -29,15 +35,24 @@
29
35
  *
30
36
  * @example
31
37
  * ```tsx
32
- * import { ListingCard, ListingCardMedia, ListingCardBody, ListingCardActions, ListingGrid } from '@molecule/app-listing-card-react'
38
+ * import { ListingCard, ListingCardMedia, ListingCardBody, ListingGrid } from '@molecule/app-listing-card-react'
33
39
  *
34
40
  * <ListingGrid columns={3}>
35
- * <ListingCard onClick={() => navigate(`/listings/${item.id}`)}>
41
+ * <ListingCard
42
+ * key={item.id}
43
+ * onClick={() => navigate(`/listings/${item.id}`)}
44
+ * toggle={{
45
+ * value: { pressed: item.recommended, count: item.recommendations },
46
+ * commit: async (desired) => {
47
+ * const saved = await setRecommendation(item.id, desired)
48
+ * return { pressed: saved.recommended, count: saved.recommendations }
49
+ * },
50
+ * activeLabel: t('listing.undoRecommendation', undefined, { defaultValue: 'Undo recommendation' }),
51
+ * inactiveLabel: t('listing.recommend', undefined, { defaultValue: 'Recommend' }),
52
+ * }}
53
+ * >
36
54
  * <ListingCardMedia src={item.imageUrl} aspect="4/3" alt={item.name} />
37
55
  * <ListingCardBody title={item.name} subtitle={item.location} price={`$${item.price}/night`} />
38
- * <ListingCardActions>
39
- * <button onClick={(e) => { e.stopPropagation(); saveListing(item.id) }}>Save</button>
40
- * </ListingCardActions>
41
56
  * </ListingCard>
42
57
  * </ListingGrid>
43
58
  * ```
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AACvC,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AACvC,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA"}
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * React product / property / listing card primitives.
3
3
  *
4
4
  * Exports:
5
- * - `<ListingCard>` — outer Card shell. Props: `children`, `onClick?`,
5
+ * - `<ListingCard>` — outer Card shell. Props: `children`, `toggle?`, `onClick?`,
6
6
  * `className?`, `dataMolId?`.
7
7
  * - `<ListingCardMedia>` — top image slot. Props: `src?`, `alt?`, `children?`
8
8
  * (custom media node), `aspect?` (`'1/1' | '4/3' | '16/9' | '3/2'`, default
@@ -18,7 +18,13 @@
18
18
  * @remarks
19
19
  * - `ListingCard.onClick` is attached to the WHOLE card — clicks on buttons inside
20
20
  * `<ListingCardActions>` bubble into it. Call `e.stopPropagation()` in every
21
- * action handler (as in the example) or the card navigation fires too.
21
+ * custom action handler or the card navigation fires too. The built-in
22
+ * `toggle` handles that itself.
23
+ * - Pass `toggle` for a server-backed action with a count, such as a vote or
24
+ * endorsement. The card renders the button and count together, including zero.
25
+ * Pass the loaded `{ pressed, count }` and return both saved fields from
26
+ * `commit(desired)`. Keep the card mounted and keyed by record ID. Omit
27
+ * `toggle` for cards without that action.
22
28
  * - `<ListingGrid columns={n}>` renders a responsive n-column grid (via `cm.grid`):
23
29
  * 1 column on phones, stepping up to `n` at larger breakpoints, so listings
24
30
  * don't overflow on mobile. Override `className` for a fixed grid.
@@ -29,15 +35,24 @@
29
35
  *
30
36
  * @example
31
37
  * ```tsx
32
- * import { ListingCard, ListingCardMedia, ListingCardBody, ListingCardActions, ListingGrid } from '@molecule/app-listing-card-react'
38
+ * import { ListingCard, ListingCardMedia, ListingCardBody, ListingGrid } from '@molecule/app-listing-card-react'
33
39
  *
34
40
  * <ListingGrid columns={3}>
35
- * <ListingCard onClick={() => navigate(`/listings/${item.id}`)}>
41
+ * <ListingCard
42
+ * key={item.id}
43
+ * onClick={() => navigate(`/listings/${item.id}`)}
44
+ * toggle={{
45
+ * value: { pressed: item.recommended, count: item.recommendations },
46
+ * commit: async (desired) => {
47
+ * const saved = await setRecommendation(item.id, desired)
48
+ * return { pressed: saved.recommended, count: saved.recommendations }
49
+ * },
50
+ * activeLabel: t('listing.undoRecommendation', undefined, { defaultValue: 'Undo recommendation' }),
51
+ * inactiveLabel: t('listing.recommend', undefined, { defaultValue: 'Recommend' }),
52
+ * }}
53
+ * >
36
54
  * <ListingCardMedia src={item.imageUrl} aspect="4/3" alt={item.name} />
37
55
  * <ListingCardBody title={item.name} subtitle={item.location} price={`$${item.price}/night`} />
38
- * <ListingCardActions>
39
- * <button onClick={(e) => { e.stopPropagation(); saveListing(item.id) }}>Save</button>
40
- * </ListingCardActions>
41
56
  * </ListingCard>
42
57
  * </ListingGrid>
43
58
  * ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-listing-card-react",
3
- "version": "3.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "React listing/product-card primitives with media, body, actions slots",
5
5
  "homepage": "https://www.molecule.dev/packages/app-listing-card-react",
6
6
  "type": "module",
@@ -33,15 +33,15 @@
33
33
  "directory": "packages/app/features/listing-card-react"
34
34
  },
35
35
  "peerDependencies": {
36
- "@molecule/app-react": "^3.0.0",
36
+ "@molecule/app-react": "^4.0.0",
37
37
  "@molecule/app-ui": "^1.0.1",
38
- "@molecule/app-ui-react": "^3.0.0",
38
+ "@molecule/app-ui-react": "^4.0.0",
39
39
  "react": "^18.0.0 || ^19.0.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@molecule/app-react": "3.0.0",
43
- "@molecule/app-ui": "1.3.8",
44
- "@molecule/app-ui-react": "3.0.0",
42
+ "@molecule/app-react": "4.8.0",
43
+ "@molecule/app-ui": "1.4.0",
44
+ "@molecule/app-ui-react": "4.1.6",
45
45
  "@types/node": "26.6.5",
46
46
  "@types/react": "19.3.0",
47
47
  "@types/react-dom": "19.3.0",