@molecule/app-vote-cluster-react 1.0.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/LICENSE +115 -0
- package/README.md +190 -0
- package/dist/VoteCluster.d.ts +20 -0
- package/dist/VoteCluster.d.ts.map +1 -0
- package/dist/VoteCluster.js +127 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/types.d.ts +57 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T00:41:39.580Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-vote-cluster-react
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Reddit/HN-style vote cluster — stacked up/down arrows + score readout.
|
|
16
|
+
|
|
17
|
+
Used by link-aggregator and news-aggregator templates, and composable
|
|
18
|
+
inside `@molecule/app-forum-thread-row-react`. Toggle semantics handle
|
|
19
|
+
upvote, downvote, and "click again to clear" optimistic flows. All UI
|
|
20
|
+
text flows through `t()` with translations supplied by the companion
|
|
21
|
+
`@molecule/app-locales-vote-cluster` package.
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { useState } from 'react'
|
|
27
|
+
|
|
28
|
+
import { VoteCluster, type VoteValue } from '@molecule/app-vote-cluster-react'
|
|
29
|
+
|
|
30
|
+
function PostRow({ post }: { post: { id: string; score: number; myVote?: VoteValue } }) {
|
|
31
|
+
const [score, setScore] = useState(post.score)
|
|
32
|
+
const [myVote, setMyVote] = useState<VoteValue>(post.myVote ?? 0)
|
|
33
|
+
return (
|
|
34
|
+
<VoteCluster
|
|
35
|
+
score={score}
|
|
36
|
+
myVote={myVote}
|
|
37
|
+
onVote={(next) => {
|
|
38
|
+
setScore(score - myVote + next)
|
|
39
|
+
setMyVote(next)
|
|
40
|
+
void fetch(`/api/posts/${post.id}/vote`, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'Content-Type': 'application/json' },
|
|
43
|
+
body: JSON.stringify({ vote: next }),
|
|
44
|
+
})
|
|
45
|
+
}}
|
|
46
|
+
/>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Type
|
|
52
|
+
|
|
53
|
+
`feature`
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install @molecule/app-vote-cluster-react @molecule/app-i18n @molecule/app-react @molecule/app-ui react
|
|
59
|
+
npm install -D @types/react
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## API
|
|
63
|
+
|
|
64
|
+
### Interfaces
|
|
65
|
+
|
|
66
|
+
#### `VoteClusterProps`
|
|
67
|
+
|
|
68
|
+
Props for {@link VoteCluster}.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
interface VoteClusterProps {
|
|
72
|
+
/** Current score (number to display between the arrows). */
|
|
73
|
+
score: number
|
|
74
|
+
/**
|
|
75
|
+
* The viewer's current vote.
|
|
76
|
+
*
|
|
77
|
+
* Pass this prop along with `onVote` to use the cluster in **controlled**
|
|
78
|
+
* mode (parent owns state). Omit to use **uncontrolled** mode (the cluster
|
|
79
|
+
* tracks the active vote internally).
|
|
80
|
+
*/
|
|
81
|
+
myVote?: VoteValue
|
|
82
|
+
/**
|
|
83
|
+
* Initial vote value used in uncontrolled mode.
|
|
84
|
+
*
|
|
85
|
+
* Ignored when `myVote` is provided.
|
|
86
|
+
*/
|
|
87
|
+
defaultVote?: VoteValue
|
|
88
|
+
/**
|
|
89
|
+
* Called whenever the viewer's vote toggles.
|
|
90
|
+
*
|
|
91
|
+
* Toggle semantics:
|
|
92
|
+
* - Clicking up while `myVote === 1` → emits `0` (clears).
|
|
93
|
+
* - Clicking up while `myVote === -1 | 0` → emits `1`.
|
|
94
|
+
* - Clicking down while `myVote === -1` → emits `0` (clears).
|
|
95
|
+
* - Clicking down while `myVote === 1 | 0` → emits `-1`.
|
|
96
|
+
*
|
|
97
|
+
* Parents are expected to update the score accordingly (the component
|
|
98
|
+
* itself does not mutate the `score` prop).
|
|
99
|
+
*/
|
|
100
|
+
onVote: (next: VoteValue) => void
|
|
101
|
+
/** Disables both arrow buttons. */
|
|
102
|
+
disabled?: boolean
|
|
103
|
+
/** Layout direction. Defaults to `'vertical'` (Reddit-style stacking). */
|
|
104
|
+
direction?: VoteClusterDirection
|
|
105
|
+
/** Optional accessible label for the whole cluster. */
|
|
106
|
+
ariaLabel?: string
|
|
107
|
+
/** Optional `data-mol-id` for AI-agent automation. */
|
|
108
|
+
dataMolId?: string
|
|
109
|
+
/** Extra classes appended via the ClassMap `cn()` helper. */
|
|
110
|
+
className?: string
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Types
|
|
115
|
+
|
|
116
|
+
#### `VoteClusterDirection`
|
|
117
|
+
|
|
118
|
+
Layout direction for the vote cluster.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
type VoteClusterDirection = 'vertical' | 'horizontal'
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### `VoteValue`
|
|
125
|
+
|
|
126
|
+
Allowed vote values:
|
|
127
|
+
|
|
128
|
+
- `1` — upvoted
|
|
129
|
+
- `-1` — downvoted
|
|
130
|
+
- `0` — no vote (cleared)
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
type VoteValue = 1 | -1 | 0
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Functions
|
|
137
|
+
|
|
138
|
+
#### `VoteCluster(props)`
|
|
139
|
+
|
|
140
|
+
Reddit/HN-style stacked up-arrow + score + down-arrow cluster.
|
|
141
|
+
|
|
142
|
+
Renders two arrow buttons with a score readout between them. Click
|
|
143
|
+
semantics follow link-aggregator convention (toggling the same arrow
|
|
144
|
+
clears the vote; clicking the opposite arrow swaps it). The score is
|
|
145
|
+
**not** mutated by the cluster — parents own that calculation and pass
|
|
146
|
+
the new score back via `score` after handling `onVote`.
|
|
147
|
+
|
|
148
|
+
Styling is delegated entirely to `getClassMap()`. The active arrow is
|
|
149
|
+
painted via a CSS custom property (`--mol-color-*`) — no Tailwind /
|
|
150
|
+
raw class strings escape this component.
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
function VoteCluster({
|
|
154
|
+
score,
|
|
155
|
+
myVote,
|
|
156
|
+
defaultVote = 0,
|
|
157
|
+
onVote,
|
|
158
|
+
disabled,
|
|
159
|
+
direction = 'vertical',
|
|
160
|
+
ariaLabel,
|
|
161
|
+
dataMolId,
|
|
162
|
+
className,
|
|
163
|
+
}: VoteClusterProps): JSX.Element
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
- `props` — Component props.
|
|
167
|
+
|
|
168
|
+
**Returns:** The rendered cluster element.
|
|
169
|
+
|
|
170
|
+
## Injection Notes
|
|
171
|
+
|
|
172
|
+
### Requirements
|
|
173
|
+
|
|
174
|
+
Peer dependencies:
|
|
175
|
+
|
|
176
|
+
- `@molecule/app-i18n` ^1.0.0
|
|
177
|
+
- `@molecule/app-react` ^1.0.0
|
|
178
|
+
- `@molecule/app-ui` ^1.0.0
|
|
179
|
+
- `react` ^18.0.0 || ^19.0.0
|
|
180
|
+
|
|
181
|
+
### Runtime Dependencies
|
|
182
|
+
|
|
183
|
+
- `@molecule/app-i18n`
|
|
184
|
+
- `@molecule/app-react`
|
|
185
|
+
- `@molecule/app-ui`
|
|
186
|
+
- `react`
|
|
187
|
+
|
|
188
|
+
## Translations
|
|
189
|
+
|
|
190
|
+
Translation strings are provided by `@molecule/app-locales-vote-cluster`.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { JSX } from 'react';
|
|
2
|
+
import type { VoteClusterProps } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Reddit/HN-style stacked up-arrow + score + down-arrow cluster.
|
|
5
|
+
*
|
|
6
|
+
* Renders two arrow buttons with a score readout between them. Click
|
|
7
|
+
* semantics follow link-aggregator convention (toggling the same arrow
|
|
8
|
+
* clears the vote; clicking the opposite arrow swaps it). The score is
|
|
9
|
+
* **not** mutated by the cluster — parents own that calculation and pass
|
|
10
|
+
* the new score back via `score` after handling `onVote`.
|
|
11
|
+
*
|
|
12
|
+
* Styling is delegated entirely to `getClassMap()`. The active arrow is
|
|
13
|
+
* painted via a CSS custom property (`--mol-color-*`) — no Tailwind /
|
|
14
|
+
* raw class strings escape this component.
|
|
15
|
+
*
|
|
16
|
+
* @param props - Component props.
|
|
17
|
+
* @returns The rendered cluster element.
|
|
18
|
+
*/
|
|
19
|
+
export declare function VoteCluster({ score, myVote, defaultVote, onVote, disabled, direction, ariaLabel, dataMolId, className, }: VoteClusterProps): JSX.Element;
|
|
20
|
+
//# sourceMappingURL=VoteCluster.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VoteCluster.d.ts","sourceRoot":"","sources":["../src/VoteCluster.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,GAAG,EAAE,MAAM,OAAO,CAAA;AAM/C,OAAO,KAAK,EAAE,gBAAgB,EAAa,MAAM,YAAY,CAAA;AAoD7D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,EAC1B,KAAK,EACL,MAAM,EACN,WAAe,EACf,MAAM,EACN,QAAQ,EACR,SAAsB,EACtB,SAAS,EACT,SAAS,EACT,SAAS,GACV,EAAE,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAkHhC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useState } from 'react';
|
|
3
|
+
import { useTranslation } from '@molecule/app-react';
|
|
4
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
5
|
+
/**
|
|
6
|
+
* Computes the next vote value for a click on the up or down arrow.
|
|
7
|
+
*
|
|
8
|
+
* Toggle rules:
|
|
9
|
+
* - Up clicked while currently up → `0` (clear).
|
|
10
|
+
* - Up clicked otherwise → `1`.
|
|
11
|
+
* - Down clicked while currently down → `0` (clear).
|
|
12
|
+
* - Down clicked otherwise → `-1`.
|
|
13
|
+
*
|
|
14
|
+
* @param current - The viewer's current vote.
|
|
15
|
+
* @param direction - Which arrow was clicked (`1` = up, `-1` = down).
|
|
16
|
+
* @returns The next vote value to emit.
|
|
17
|
+
*/
|
|
18
|
+
function nextVote(current, direction) {
|
|
19
|
+
if (current === direction)
|
|
20
|
+
return 0;
|
|
21
|
+
return direction;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Inline arrow glyphs. We use SVG paths rather than icon-font glyphs so the
|
|
25
|
+
* cluster works in environments without an icon font (and so the active
|
|
26
|
+
* state can drive `currentColor`).
|
|
27
|
+
*
|
|
28
|
+
* @param root0 - Component props.
|
|
29
|
+
* @param root0.size - Pixel size of the arrow.
|
|
30
|
+
* @returns The rendered up-arrow SVG.
|
|
31
|
+
*/
|
|
32
|
+
function UpArrow({ size }) {
|
|
33
|
+
return (_jsx("svg", { width: size, height: size, viewBox: "0 0 24 24", "aria-hidden": true, focusable: false, children: _jsx("path", { d: "M12 5l7 9H5l7-9z", fill: "currentColor" }) }));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Inline down-arrow SVG, mirroring UpArrow.
|
|
37
|
+
*
|
|
38
|
+
* @param root0 - Component props.
|
|
39
|
+
* @param root0.size - Pixel size of the arrow.
|
|
40
|
+
* @returns The rendered down-arrow SVG.
|
|
41
|
+
*/
|
|
42
|
+
function DownArrow({ size }) {
|
|
43
|
+
return (_jsx("svg", { width: size, height: size, viewBox: "0 0 24 24", "aria-hidden": true, focusable: false, children: _jsx("path", { d: "M12 19l-7-9h14l-7 9z", fill: "currentColor" }) }));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Reddit/HN-style stacked up-arrow + score + down-arrow cluster.
|
|
47
|
+
*
|
|
48
|
+
* Renders two arrow buttons with a score readout between them. Click
|
|
49
|
+
* semantics follow link-aggregator convention (toggling the same arrow
|
|
50
|
+
* clears the vote; clicking the opposite arrow swaps it). The score is
|
|
51
|
+
* **not** mutated by the cluster — parents own that calculation and pass
|
|
52
|
+
* the new score back via `score` after handling `onVote`.
|
|
53
|
+
*
|
|
54
|
+
* Styling is delegated entirely to `getClassMap()`. The active arrow is
|
|
55
|
+
* painted via a CSS custom property (`--mol-color-*`) — no Tailwind /
|
|
56
|
+
* raw class strings escape this component.
|
|
57
|
+
*
|
|
58
|
+
* @param props - Component props.
|
|
59
|
+
* @returns The rendered cluster element.
|
|
60
|
+
*/
|
|
61
|
+
export function VoteCluster({ score, myVote, defaultVote = 0, onVote, disabled, direction = 'vertical', ariaLabel, dataMolId, className, }) {
|
|
62
|
+
const cm = getClassMap();
|
|
63
|
+
const { t } = useTranslation();
|
|
64
|
+
// Controlled if `myVote` is supplied, otherwise uncontrolled.
|
|
65
|
+
const controlled = myVote !== undefined;
|
|
66
|
+
const [internal, setInternal] = useState(defaultVote);
|
|
67
|
+
const active = controlled ? myVote : internal;
|
|
68
|
+
const emit = useCallback((dir) => {
|
|
69
|
+
if (disabled)
|
|
70
|
+
return;
|
|
71
|
+
const next = nextVote(active, dir);
|
|
72
|
+
if (!controlled)
|
|
73
|
+
setInternal(next);
|
|
74
|
+
onVote(next);
|
|
75
|
+
}, [active, controlled, disabled, onVote]);
|
|
76
|
+
const upPressed = active === 1;
|
|
77
|
+
const downPressed = active === -1;
|
|
78
|
+
const groupLabel = ariaLabel ??
|
|
79
|
+
t('vote-cluster.group', { score }, { defaultValue: 'Vote cluster, current score {{score}}' });
|
|
80
|
+
const upLabel = upPressed
|
|
81
|
+
? t('vote-cluster.removeUpvote', {}, { defaultValue: 'Remove upvote' })
|
|
82
|
+
: t('vote-cluster.upvote', {}, { defaultValue: 'Upvote' });
|
|
83
|
+
const downLabel = downPressed
|
|
84
|
+
? t('vote-cluster.removeDownvote', {}, { defaultValue: 'Remove downvote' })
|
|
85
|
+
: t('vote-cluster.downvote', {}, { defaultValue: 'Downvote' });
|
|
86
|
+
const scoreLabel = t('vote-cluster.score', { score }, { defaultValue: 'Score: {{score}}' });
|
|
87
|
+
// Inline styles cover only what ClassMap can't express: the bare
|
|
88
|
+
// button reset + per-state `color` driven by `--mol-color-*` tokens
|
|
89
|
+
// and the smooth color transition on vote change.
|
|
90
|
+
const buttonBase = {
|
|
91
|
+
background: 'none',
|
|
92
|
+
border: 'none',
|
|
93
|
+
padding: 0,
|
|
94
|
+
cursor: disabled ? 'not-allowed' : 'pointer',
|
|
95
|
+
opacity: disabled ? 0.5 : 1,
|
|
96
|
+
display: 'inline-flex',
|
|
97
|
+
alignItems: 'center',
|
|
98
|
+
justifyContent: 'center',
|
|
99
|
+
transition: 'color 150ms ease, transform 150ms ease',
|
|
100
|
+
lineHeight: 0,
|
|
101
|
+
};
|
|
102
|
+
const upStyle = {
|
|
103
|
+
...buttonBase,
|
|
104
|
+
color: upPressed ? 'var(--mol-color-primary)' : 'var(--mol-color-on-surface-variant)',
|
|
105
|
+
transform: upPressed ? 'scale(1.1)' : 'scale(1)',
|
|
106
|
+
};
|
|
107
|
+
const downStyle = {
|
|
108
|
+
...buttonBase,
|
|
109
|
+
color: downPressed ? 'var(--mol-color-info)' : 'var(--mol-color-on-surface-variant)',
|
|
110
|
+
transform: downPressed ? 'scale(1.1)' : 'scale(1)',
|
|
111
|
+
};
|
|
112
|
+
const scoreStyle = {
|
|
113
|
+
transition: 'color 150ms ease',
|
|
114
|
+
color: upPressed
|
|
115
|
+
? 'var(--mol-color-primary)'
|
|
116
|
+
: downPressed
|
|
117
|
+
? 'var(--mol-color-info)'
|
|
118
|
+
: 'inherit',
|
|
119
|
+
fontVariantNumeric: 'tabular-nums',
|
|
120
|
+
minWidth: '2ch',
|
|
121
|
+
textAlign: 'center',
|
|
122
|
+
};
|
|
123
|
+
const wrapperClass = cm.cn(direction === 'vertical'
|
|
124
|
+
? cm.flex({ direction: 'col', align: 'center', justify: 'center', gap: 'xs' })
|
|
125
|
+
: cm.flex({ align: 'center', justify: 'center', gap: 'xs' }), className);
|
|
126
|
+
return (_jsxs("div", { className: wrapperClass, role: "group", "aria-label": groupLabel, "data-mol-id": dataMolId, children: [_jsx("button", { type: "button", onClick: () => emit(1), disabled: disabled, "aria-label": upLabel, "aria-pressed": upPressed, "data-vote": "up", style: upStyle, children: _jsx(UpArrow, { size: 20 }) }), _jsx("span", { className: cm.cn(cm.textSize('sm'), cm.fontWeight('semibold')), "aria-label": scoreLabel, "aria-live": "polite", "data-vote": "score", style: scoreStyle, children: score }), _jsx("button", { type: "button", onClick: () => emit(-1), disabled: disabled, "aria-label": downLabel, "aria-pressed": downPressed, "data-vote": "down", style: downStyle, children: _jsx(DownArrow, { size: 20 }) })] }));
|
|
127
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reddit/HN-style vote cluster — stacked up/down arrows + score readout.
|
|
3
|
+
*
|
|
4
|
+
* Used by link-aggregator and news-aggregator templates, and composable
|
|
5
|
+
* inside `@molecule/app-forum-thread-row-react`. Toggle semantics handle
|
|
6
|
+
* upvote, downvote, and "click again to clear" optimistic flows. All UI
|
|
7
|
+
* text flows through `t()` with translations supplied by the companion
|
|
8
|
+
* `@molecule/app-locales-vote-cluster` package.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* import { useState } from 'react'
|
|
13
|
+
*
|
|
14
|
+
* import { VoteCluster, type VoteValue } from '@molecule/app-vote-cluster-react'
|
|
15
|
+
*
|
|
16
|
+
* function PostRow({ post }: { post: { id: string; score: number; myVote?: VoteValue } }) {
|
|
17
|
+
* const [score, setScore] = useState(post.score)
|
|
18
|
+
* const [myVote, setMyVote] = useState<VoteValue>(post.myVote ?? 0)
|
|
19
|
+
* return (
|
|
20
|
+
* <VoteCluster
|
|
21
|
+
* score={score}
|
|
22
|
+
* myVote={myVote}
|
|
23
|
+
* onVote={(next) => {
|
|
24
|
+
* setScore(score - myVote + next)
|
|
25
|
+
* setMyVote(next)
|
|
26
|
+
* void fetch(`/api/posts/${post.id}/vote`, {
|
|
27
|
+
* method: 'POST',
|
|
28
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
29
|
+
* body: JSON.stringify({ vote: next }),
|
|
30
|
+
* })
|
|
31
|
+
* }}
|
|
32
|
+
* />
|
|
33
|
+
* )
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @module
|
|
38
|
+
*/
|
|
39
|
+
export * from './types.js';
|
|
40
|
+
export * from './VoteCluster.js';
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reddit/HN-style vote cluster — stacked up/down arrows + score readout.
|
|
3
|
+
*
|
|
4
|
+
* Used by link-aggregator and news-aggregator templates, and composable
|
|
5
|
+
* inside `@molecule/app-forum-thread-row-react`. Toggle semantics handle
|
|
6
|
+
* upvote, downvote, and "click again to clear" optimistic flows. All UI
|
|
7
|
+
* text flows through `t()` with translations supplied by the companion
|
|
8
|
+
* `@molecule/app-locales-vote-cluster` package.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* import { useState } from 'react'
|
|
13
|
+
*
|
|
14
|
+
* import { VoteCluster, type VoteValue } from '@molecule/app-vote-cluster-react'
|
|
15
|
+
*
|
|
16
|
+
* function PostRow({ post }: { post: { id: string; score: number; myVote?: VoteValue } }) {
|
|
17
|
+
* const [score, setScore] = useState(post.score)
|
|
18
|
+
* const [myVote, setMyVote] = useState<VoteValue>(post.myVote ?? 0)
|
|
19
|
+
* return (
|
|
20
|
+
* <VoteCluster
|
|
21
|
+
* score={score}
|
|
22
|
+
* myVote={myVote}
|
|
23
|
+
* onVote={(next) => {
|
|
24
|
+
* setScore(score - myVote + next)
|
|
25
|
+
* setMyVote(next)
|
|
26
|
+
* void fetch(`/api/posts/${post.id}/vote`, {
|
|
27
|
+
* method: 'POST',
|
|
28
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
29
|
+
* body: JSON.stringify({ vote: next }),
|
|
30
|
+
* })
|
|
31
|
+
* }}
|
|
32
|
+
* />
|
|
33
|
+
* )
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @module
|
|
38
|
+
*/
|
|
39
|
+
export * from './types.js';
|
|
40
|
+
export * from './VoteCluster.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for `<VoteCluster>`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Allowed vote values:
|
|
8
|
+
* - `1` — upvoted
|
|
9
|
+
* - `-1` — downvoted
|
|
10
|
+
* - `0` — no vote (cleared)
|
|
11
|
+
*/
|
|
12
|
+
export type VoteValue = 1 | -1 | 0;
|
|
13
|
+
/** Layout direction for the vote cluster. */
|
|
14
|
+
export type VoteClusterDirection = 'vertical' | 'horizontal';
|
|
15
|
+
/** Props for {@link VoteCluster}. */
|
|
16
|
+
export interface VoteClusterProps {
|
|
17
|
+
/** Current score (number to display between the arrows). */
|
|
18
|
+
score: number;
|
|
19
|
+
/**
|
|
20
|
+
* The viewer's current vote.
|
|
21
|
+
*
|
|
22
|
+
* Pass this prop along with `onVote` to use the cluster in **controlled**
|
|
23
|
+
* mode (parent owns state). Omit to use **uncontrolled** mode (the cluster
|
|
24
|
+
* tracks the active vote internally).
|
|
25
|
+
*/
|
|
26
|
+
myVote?: VoteValue;
|
|
27
|
+
/**
|
|
28
|
+
* Initial vote value used in uncontrolled mode.
|
|
29
|
+
*
|
|
30
|
+
* Ignored when `myVote` is provided.
|
|
31
|
+
*/
|
|
32
|
+
defaultVote?: VoteValue;
|
|
33
|
+
/**
|
|
34
|
+
* Called whenever the viewer's vote toggles.
|
|
35
|
+
*
|
|
36
|
+
* Toggle semantics:
|
|
37
|
+
* - Clicking up while `myVote === 1` → emits `0` (clears).
|
|
38
|
+
* - Clicking up while `myVote === -1 | 0` → emits `1`.
|
|
39
|
+
* - Clicking down while `myVote === -1` → emits `0` (clears).
|
|
40
|
+
* - Clicking down while `myVote === 1 | 0` → emits `-1`.
|
|
41
|
+
*
|
|
42
|
+
* Parents are expected to update the score accordingly (the component
|
|
43
|
+
* itself does not mutate the `score` prop).
|
|
44
|
+
*/
|
|
45
|
+
onVote: (next: VoteValue) => void;
|
|
46
|
+
/** Disables both arrow buttons. */
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
/** Layout direction. Defaults to `'vertical'` (Reddit-style stacking). */
|
|
49
|
+
direction?: VoteClusterDirection;
|
|
50
|
+
/** Optional accessible label for the whole cluster. */
|
|
51
|
+
ariaLabel?: string;
|
|
52
|
+
/** Optional `data-mol-id` for AI-agent automation. */
|
|
53
|
+
dataMolId?: string;
|
|
54
|
+
/** Extra classes appended via the ClassMap `cn()` helper. */
|
|
55
|
+
className?: string;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;AAElC,6CAA6C;AAC7C,MAAM,MAAM,oBAAoB,GAAG,UAAU,GAAG,YAAY,CAAA;AAE5D,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,KAAK,EAAE,MAAM,CAAA;IACb;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB;;;;OAIG;IACH,WAAW,CAAC,EAAE,SAAS,CAAA;IACvB;;;;;;;;;;;OAWG;IACH,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACjC,mCAAmC;IACnC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,0EAA0E;IAC1E,SAAS,CAAC,EAAE,oBAAoB,CAAA;IAChC,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,sDAAsD;IACtD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-vote-cluster-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Up/down arrows + score; HN/Reddit-style voter cluster",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"molecule",
|
|
25
|
+
"vote-cluster",
|
|
26
|
+
"voting",
|
|
27
|
+
"react"
|
|
28
|
+
],
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@molecule/app-i18n": "^1.0.0",
|
|
32
|
+
"@molecule/app-react": "^1.0.0",
|
|
33
|
+
"@molecule/app-ui": "^1.0.0",
|
|
34
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@molecule/app-i18n": "1.0.0",
|
|
38
|
+
"@molecule/app-react": "1.0.0",
|
|
39
|
+
"@molecule/app-ui": "1.0.0",
|
|
40
|
+
"@molecule/app-ui-tailwind": "1.0.0",
|
|
41
|
+
"@testing-library/react": "16.3.2",
|
|
42
|
+
"@types/node": "26.1.2",
|
|
43
|
+
"@types/react": "19.2.17",
|
|
44
|
+
"@types/react-dom": "19.2.3",
|
|
45
|
+
"jsdom": "30.0.1",
|
|
46
|
+
"react": "19.2.8",
|
|
47
|
+
"react-dom": "19.2.8",
|
|
48
|
+
"typescript": "6.0.3",
|
|
49
|
+
"vitest": "4.1.10"
|
|
50
|
+
}
|
|
51
|
+
}
|