@widgetic/chat 0.1.16 → 0.1.18

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.
@@ -1,5 +1,14 @@
1
1
  <script lang="ts">
2
2
  import type { ChatContext, ChatConversation } from '../types/index.js';
3
+ import { useChatLanguage } from '../i18n/language.js';
4
+ import { contextTypeLabel } from '../i18n/labels.js';
5
+ import { dirForUserText, useTextDirection } from '@widgetic/design-system/components/textDirection';
6
+
7
+ // The words of the header, in the language of the chat above
8
+ const { t } = useChatLanguage();
9
+
10
+ // The way the screen is read: a title is what a person began the conversation with, and reads by its own first letter on a right to left screen.
11
+ const textDirection = useTextDirection();
3
12
 
4
13
  // Props
5
14
  interface Props {
@@ -28,15 +37,6 @@
28
37
  onSwitchConversation(conversationId);
29
38
  }
30
39
 
31
- function formatContextType(type: string): string {
32
- switch (type) {
33
- case 'frame': return '🖼️ Frame';
34
- case 'text': return '📄 Document';
35
- case 'canvas': return '🎨 Canvas';
36
- default: return '💬 Chat';
37
- }
38
- }
39
-
40
40
  function truncateTitle(title: string, maxLength: number = 30): string {
41
41
  return title.length > maxLength ? title.substring(0, maxLength) + '...' : title;
42
42
  }
@@ -65,7 +65,7 @@
65
65
  {context.title || context.id}
66
66
  </h3>
67
67
  <p class="text-xs text-gray-500 dark:text-gray-400">
68
- {context.type} chat
68
+ {$t('chat.header.typeChat', { type: contextTypeLabel(context.type, $t) })}
69
69
  </p>
70
70
  </div>
71
71
  </div>
@@ -78,8 +78,8 @@
78
78
  <button
79
79
  class="w-8 h-8 rounded-md flex items-center justify-center text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
80
80
  onclick={() => showConversationMenu = !showConversationMenu}
81
- title="Switch conversation"
82
- aria-label="Switch conversation"
81
+ title={$t('chat.header.switchConversation')}
82
+ aria-label={$t('chat.header.switchConversation')}
83
83
  >
84
84
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="w-4 h-4">
85
85
  <circle cx="12" cy="12" r="1"/>
@@ -97,10 +97,10 @@
97
97
  onclick={() => showConversationMenu = false}
98
98
  ></div>
99
99
 
100
- <div class="absolute top-full right-0 mt-1 w-64 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-600 rounded-lg shadow-lg z-50 max-h-80 overflow-hidden">
100
+ <div class="absolute top-full end-0 mt-1 w-64 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-600 rounded-lg shadow-lg z-50 max-h-80 overflow-hidden">
101
101
  <!-- Menu Header -->
102
102
  <div class="flex items-center justify-between p-3 border-b border-gray-200 dark:border-gray-600">
103
- <span class="font-medium text-gray-900 dark:text-gray-100 text-sm">Conversations</span>
103
+ <span class="font-medium text-gray-900 dark:text-gray-100 text-sm">{$t('chat.header.conversations')}</span>
104
104
  <button
105
105
  class="flex items-center gap-1 px-2 py-1 text-xs font-medium text-blue-600 hover:text-blue-800 hover:bg-blue-50 rounded transition-colors"
106
106
  onclick={onNewConversation}
@@ -109,7 +109,7 @@
109
109
  <path d="M12 5v14"/>
110
110
  <path d="M5 12h14"/>
111
111
  </svg>
112
- New
112
+ {$t('chat.header.new')}
113
113
  </button>
114
114
  </div>
115
115
 
@@ -117,14 +117,14 @@
117
117
  <div class="max-h-60 overflow-y-auto">
118
118
  {#each conversations as conversation (conversation.id)}
119
119
  <button
120
- class="w-full flex items-center justify-between p-3 text-left hover:bg-gray-50 dark:hover:bg-gray-700 border-b border-gray-100 dark:border-gray-600 last:border-b-0 transition-colors"
120
+ class="w-full flex items-center justify-between p-3 text-start hover:bg-gray-50 dark:hover:bg-gray-700 border-b border-gray-100 dark:border-gray-600 last:border-b-0 transition-colors"
121
121
  class:active={conversation.id === currentConversationId}
122
122
  onclick={() => {
123
123
  onSwitchConversation(conversation.id);
124
124
  showConversationMenu = false;
125
125
  }}
126
126
  >
127
- <span class="font-medium text-sm truncate text-gray-900 dark:text-gray-100">{conversation.title || 'Untitled'}</span>
127
+ <span class="font-medium text-sm truncate text-gray-900 dark:text-gray-100" dir={dirForUserText($textDirection)}>{conversation.title || $t('chat.header.untitled')}</span>
128
128
  <div class="flex items-center gap-2">
129
129
  {#if conversation._unreadCount && conversation._unreadCount > 0}
130
130
  <span class="bg-blue-600 text-white text-xs px-1.5 py-0.5 rounded-full min-w-[1.25rem] h-5 flex items-center justify-center">
@@ -143,8 +143,8 @@
143
143
  <button
144
144
  class="w-8 h-8 rounded-md flex items-center justify-center text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
145
145
  onclick={onNewConversation}
146
- title="New conversation"
147
- aria-label="Start new conversation"
146
+ title={$t('chat.header.newConversationTitle')}
147
+ aria-label={$t('chat.header.newConversation')}
148
148
  >
149
149
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="w-4 h-4">
150
150
  <path d="M12 5v14"/>
@@ -157,8 +157,8 @@
157
157
  <button
158
158
  class="w-8 h-8 rounded-md flex items-center justify-center text-gray-500 dark:text-gray-400 hover:text-red-600 dark:hover:text-red-400 hover:bg-red-50 dark:hover:bg-red-900 transition-colors"
159
159
  onclick={onClose}
160
- title="Close chat"
161
- aria-label="Close chat"
160
+ title={$t('chat.header.close')}
161
+ aria-label={$t('chat.header.close')}
162
162
  >
163
163
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="w-4 h-4">
164
164
  <path d="M18 6L6 18"/>
@@ -2,6 +2,14 @@
2
2
  import { onMount, onDestroy } from 'svelte';
3
3
  import { chatLog } from '../utils/logger.js';
4
4
  import type { AttachmentInput } from '../types/attachment.js';
5
+ import { useChatLanguage } from '../i18n/language.js';
6
+ import { dirForUserText, useTextDirection } from '@widgetic/design-system/components/textDirection';
7
+
8
+ // The words of the input, in the language of the chat above
9
+ const { t } = useChatLanguage();
10
+
11
+ // The way the screen is read: what a person types reads by its own first letter on a right to left screen.
12
+ const textDirection = useTextDirection();
5
13
 
6
14
  interface Props {
7
15
  value: string;
@@ -21,6 +29,7 @@
21
29
  onAttachmentsChange: (attachments: AttachmentInput[]) => void;
22
30
  /** OS file drag-and-drop onto the input area (not attachment reorder). */
23
31
  onFilesDropped?: (files: File[]) => void;
32
+ /** What the empty box says. Left out, it says the chat's own words (`chat.input.placeholder`). */
24
33
  placeholder?: string;
25
34
  class?: string;
26
35
  style?: string;
@@ -39,13 +48,18 @@
39
48
  onInputChange,
40
49
  onAttachmentsChange,
41
50
  onFilesDropped,
42
- placeholder = "Type your message...",
51
+ placeholder,
43
52
  class: className = '',
44
53
  style = '',
45
54
  }: Props = $props();
46
55
 
47
56
  let isFileDragOver = $state(false);
48
57
 
58
+ // What the send button is called (and says in its tooltip): it turns into Stop while the assistant answers
59
+ let sendLabel = $derived(
60
+ isGenerating ? $t('chat.input.stop') : isSending ? $t('chat.input.sending') : $t('chat.input.send')
61
+ );
62
+
49
63
  function isOsFileDrag(event: DragEvent): boolean {
50
64
  return !!event.dataTransfer?.types?.includes('Files');
51
65
  }
@@ -99,10 +113,10 @@
99
113
 
100
114
  onSend(value.trim(), attachments.length > 0 ? attachments : undefined);
101
115
 
102
- // Keep focus on textarea after sending
116
+ // Keep focus on textarea after sending (without scrolling the page the chat is on)
103
117
  setTimeout(() => {
104
118
  if (textArea) {
105
- textArea.focus();
119
+ textArea.focus({ preventScroll: true });
106
120
  }
107
121
  }, 100);
108
122
  }
@@ -223,10 +237,12 @@
223
237
  textArea.style.height = Math.min(textArea.scrollHeight, maxH) + 'px';
224
238
 
225
239
  // If value was cleared (after sending), refocus
226
- if (value === '' && document.activeElement !== textArea) {
240
+ // (Asked of the tree the text box is in: in a shadow root, on a partner's page, `document.activeElement` is the host element.)
241
+ if (value === '' && (textArea.getRootNode() as Document | ShadowRoot).activeElement !== textArea) {
227
242
  setTimeout(() => {
228
243
  if (textArea) {
229
- textArea.focus();
244
+ // preventScroll: taking the focus back must not scroll the page the chat is on
245
+ textArea.focus({ preventScroll: true });
230
246
  }
231
247
  }, 50);
232
248
  }
@@ -262,13 +278,13 @@
262
278
  ondrop={(e) => handleAttachmentDrop(index, e)}
263
279
  ondragend={handleAttachmentDragEnd}
264
280
  >
265
- <span class="attachment-index-badge absolute top-0.5 left-0.5 z-10 w-4 h-4 flex items-center justify-center rounded-full bg-blue-600 text-white text-[9px] font-bold shadow">{index + 1}</span>
281
+ <span class="attachment-index-badge absolute top-0.5 start-0.5 z-10 w-4 h-4 flex items-center justify-center rounded-full bg-blue-600 text-white text-[9px] font-bold shadow">{index + 1}</span>
266
282
  {#if isImageAttachment(attachment)}
267
283
  <!-- svelte-ignore a11y_click_events_have_key_events -->
268
284
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
269
285
  <img
270
286
  src={getAttachmentPreviewUrl(attachment)}
271
- alt={attachment.file?.name || 'attachment'}
287
+ alt={attachment.file?.name || $t('chat.attachment.namelessAlt')}
272
288
  class="image-preview-ct w-16 h-16 object-cover cursor-pointer"
273
289
  onclick={() => openLightbox(attachment)}
274
290
  />
@@ -276,21 +292,21 @@
276
292
  <!-- File Preview -->
277
293
  <div class="file-preview-ct flex items-center gap-2 p-3 w-32">
278
294
  <span class="text-lg">📄</span>
279
- <span class="text-xs text-gray-600 dark:text-gray-300 truncate flex-1">{attachment.file?.name || 'attachment'}</span>
295
+ <span class="text-xs text-gray-600 dark:text-gray-300 truncate flex-1">{attachment.file?.name || $t('chat.attachment.namelessAlt')}</span>
280
296
  </div>
281
297
  {/if}
282
298
 
283
299
  <!-- Remove Button -->
284
300
  <button
285
- class="remove-attachment-btn absolute top-1 right-1 w-5 h-5 bg-red-500 text-white rounded-full flex items-center justify-center text-xs hover:bg-red-600 cursor-pointer"
301
+ class="remove-attachment-btn absolute top-1 end-1 w-5 h-5 bg-red-500 text-white rounded-full flex items-center justify-center text-xs hover:bg-red-600 cursor-pointer"
286
302
  onclick={() => removeAttachment(index)}
287
- aria-label="Remove attachment"
303
+ aria-label={$t('chat.attachment.remove')}
288
304
  >
289
305
  ✕
290
306
  </button>
291
307
 
292
308
  {#if attachment.uploadProgress && attachment.uploadProgress > 0}
293
- <div class="absolute bottom-0 left-0 right-0 h-1 bg-gray-200">
309
+ <div class="absolute bottom-0 start-0 end-0 h-1 bg-gray-200">
294
310
  <div class="h-full bg-blue-500 transition-all duration-300" style="width: {attachment.uploadProgress}%"></div>
295
311
  </div>
296
312
  {/if}
@@ -307,7 +323,8 @@
307
323
  value={value}
308
324
  oninput={(e) => onInputChange((e.target as HTMLTextAreaElement).value)}
309
325
  onkeydown={handleKeyDown}
310
- {placeholder}
326
+ placeholder={placeholder ?? $t('chat.input.placeholder')}
327
+ dir={value ? dirForUserText($textDirection) : undefined}
311
328
  class="flex-1 bg-transparent border-none outline-none resize-y text-sm placeholder-gray-500 dark:text-gray-100 dark:placeholder-gray-400"
312
329
  style="min-height: 30px; max-height: 120px; overflow-y: auto;"
313
330
  rows="1"
@@ -332,8 +349,8 @@
332
349
  handleSubmit();
333
350
  }}
334
351
  disabled={!isGenerating && ((!value.trim() && attachments.length === 0) || isSending)}
335
- aria-label={isGenerating ? 'Stop generation' : (isSending ? 'Sending message...' : 'Send message')}
336
- title={isGenerating ? 'Stop generation' : (isSending ? 'Sending message...' : 'Send message')}
352
+ aria-label={sendLabel}
353
+ title={sendLabel}
337
354
  >
338
355
  {#if isGenerating}
339
356
  <span class="block w-3 h-3 rounded-[2px] bg-white"></span>
@@ -343,7 +360,7 @@
343
360
  <path d="M22 12a10 10 0 0 1-10 10" fill="none" stroke="white" stroke-width="2" stroke-linecap="round"/>
344
361
  </svg>
345
362
  {:else}
346
- <svg viewBox="0 0 24 24" fill="none" stroke="white" class="w-4 h-4">
363
+ <svg viewBox="0 0 24 24" fill="none" stroke="white" class="w-4 h-4 rtl:-scale-x-100">
347
364
  <path d="M22 2L11 13M22 2l-7 20-4-9-9-4z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
348
365
  </svg>
349
366
  {/if}
@@ -358,11 +375,15 @@
358
375
  <!-- svelte-ignore a11y_click_events_have_key_events -->
359
376
  <!-- svelte-ignore a11y_no_static_element_interactions -->
360
377
  <div class="input-lightbox-overlay" onclick={() => lightboxSrc = null}>
361
- <button class="input-lightbox-close" onclick={() => lightboxSrc = null} title="Close">✕</button>
378
+ <button
379
+ class="input-lightbox-close"
380
+ onclick={() => lightboxSrc = null}
381
+ title={$t('chat.attachment.close')}
382
+ aria-label={$t('chat.attachment.close')}>✕</button>
362
383
  <!-- svelte-ignore a11y_click_events_have_key_events -->
363
384
  <!-- svelte-ignore a11y_no_static_element_interactions -->
364
385
  <div class="input-lightbox-content" onclick={(e) => e.stopPropagation()}>
365
- <img src={lightboxSrc} alt="Attachment preview" class="input-lightbox-image" />
386
+ <img src={lightboxSrc} alt={$t('chat.attachment.previewAlt')} class="input-lightbox-image" />
366
387
  </div>
367
388
  </div>
368
389
  {/if}
@@ -387,7 +408,7 @@
387
408
  .input-lightbox-close {
388
409
  position: absolute;
389
410
  top: 16px;
390
- right: 16px;
411
+ inset-inline-end: 16px;
391
412
  width: 36px;
392
413
  height: 36px;
393
414
  border-radius: 50%;
@@ -17,6 +17,7 @@ interface Props {
17
17
  onAttachmentsChange: (attachments: AttachmentInput[]) => void;
18
18
  /** OS file drag-and-drop onto the input area (not attachment reorder). */
19
19
  onFilesDropped?: (files: File[]) => void;
20
+ /** What the empty box says. Left out, it says the chat's own words (`chat.input.placeholder`). */
20
21
  placeholder?: string;
21
22
  class?: string;
22
23
  style?: string;