@liquidcommerce/elements-sdk 2.7.0 → 2.7.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.
- package/README.md +83 -2750
- package/dist/index.checkout.esm.js +6898 -6837
- package/dist/index.esm.js +11463 -10993
- package/dist/types/auto-initialize/shared-utils.d.ts +5 -0
- package/dist/types/constants/core.constant.d.ts +0 -4
- package/dist/types/core/base-component.service.d.ts +2 -1
- package/dist/types/core/pubsub/interfaces/checkout.interface.d.ts +1 -0
- package/dist/types/enums/core.enum.d.ts +11 -0
- package/dist/types/interfaces/configs/product-list.interface.d.ts +2 -2
- package/dist/types/interfaces/core.interface.d.ts +5 -4
- package/dist/types/modules/address/address-input.component.d.ts +11 -0
- package/dist/types/modules/checkout/components/checkout-stripe-form.component.d.ts +2 -1
- package/dist/types/modules/product-list/components/card-components/index.d.ts +3 -0
- package/dist/types/modules/product-list/components/card-components/product-badge.d.ts +8 -0
- package/dist/types/modules/product-list/components/card-components/product-fulfillments.d.ts +2 -0
- package/dist/types/modules/product-list/components/card-components/product-price-and-personalization.d.ts +13 -0
- package/dist/types/modules/product-list/components/card-components/product-title.d.ts +6 -0
- package/dist/types/modules/product-list/components/index.d.ts +1 -0
- package/dist/types/modules/product-list/components/product-list-engraving.component.d.ts +5 -1
- package/dist/types/modules/product-list/components/product-list-product-pre-cart.component.d.ts +28 -0
- package/dist/types/modules/product-list/components/product-list-retailers.component.d.ts +10 -2
- package/dist/types/modules/product-list/product-list-card.component.d.ts +0 -5
- package/dist/types/modules/product-list/product-list.commands.d.ts +11 -2
- package/dist/types/modules/product-list/product-list.interface.d.ts +1 -0
- package/dist/types/modules/ui-components/engraving/engraving-form.component.d.ts +11 -2
- package/dist/types/modules/ui-components/lce-element/lce-element.component.d.ts +2 -1
- package/dist/types/utils/dom-compat.d.ts +2 -0
- package/docs/gitbook/actions.md +964 -0
- package/docs/gitbook/address.md +48 -0
- package/docs/gitbook/cart.md +65 -0
- package/docs/gitbook/checkout.md +131 -0
- package/docs/gitbook/events.md +1765 -0
- package/docs/gitbook/overview.md +166 -0
- package/docs/gitbook/product.md +64 -0
- package/docs/gitbook/quick-start-guide.md +393 -0
- package/docs/v1/README.md +210 -0
- package/docs/v1/api/actions/address-actions.md +281 -0
- package/docs/v1/api/actions/cart-actions.md +337 -0
- package/docs/v1/api/actions/checkout-actions.md +387 -0
- package/docs/v1/api/actions/product-actions.md +115 -0
- package/docs/v1/api/client.md +482 -0
- package/docs/v1/api/configuration.md +1 -0
- package/docs/v1/api/injection-methods.md +247 -0
- package/docs/v1/api/typescript-types.md +1 -0
- package/docs/v1/api/ui-helpers.md +200 -0
- package/docs/v1/examples/advanced-patterns.md +96 -0
- package/docs/v1/examples/checkout-flow.md +91 -0
- package/docs/v1/examples/custom-theming.md +63 -0
- package/docs/v1/examples/multi-product-page.md +90 -0
- package/docs/v1/examples/simple-product-page.md +89 -0
- package/docs/v1/getting-started/concepts.md +507 -0
- package/docs/v1/getting-started/installation.md +328 -0
- package/docs/v1/getting-started/quick-start.md +405 -0
- package/docs/v1/guides/address-component.md +431 -0
- package/docs/v1/guides/best-practices.md +324 -0
- package/docs/v1/guides/cart-component.md +737 -0
- package/docs/v1/guides/checkout-component.md +672 -0
- package/docs/v1/guides/events.md +926 -0
- package/docs/v1/guides/product-component.md +686 -0
- package/docs/v1/guides/product-list-component.md +598 -0
- package/docs/v1/guides/theming.md +216 -0
- package/docs/v1/integration/angular.md +39 -0
- package/docs/v1/integration/laravel.md +41 -0
- package/docs/v1/integration/nextjs.md +60 -0
- package/docs/v1/integration/proxy-setup.md +89 -0
- package/docs/v1/integration/react.md +64 -0
- package/docs/v1/integration/vanilla-js.md +84 -0
- package/docs/v1/integration/vue.md +34 -0
- package/docs/v1/reference/browser-support.md +44 -0
- package/docs/v1/reference/error-handling.md +70 -0
- package/docs/v1/reference/performance.md +54 -0
- package/docs/v1/reference/troubleshooting.md +64 -0
- package/package.json +1 -1
- package/docs/ACTIONS.md +0 -1301
- package/docs/BROWSER_SUPPORT.md +0 -279
- package/docs/CONFIGURATION.md +0 -997
- package/docs/DOCUMENTATION_INDEX.md +0 -319
- package/docs/EVENTS.md +0 -798
- package/docs/PROXY.md +0 -228
- package/docs/THEMING.md +0 -681
- package/docs/TROUBLESHOOTING.md +0 -793
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Multi-Product Page
|
|
2
|
+
|
|
3
|
+
Show multiple products on a single page using declarative attributes or programmatic injection.
|
|
4
|
+
|
|
5
|
+
## Option A: Declarative (Data Attributes)
|
|
6
|
+
|
|
7
|
+
```html
|
|
8
|
+
<!DOCTYPE html>
|
|
9
|
+
<html lang="en">
|
|
10
|
+
<head>
|
|
11
|
+
<meta charset="UTF-8" />
|
|
12
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
13
|
+
<title>Multi-Product Page</title>
|
|
14
|
+
|
|
15
|
+
<script
|
|
16
|
+
defer
|
|
17
|
+
data-liquid-commerce-elements
|
|
18
|
+
data-token="YOUR_API_KEY"
|
|
19
|
+
data-env="production"
|
|
20
|
+
data-container-1="product-1"
|
|
21
|
+
data-product-1="00619947000020"
|
|
22
|
+
data-container-2="product-2"
|
|
23
|
+
data-product-2="08504405135"
|
|
24
|
+
data-container-3="product-3"
|
|
25
|
+
data-product-3="08068660001"
|
|
26
|
+
type="text/javascript"
|
|
27
|
+
src="https://assets-elements.liquidcommerce.us/all/elements.js"
|
|
28
|
+
></script>
|
|
29
|
+
|
|
30
|
+
<style>
|
|
31
|
+
body { font-family: Arial, sans-serif; margin: 0; padding: 24px; background: #f7f7f7; }
|
|
32
|
+
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; }
|
|
33
|
+
.card { background: #fff; border-radius: 10px; padding: 16px; min-height: 520px; }
|
|
34
|
+
</style>
|
|
35
|
+
</head>
|
|
36
|
+
<body>
|
|
37
|
+
<h1>Featured Products</h1>
|
|
38
|
+
<div class="grid">
|
|
39
|
+
<div id="product-1" class="card"></div>
|
|
40
|
+
<div id="product-2" class="card"></div>
|
|
41
|
+
<div id="product-3" class="card"></div>
|
|
42
|
+
</div>
|
|
43
|
+
</body>
|
|
44
|
+
</html>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Option B: JSON Configuration
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script
|
|
51
|
+
defer
|
|
52
|
+
data-liquid-commerce-elements
|
|
53
|
+
data-token="YOUR_API_KEY"
|
|
54
|
+
data-env="production"
|
|
55
|
+
type="text/javascript"
|
|
56
|
+
src="https://assets-elements.liquidcommerce.us/all/elements.js"
|
|
57
|
+
></script>
|
|
58
|
+
|
|
59
|
+
<script data-liquid-commerce-elements-products type="application/json">
|
|
60
|
+
[
|
|
61
|
+
{ "containerId": "product-1", "identifier": "00619947000020" },
|
|
62
|
+
{ "containerId": "product-2", "identifier": "08504405135" },
|
|
63
|
+
{ "containerId": "product-3", "identifier": "08068660001" }
|
|
64
|
+
]
|
|
65
|
+
</script>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Option C: Programmatic Injection (NPM)
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
import { Elements } from '@liquidcommerce/elements-sdk';
|
|
72
|
+
|
|
73
|
+
const client = await Elements('YOUR_API_KEY', { env: 'production' });
|
|
74
|
+
|
|
75
|
+
await client.injectProductElement([
|
|
76
|
+
{ containerId: 'product-1', identifier: '00619947000020' },
|
|
77
|
+
{ containerId: 'product-2', identifier: '08504405135' },
|
|
78
|
+
{ containerId: 'product-3', identifier: '08068660001' }
|
|
79
|
+
]);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Tips
|
|
83
|
+
|
|
84
|
+
- Give each container a `min-height` to reduce layout shift while data loads.
|
|
85
|
+
- Add a cart button once per page (see [Cart Component](../guides/cart-component.md)).
|
|
86
|
+
|
|
87
|
+
## Related Docs
|
|
88
|
+
|
|
89
|
+
- [Product Component](../guides/product-component.md)
|
|
90
|
+
- [Quick Start](../getting-started/quick-start.md)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Simple Product Page
|
|
2
|
+
|
|
3
|
+
A minimal, production-ready product page using the CDN build with a single Product component and the default cart drawer.
|
|
4
|
+
|
|
5
|
+
## What You'll Build
|
|
6
|
+
|
|
7
|
+
- One product component
|
|
8
|
+
- Optional cart button with item count
|
|
9
|
+
- Default cart and checkout flow
|
|
10
|
+
|
|
11
|
+
## Prerequisites
|
|
12
|
+
|
|
13
|
+
- LiquidCommerce API key
|
|
14
|
+
- One product identifier (UPC or grouping ID)
|
|
15
|
+
|
|
16
|
+
## HTML (CDN)
|
|
17
|
+
|
|
18
|
+
```html
|
|
19
|
+
<!DOCTYPE html>
|
|
20
|
+
<html lang="en">
|
|
21
|
+
<head>
|
|
22
|
+
<meta charset="UTF-8" />
|
|
23
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
24
|
+
<title>Simple Product Page</title>
|
|
25
|
+
|
|
26
|
+
<script
|
|
27
|
+
defer
|
|
28
|
+
data-liquid-commerce-elements
|
|
29
|
+
data-token="YOUR_API_KEY"
|
|
30
|
+
data-env="production"
|
|
31
|
+
data-container-1="product"
|
|
32
|
+
data-product-1="00619947000020"
|
|
33
|
+
data-cart-badge-button="header-cart"
|
|
34
|
+
type="text/javascript"
|
|
35
|
+
src="https://assets-elements.liquidcommerce.us/all/elements.js"
|
|
36
|
+
></script>
|
|
37
|
+
|
|
38
|
+
<style>
|
|
39
|
+
body {
|
|
40
|
+
font-family: Arial, sans-serif;
|
|
41
|
+
margin: 0;
|
|
42
|
+
padding: 0;
|
|
43
|
+
background: #f7f7f7;
|
|
44
|
+
}
|
|
45
|
+
header {
|
|
46
|
+
background: #fff;
|
|
47
|
+
padding: 16px 24px;
|
|
48
|
+
box-shadow: 0 2px 6px rgba(0,0,0,0.08);
|
|
49
|
+
display: flex;
|
|
50
|
+
justify-content: space-between;
|
|
51
|
+
align-items: center;
|
|
52
|
+
}
|
|
53
|
+
main {
|
|
54
|
+
max-width: 1100px;
|
|
55
|
+
margin: 24px auto;
|
|
56
|
+
padding: 0 24px;
|
|
57
|
+
}
|
|
58
|
+
#product {
|
|
59
|
+
background: #fff;
|
|
60
|
+
border-radius: 10px;
|
|
61
|
+
padding: 20px;
|
|
62
|
+
min-height: 600px;
|
|
63
|
+
}
|
|
64
|
+
</style>
|
|
65
|
+
</head>
|
|
66
|
+
<body>
|
|
67
|
+
<header>
|
|
68
|
+
<h1>Premium Spirits</h1>
|
|
69
|
+
<div id="header-cart"></div>
|
|
70
|
+
</header>
|
|
71
|
+
|
|
72
|
+
<main>
|
|
73
|
+
<div id="product"></div>
|
|
74
|
+
</main>
|
|
75
|
+
</body>
|
|
76
|
+
</html>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## What to Tweak Next
|
|
80
|
+
|
|
81
|
+
- Change the product identifier: `data-product-1="YOUR_PRODUCT_ID"`
|
|
82
|
+
- Add a second product (see [Multi-Product Page](./multi-product-page.md))
|
|
83
|
+
- Customize styling via theming (see [Theming Guide](../guides/theming.md))
|
|
84
|
+
|
|
85
|
+
## Related Docs
|
|
86
|
+
|
|
87
|
+
- [Product Component](../guides/product-component.md)
|
|
88
|
+
- [Cart Component](../guides/cart-component.md)
|
|
89
|
+
- [Quick Start](../getting-started/quick-start.md)
|
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
# Core Concepts
|
|
2
|
+
|
|
3
|
+
Understanding these fundamental concepts will help you work effectively with the LiquidCommerce Elements SDK.
|
|
4
|
+
|
|
5
|
+
## Web Components Architecture
|
|
6
|
+
|
|
7
|
+
The Elements SDK is built using **Web Components**, a set of web platform APIs that allow you to create custom, reusable HTML elements.
|
|
8
|
+
|
|
9
|
+
### What Are Web Components?
|
|
10
|
+
|
|
11
|
+
Web Components are native browser features that provide:
|
|
12
|
+
|
|
13
|
+
1. **Custom Elements** - Define your own HTML tags
|
|
14
|
+
2. **Shadow DOM** - Encapsulated styling and markup
|
|
15
|
+
3. **HTML Templates** - Reusable DOM fragments
|
|
16
|
+
|
|
17
|
+
### Why Web Components?
|
|
18
|
+
|
|
19
|
+
**Framework Agnostic**
|
|
20
|
+
Works with any JavaScript framework or plain HTML:
|
|
21
|
+
- React, Vue, Angular, Svelte
|
|
22
|
+
- Vanilla JavaScript
|
|
23
|
+
- Static HTML pages
|
|
24
|
+
- Server-rendered pages
|
|
25
|
+
|
|
26
|
+
**Style Encapsulation**
|
|
27
|
+
Shadow DOM prevents CSS conflicts:
|
|
28
|
+
- SDK styles don't leak into your page
|
|
29
|
+
- Your page styles don't affect the SDK
|
|
30
|
+
- Components look consistent everywhere
|
|
31
|
+
|
|
32
|
+
**Future-Proof**
|
|
33
|
+
Built on web standards:
|
|
34
|
+
- No framework lock-in
|
|
35
|
+
- Works in modern browsers natively
|
|
36
|
+
- Follows platform evolution
|
|
37
|
+
|
|
38
|
+
### How It Works
|
|
39
|
+
|
|
40
|
+
When you inject a product:
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
await client.injectProductElement([
|
|
44
|
+
{ containerId: 'product', identifier: '00619947000020' }
|
|
45
|
+
]);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The SDK:
|
|
49
|
+
1. Creates a custom element (e.g., `<product-lc>`)
|
|
50
|
+
2. Attaches it to your container
|
|
51
|
+
3. Renders content in Shadow DOM
|
|
52
|
+
4. Registers event listeners
|
|
53
|
+
|
|
54
|
+
## Client Initialization
|
|
55
|
+
|
|
56
|
+
The SDK client is the main interface for all operations.
|
|
57
|
+
|
|
58
|
+
### Initialization Modes
|
|
59
|
+
|
|
60
|
+
#### Auto-Initialization (CDN)
|
|
61
|
+
|
|
62
|
+
When using the CDN, the SDK auto-initializes on page load:
|
|
63
|
+
|
|
64
|
+
```html
|
|
65
|
+
<script
|
|
66
|
+
defer
|
|
67
|
+
data-liquid-commerce-elements
|
|
68
|
+
data-token="YOUR_API_KEY"
|
|
69
|
+
data-env="production"
|
|
70
|
+
type="text/javascript"
|
|
71
|
+
src="https://assets-elements.liquidcommerce.us/all/elements.js"
|
|
72
|
+
></script>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The client becomes available globally as `window.elements`.
|
|
76
|
+
|
|
77
|
+
#### Programmatic Initialization (NPM)
|
|
78
|
+
|
|
79
|
+
With NPM, you explicitly create the client:
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
import { Elements } from '@liquidcommerce/elements-sdk';
|
|
83
|
+
|
|
84
|
+
const client = await Elements('YOUR_API_KEY', {
|
|
85
|
+
env: 'production'
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Phased Initialization
|
|
90
|
+
|
|
91
|
+
The SDK uses a two-phase initialization strategy for optimal performance:
|
|
92
|
+
|
|
93
|
+
**Phase 1: Essential Services (Immediate)**
|
|
94
|
+
- Authentication
|
|
95
|
+
- Configuration loading
|
|
96
|
+
- Store initialization
|
|
97
|
+
- Theme setup
|
|
98
|
+
- Core component registration
|
|
99
|
+
|
|
100
|
+
**Phase 2: Deferred Services (After 100ms)**
|
|
101
|
+
- Analytics/telemetry
|
|
102
|
+
- Cart pre-loading
|
|
103
|
+
- Heavy component registration
|
|
104
|
+
- Debug panel (if enabled)
|
|
105
|
+
|
|
106
|
+
This ensures fast initial page loads while still providing full functionality.
|
|
107
|
+
|
|
108
|
+
### Client Ready Event
|
|
109
|
+
|
|
110
|
+
Listen for the client ready event to know when the SDK is initialized:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
window.addEventListener('lce:actions.client_ready', (event) => {
|
|
114
|
+
console.log('SDK version:', event.detail.version);
|
|
115
|
+
console.log('Ready at:', event.detail.timestamp);
|
|
116
|
+
|
|
117
|
+
// Safe to use client
|
|
118
|
+
window.elements.actions.cart.openCart();
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Declarative vs Programmatic
|
|
123
|
+
|
|
124
|
+
The SDK offers two approaches for component injection.
|
|
125
|
+
|
|
126
|
+
### Declarative (HTML Attributes)
|
|
127
|
+
|
|
128
|
+
Configure components using HTML data attributes:
|
|
129
|
+
|
|
130
|
+
```html
|
|
131
|
+
<script
|
|
132
|
+
defer
|
|
133
|
+
data-liquid-commerce-elements
|
|
134
|
+
data-token="YOUR_API_KEY"
|
|
135
|
+
data-container-1="product"
|
|
136
|
+
data-product-1="00619947000020"
|
|
137
|
+
type="text/javascript"
|
|
138
|
+
src="https://assets-elements.liquidcommerce.us/all/elements.js"
|
|
139
|
+
></script>
|
|
140
|
+
|
|
141
|
+
<div id="product"></div>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**Advantages:**
|
|
145
|
+
- No JavaScript required
|
|
146
|
+
- Simple for static pages
|
|
147
|
+
- Automatic initialization
|
|
148
|
+
|
|
149
|
+
**Best for:**
|
|
150
|
+
- Static HTML sites
|
|
151
|
+
- CMS platforms (WordPress, Shopify)
|
|
152
|
+
- Quick prototypes
|
|
153
|
+
|
|
154
|
+
### Programmatic (JavaScript API)
|
|
155
|
+
|
|
156
|
+
Use JavaScript methods to inject components:
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
const client = await Elements('YOUR_API_KEY', { env: 'production' });
|
|
160
|
+
|
|
161
|
+
await client.injectProductElement([
|
|
162
|
+
{ containerId: 'product', identifier: '00619947000020' }
|
|
163
|
+
]);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**Advantages:**
|
|
167
|
+
- Dynamic product selection
|
|
168
|
+
- Conditional rendering
|
|
169
|
+
- Framework integration
|
|
170
|
+
- Full control over timing
|
|
171
|
+
|
|
172
|
+
**Best for:**
|
|
173
|
+
- Single-page applications
|
|
174
|
+
- Dynamic content
|
|
175
|
+
- React/Vue/Angular apps
|
|
176
|
+
- Complex interactions
|
|
177
|
+
|
|
178
|
+
### Mixing Both Approaches
|
|
179
|
+
|
|
180
|
+
You can use declarative initialization and programmatic control:
|
|
181
|
+
|
|
182
|
+
```html
|
|
183
|
+
<!-- Auto-initialize SDK -->
|
|
184
|
+
<script
|
|
185
|
+
defer
|
|
186
|
+
data-liquid-commerce-elements
|
|
187
|
+
data-token="YOUR_API_KEY"
|
|
188
|
+
data-env="production"
|
|
189
|
+
type="text/javascript"
|
|
190
|
+
src="https://assets-elements.liquidcommerce.us/all/elements.js"
|
|
191
|
+
></script>
|
|
192
|
+
|
|
193
|
+
<script>
|
|
194
|
+
// Later, add products programmatically
|
|
195
|
+
window.addEventListener('lce:actions.client_ready', async () => {
|
|
196
|
+
await window.elements.injectProductElement([
|
|
197
|
+
{ containerId: 'dynamic-product', identifier: selectedProductId }
|
|
198
|
+
]);
|
|
199
|
+
});
|
|
200
|
+
</script>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## State Management
|
|
204
|
+
|
|
205
|
+
The SDK maintains its own internal state for cart, checkout, and user data.
|
|
206
|
+
|
|
207
|
+
### Store Service
|
|
208
|
+
|
|
209
|
+
The SDK uses a centralized store service that:
|
|
210
|
+
- Maintains reactive state
|
|
211
|
+
- Persists data to localStorage
|
|
212
|
+
- Falls back to API storage
|
|
213
|
+
- Syncs across browser tabs
|
|
214
|
+
|
|
215
|
+
### State Persistence
|
|
216
|
+
|
|
217
|
+
**Cart State:**
|
|
218
|
+
- Saved to localStorage
|
|
219
|
+
- Persists across sessions
|
|
220
|
+
- Syncs across tabs automatically
|
|
221
|
+
- Survives page refreshes
|
|
222
|
+
|
|
223
|
+
**Address State:**
|
|
224
|
+
- Saved when set by user
|
|
225
|
+
- Used for availability checking
|
|
226
|
+
- Cleared when user explicitly clears it
|
|
227
|
+
|
|
228
|
+
**Checkout State:**
|
|
229
|
+
- Created from cart
|
|
230
|
+
- Exists during checkout flow
|
|
231
|
+
- Cleared after order completion
|
|
232
|
+
|
|
233
|
+
### Cross-Tab Synchronization
|
|
234
|
+
|
|
235
|
+
When you update the cart in one tab, all other tabs update automatically:
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
// Tab 1
|
|
239
|
+
await window.elements.actions.cart.addProduct([...]);
|
|
240
|
+
|
|
241
|
+
// Tab 2 automatically receives the update
|
|
242
|
+
// No additional code needed
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Component Lifecycle
|
|
246
|
+
|
|
247
|
+
Understanding how components work helps with debugging and customization.
|
|
248
|
+
|
|
249
|
+
### Injection Lifecycle
|
|
250
|
+
|
|
251
|
+
1. **Validation** - Verify container exists
|
|
252
|
+
2. **Creation** - Create Web Component
|
|
253
|
+
3. **Attachment** - Add to DOM
|
|
254
|
+
4. **Data Loading** - Fetch product/cart data
|
|
255
|
+
5. **Rendering** - Display content
|
|
256
|
+
6. **Event Setup** - Register listeners
|
|
257
|
+
|
|
258
|
+
### Component Rerendering
|
|
259
|
+
|
|
260
|
+
Components automatically rerender when data changes:
|
|
261
|
+
|
|
262
|
+
```javascript
|
|
263
|
+
// This triggers a rerender
|
|
264
|
+
await window.elements.actions.cart.addProduct([...]);
|
|
265
|
+
// Cart component updates automatically
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Manual rerendering:
|
|
269
|
+
|
|
270
|
+
```javascript
|
|
271
|
+
const components = window.elements.getInjectedComponents();
|
|
272
|
+
const productComponent = components.get('product-1');
|
|
273
|
+
|
|
274
|
+
// Force rerender
|
|
275
|
+
productComponent.rerender();
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Component Removal
|
|
279
|
+
|
|
280
|
+
Components are removed when:
|
|
281
|
+
- Their container is removed from DOM
|
|
282
|
+
- Page navigation occurs
|
|
283
|
+
- You explicitly remove them
|
|
284
|
+
|
|
285
|
+
To clean up manually:
|
|
286
|
+
|
|
287
|
+
```javascript
|
|
288
|
+
const container = document.getElementById('product');
|
|
289
|
+
container.innerHTML = ''; // Removes the component
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Event System
|
|
293
|
+
|
|
294
|
+
The SDK uses a publish-subscribe pattern for events.
|
|
295
|
+
|
|
296
|
+
### Event Namespaces
|
|
297
|
+
|
|
298
|
+
All SDK events are namespaced to prevent conflicts:
|
|
299
|
+
|
|
300
|
+
```javascript
|
|
301
|
+
// Action events (user interactions)
|
|
302
|
+
lce:actions.cart_opened
|
|
303
|
+
lce:actions.product_add_to_cart
|
|
304
|
+
|
|
305
|
+
// Form events (checkout forms)
|
|
306
|
+
lce:forms.customer
|
|
307
|
+
lce:forms.billing
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Event Flow
|
|
311
|
+
|
|
312
|
+
1. User interacts with component
|
|
313
|
+
2. Component publishes event
|
|
314
|
+
3. SDK updates internal state
|
|
315
|
+
4. Event bubbles to window
|
|
316
|
+
5. Your code can listen and react
|
|
317
|
+
|
|
318
|
+
### Subscribing to Events
|
|
319
|
+
|
|
320
|
+
```javascript
|
|
321
|
+
window.addEventListener('lce:actions.cart_item_added', (event) => {
|
|
322
|
+
console.log('Item added:', event.detail);
|
|
323
|
+
// { cartId, itemId, quantity, ... }
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
See [Events Guide](../guides/events.md) for all available events.
|
|
328
|
+
|
|
329
|
+
## Error Handling
|
|
330
|
+
|
|
331
|
+
The SDK is designed to fail gracefully and not crash your site.
|
|
332
|
+
|
|
333
|
+
### Error Isolation
|
|
334
|
+
|
|
335
|
+
SDK errors are caught and logged, but don't propagate to your application:
|
|
336
|
+
|
|
337
|
+
```javascript
|
|
338
|
+
// Even if SDK throws an error, your code continues
|
|
339
|
+
try {
|
|
340
|
+
await window.elements.actions.cart.addProduct([/* invalid data */]);
|
|
341
|
+
} catch (error) {
|
|
342
|
+
console.log('This error is caught by SDK internally');
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Your page keeps working
|
|
346
|
+
console.log('Page still functional');
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### Error Types
|
|
350
|
+
|
|
351
|
+
The SDK uses a custom `SDKError` class for all errors:
|
|
352
|
+
|
|
353
|
+
```javascript
|
|
354
|
+
class SDKError extends Error {
|
|
355
|
+
constructor(message, reThrow = false) {
|
|
356
|
+
super(message);
|
|
357
|
+
this.name = 'SDKError';
|
|
358
|
+
this.isSdk = true;
|
|
359
|
+
this.reThrow = reThrow; // Whether to re-throw to the user
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### Debug Mode
|
|
365
|
+
|
|
366
|
+
Enable debug mode for detailed logging:
|
|
367
|
+
|
|
368
|
+
```javascript
|
|
369
|
+
const client = await Elements('YOUR_API_KEY', {
|
|
370
|
+
env: 'development',
|
|
371
|
+
debugMode: 'console' // or 'panel'
|
|
372
|
+
});
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
**Debug Modes:**
|
|
376
|
+
- `'none'` - No debug output (production default)
|
|
377
|
+
- `'console'` - Log to browser console
|
|
378
|
+
- `'panel'` - Show debug panel on page
|
|
379
|
+
|
|
380
|
+
## Security & API Keys
|
|
381
|
+
|
|
382
|
+
### API Key Protection
|
|
383
|
+
|
|
384
|
+
Your API key is used for authentication but has limited privileges:
|
|
385
|
+
|
|
386
|
+
- ✅ Read product catalog
|
|
387
|
+
- ✅ Create carts and orders
|
|
388
|
+
- ✅ Process payments
|
|
389
|
+
- ❌ Access other merchants' data
|
|
390
|
+
- ❌ Modify product catalog
|
|
391
|
+
- ❌ Access admin functions
|
|
392
|
+
|
|
393
|
+
### Environment Separation
|
|
394
|
+
|
|
395
|
+
Use different API keys per environment:
|
|
396
|
+
|
|
397
|
+
```javascript
|
|
398
|
+
// Development
|
|
399
|
+
const client = await Elements('dev_key_abc123', {
|
|
400
|
+
env: 'development'
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Production
|
|
404
|
+
const client = await Elements('prod_key_xyz789', {
|
|
405
|
+
env: 'production'
|
|
406
|
+
});
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Proxy Configuration
|
|
410
|
+
|
|
411
|
+
To hide your API key and avoid ad blockers, use a proxy:
|
|
412
|
+
|
|
413
|
+
```javascript
|
|
414
|
+
const client = await Elements('YOUR_API_KEY', {
|
|
415
|
+
env: 'production',
|
|
416
|
+
proxy: {
|
|
417
|
+
baseUrl: 'https://yourdomain.com/api/elements-proxy'
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
See [Proxy Setup Guide](../integration/proxy-setup.md) for implementation details.
|
|
423
|
+
|
|
424
|
+
## Browser Support
|
|
425
|
+
|
|
426
|
+
The SDK requires modern browser features:
|
|
427
|
+
|
|
428
|
+
**Minimum Versions:**
|
|
429
|
+
- Chrome 66+ (March 2018)
|
|
430
|
+
- Firefox 60+ (May 2018)
|
|
431
|
+
- Safari 12+ (September 2018)
|
|
432
|
+
- Edge 79+ (January 2020)
|
|
433
|
+
|
|
434
|
+
**Required Features:**
|
|
435
|
+
- Custom Elements (Web Components)
|
|
436
|
+
- Shadow DOM
|
|
437
|
+
- ES2018 JavaScript
|
|
438
|
+
- Fetch API
|
|
439
|
+
- LocalStorage
|
|
440
|
+
|
|
441
|
+
For older browsers, include polyfills:
|
|
442
|
+
|
|
443
|
+
```html
|
|
444
|
+
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-bundle.js"></script>
|
|
445
|
+
<script data-liquid-commerce-elements ...></script>
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
See [Browser Support](../reference/browser-support.md) for details.
|
|
449
|
+
|
|
450
|
+
## Performance Considerations
|
|
451
|
+
|
|
452
|
+
The SDK is optimized for performance, but you can help:
|
|
453
|
+
|
|
454
|
+
### Lazy Loading
|
|
455
|
+
|
|
456
|
+
Load the SDK script with `defer`:
|
|
457
|
+
|
|
458
|
+
```html
|
|
459
|
+
<script defer data-liquid-commerce-elements ...></script>
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### Tree Shaking
|
|
463
|
+
|
|
464
|
+
Use the checkout-only build when you don't need products:
|
|
465
|
+
|
|
466
|
+
```javascript
|
|
467
|
+
import { ElementsCheckout } from '@liquidcommerce/elements-sdk/checkout';
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
This reduces bundle size by ~60%.
|
|
471
|
+
|
|
472
|
+
### Component Injection Timing
|
|
473
|
+
|
|
474
|
+
Inject components when needed, not all at once:
|
|
475
|
+
|
|
476
|
+
```javascript
|
|
477
|
+
// Good: Inject visible products first
|
|
478
|
+
await client.injectProductElement([
|
|
479
|
+
{ containerId: 'hero-product', identifier: '001' }
|
|
480
|
+
]);
|
|
481
|
+
|
|
482
|
+
// Then inject others after a delay
|
|
483
|
+
setTimeout(async () => {
|
|
484
|
+
await client.injectProductElement([
|
|
485
|
+
{ containerId: 'related-1', identifier: '002' },
|
|
486
|
+
{ containerId: 'related-2', identifier: '003' }
|
|
487
|
+
]);
|
|
488
|
+
}, 1000);
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
### Image Optimization
|
|
492
|
+
|
|
493
|
+
The SDK automatically:
|
|
494
|
+
- Lazy loads images
|
|
495
|
+
- Uses responsive images
|
|
496
|
+
- Implements carousel virtualization
|
|
497
|
+
|
|
498
|
+
## Next Steps
|
|
499
|
+
|
|
500
|
+
Now that you understand the core concepts:
|
|
501
|
+
|
|
502
|
+
- **[Product Component](../guides/product-component.md)** - Learn about product displays
|
|
503
|
+
- **[Cart Component](../guides/cart-component.md)** - Understand cart functionality
|
|
504
|
+
- **[Checkout Component](../guides/checkout-component.md)** - Master the checkout flow
|
|
505
|
+
- **[Theming](../guides/theming.md)** - Customize the look and feel
|
|
506
|
+
- **[Events](../guides/events.md)** - React to user actions
|
|
507
|
+
- **[API Reference](../api/client.md)** - Explore all available methods
|