@qr-platform/qr-code.js 0.8.24 → 0.9.0

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.
@@ -515,6 +515,24 @@ if (validationResult.isValid) {
515
515
  - **`QRCodeJs.configureLicenseFetcher(fetcher: (licenseKey: string) => Promise<string>): void`**
516
516
  Configures a custom function for fetching license tokens.
517
517
 
518
+ - **`QRCodeJs.useTemplate(templateNameOrOptions: string | RecursivePartial<Options>): QRCodeBuilder`**
519
+ Creates a `QRCodeBuilder` instance initialized with a specific template.
520
+
521
+ ```typescript
522
+ // Using a template name
523
+ const qrBuilder1 = QRCodeJs.useTemplate('rounded').options({
524
+ data: 'Built with rounded template',
525
+ dotsOptions: { color: '#FF8C00' } // DarkOrange override
526
+ });
527
+
528
+ // Using inline options
529
+ const qrBuilder2 = QRCodeJs.useTemplate({ shape: 'circle' }).options({
530
+ data: 'Built with inline circle shape'
531
+ });
532
+ ```
533
+
534
+ - **`QRCodeJs.useStyle(styleNameOrOptions: string | StyleOptions): QRCodeBuilder`**
535
+ Creates a `QRCodeBuilder` instance initialized with a specific style.
518
536
 
519
537
  - **`QRCodeJs.setTemplate(templateNameOrOptions: string | RecursivePartial<Options>): void`**
520
538
  Sets a default template to be used for subsequent `QRCodeJs` instances. This template's options will be merged with the options provided during instantiation, with the instantiation options taking precedence. You can provide either the name of a predefined template (e.g., `'rounded'`, `'dots'`) or a custom options object.
@@ -536,6 +554,35 @@ if (validationResult.isValid) {
536
554
  ```
537
555
 
538
556
 
557
+
558
+
559
+ ## Fluent Builder Pattern (`useTemplate`, `useStyle`, `build`)
560
+
561
+ For a more structured and readable configuration process, especially when combining predefined settings with custom overrides, QRCode.js offers a fluent builder pattern.
562
+
563
+ **How it Works:**
564
+
565
+ 1. **Start:** Begin by calling `QRCodeJs.useTemplate()` or `QRCodeJs.useStyle()` with either a predefined name (e.g., `'rounded'`) or a custom options/style object. This returns a `QRCodeBuilder` instance.
566
+ 2. **Chain (Optional):** Call `.useTemplate()` or `.useStyle()` again on the builder instance to layer additional templates or styles. Options are merged, with later calls overriding earlier ones for the same properties.
567
+ 3. **Finalize:**
568
+ * Call `.options(yourOptions)` to merge any final, specific options.
569
+ * Or call `.build()` to create the `QRCodeJs` instance with the accumulated configuration. This is optional if `.options(yourOptions)` is NOT called. If `.options(yourOptions)` is called, calling `.build()` is not necessary. You can use `.update()` after `.build()` to update the data.
570
+
571
+ **Combining Templates and Styles:**
572
+
573
+ When you chain `useTemplate` and `useStyle`, their options are merged. If both define the same property (e.g., `dotsOptions.color`), the value from the `useStyles` is always applied over the `useTemplate` options (e.g., `dotsOptions.color` from the `useTemplate` is overridden by the `dotsOptions.color` from the `useStyle`).
574
+
575
+ **Example:**
576
+
577
+ ```typescript
578
+ // Start with 'dots' template, override color with a style, add final data
579
+ const qrCode = QRCodeJs.useTemplate('dots') // Base: dots template (e.g., black square dots)
580
+ .useStyle({ dotsOptions: { color: 'navy' } }) // Style: Change dot color to navy
581
+ .options({ data: 'Built with Template and Style' }) // Final data
582
+
583
+ qrCode.append(document.getElementById('builder-example-container'));
584
+ ```
585
+
539
586
  ## FAQ
540
587
 
541
588
  ### How do I handle CORS issues with embedded images?
@@ -1,5 +1,6 @@
1
1
  import { CanvasOptions } from '~/utils/canvas-options';
2
2
  import { RecursivePartial } from '../types/helper';
3
+ import { StyleOptions } from '../types/style-options';
3
4
  import { Options } from '../utils/options';
4
5
  export declare enum FileExtension {
5
6
  svg = "svg",
@@ -12,6 +13,7 @@ export declare class QRCodeJs {
12
13
  /** Library version injected at build time */
13
14
  static version: string;
14
15
  private static _selectedTemplate;
16
+ private static _selectedStyle;
15
17
  private options;
16
18
  private container?;
17
19
  private qr?;
@@ -24,6 +26,13 @@ export declare class QRCodeJs {
24
26
  } | undefined;
25
27
  constructor(options: RecursivePartial<Options>);
26
28
  static setTemplate(templateNameOrOptions: string | RecursivePartial<Options>): typeof QRCodeJs;
29
+ /**
30
+ * Sets the static style to be used as a base for new instances.
31
+ * Accepts either a predefined style name or a StyleOptions object.
32
+ * @param styleNameOrOptions - The name of the style or the StyleOptions object.
33
+ * @returns The QRCodeJs class for chaining.
34
+ */
35
+ static setStyle(styleNameOrOptions: string | StyleOptions): typeof QRCodeJs;
27
36
  update(options?: RecursivePartial<Options>): Promise<void>;
28
37
  append(
29
38
  /** This container will be used for appending of the QR code */
@@ -0,0 +1,4 @@
1
+ import { StyleOptions } from '../types/style-options';
2
+ export declare const qrStyles: {
3
+ [key: string]: StyleOptions;
4
+ };
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { QRCodeJs as _QRCodeJs } from './core/qr-code-js';
2
2
  import { RecursivePartial } from './types/helper';
3
+ import { StyleOptions } from './types/style-options';
3
4
  import { Options, type Options as QRCodeJsOptions } from './utils/options';
4
5
  import { ScanValidatorResponse } from './utils/scan-validators/abstract-scan-validator';
5
6
  import { type DecodedLicenseToken } from './utils/token-validator';
@@ -12,7 +13,7 @@ export { GradientType, type Gradient } from './utils/gradient';
12
13
  export { STORAGE_KEY } from './license/LicenseManager';
13
14
  export { type BorderOptions, CornerDotType, CornerSquareType, DotType, ImageMode, ShapeType, type Options as QRCodeJsOptions } from './utils/options.js';
14
15
  export declare class QRCodeJs extends _QRCodeJs {
15
- constructor(options: QRCodeJsOptions);
16
+ constructor(options: RecursivePartial<QRCodeJsOptions>);
16
17
  protected _hls(): boolean;
17
18
  static initializeIfNeeded(): Promise<boolean>;
18
19
  static get hls(): boolean;
@@ -50,9 +51,17 @@ export declare class QRCodeJs extends _QRCodeJs {
50
51
  * Creates a QRCodeBuilder instance initialized with a specific template.
51
52
  * Allows for fluent configuration chaining. We need it here to avoid circular dependency
52
53
  * @param templateName - The name of the template to start with.
54
+ * @param templateNameOrOptions - The name of the template or a partial options object to start with.
55
+ * @returns A new QRCodeBuilder instance.
56
+ */
57
+ static useTemplate(templateNameOrOptions: string | RecursivePartial<Options>): QRCodeBuilder;
58
+ /**
59
+ * Creates a QRCodeBuilder instance initialized with a specific style.
60
+ * Allows for fluent configuration chaining.
61
+ * @param styleNameOrOptions - The name of the predefined style or a StyleOptions object.
53
62
  * @returns A new QRCodeBuilder instance.
54
63
  */
55
- static useTemplate(templateName: string): QRCodeBuilder;
64
+ static useStyle(styleNameOrOptions: string | StyleOptions): QRCodeBuilder;
56
65
  }
57
66
  export declare class _ extends QRCodeJs {
58
67
  protected _hls(): boolean;
@@ -61,14 +70,32 @@ export declare class _ extends QRCodeJs {
61
70
  declare class QRCodeBuilder {
62
71
  protected config: RecursivePartial<Options>;
63
72
  /**
64
- * Creates a new QRCodeBuilderCore instance.
65
- * @param templateName - Optional name of a predefined template to start with.
73
+ * Creates a new QRCodeBuilder instance.
74
+ * @param templateNameOrOptions - Optional name of a predefined template or a partial options object to start with.
75
+ */
76
+ constructor(templateNameOrOptions?: string | RecursivePartial<Options>);
77
+ /**
78
+ * Applies a template to the builder's configuration. Template options
79
+ * will be overridden by subsequently chained .style() or .options() calls.
80
+ * @param templateNameOrOptions - The name of the template or a partial options object to apply.
81
+ * @returns The builder instance for chaining.
66
82
  */
67
- constructor(templateName?: string);
83
+ useTemplate(templateNameOrOptions: string | RecursivePartial<Options>): this;
68
84
  /**
69
- * Merges the provided options into the builder's configuration and creates the QRCodeJs instance.
85
+ * Applies style options to the builder's configuration.
86
+ * @param styleNameOrOptions - Name of a predefined style or Style options object to apply.
87
+ * @returns The builder instance for chaining.
88
+ */
89
+ useStyle(styleNameOrOptions: string | StyleOptions): this;
90
+ /**
91
+ * Merges the provided options into the builder's configuration.
70
92
  * @param options - A partial options object to merge.
71
- * @returns The created QRCodeJs instance.
93
+ * @returns The builder instance for chaining.
72
94
  */
73
95
  options(options: RecursivePartial<Options>): QRCodeJs;
96
+ /**
97
+ * Builds the QRCodeJs instance with the accumulated configuration.
98
+ * @returns The created QRCodeJs instance.
99
+ */
100
+ build(): QRCodeJs;
74
101
  }