Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/welcome-bubble-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chatcops/widget": minor
---

Make welcomeBubble text optional, defaulting to the welcomeBubbleDefault locale string. An empty data-welcome-bubble attribute now enables the welcome bubble with the default text.
2 changes: 1 addition & 1 deletion api/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface WidgetConfig {
welcomeBubble?: {
delay?: number;
showOnce?: boolean;
text: string;
text?: string;
};
placeholder?: string;
persistence?: Persistence;
Expand Down
2 changes: 1 addition & 1 deletion api/widget.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export interface WidgetConfig {
welcomeBubble?: {
delay?: number;
showOnce?: boolean;
text: string;
text?: string;
};
placeholder?: string;
persistence?: Persistence;
Expand Down
4 changes: 2 additions & 2 deletions packages/widget/src/script-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export function configFromScript(script: HTMLScriptElement): WidgetConfig | unde
subtitle: script.dataset.brandSubtitle,
},
welcomeMessage: script.dataset.welcomeMessage,
welcomeBubble: script.dataset.welcomeBubble
welcomeBubble: script.dataset.welcomeBubble != null
? {
text: script.dataset.welcomeBubble,
text: script.dataset.welcomeBubble || undefined,
delay: /^\d+$/.test(script.dataset.welcomeBubbleDelay ?? '') ? Number(script.dataset.welcomeBubbleDelay) : undefined,
}
: undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/widget/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface WidgetConfig {
disclosure?: false | { text?: string; learnMoreUrl?: string };
suggestions?: string[];
welcomeBubble?: {
text: string;
text?: string;
delay?: number;
showOnce?: boolean;
};
Expand Down Expand Up @@ -331,7 +331,7 @@ export class Widget {
private createBubble(): void {
if (this.isInline || this.bubble || !this.config.welcomeBubble) return;
this.bubble = new WelcomeBubble(this.shadow, {
text: this.config.welcomeBubble.text,
text: this.config.welcomeBubble.text || this.locale.welcomeBubbleDefault,
delay: this.config.welcomeBubble.delay,
showOnce: this.config.welcomeBubble.showOnce ?? true,
store: this.storage.store,
Expand Down
24 changes: 23 additions & 1 deletion packages/widget/tests/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createLoader, localeUrlFor, panelUrlFor, type PanelModule } from '../src/loader/loader.js';
import { Widget } from '../src/widget.js';
import { Widget, type WidgetConfig } from '../src/widget.js';

/** The panel chunk, as the loader would import it, with a way to hold or fail the load. */
function panel() {
Expand Down Expand Up @@ -435,6 +435,28 @@ describe('translations in the loader', () => {
expect(welcome()).toContain('How can I help you today?');
loader.destroy();
});

it('passes the loaded translation via strings when the welcome bubble is due', async () => {
vi.useFakeTimers();
let passedConfig: WidgetConfig | undefined;
class SpiedWidget extends Widget {
constructor(config: WidgetConfig) {
super(config);
passedConfig = config;
}
}
const load = vi.fn(() => Promise.resolve({ Widget: SpiedWidget as unknown as typeof Widget }));
const loadLocale = vi.fn(() => Promise.resolve({ default: { welcomeBubbleDefault: 'Brauchen Sie Hilfe?' } }));
const loader = createLoader(load, loadLocale);
loader.init({ apiUrl: 'https://api.test/chat', locale: 'de', welcomeBubble: {} });

await vi.advanceTimersByTimeAsync(3000);
expect(loadLocale).toHaveBeenCalledWith('de');
expect(passedConfig?.strings).toEqual(expect.objectContaining({ welcomeBubbleDefault: 'Brauchen Sie Hilfe?' }));
const bubble = shadow()?.querySelector('.cc-bubble');
expect(bubble?.querySelector('.cc-bubble-trigger')?.textContent).toBe('Brauchen Sie Hilfe?');
loader.destroy();
});
});

describe('localeUrlFor', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/widget/tests/script-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ describe('configFromScript', () => {
expect(config).toMatchObject({ apiUrl: '/api/chat', mode: undefined, container: undefined, welcomeBubble: undefined, autoOpen: undefined });
});

it('enables the bubble with default text when data-welcome-bubble is empty, and shows no bubble without the attribute', () => {
const withEmpty = configFromScript(script({ apiUrl: '/api/chat', welcomeBubble: '' }));
expect(withEmpty?.welcomeBubble).toEqual({ text: undefined, delay: undefined });

const without = configFromScript(script({ apiUrl: '/api/chat' }));
expect(without?.welcomeBubble).toBeUndefined();
});

it('reads the nonce property from a script tag', () => {
const tag = script({ apiUrl: '/api/chat' });
tag.nonce = 'request-nonce';
Expand Down
31 changes: 31 additions & 0 deletions packages/widget/tests/widget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,37 @@ describe('Widget', () => {
expect(bubbleOf(widget).classList.contains('cc-visible')).toBe(true);
widget.destroy();
});

it('shows the default text when welcomeBubble text is omitted, including translated strings', () => {
const { widget } = start({ welcomeBubble: {} });
vi.advanceTimersByTime(3001);
const bubble = bubbleOf(widget);
expect(bubble.classList.contains('cc-visible')).toBe(true);
expect(bubble.querySelector('.cc-bubble-trigger')?.textContent).toBe('Need help? Chat with us!');
widget.destroy();

localStorage.clear();
const translated = start({
welcomeBubble: {},
strings: { welcomeBubbleDefault: 'Brauchen Sie Hilfe? Schreiben Sie uns!' },
});
vi.advanceTimersByTime(3001);
const translatedBubble = bubbleOf(translated.widget);
expect(translatedBubble.classList.contains('cc-visible')).toBe(true);
expect(translatedBubble.querySelector('.cc-bubble-trigger')?.textContent).toBe(
'Brauchen Sie Hilfe? Schreiben Sie uns!'
);
translated.widget.destroy();

localStorage.clear();
const delayed = start({ welcomeBubble: { delay: 5000 } });
vi.advanceTimersByTime(4999);
expect(bubbleOf(delayed.widget).classList.contains('cc-visible')).toBe(false);
vi.advanceTimersByTime(2);
expect(bubbleOf(delayed.widget).classList.contains('cc-visible')).toBe(true);
expect(bubbleOf(delayed.widget).querySelector('.cc-bubble-trigger')?.textContent).toBe('Need help? Chat with us!');
delayed.widget.destroy();
});
});

it.each(['bottom-right', 'bottom-left'] as const)(
Expand Down
4 changes: 2 additions & 2 deletions website/src/content/docs/widget/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ interface WidgetConfig {
};
suggestions?: string[]; // Suggested question buttons shown in a new conversation
welcomeBubble?: { // Popup mode only
text: string; // Text shown on hover bubble
text?: string; // Text shown on bubble, default: welcomeBubbleDefault
delay?: number; // ms before bubble appears, 0 to 2147483647 (about 24.8 days), default: 3000. A larger number waits that long; NaN, a negative or a non-number uses the default
showOnce?: boolean; // Show only once (default: true): per browser with 'local' persistence, per tab with 'session', every page load with 'none'
};
Expand Down Expand Up @@ -372,7 +372,7 @@ See the [pre-chat form guide](/widget/pre-chat-form) for field behavior, validat
| `data-disclosure` | `disclosure` | `"false"` turns the AI disclosure off |
| `data-disclosure-text` | `disclosure.text` | Wording of the AI disclosure |
| `data-disclosure-url` | `disclosure.learnMoreUrl` | "Learn more" link in the AI disclosure |
| `data-welcome-bubble` | `welcomeBubble.text` | Welcome bubble text |
| `data-welcome-bubble` | `welcomeBubble.text` | Welcome bubble text; an empty value enables the bubble with default text |
| `data-welcome-bubble-delay` | `welcomeBubble.delay` | Bubble delay in ms (0–2147483647) |
| `data-placeholder` | `placeholder` | Input placeholder |
| `data-auto-open` | `autoOpen` | Auto-open delay in ms (0–2147483647) or `true`; invalid values are ignored |
Expand Down
Loading