Skip to content

Commit d414cb5

Browse files
authored
fix(web): question options show a radio and an aligned shortcut key (#23255)
1 parent ba006f2 commit d414cb5

2 files changed

Lines changed: 84 additions & 38 deletions

File tree

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx‎

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
derivePendingUserInputProgress,
88
type PendingUserInputDraftAnswer,
99
} from "../../pendingUserInput";
10-
import { CheckIcon } from "lucide-react";
10+
import { CheckboxMark } from "../ui/checkbox";
1111
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
12+
import { Kbd } from "../ui/kbd";
1213
import { cn } from "~/lib/utils";
1314
import { ComposerBanner } from "./ComposerBanner";
1415
import ChatMarkdown from "../ChatMarkdown";
@@ -189,23 +190,24 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
189190
isCollapsed ? "Show the question and its options" : "Hide the question and its options"
190191
}
191192
data-pending-user-input-toggle={isCollapsed ? "collapsed" : "expanded"}
193+
className="px-4 pt-1.5"
192194
>
193-
<ComposerBanner.Icon />
194195
<ComposerBanner.Content>
195-
<span className="shrink-0 font-medium text-muted-foreground">
196-
{activeQuestion.header}
197-
</span>
196+
<span className="shrink-0 text-muted-foreground">{activeQuestion.header}</span>
198197
{isCollapsed ? (
199-
<span className="min-w-0 flex-1 truncate text-secondary-label">
200-
{activeQuestion.question}
201-
</span>
198+
<>
199+
<ComposerBanner.Separator />
200+
<span className="min-w-0 flex-1 truncate text-secondary-label">
201+
{activeQuestion.question}
202+
</span>
203+
</>
202204
) : null}
203205
</ComposerBanner.Content>
204-
<ComposerBanner.Actions>
206+
<ComposerBanner.Actions className="gap-0">
205207
{prompt.questions.length > 1 ? (
206-
<span className="text-3xs font-medium text-muted-foreground tabular-nums">
208+
<ComposerBanner.Count>
207209
{questionIndex + 1}/{prompt.questions.length}
208-
</span>
210+
</ComposerBanner.Count>
209211
) : null}
210212
<ComposerBanner.ToggleIcon expanded={!isCollapsed} />
211213
{prompt.dismissible ? (
@@ -232,58 +234,56 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
232234
</CollapsibleTrigger>
233235
<CollapsiblePanel>
234236
<ComposerBanner.Scroll>
235-
<ComposerBanner.Body className="pe-1 pb-1 wrap-anywhere">
237+
<ComposerBanner.Body className="ps-6 pe-4 pb-1 wrap-anywhere sm:ps-5.5">
236238
<ChatMarkdown
237239
text={activeQuestion.question}
238240
cwd={cwd}
239241
threadRef={threadRef}
240-
className="text-foreground/85"
242+
className="pt-2 text-foreground/85"
241243
/>
242-
{activeQuestion.multiSelect ? (
243-
<p className="mt-1 text-secondary-label text-xs">Select one or more options.</p>
244-
) : null}
245-
<div className="mt-2 space-y-0.5">
244+
{/* Rows span the header row's width so the radios align with the question text and the shortcut column sits under the dismiss button. */}
245+
<div className="-ms-2 mt-1.5 sm:-ms-1.5">
246246
{activeQuestion.options.map((option, index) => {
247247
const optionValue = option.value ?? option.label;
248248
const isOptimisticallySelected =
249249
optimisticSingleSelect?.questionId === activeQuestion.id &&
250250
optimisticSingleSelect.optionValue === optionValue;
251251
const isSelected =
252252
isOptimisticallySelected || draftSelectedOptions?.[index] === true;
253-
const shortcutKey = index < 9 ? index + 1 : null;
254253
const className = cn(
255-
"group flex w-full items-center gap-2 rounded-md px-2.5 py-2 text-left outline-none transition-colors duration-150 focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-primary/25",
256-
isSelected
257-
? "bg-muted/55 text-foreground"
258-
: "bg-transparent text-foreground/85 hover:bg-muted/30",
254+
"group flex w-full items-start gap-2.5 rounded-md bg-transparent py-1.5 ps-2 text-left sm:ps-1.5 outline-none transition-colors duration-150 focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-primary/25",
255+
isSelected ? "text-foreground" : "text-foreground/70 hover:bg-muted/30",
259256
(disabled || isResponding) && "opacity-50 cursor-not-allowed",
260257
!disabled && !isResponding && "cursor-pointer",
261258
);
259+
// The h-5 slots match the label's line box, so the indicator and shortcut stay on its first line.
262260
const content = (
263261
<>
262+
<span className="pointer-events-none flex h-5 shrink-0 items-center">
263+
{activeQuestion.multiSelect ? (
264+
<CheckboxMark checked={isSelected} />
265+
) : (
266+
<OptionRadio checked={isSelected} />
267+
)}
268+
</span>
264269
<div className="min-w-0 flex-1 flex flex-col gap-0.5">
265270
<span className="text-sm font-medium">{option.label}</span>
266271
{option.description && option.description !== option.label ? (
267272
<span className="text-secondary-label text-2xs">{option.description}</span>
268273
) : null}
269274
</div>
270-
{isSelected ? (
271-
<CheckIcon className="size-3.5 shrink-0 text-primary" />
272-
) : shortcutKey !== null ? (
273-
<kbd
274-
className={cn(
275-
"flex size-5 shrink-0 items-center justify-center text-3xs font-medium text-muted-foreground tabular-nums",
276-
)}
277-
>
278-
{shortcutKey}
279-
</kbd>
275+
{index < 9 ? (
276+
<span className="flex h-5 w-7 shrink-0 items-center justify-center sm:w-6">
277+
<Kbd>{index + 1}</Kbd>
278+
</span>
280279
) : null}
281280
</>
282281
);
283282
return (
284283
<button
285284
key={`${activeQuestion.id}:${optionValue}`}
286285
type="button"
286+
aria-pressed={isSelected}
287287
disabled={disabled || isResponding}
288288
onClick={() => {
289289
handleOptionSelection(activeQuestion.id, optionValue);
@@ -301,3 +301,21 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
301301
</Collapsible>
302302
);
303303
});
304+
305+
// Drawn with ui/radio-group's look instead of using its Radio: Radio only checks inside a
306+
// RadioGroup, whose arrow keys select options and would trip the single-select auto-advance.
307+
function OptionRadio({ checked }: { checked: boolean }) {
308+
return (
309+
<span
310+
aria-hidden
311+
className={cn(
312+
"relative inline-flex size-4.5 shrink-0 items-center justify-center rounded-full border border-input bg-background not-dark:bg-clip-padding sm:size-4 dark:bg-input/32",
313+
checked ? "shadow-none" : "shadow-xs/5",
314+
)}
315+
>
316+
{checked ? (
317+
<span className="-inset-px absolute flex items-center justify-center rounded-full bg-primary before:size-2 before:rounded-full before:bg-primary-foreground sm:before:size-1.5" />
318+
) : null}
319+
</span>
320+
);
321+
}

‎apps/web/src/components/ui/checkbox.tsx‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@ import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox";
22

33
import { cn } from "~/lib/utils";
44

5+
const rootClassName =
6+
"relative inline-flex size-4.5 shrink-0 items-center justify-center rounded-[.25rem] border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[3px] not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-ring aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:outline-destructive/48 data-disabled:opacity-64 sm:size-4 dark:not-data-checked:bg-input/32 dark:focus-visible:aria-invalid:outline-destructive/24 dark:not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)] [[data-disabled],[data-checked],[aria-invalid]]:shadow-none";
7+
8+
const indicatorClassName =
9+
"-inset-px absolute flex items-center justify-center rounded-[.25rem] text-primary-foreground data-unchecked:hidden data-checked:bg-primary data-indeterminate:text-foreground in-[[data-slot=checkbox]:focus-visible]:inset-shadow-[0_0_0_3px_var(--background)] in-[[data-slot=checkbox]:focus-visible]:outline-2 in-[[data-slot=checkbox]:focus-visible]:outline-solid in-[[data-slot=checkbox]:focus-visible]:-outline-offset-2 in-[[data-slot=checkbox]:focus-visible]:outline-inherit";
10+
511
function Checkbox({ className, ...props }: CheckboxPrimitive.Root.Props) {
612
return (
713
<CheckboxPrimitive.Root
8-
className={cn(
9-
"relative inline-flex size-4.5 shrink-0 items-center justify-center rounded-[.25rem] border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[3px] not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-ring aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:outline-destructive/48 data-disabled:opacity-64 sm:size-4 dark:not-data-checked:bg-input/32 dark:focus-visible:aria-invalid:outline-destructive/24 dark:not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)] [[data-disabled],[data-checked],[aria-invalid]]:shadow-none",
10-
className,
11-
)}
14+
className={cn(rootClassName, className)}
1215
data-slot="checkbox"
1316
{...props}
1417
>
1518
{/* Keep an inner gap in the focus outline. Explicit glyph colors resist row icon-muting rules. */}
1619
<CheckboxPrimitive.Indicator
17-
className="-inset-px absolute flex items-center justify-center rounded-[.25rem] text-primary-foreground data-unchecked:hidden data-checked:bg-primary data-indeterminate:text-foreground in-[[data-slot=checkbox]:focus-visible]:inset-shadow-[0_0_0_3px_var(--background)] in-[[data-slot=checkbox]:focus-visible]:outline-2 in-[[data-slot=checkbox]:focus-visible]:outline-solid in-[[data-slot=checkbox]:focus-visible]:-outline-offset-2 in-[[data-slot=checkbox]:focus-visible]:outline-inherit"
20+
className={indicatorClassName}
1821
data-slot="checkbox-indicator"
1922
render={(props, state) => (
2023
<span {...props}>
@@ -56,4 +59,29 @@ function Checkbox({ className, ...props }: CheckboxPrimitive.Root.Props) {
5659
);
5760
}
5861

59-
export { Checkbox };
62+
// Checkbox's look without its hidden input, for a row that is itself the button.
63+
function CheckboxMark({ checked }: { checked: boolean }) {
64+
const state = checked ? { "data-checked": "" } : { "data-unchecked": "" };
65+
return (
66+
<span aria-hidden className={rootClassName} data-slot="checkbox" {...state}>
67+
<span className={indicatorClassName} data-slot="checkbox-indicator" {...state}>
68+
<svg
69+
className="size-3.5 text-current sm:size-3"
70+
fill="none"
71+
height="24"
72+
stroke="currentColor"
73+
strokeLinecap="round"
74+
strokeLinejoin="round"
75+
strokeWidth="3"
76+
viewBox="0 0 24 24"
77+
width="24"
78+
xmlns="http://www.w3.org/2000/svg"
79+
>
80+
<path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
81+
</svg>
82+
</span>
83+
</span>
84+
);
85+
}
86+
87+
export { Checkbox, CheckboxMark };

0 commit comments

Comments
 (0)