@trycourier/react-designer 0.0.0-canary-20251210164636 → 0.0.0-canary-20251219131027

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 (63) hide show
  1. package/README.md +244 -2
  2. package/dist/cjs/index.js +70 -56
  3. package/dist/cjs/index.js.map +4 -4
  4. package/dist/cjs/styles.css +182 -37
  5. package/dist/components/BrandEditor/Editor/Editor.d.ts +16 -1
  6. package/dist/components/Providers/api/template.d.ts +28 -1
  7. package/dist/components/Providers/store.d.ts +6 -0
  8. package/dist/components/Providers/useBrandActions.d.ts +1 -1
  9. package/dist/components/Providers/useTemplateActions.d.ts +6 -4
  10. package/dist/components/TemplateEditor/Channels/Email/Email.d.ts +3 -2
  11. package/dist/components/TemplateEditor/Channels/Email/EmailEditor.d.ts +2 -1
  12. package/dist/components/TemplateEditor/Channels/Email/EmailLayout.d.ts +1 -1
  13. package/dist/components/TemplateEditor/Channels/Email/SideBar/SideBar.d.ts +6 -3
  14. package/dist/components/TemplateEditor/Channels/Inbox/Inbox.d.ts +1 -1
  15. package/dist/components/TemplateEditor/Channels/Inbox/InboxLayout.d.ts +1 -1
  16. package/dist/components/TemplateEditor/Channels/MSTeams/MSTeams.d.ts +3 -2
  17. package/dist/components/TemplateEditor/Channels/MSTeams/MSTeamsLayout.d.ts +1 -1
  18. package/dist/components/TemplateEditor/Channels/MSTeams/SideBar/SideBar.d.ts +2 -3
  19. package/dist/components/TemplateEditor/Channels/Push/Push.d.ts +1 -1
  20. package/dist/components/TemplateEditor/Channels/Push/PushLayout.d.ts +1 -1
  21. package/dist/components/TemplateEditor/Channels/SMS/SMS.d.ts +1 -1
  22. package/dist/components/TemplateEditor/Channels/SMS/SMSLayout.d.ts +1 -1
  23. package/dist/components/TemplateEditor/Channels/Slack/SideBar/SideBar.d.ts +2 -3
  24. package/dist/components/TemplateEditor/Channels/Slack/Slack.d.ts +3 -2
  25. package/dist/components/TemplateEditor/Channels/Slack/SlackLayout.d.ts +1 -1
  26. package/dist/components/TemplateEditor/Channels/useChannels.d.ts +8 -0
  27. package/dist/components/TemplateEditor/TemplateEditor.d.ts +16 -1
  28. package/dist/components/TemplateEditor/hooks/usePragmaticDnd.d.ts +3 -2
  29. package/dist/components/TemplateEditor/hooks/useSyncEditorItems.d.ts +2 -1
  30. package/dist/components/TemplateEditor/index.d.ts +2 -14
  31. package/dist/components/TemplateEditor/store.d.ts +131 -0
  32. package/dist/components/extensions/Blockquote/Blockquote.types.d.ts +2 -2
  33. package/dist/components/extensions/Button/Button.types.d.ts +12 -12
  34. package/dist/components/extensions/Column/Column.types.d.ts +6 -6
  35. package/dist/components/extensions/Divider/Divider.types.d.ts +1 -1
  36. package/dist/components/extensions/ImageBlock/ImageBlock.types.d.ts +4 -4
  37. package/dist/components/extensions/TextBlock/TextBlock.types.d.ts +4 -4
  38. package/dist/components/extensions/Variable/Variable.d.ts +14 -1
  39. package/dist/components/extensions/Variable/Variable.types.d.ts +37 -0
  40. package/dist/components/extensions/Variable/VariableSuggestions.d.ts +7 -0
  41. package/dist/components/extensions/Variable/index.d.ts +1 -0
  42. package/dist/components/extensions/Variable/suggestion.d.ts +2 -0
  43. package/dist/components/extensions/extension-kit.d.ts +7 -2
  44. package/dist/components/extensions/index.d.ts +1 -1
  45. package/dist/components/hooks/index.d.ts +1 -0
  46. package/dist/components/hooks/useBlockConfig.d.ts +110 -0
  47. package/dist/components/ui/Blocks/CustomCodeBlock/index.d.ts +1 -1
  48. package/dist/components/ui/VariableEditor/VariableAutocomplete.d.ts +16 -0
  49. package/dist/components/ui/VariableEditor/VariableChipBase.d.ts +1 -1
  50. package/dist/components/ui/VariableEditor/VariableEditorToolbar.d.ts +15 -0
  51. package/dist/components/ui/VariableEditor/VariableInput.d.ts +2 -0
  52. package/dist/components/ui/VariableEditor/VariableTextarea.d.ts +2 -0
  53. package/dist/components/ui/VariableEditor/index.d.ts +2 -0
  54. package/dist/esm/index.js +70 -56
  55. package/dist/esm/index.js.map +4 -4
  56. package/dist/esm/styles.css +182 -37
  57. package/dist/index.d.ts +14 -0
  58. package/dist/lib/utils/index.d.ts +1 -0
  59. package/dist/styles.css +182 -37
  60. package/dist/types/elemental.schema.d.ts +3 -3
  61. package/dist/types/index.d.ts +1 -0
  62. package/dist/types/validation.types.d.ts +53 -0
  63. package/package.json +1 -1
package/README.md CHANGED
@@ -289,6 +289,171 @@ The Courier Editor supports any valid variable name, including nested structures
289
289
 
290
290
  Type `{{variableName}}` directly in the editor. The variable will be automatically converted to a variable node when you finish typing. Variable names must follow valid JSON property naming conventions (e.g., `user.firstName`, `company.name`).
291
291
 
292
+ ### Variable Autocomplete
293
+
294
+ When you provide a `variables` prop, the editor shows an autocomplete dropdown when users type `{{`. This guides users to select from available variables and helps prevent typos.
295
+
296
+ ```tsx
297
+ import "@trycourier/react-designer/styles.css";
298
+ import { TemplateEditor, TemplateProvider } from "@trycourier/react-designer";
299
+
300
+ function App() {
301
+ // Define available variables (can be nested objects)
302
+ const variables = {
303
+ user: {
304
+ firstName: "",
305
+ lastName: "",
306
+ email: "",
307
+ },
308
+ order: {
309
+ id: "",
310
+ total: "",
311
+ date: "",
312
+ },
313
+ company: {
314
+ name: "",
315
+ },
316
+ };
317
+
318
+ return (
319
+ <TemplateProvider templateId="template-123" tenantId="tenant-123" token="jwt">
320
+ <TemplateEditor variables={variables} />
321
+ </TemplateProvider>
322
+ );
323
+ }
324
+ ```
325
+
326
+ When users type `{{` in the editor, they'll see a dropdown with:
327
+ - `user.firstName`
328
+ - `user.lastName`
329
+ - `user.email`
330
+ - `order.id`
331
+ - `order.total`
332
+ - `order.date`
333
+ - `company.name`
334
+
335
+ The dropdown filters as the user types, making it easy to find the right variable.
336
+
337
+ **Disabling Autocomplete:**
338
+
339
+ If you want to allow users to type any variable name without suggestions, use the `disableVariablesAutocomplete` prop:
340
+
341
+ ```tsx
342
+ <TemplateEditor disableVariablesAutocomplete />
343
+ ```
344
+
345
+ This is useful when you don't have a predefined list of variables and want to allow any variable name.
346
+
347
+ ### Variable Validation
348
+
349
+ The editor supports custom variable validation, allowing you to restrict which variable names users can enter. This is useful when you have a specific set of allowed replacement variables and want to prevent users from entering arbitrary names that could cause errors downstream.
350
+
351
+ ```tsx
352
+ import "@trycourier/react-designer/styles.css";
353
+ import { TemplateEditor, TemplateProvider } from "@trycourier/react-designer";
354
+ import type { VariableValidationConfig } from "@trycourier/react-designer";
355
+
356
+ const ALLOWED_VARIABLES = [
357
+ "user.firstName",
358
+ "user.lastName",
359
+ "user.email",
360
+ "order.id",
361
+ "order.total",
362
+ ];
363
+
364
+ function App() {
365
+ const variableValidation: VariableValidationConfig = {
366
+ // Custom validator - return true if variable is allowed
367
+ validate: (name) => ALLOWED_VARIABLES.includes(name),
368
+
369
+ // Behavior when validation fails: "mark" (default) or "remove"
370
+ onInvalid: "mark",
371
+
372
+ // Toast message shown on invalid variable
373
+ invalidMessage: (name) =>
374
+ `Variable "${name}" is not allowed. Available: ${ALLOWED_VARIABLES.join(", ")}`,
375
+ };
376
+
377
+ return (
378
+ <TemplateProvider templateId="template-123" tenantId="tenant-123" token="jwt">
379
+ <TemplateEditor variableValidation={variableValidation} />
380
+ </TemplateProvider>
381
+ );
382
+ }
383
+ ```
384
+
385
+ **VariableValidationConfig Options:**
386
+
387
+ | Option | Type | Default | Description |
388
+ |--------|------|---------|-------------|
389
+ | `validate` | `(name: string) => boolean` | - | Custom validator function. Returns `true` if the variable is allowed. Runs after built-in format validation. |
390
+ | `onInvalid` | `"mark" \| "remove"` | `"mark"` | Behavior when validation fails. `"mark"` keeps the chip with invalid styling (red). `"remove"` deletes the chip. |
391
+ | `invalidMessage` | `string \| ((name: string) => string)` | - | Message to show as a toast when validation fails. Can be a static string or a function for dynamic messages. |
392
+ | `overrideFormatValidation` | `boolean` | `false` | If `true`, bypasses built-in format validation entirely. Only the custom `validate` function is used. Use with caution. |
393
+
394
+ **Validation Flow:**
395
+
396
+ 1. **Built-in format validation** runs first (unless `overrideFormatValidation` is `true`), ensuring the variable name follows valid JSON property conventions.
397
+ 2. **Custom validation** runs second (if `validate` is provided), checking against your allowed list or custom rules.
398
+ 3. If validation fails, the `onInvalid` behavior is applied and a toast is shown if `invalidMessage` is configured.
399
+
400
+ ### Block Library Customization
401
+
402
+ The `useBlockConfig` hook allows you to customize the blocks available in the sidebar, set default attributes, and create pre-configured block presets.
403
+
404
+ _Note: `useBlockConfig` must be used inside of the `<TemplateProvider />` context_
405
+
406
+ ```tsx
407
+ import { useBlockConfig, TemplateEditor, TemplateProvider } from "@trycourier/react-designer";
408
+
409
+ function CustomBlocksEditor() {
410
+ const { setVisibleBlocks, setDefaults, registerPreset } = useBlockConfig();
411
+
412
+ useEffect(() => {
413
+ // Register a preset (pre-configured block variant)
414
+ registerPreset({
415
+ type: "button",
416
+ key: "console",
417
+ label: "Go to Console",
418
+ icon: <ExternalLinkIcon />, // Optional custom icon
419
+ attributes: { link: "https://console.example.com", backgroundColor: "#007bff" },
420
+ });
421
+
422
+ // Set default attributes for all new buttons (borderRadius is a number in pixels)
423
+ setDefaults("button", { borderRadius: 4 });
424
+
425
+ // Control which blocks appear in sidebar (and their order)
426
+ setVisibleBlocks([
427
+ "heading",
428
+ "text",
429
+ { type: "button", preset: "console" }, // Preset between built-in blocks
430
+ "image",
431
+ "button",
432
+ ]);
433
+ }, []);
434
+
435
+ return <TemplateEditor />;
436
+ }
437
+ ```
438
+
439
+ **API Reference:**
440
+
441
+ | Function | Description |
442
+ |----------|-------------|
443
+ | `visibleBlocks` | Current visible blocks array |
444
+ | `setVisibleBlocks(blocks)` | Set which blocks appear in sidebar |
445
+ | `resetVisibleBlocks()` | Reset to default blocks |
446
+ | `setDefaults(type, attrs)` | Set default attributes for a block type |
447
+ | `getDefaults(type)` | Get defaults for a block type |
448
+ | `clearDefaults(type)` | Clear defaults for a block type |
449
+ | `registerPreset(preset)` | Register a pre-configured block variant |
450
+ | `unregisterPreset(type, key)` | Remove a preset |
451
+ | `presets` | All registered presets |
452
+ | `getPresetsForType(type)` | Get presets for a specific block type |
453
+ | `insertBlock(type, presetKey?)` | Programmatically insert a block |
454
+
455
+ **Block Types:** `heading`, `text`, `image`, `button`, `divider`, `spacer`, `customCode`, `column`, `blockquote`
456
+
292
457
  ## Error Handling
293
458
 
294
459
  The Courier Editor includes a comprehensive error handling system that automatically provides user-friendly notifications for various error types including API errors, network failures, validation issues, and file upload problems.
@@ -460,6 +625,79 @@ function App() {
460
625
  }
461
626
  ```
462
627
 
628
+ ## Template Duplication
629
+
630
+ The `useTemplateActions` hook provides a `duplicateTemplate` function that allows you to create a copy of the current template with a new ID. This is useful for creating variations of existing templates or providing users with a "Save As" functionality.
631
+
632
+ _Note: `useTemplateActions` must be used inside of the `<TemplateProvider />` context_
633
+
634
+ ```tsx
635
+ import { useTemplateActions, TemplateEditor, TemplateProvider } from "@trycourier/react-designer";
636
+ import { useState } from "react";
637
+
638
+ function DuplicateTemplateExample() {
639
+ const { duplicateTemplate } = useTemplateActions();
640
+
641
+ // Simple duplicate with auto-generated name (adds "-copy" suffix)
642
+ const handleQuickDuplicate = async () => {
643
+ const result = await duplicateTemplate();
644
+ if (result?.success) {
645
+ console.log(`Template duplicated to: ${result.templateId}`);
646
+ }
647
+ };
648
+
649
+ // Duplicate with custom ID
650
+ const handleCustomDuplicate = async () => {
651
+ const result = await duplicateTemplate({
652
+ targetTemplateId: "my-custom-template-id",
653
+ // Optionally provide a custom display name:
654
+ // name: "My Duplicated Template",
655
+ });
656
+ if (result?.success) {
657
+ console.log(`Template duplicated! New ID: ${result.templateId}, Version: ${result.version}`);
658
+ }
659
+ };
660
+
661
+ return (
662
+ <div>
663
+ <TemplateEditor />
664
+ <button onClick={handleQuickDuplicate}>Quick Duplicate</button>
665
+ <button onClick={handleCustomDuplicate}>Duplicate with Custom ID</button>
666
+ </div>
667
+ );
668
+ }
669
+
670
+ function App() {
671
+ return (
672
+ <TemplateProvider templateId="original-template" tenantId="tenant-123" token="jwt">
673
+ <DuplicateTemplateExample />
674
+ </TemplateProvider>
675
+ );
676
+ }
677
+ ```
678
+
679
+ ### Duplicate Template Options
680
+
681
+ All options are optional. If called with no arguments, the function will create a duplicate with ID `{currentTemplateId}-copy`.
682
+
683
+ | Option | Type | Required | Description |
684
+ |--------|------|----------|-------------|
685
+ | `targetTemplateId` | `string` | No | The ID for the new duplicated template. Defaults to `{currentTemplateId}-copy` |
686
+ | `content` | `ElementalContent` | No | Override the content to duplicate (defaults to current editor content) |
687
+ | `name` | `string` | No | Custom display name for the new template (defaults to targetTemplateId) |
688
+
689
+ ### Duplicate Template Result
690
+
691
+ The `duplicateTemplate` function returns a promise that resolves to:
692
+
693
+ ```tsx
694
+ interface DuplicateTemplateResult {
695
+ success: boolean; // Whether the duplication succeeded
696
+ templateId: string; // The ID of the new template
697
+ version?: string; // The version of the newly created template
698
+ }
699
+ ```
700
+
463
701
  ## Brand Editor
464
702
 
465
703
  The Brand Editor component allows you to customize and manage a tenant's brand settings directly within your application. This specialized editor provides an interface for modifying brand colors, logos, and other visual elements that will be applied to your templates.
@@ -550,7 +788,9 @@ The Editor component is the core element that provides the template editing inte
550
788
  | routing | { method: string; channels: string[] } | | Configures multi-channel routing and delivery behavior. The `method` property determines delivery strategy: "single" (deliver via first available channel) or "all" (deliver via all configured channels). The `channels` array defines which delivery channels are available in the editor. |
551
789
  | theme | ThemeObj \| cssClass | | Controls the visual appearance of the editor. Can be a Theme object with styling properties or a CSS class name. |
552
790
  | value | ElementalContent | | Initial content for the editor in ElementalContent format. Used as the starting template when the editor loads. |
553
- | variables | Record<string, any> | | **Deprecated.** Previously used for autocomplete suggestions. Users can now type any variable directly. This prop has no effect. |
791
+ | variableValidation | VariableValidationConfig | | Configuration for custom variable validation. Allows restricting which variable names are allowed and defining behavior on validation failure. See [Variable Validation](#variable-validation) section for details. |
792
+ | variables | Record<string, any> | | Variables available for autocomplete suggestions. When provided, typing `{{` shows a dropdown with matching variables. See [Variable Autocomplete](#variable-autocomplete) section for details. |
793
+ | disableVariablesAutocomplete | boolean | false | When `true`, disables variable autocomplete and allows users to type any variable name directly. When `false` (default), shows autocomplete dropdown with variables from the `variables` prop. |
554
794
 
555
795
  ### Multi-Channel Routing
556
796
 
@@ -606,7 +846,9 @@ The Brand Editor component accepts properties that allow you to customize its be
606
846
  | onChange | (value: BrandSettings) => void | | Callback function that fires whenever brand settings are modified, providing the updated brand configuration values. |
607
847
  | theme | ThemeObj \| cssClass | | Controls the visual appearance of the editor. Can be a Theme object with styling properties or a CSS class name. |
608
848
  | value | BrandSettings | | Initial brand settings values to populate the editor with, including colors, logo, social links, and header style preferences. |
609
- | variables | Record<string, any> | | **Deprecated.** Previously used for autocomplete suggestions. Users can now type any variable directly. This prop has no effect. |
849
+ | variableValidation | VariableValidationConfig | | Configuration for custom variable validation. See [Variable Validation](#variable-validation) section for details. |
850
+ | variables | Record<string, any> | | Variables available for autocomplete suggestions. When provided, typing `{{` shows a dropdown with matching variables. |
851
+ | disableVariablesAutocomplete | boolean | false | When `true`, disables variable autocomplete and allows users to type any variable name directly. |
610
852
 
611
853
  ### Brand Provider
612
854