@molecule/app-rating-form-svelte 0.0.1-stage → 1.1.1

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.
@@ -5,12 +5,9 @@
5
5
  input; node props are snippets (or plain strings) and every visible string
6
6
  goes through `t()` so a locale switch re-renders it.
7
7
 
8
- The `svelte-ignore` lines below mark two deliberate trade-offs React's package
8
+ The `svelte-ignore` line below marks a deliberate trade-off React's package
9
9
  documents: `rating` captures the initial `defaultRating` prop value once (a
10
- later prop write must not move the selection), and the radiogroup `<div>`
11
- carries no `tabindex` — React renders none, and the star `<button>`s are the
12
- focusable controls, so adding one would change the DOM this package must
13
- reproduce exactly.
10
+ later prop write must not move the selection).
14
11
  -->
15
12
  <script lang="ts">
16
13
  import { useTranslation } from '@molecule/app-svelte'
@@ -45,6 +42,14 @@
45
42
 
46
43
  const display = $derived(hover || rating)
47
44
  const stars = $derived(Array.from({ length: max }, (_value, index) => index + 1))
45
+ // The star holding the group's one tab stop: the selected one, or the first
46
+ // when no star is selected — no rating yet, or a defaultRating outside
47
+ // 1..max, which marks no star (an out-of-range value must still leave the
48
+ // group reachable from the keyboard).
49
+ const tabStop = $derived(rating >= 1 && rating <= max ? rating : 1)
50
+
51
+ // The star buttons by number, for the keyboard handler's focus moves.
52
+ let starEls: Record<number, HTMLButtonElement> = {}
48
53
 
49
54
  // Every `t(…)` runs inside a `$derived`: it reads the i18n version under the
50
55
  // hood, so these re-run on a locale switch the way React's render-time `t`
@@ -54,6 +59,7 @@
54
59
  commentPlaceholder ??
55
60
  t('rating.commentPlaceholder', {}, { defaultValue: 'Share your thoughts (optional)' }),
56
61
  )
62
+ const commentLabel = $derived(t('rating.comment', {}, { defaultValue: 'Comment' }))
57
63
  const submittingText = $derived(t('rating.submitting', {}, { defaultValue: 'Submitting…' }))
58
64
  const submitText = $derived(t('rating.submit', {}, { defaultValue: 'Submit review' }))
59
65
 
@@ -74,6 +80,71 @@
74
80
  submitting = false
75
81
  }
76
82
  }
83
+
84
+ /**
85
+ * A capture action for a star button: records the node under its star
86
+ * number, so the keyboard handler can move focus with `focus()`. An action
87
+ * runs at mount with the node itself, which `bind:this` to a plain object
88
+ * member does not guarantee.
89
+ *
90
+ * @param node - The star button.
91
+ * @param n - The star number.
92
+ * @returns An update hook, so a re-keyed button re-registers under its new number.
93
+ */
94
+ function captureStar(node: HTMLButtonElement, n: number): { update: (next: number) => void } {
95
+ starEls[n] = node
96
+ return {
97
+ update: (next) => {
98
+ starEls[next] = node
99
+ },
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Selects a star and moves focus with it — the APG radio group's roving
105
+ * tabindex, where the checked star is the group's one keyboard stop.
106
+ *
107
+ * @param n - The star to select.
108
+ */
109
+ function selectAndFocus(n: number): void {
110
+ rating = n
111
+ starEls[n]?.focus()
112
+ }
113
+
114
+ /**
115
+ * Handles the radio keys on a star (the APG radio group pattern):
116
+ * ArrowRight / ArrowDown select and focus the next star, ArrowLeft / ArrowUp
117
+ * the previous one, both wrapping; Home and End jump to the first and last.
118
+ * The handled keys are prevented (they would otherwise scroll or move
119
+ * focus); Space and Enter stay the native button click, and any other key is
120
+ * ignored (React's `onStarKeyDown`).
121
+ *
122
+ * @param event - The key event on a star button.
123
+ */
124
+ function onStarKeyDown(event: KeyboardEvent): void {
125
+ const current = tabStop
126
+ let next: number
127
+ switch (event.key) {
128
+ case 'ArrowRight':
129
+ case 'ArrowDown':
130
+ next = current >= max ? 1 : current + 1
131
+ break
132
+ case 'ArrowLeft':
133
+ case 'ArrowUp':
134
+ next = current <= 1 ? max : current - 1
135
+ break
136
+ case 'Home':
137
+ next = 1
138
+ break
139
+ case 'End':
140
+ next = max
141
+ break
142
+ default:
143
+ return
144
+ }
145
+ event.preventDefault()
146
+ selectAndFocus(next)
147
+ }
77
148
  </script>
78
149
 
79
150
  <form class={cm.cn(cm.stack(3), className)} onsubmit={handle}>
@@ -82,9 +153,14 @@
82
153
  {#if typeof title === 'function'}{@render title()}{:else}{title}{/if}
83
154
  </h3>
84
155
  {/if}
85
- <!-- svelte-ignore a11y_interactive_supports_focus -->
156
+ <!-- jsx-a11y's interactive-supports-focus asks the group (it carries
157
+ onmouseleave) to be focusable, so it keeps a tabindex — but at -1 the
158
+ group is only ever focused through script, never a tab stop. The stars
159
+ carry the keyboard instead (React's `tabIndex={-1}`, the APG radio
160
+ group's roving tabindex). -->
86
161
  <div
87
162
  role="radiogroup"
163
+ tabindex="-1"
88
164
  aria-label={ratingLabel}
89
165
  class={cm.flex({ align: 'center', gap: 'xs' })}
90
166
  onmouseleave={() => (hover = 0)}
@@ -94,11 +170,14 @@
94
170
  type="button"
95
171
  role="radio"
96
172
  data-mol-id="rating-form-star"
173
+ tabindex={n === tabStop ? 0 : -1}
97
174
  aria-checked={rating === n}
98
175
  aria-label={t('rating.star', { n, max }, { defaultValue: '{{n}} of {{max}}' })}
99
176
  onclick={() => (rating = n)}
177
+ onkeydown={onStarKeyDown}
100
178
  onmouseenter={() => (hover = n)}
101
179
  class={cm.cn(cm.cursorPointer, cm.touchTarget)}
180
+ use:captureStar={n}
102
181
  use:starStyle={n <= display}
103
182
  >
104
183
  ★
@@ -110,7 +189,7 @@
110
189
  onChange={(event: Event) => (comment = (event.target as HTMLTextAreaElement).value)}
111
190
  placeholder={placeholderText}
112
191
  rows={4}
113
- aria-label="Comment"
192
+ aria-label={commentLabel}
114
193
  />
115
194
  <Button type="submit" variant="solid" color="primary" disabled={submitting || !rating}>
116
195
  {#if submitting}
@@ -1 +1 @@
1
- {"version":3,"file":"RatingForm.svelte.d.ts","sourceRoot":"","sources":["../src/RatingForm.svelte"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAiGlD,QAAA,MAAM,UAAU,qDAAwC,CAAC;AACzD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;AAChD,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"RatingForm.svelte.d.ts","sourceRoot":"","sources":["../src/RatingForm.svelte"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AA2KlD,QAAA,MAAM,UAAU,qDAAwC,CAAC;AACzD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;AAChD,eAAe,UAAU,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-rating-form-svelte",
3
- "version": "0.0.1-stage",
3
+ "version": "1.1.1",
4
4
  "description": "Interactive star rating + textarea + submit form for collecting reviews",
5
5
  "homepage": "https://www.molecule.dev/packages/app-rating-form-svelte",
6
6
  "type": "module",
@@ -55,11 +55,11 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@molecule/app-framework-conformance": "0.0.0",
58
- "@molecule/app-i18n": "1.2.1",
58
+ "@molecule/app-i18n": "1.2.2",
59
59
  "@molecule/app-icons": "1.0.4",
60
- "@molecule/app-svelte": "3.6.0",
61
- "@molecule/app-ui": "1.3.11",
62
- "@molecule/app-ui-svelte": "1.1.3",
60
+ "@molecule/app-svelte": "3.9.0",
61
+ "@molecule/app-ui": "1.5.0",
62
+ "@molecule/app-ui-svelte": "1.2.1",
63
63
  "@sveltejs/package": "3.0.2",
64
64
  "@sveltejs/vite-plugin-svelte": "7.1.2",
65
65
  "@types/node": "26.6.5",