@adea-ai/ui 0.150.0 → 0.151.2

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.
Files changed (43) hide show
  1. package/dist/components/composites/inline-citation/inline-citation.d.ts +2 -1
  2. package/dist/components/composites/inline-citation/inline-citation.d.ts.map +1 -1
  3. package/dist/components/composites/inline-citation/inline-citation.js +36 -3
  4. package/dist/components/composites/inline-citation/inline-citation.js.map +1 -1
  5. package/dist/components/conversation/internal/chat-composer-shell.d.ts.map +1 -1
  6. package/dist/components/conversation/internal/chat-composer-shell.js +28 -4
  7. package/dist/components/conversation/internal/chat-composer-shell.js.map +1 -1
  8. package/dist/components/conversation/paste-token-editor.d.ts.map +1 -1
  9. package/dist/components/conversation/paste-token-editor.js +127 -37
  10. package/dist/components/conversation/paste-token-editor.js.map +1 -1
  11. package/dist/components/layout/contextual-sidebar/contextual-sidebar.d.ts +5 -0
  12. package/dist/components/layout/contextual-sidebar/contextual-sidebar.d.ts.map +1 -1
  13. package/dist/components/layout/contextual-sidebar/contextual-sidebar.js +54 -23
  14. package/dist/components/layout/contextual-sidebar/contextual-sidebar.js.map +1 -1
  15. package/dist/components/ui/alert-dialog/alert-dialog.d.ts.map +1 -1
  16. package/dist/components/ui/alert-dialog/alert-dialog.js +9 -4
  17. package/dist/components/ui/alert-dialog/alert-dialog.js.map +1 -1
  18. package/dist/components/ui/dialog/dialog.js +1 -1
  19. package/dist/components/ui/dialog/dialog.js.map +1 -1
  20. package/dist/components/ui/modal-dialog/modal-dialog.d.ts +18 -3
  21. package/dist/components/ui/modal-dialog/modal-dialog.d.ts.map +1 -1
  22. package/dist/components/ui/modal-dialog/modal-dialog.js +29 -3
  23. package/dist/components/ui/modal-dialog/modal-dialog.js.map +1 -1
  24. package/dist/r/appearance-editor.json +1 -1
  25. package/dist/r/lib.json +1 -1
  26. package/dist/r/registry.json +3 -3
  27. package/dist/r/src/components/composites/inline-citation/inline-citation.tsx +53 -4
  28. package/dist/r/src/components/conversation/internal/chat-composer-shell.tsx +36 -2
  29. package/dist/r/src/components/conversation/paste-token-editor.tsx +191 -49
  30. package/dist/r/src/components/layout/contextual-sidebar/contextual-sidebar.tsx +76 -28
  31. package/dist/r/src/components/ui/alert-dialog/alert-dialog.tsx +11 -2
  32. package/dist/r/src/components/ui/dialog/dialog.tsx +1 -1
  33. package/dist/r/src/components/ui/modal-dialog/modal-dialog.tsx +33 -4
  34. package/dist/r/theming.json +1 -1
  35. package/package.json +2 -2
  36. package/registry.json +3 -3
  37. package/src/components/composites/inline-citation/inline-citation.tsx +53 -4
  38. package/src/components/conversation/internal/chat-composer-shell.tsx +36 -2
  39. package/src/components/conversation/paste-token-editor.tsx +191 -49
  40. package/src/components/layout/contextual-sidebar/contextual-sidebar.tsx +76 -28
  41. package/src/components/ui/alert-dialog/alert-dialog.tsx +11 -2
  42. package/src/components/ui/dialog/dialog.tsx +1 -1
  43. package/src/components/ui/modal-dialog/modal-dialog.tsx +33 -4
@@ -33,6 +33,35 @@ import { Tooltip as KobalteTooltip } from '@kobalte/core/tooltip'
33
33
  export type PasteTokenDraft = { text: string; blocks: PasteBlock[] }
34
34
  type TokenRange = { start: number; end: number; block: PasteBlock }
35
35
  type Segment = { text: string; block?: PasteBlock }
36
+ type SelectionDirection = 'forward' | 'backward' | 'none'
37
+ /**
38
+ * A selection the editor owes the field after one of its own edits or key
39
+ * handlers. `text` is the draft the claim was measured against. A claim
40
+ * exists only while it is the latest selection owner: every newer intent
41
+ * advances the epoch and drops it (see `yieldSelection`, `claimSelection`).
42
+ */
43
+ type SelectionClaim = {
44
+ start: number
45
+ end: number
46
+ direction: SelectionDirection
47
+ text: string
48
+ blocks: readonly PasteBlock[]
49
+ focus: boolean
50
+ /** The field selection when the claim was made, to tell later `select`s apart. */
51
+ observed: string
52
+ }
53
+
54
+ /**
55
+ * Whether a key is a selection intent. A bare modifier or a clipboard shortcut
56
+ * does not move the selection itself, so it must not cancel a claim the next
57
+ * paste builds on.
58
+ */
59
+ function isSelectionIntent(event: KeyboardEvent): boolean {
60
+ return !(
61
+ /^(Shift|Control|Alt|Meta|CapsLock|Fn|OS)$/.test(event.key) ||
62
+ ((event.metaKey || event.ctrlKey) && !event.altKey && /^[cvx]$/i.test(event.key))
63
+ )
64
+ }
36
65
 
37
66
  export type PasteTokenEditorProps = {
38
67
  value: string
@@ -88,8 +117,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
88
117
  let scheduledBlockId: string | null = null
89
118
  /**
90
119
  * What scheduled the current preview. A caret event must not cancel a preview
91
- * the pointer is owed: a programmatic caret restore (`setCaret` defers to an
92
- * animation frame) dispatches its `select` event as a separate task that can
120
+ * the pointer is owed: a programmatic caret restore (`claimSelection`, which
121
+ * may defer to an animation frame) dispatches its `select` event as a separate task that can
93
122
  * land *after* a pointermove has already scheduled the hover preview, and a
94
123
  * caret with no token under it would otherwise tear that preview down with no
95
124
  * further pointermove ever arriving to reschedule it. The pointer's own leave,
@@ -102,6 +131,16 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
102
131
  let keyboardNavigationPending = false
103
132
  let acceptingTextDrop = false
104
133
  let snappedCaretPreview: { blockId: string; position: number } | null = null
134
+ /**
135
+ * The latest selection owner wins. Every selection intent (a pointer press, a
136
+ * key, IME composition, an external `select`, or one of the editor's own
137
+ * claims) advances the epoch; a claim or deferred snap from an older epoch is
138
+ * discarded instead of overriding the newer selection. Comparing positions
139
+ * cannot do this: replacing a controlled value parks the caret at the end of
140
+ * the text, exactly where an intentional caret at the end also sits.
141
+ */
142
+ let selectionEpoch = 0
143
+ let selectionClaim: SelectionClaim | null = null
105
144
 
106
145
  const ranges = () => findTokenRanges(props.value, props.blocks)
107
146
  const clearTimer = () => {
@@ -145,12 +184,79 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
145
184
  }
146
185
  return position
147
186
  }
148
- const setCaret = (position: number, focus = false) => {
149
- requestAnimationFrame(() => {
150
- if (!field) return
151
- if (focus) field.focus()
152
- field.setSelectionRange(position, position)
153
- })
187
+ /** A newer selection intent takes ownership; any owed claim is dropped. */
188
+ const yieldSelection = () => {
189
+ selectionEpoch += 1
190
+ selectionClaim = null
191
+ }
192
+ /**
193
+ * Applies the owed claim once the field holds the draft it was measured
194
+ * against; until then the claim stays owed.
195
+ */
196
+ const settleSelectionClaim = () => {
197
+ const claim = selectionClaim
198
+ if (!claim) return
199
+ if (!field || field.value !== claim.text) return
200
+ selectionClaim = null
201
+ if (claim.focus && document.activeElement !== field) field.focus()
202
+ if (
203
+ field.selectionStart !== claim.start ||
204
+ field.selectionEnd !== claim.end ||
205
+ (claim.direction !== 'none' && field.selectionDirection !== claim.direction)
206
+ ) {
207
+ field.setSelectionRange(claim.start, claim.end, claim.direction)
208
+ }
209
+ }
210
+ /**
211
+ * Claims the selection for an edit or key the editor just handled. The claim
212
+ * lands as soon as the field holds `text`: at once for a host that writes the
213
+ * controlled value synchronously (so a second paste in the same task is
214
+ * measured from this caret, not from the end-of-text caret a value write
215
+ * parks), otherwise on the host's write. A newer selection intent before that
216
+ * write owns the field instead, and nothing is ever restored a frame later.
217
+ */
218
+ const claimSelection = (
219
+ start: number,
220
+ end = start,
221
+ direction: SelectionDirection = 'none',
222
+ text = props.value,
223
+ blocks: readonly PasteBlock[] = props.blocks,
224
+ focus = false
225
+ ) => {
226
+ selectionEpoch += 1
227
+ selectionClaim = {
228
+ start,
229
+ end,
230
+ direction,
231
+ text,
232
+ blocks,
233
+ focus,
234
+ observed: field ? `${field.selectionStart}:${field.selectionEnd}` : '',
235
+ }
236
+ settleSelectionClaim()
237
+ }
238
+ /** Writes an edit and claims a collapsed caret in it. */
239
+ const commit = (
240
+ text: string,
241
+ caret: number,
242
+ blocks: readonly PasteBlock[] = props.blocks,
243
+ focus = false
244
+ ) => {
245
+ const pruned = pruneBlocks(text, [...blocks])
246
+ props.onChange({ text, blocks: pruned })
247
+ claimSelection(caret, caret, 'none', text, pruned, focus)
248
+ }
249
+ /**
250
+ * A `select` event that moved the selection while a claim is still owed is a
251
+ * newer selection someone else made, so it owns the field. Replacing a value
252
+ * parks the caret without firing `select`, and a `select` queued before the
253
+ * claim reports the selection the claim already observed.
254
+ */
255
+ const noticeSelect = () => {
256
+ const claim = selectionClaim
257
+ if (!claim || !field) return
258
+ if (`${field.selectionStart}:${field.selectionEnd}` === claim.observed) return
259
+ yieldSelection()
154
260
  }
155
261
  const replaceRange = (start: number, end: number) => {
156
262
  if (!field) return
@@ -159,21 +265,22 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
159
265
  const nextStart = Math.min(start, ...removed.map((range) => range.start))
160
266
  const nextEnd = Math.max(end, ...removed.map((range) => range.end))
161
267
  const next = props.value.slice(0, nextStart) + props.value.slice(nextEnd)
162
- change(
268
+ commit(
163
269
  next,
270
+ nextStart,
164
271
  props.blocks.filter((block) => !removed.some((range) => range.block.id === block.id))
165
272
  )
166
- setCaret(nextStart)
167
273
  }
168
274
  const expandToken = (range: TokenRange) => {
169
275
  const next =
170
276
  props.value.slice(0, range.start) + range.block.content + props.value.slice(range.end)
171
- change(
277
+ closePreview()
278
+ commit(
172
279
  next,
173
- props.blocks.filter((block) => block.id !== range.block.id)
280
+ range.start + range.block.content.length,
281
+ props.blocks.filter((block) => block.id !== range.block.id),
282
+ true
174
283
  )
175
- closePreview()
176
- setCaret(range.start + range.block.content.length, true)
177
284
  }
178
285
 
179
286
  const segments = (): Segment[] => {
@@ -283,6 +390,7 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
283
390
 
284
391
  const handleKeyDown = (event: KeyboardEvent) => {
285
392
  snappedCaretPreview = null
393
+ if (isSelectionIntent(event)) yieldSelection()
286
394
  rawPasteNext =
287
395
  (event.metaKey || event.ctrlKey) &&
288
396
  event.shiftKey &&
@@ -350,7 +458,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
350
458
  : undefined
351
459
  if (adjacent) {
352
460
  event.preventDefault()
353
- setCaret(left ? adjacent.start : adjacent.end)
461
+ const caret = left ? adjacent.start : adjacent.end
462
+ claimSelection(caret)
354
463
  props.onKeyDown(event)
355
464
  return
356
465
  }
@@ -366,20 +475,13 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
366
475
  : undefined
367
476
  if (adjacent) {
368
477
  event.preventDefault()
369
- requestAnimationFrame(() => {
370
- if (!field) return
371
- if (event.key === 'ArrowLeft' && direction === 'backward')
372
- field.setSelectionRange(adjacent.start, end, 'backward')
373
- else if (event.key === 'ArrowLeft')
374
- field.setSelectionRange(
375
- start,
376
- adjacent.start,
377
- start <= adjacent.start ? 'forward' : 'backward'
378
- )
379
- else if (direction === 'backward')
380
- field.setSelectionRange(adjacent.end, end, adjacent.end <= end ? 'backward' : 'forward')
381
- else field.setSelectionRange(start, adjacent.end, 'forward')
382
- })
478
+ const anchor = direction === 'backward' ? end : start
479
+ const focus = event.key === 'ArrowLeft' ? adjacent.start : adjacent.end
480
+ claimSelection(
481
+ Math.min(anchor, focus),
482
+ Math.max(anchor, focus),
483
+ focus < anchor ? 'backward' : 'forward'
484
+ )
383
485
  props.onKeyDown(event)
384
486
  return
385
487
  }
@@ -392,9 +494,12 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
392
494
  (event.metaKey || event.ctrlKey || event.altKey))
393
495
  if (navigation) {
394
496
  const leftward = event.key === 'ArrowLeft' || event.key === 'Home'
497
+ const owner = selectionEpoch
395
498
  keyboardNavigationPending = true
396
499
  requestAnimationFrame(() => {
397
- if (field) {
500
+ // A newer selection intent since this key owns the field; its own
501
+ // `select` snaps it.
502
+ if (field && owner === selectionEpoch) {
398
503
  const nextRanges = findTokenRanges(field.value, props.blocks)
399
504
  const edge = leftward ? 'start' : 'end'
400
505
  const nextStart = snapToTokenEdge(field.selectionStart, nextRanges, edge)
@@ -432,17 +537,30 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
432
537
  const pasted = event.clipboardData?.getData('text/plain') ?? ''
433
538
  if (!pasted || !field || props.readOnly || props.disabled) return
434
539
  const cleaned = forceRaw ? pasted : stripTrailingBlankLines(pasted)
435
- const start = field.selectionStart
436
- const end = field.selectionEnd
437
- const before = props.value.slice(0, start)
438
- const after = props.value.slice(end)
540
+ // Pastes batched before a deferred host writes the previous one build on
541
+ // that owed draft and caret, not on the stale value still in the field.
542
+ // The owed claim is the draft and caret the host has not written yet.
543
+ const owed = selectionClaim
544
+ const {
545
+ text: value,
546
+ blocks,
547
+ start,
548
+ end,
549
+ } = owed ?? {
550
+ text: props.value,
551
+ blocks: props.blocks,
552
+ start: field.selectionStart,
553
+ end: field.selectionEnd,
554
+ }
555
+ const before = value.slice(0, start)
556
+ const after = value.slice(end)
439
557
 
440
558
  if (!forceRaw && shouldCollapse(cleaned)) {
441
559
  const id = props.createBlockId()
442
- if (props.blocks.some((block) => block.id === id)) return
560
+ if (blocks.some((block) => block.id === id)) return
443
561
  const block: PasteBlock = {
444
562
  id,
445
- seq: nextSeq([...props.blocks]),
563
+ seq: nextSeq([...blocks]),
446
564
  lines: countLines(cleaned),
447
565
  content: cleaned,
448
566
  }
@@ -453,9 +571,16 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
453
571
  const trailing = after && !after.startsWith('\n') ? '\n' : ''
454
572
  const insert = leading + formatToken(block) + trailing
455
573
  event.preventDefault()
456
- change(before + insert + after, [...props.blocks, block])
457
574
  closePreview()
458
- setCaret(before.length + insert.length)
575
+ commit(before + insert + after, before.length + insert.length, [...blocks, block])
576
+ return
577
+ }
578
+ if (owed) {
579
+ // The field still shows an older value, so a native insertion would land
580
+ // in the wrong text: insert into the owed draft instead.
581
+ const text = forceRaw || cleaned === '' ? pasted : cleaned
582
+ event.preventDefault()
583
+ commit(before + text + after, before.length + text.length, blocks)
459
584
  return
460
585
  }
461
586
  if (forceRaw) return
@@ -475,7 +600,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
475
600
  if (!nativePasteInputSeen) suppressPasteInput = false
476
601
  change(next)
477
602
  if (inserted && field.value === next) return
478
- setCaret(before.length + cleaned.length)
603
+ const caret = before.length + cleaned.length
604
+ claimSelection(caret, caret, 'none', next)
479
605
  }
480
606
  }
481
607
 
@@ -519,9 +645,9 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
519
645
  end = Math.max(end, ...removed.map((range) => range.end))
520
646
  }
521
647
 
522
- change(props.value.slice(0, start) + dropped + props.value.slice(end))
648
+ const next = props.value.slice(0, start) + dropped + props.value.slice(end)
523
649
  closePreview()
524
- setCaret(start + dropped.length)
650
+ commit(next, start + dropped.length)
525
651
  }
526
652
 
527
653
  const handleClipboard = (event: ClipboardEvent) => {
@@ -533,8 +659,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
533
659
  event.clipboardData.setData('text/plain', expanded)
534
660
  event.preventDefault()
535
661
  if (event.type === 'cut') {
536
- change(props.value.slice(0, start) + props.value.slice(end))
537
- setCaret(start)
662
+ const next = props.value.slice(0, start) + props.value.slice(end)
663
+ commit(next, start)
538
664
  }
539
665
  }
540
666
 
@@ -543,18 +669,27 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
543
669
  const range = tokenRangeAt(props.value, props.blocks, field.selectionStart)
544
670
  if (!range) return
545
671
  if (isTouchDevice() || event.detail >= 2) expandToken(range)
546
- else {
547
- requestAnimationFrame(() => field?.setSelectionRange(range.start, range.end))
548
- }
672
+ else claimSelection(range.start, range.end)
549
673
  }
550
674
 
551
- createEffect(on(() => props.value, closePreview))
675
+ createEffect(
676
+ on(
677
+ () => props.value,
678
+ () => {
679
+ closePreview()
680
+ // The controlled value has just been written; a claim measured against
681
+ // it lands before any queued selection task can read the parked caret.
682
+ settleSelectionClaim()
683
+ }
684
+ )
685
+ )
552
686
  createEffect(on(() => props.blocks, closePreview))
553
687
  createEffect(
554
688
  on(
555
689
  () => props.resetKey,
556
690
  () => {
557
691
  rawPasteNext = false
692
+ yieldSelection()
558
693
  closePreview()
559
694
  }
560
695
  )
@@ -634,7 +769,11 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
634
769
  onCopy={handleClipboard}
635
770
  onCut={handleClipboard}
636
771
  onClick={handleClick}
637
- onSelect={snapSelection}
772
+ onSelect={() => {
773
+ noticeSelect()
774
+ snapSelection()
775
+ }}
776
+ onPointerDown={yieldSelection}
638
777
  onMouseMove={schedulePointerPreview}
639
778
  onMouseLeave={closePreview}
640
779
  onScroll={(event) => {
@@ -644,7 +783,10 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
644
783
  }
645
784
  closePreview()
646
785
  }}
647
- onCompositionStart={props.onCompositionStart}
786
+ onCompositionStart={() => {
787
+ yieldSelection()
788
+ props.onCompositionStart()
789
+ }}
648
790
  onCompositionEnd={props.onCompositionEnd}
649
791
  onFocus={props.onFocus}
650
792
  onBlur={() => {
@@ -1,5 +1,5 @@
1
1
  import type { Accessor, JSX } from 'solid-js'
2
- import { Show, createSignal, createUniqueId, onCleanup, onMount } from 'solid-js'
2
+ import { Show, createEffect, createSignal, createUniqueId, onCleanup, untrack } from 'solid-js'
3
3
  import { cn } from '#lib/utils'
4
4
  import {
5
5
  SidebarNav,
@@ -37,6 +37,11 @@ export type ContextualSidebarProps = {
37
37
  /** Host-controlled expanded state for the desktop panel and mobile dialog. */
38
38
  open: boolean
39
39
  onOpenChange(open: boolean): void
40
+ /**
41
+ * Presentation mode. `auto` (default) uses the built-in viewport breakpoint;
42
+ * hosts can select `desktop` or the shared mobile `Sheet` themselves.
43
+ */
44
+ responsiveMode?: 'auto' | 'desktop' | 'mobile'
40
45
  /** Current host-owned pixel width, reflected by the resize handle. */
41
46
  width: number
42
47
  minimum: number
@@ -80,20 +85,32 @@ export type ContextualSidebarProps = {
80
85
  * persisted preference, open state, and view-specific navigation content.
81
86
  */
82
87
  export function ContextualSidebar(props: ContextualSidebarProps) {
83
- const [isNarrowViewport, setIsNarrowViewport] = createSignal(false)
88
+ const [isMobile, setIsMobile] = createSignal(false)
84
89
  const [mobileLoad, setMobileLoad] = createSignal<MobileLoad>()
85
- let mobileRequested = false
90
+ let mobileLoadGeneration = 0
91
+ let mobileLoadPending = false
92
+ let mobileLoadSettled = false
93
+ let responsiveModeInitialized = false
86
94
  // Keep the resolved component mounted across viewport changes for Sheet cleanup.
87
95
  const loadMobile = () => {
88
- if (mobileRequested) return
89
- mobileRequested = true
96
+ if (mobileLoadPending || mobileLoadSettled) return
97
+ mobileLoadPending = true
98
+ const generation = ++mobileLoadGeneration
90
99
  // Native module caching shares successful loads without keeping a rejected promise.
91
100
  void import('./mobile-contextual-sidebar').then(
92
101
  (module) => {
93
- if (mobileRequested) setMobileLoad(module)
102
+ if (generation === mobileLoadGeneration) {
103
+ mobileLoadPending = false
104
+ mobileLoadSettled = true
105
+ setMobileLoad(module)
106
+ }
94
107
  },
95
108
  (error: unknown) => {
96
- if (mobileRequested) setMobileLoad({ error })
109
+ if (generation === mobileLoadGeneration) {
110
+ mobileLoadPending = false
111
+ mobileLoadSettled = true
112
+ setMobileLoad({ error })
113
+ }
97
114
  }
98
115
  )
99
116
  }
@@ -107,28 +124,59 @@ export function ContextualSidebar(props: ContextualSidebarProps) {
107
124
  return mount?.connected && mount.element.isConnected ? mount.element : undefined
108
125
  }
109
126
 
110
- onMount(() => {
111
- const media = window.matchMedia('(max-width: 48rem)')
112
- const updateViewport = () => {
113
- const wasNarrow = isNarrowViewport()
114
- if (media.matches) loadMobile()
115
- // Close before mounting a modal. A desktop-expanded host state is not
116
- // mobile intent, and opening the Sheet for one frame can steal focus.
117
- if (media.matches && !wasNarrow) props.onOpenChange(false)
118
- setIsNarrowViewport(media.matches)
119
- }
127
+ createEffect(() => {
128
+ const mode = props.responsiveMode ?? 'auto'
129
+ const wasMobile = untrack(isMobile)
130
+ let media: MediaQueryList | undefined
131
+ let viewportListener: (() => void) | undefined
120
132
 
121
- // A route can mount after a wide-loaded host crosses into mobile. Clear
122
- // that stale seed, but preserve a sidebar intentionally opened at a
123
- // narrow boot viewport.
124
- if (media.matches) loadMobile()
125
- if (media.matches && props.wideViewportAtLoad && props.open) props.onOpenChange(false)
126
- setIsNarrowViewport(media.matches)
127
- media.addEventListener('change', updateViewport)
128
133
  onCleanup(() => {
129
- mobileRequested = false
130
- media.removeEventListener('change', updateViewport)
134
+ mobileLoadGeneration += 1
135
+ mobileLoadPending = false
136
+ if (media && viewportListener) media.removeEventListener('change', viewportListener)
131
137
  })
138
+
139
+ if (mode === 'desktop') {
140
+ setIsMobile(false)
141
+ } else if (mode === 'mobile') {
142
+ // An explicit host transition into mobile must not briefly open a Sheet
143
+ // with stale desktop-expanded state. An initial wide-loaded seed still
144
+ // marks an open value as stale, including in host-selected mobile mode.
145
+ const shouldCloseStaleOpen = responsiveModeInitialized
146
+ ? !wasMobile && untrack(() => props.open)
147
+ : untrack(() => props.wideViewportAtLoad && props.open)
148
+ if (shouldCloseStaleOpen) untrack(() => props.onOpenChange(false))
149
+ loadMobile()
150
+ setIsMobile(true)
151
+ } else {
152
+ const mediaQuery = window.matchMedia('(max-width: 48rem)')
153
+ media = mediaQuery
154
+ const updateViewport = () => {
155
+ const nextIsMobile = mediaQuery.matches
156
+ const wasNarrow = untrack(isMobile)
157
+ if (nextIsMobile) loadMobile()
158
+ // Close before mounting a modal. A desktop-expanded host state is not
159
+ // mobile intent, and opening the Sheet for one frame can steal focus.
160
+ if (nextIsMobile && !wasNarrow) props.onOpenChange(false)
161
+ setIsMobile(nextIsMobile)
162
+ }
163
+ viewportListener = updateViewport
164
+
165
+ // Preserve a narrow-boot open intent. Initial auto and mobile modes use
166
+ // the viewport seed to clear stale state; runtime transitions close it.
167
+ if (media.matches) {
168
+ if (!responsiveModeInitialized && untrack(() => props.wideViewportAtLoad && props.open)) {
169
+ untrack(() => props.onOpenChange(false))
170
+ } else if (responsiveModeInitialized && !wasMobile) {
171
+ untrack(() => props.onOpenChange(false))
172
+ }
173
+ loadMobile()
174
+ }
175
+ setIsMobile(mediaQuery.matches)
176
+ mediaQuery.addEventListener('change', updateViewport)
177
+ }
178
+
179
+ responsiveModeInitialized = true
132
180
  })
133
181
 
134
182
  const renderSidebar = (mobile: boolean) => {
@@ -209,13 +257,13 @@ export function ContextualSidebar(props: ContextualSidebarProps) {
209
257
 
210
258
  return (
211
259
  <>
212
- <Show when={!isNarrowViewport()}>{(_wide) => renderSidebar(false)}</Show>
260
+ <Show when={!isMobile()}>{(_wide) => renderSidebar(false)}</Show>
213
261
  <Show when={mobileLoad()} keyed>
214
262
  {(loaded) => {
215
263
  if ('error' in loaded) throw loaded.error
216
264
  return (
217
265
  <loaded.MobileSidebar
218
- open={isNarrowViewport() && props.open}
266
+ open={isMobile() && props.open}
219
267
  onOpenChange={props.onOpenChange}
220
268
  label={props.label}
221
269
  sheetClass={props.sheetClass}
@@ -94,7 +94,12 @@ export function AlertDialogContent(props: ComponentProps<typeof KobalteAlertDial
94
94
  <div class={overlayPositioner}>
95
95
  <KobalteAlertDialogContent
96
96
  {...blockDismissal}
97
- class={cn(dialogSurface, overlayMotion, 'grid w-full max-w-md gap-4 p-5', local.class)}
97
+ class={cn(
98
+ dialogSurface,
99
+ overlayMotion,
100
+ 'grid max-h-full w-full max-w-md gap-4 overflow-y-auto p-5',
101
+ local.class
102
+ )}
98
103
  {...rest}
99
104
  />
100
105
  </div>
@@ -172,7 +177,11 @@ function AlertDialogChoice(props: ComponentProps<typeof KobalteAlertDialog.Close
172
177
 
173
178
  return (
174
179
  <span ref={setWrapper} class="contents">
175
- <KobalteAlertDialog.CloseButton class={cn(local.class)} {...rest} />
180
+ <KobalteAlertDialog.CloseButton
181
+ class={cn(local.class)}
182
+ aria-label={local['aria-label']}
183
+ {...rest}
184
+ />
176
185
  </span>
177
186
  )
178
187
  }
@@ -314,7 +314,7 @@ export function DialogContent(props: DialogContentProps) {
314
314
  class={cn(
315
315
  dialogSurface,
316
316
  overlayMotion,
317
- 'relative grid w-full gap-4 p-5',
317
+ 'relative grid max-h-full w-full gap-4 overflow-y-auto p-5',
318
318
  dialogContentSizes[local.size ?? 'md'],
319
319
  local.class
320
320
  )}
@@ -43,14 +43,29 @@ import {
43
43
  * Use `Dialog` directly when you need its composition; use this when you want a
44
44
  * dialog that is correct by default. `size="settings"` supplies a wide, bounded
45
45
  * shell for a shared settings layout, titled by a banded header like a sheet's;
46
- * ordinary dialogs keep their compact default.
46
+ * ordinary dialogs keep their compact default. Every centred dialog stays inside
47
+ * the window: past the window's height it scrolls rather than running off its
48
+ * edges. `size="fit"` is for a dialog whose body can grow (a scan result, a long
49
+ * plan) and whose actions must stay in view: the title and `footer` stay put and
50
+ * the children scroll between them.
47
51
  */
48
52
  export type ModalDialogProps = Omit<
49
53
  ComponentProps<typeof DialogContent>,
50
54
  'children' | 'aria-label' | 'aria-labelledby' | 'positioner'
51
55
  > & {
52
- /** Selects the bounded, scrollable shell used for grouped workspace settings. */
53
- size?: 'default' | 'settings'
56
+ /**
57
+ * `settings` selects the bounded, scrollable shell used for grouped workspace
58
+ * settings. `fit` keeps the compact default width but lets the dialog's content
59
+ * decide its height up to the window: the title band stays put, the children
60
+ * scroll, and `footer` stays in view beneath them.
61
+ */
62
+ size?: 'default' | 'settings' | 'fit'
63
+ /**
64
+ * Actions that must stay reachable however long the content is (Cancel and a
65
+ * primary button). Rendered under the scrolling children, outside the scroll
66
+ * area, and only by the `fit` size.
67
+ */
68
+ footer?: JSX.Element
54
69
  open: boolean
55
70
  onClose: () => void
56
71
  title: string
@@ -81,6 +96,7 @@ export function ModalDialog(props: ModalDialogProps) {
81
96
  'title',
82
97
  'modal',
83
98
  'size',
99
+ 'footer',
84
100
  'description',
85
101
  'headerLeading',
86
102
  'class',
@@ -144,6 +160,7 @@ export function ModalDialog(props: ModalDialogProps) {
144
160
  'gap-4',
145
161
  local.size === 'settings' &&
146
162
  'flex h-208 max-h-full min-h-0 max-w-6xl flex-col gap-0 overflow-hidden p-0',
163
+ local.size === 'fit' && 'flex flex-col overflow-hidden',
147
164
  local.class
148
165
  )}
149
166
  aria-label={local['aria-label'] ?? local.title}
@@ -170,7 +187,19 @@ export function ModalDialog(props: ModalDialogProps) {
170
187
  <DialogDescription>{local.description}</DialogDescription>
171
188
  </Show>
172
189
  </DialogHeader>
173
- {local.children}
190
+ <Show when={local.size === 'fit'} fallback={local.children}>
191
+ {/* The scrollport takes the room the title band and footer leave. The
192
+ padding and matching negative margin keep a focused control's ring
193
+ from being clipped at the scrollport's edge. */}
194
+ <div class="-mx-1 flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto p-1">
195
+ {local.children}
196
+ </div>
197
+ <Show when={local.footer}>
198
+ <div class="flex shrink-0 flex-wrap justify-end gap-2 border-t border-border pt-3">
199
+ {local.footer}
200
+ </div>
201
+ </Show>
202
+ </Show>
174
203
  </DialogContent>
175
204
  </Dialog>
176
205
  </Show>