@bagisto-native/core 1.0.0 β†’ 1.0.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 ADDED
@@ -0,0 +1,368 @@
1
+ # @bagisto-native/core
2
+
3
+ `@bagisto-native/core` is the **foundation module for Bagisto Native**, enabling seamless communication between **web applications** and **native applications** using **Hotwire technology**.
4
+
5
+ It is built to work seamlessly with [**Open Source Headless eCommerce**](https://bagisto.com/en/headless-ecommerce/
6
+ ), and any React.js/Next.js setup allowing developers to connect a headless storefront with native mobile apps.
7
+
8
+ This module provides:
9
+
10
+ - Core **Web Components** for native interaction
11
+ - Hotwire **bridge bundle (`bundle.js`)**
12
+ - Utility functions for **web ↔ native communication**
13
+
14
+
15
+ > **Note:** To create **Bagisto Headless Commerce**, use the following command:
16
+ >
17
+ > ```bash
18
+ > npx -y @bagisto-headless/create your-storefront
19
+ > ```
20
+
21
+
22
+
23
+ ## πŸ“¦ Installation
24
+
25
+ Install the module via npm:
26
+
27
+ ```bash
28
+ npm install @bagisto-native/core
29
+ ```
30
+
31
+ > **Note:** This module must be used alongside `@bagisto-native/react` if you're using React/Next.js.
32
+
33
+
34
+ ## πŸ› οΈ Setup
35
+
36
+ ### 1. Add the Hotwire Bridge Bundle
37
+
38
+ The Hotwire bridge bundle (`bundle.js`) is required for native communication.
39
+
40
+ **Steps:**
41
+
42
+ 1. Navigate to `node_modules/@bagisto-native/core/public`
43
+ 2. Copy `bundle.js`
44
+ 3. Paste it into your project's `public` directory
45
+ 4. Include it in your HTML:
46
+
47
+ ```html
48
+ <script src="/bundle.js"></script>
49
+ ```
50
+ 5. Or, include this as a static script in Next.js or similar frameworks.
51
+
52
+
53
+ ## 🌐 Web Components
54
+
55
+ `@bagisto-native/core` provides the following **Web Components**:
56
+
57
+ | Component | Description |
58
+ | -------------------- | ----------------------------------------------------------------- |
59
+ | `dynamic-button` | Handles native buttons for cart (also cart count), share, barcode, and image search |
60
+ | `hotwire-toast` | Trigger native toast messages |
61
+ | `hotwire-search` | Trigger native search events |
62
+ | `hotwire-location` | Auto-fill checkout address using native location |
63
+ | `hotwire-history-sync` | Send current page URL and tab-title to native apps |
64
+ | `hotwire-theme-mode` | Send current theme mode (light/dark) |
65
+
66
+ > These components are primarily used in web projects but are wrapped by React components in `@bagisto-native/react`.
67
+
68
+ ## ⚑ Usage Examples
69
+
70
+ ### Hotwire Toast
71
+
72
+ ```tsx
73
+ import '@bagisto-native/core';
74
+
75
+ <hotwire-toast style="display: none;"></hotwire-toast>
76
+ ```
77
+
78
+ Trigger a toast using the `triggerHotwireNativeToast(message: string)` function.
79
+
80
+ ```ts
81
+ import { triggerHotwireNativeToast } from '@bagisto-native/core';
82
+
83
+ triggerHotwireNativeToast('Hello World!');
84
+ ```
85
+
86
+
87
+ ### Hotwire History Sync
88
+
89
+ ```tsx
90
+ import '@bagisto-native/core';
91
+
92
+ <hotwire-history-sync style="display: none;"></hotwire-history-sync>
93
+ ```
94
+
95
+ Send current url and tab-title using the `triggerHistorySyncEvent(url: URL, tabTitle: string)` function.
96
+
97
+ ```ts
98
+ import { triggerHistorySyncEvent } from '@bagisto-native/core';
99
+
100
+ triggerHistorySyncEvent(new URL('https://example.com'));
101
+ ```
102
+
103
+ ### Hotwire Theme Mode
104
+
105
+ ```tsx
106
+ import '@bagisto-native/core';
107
+
108
+ <hotwire-theme-mode style="display: none;"></hotwire-theme-mode>
109
+ ```
110
+
111
+ Send theme mode using the `triggerThemeModeEvent(mode: 'light' | 'dark')` function.
112
+
113
+ ```ts
114
+ import { triggerThemeModeEvent } from '@bagisto-native/core';
115
+
116
+ triggerThemeModeEvent('light'); // 'light' or 'dark'
117
+ ```
118
+
119
+ ### Hotwire Search
120
+
121
+ ```tsx
122
+ import '@bagisto-native/core';
123
+
124
+ <hotwire-search style="display: none;"></hotwire-search>
125
+ ```
126
+
127
+ Listen to search events:
128
+
129
+ ```ts
130
+ // Listen for a custom event from Turbo.
131
+ const handleTurboSearch = (e: Event) => {
132
+ const customEvent = e as CustomEvent<{ query?: string; code?: string }>;
133
+ const query = customEvent.detail.query || customEvent.detail.code;
134
+ if (!query) return;
135
+ router.push(`/search?q=${encodeURIComponent(query)}`);
136
+ };
137
+
138
+ window.addEventListener("turbo:next-search", handleTurboSearch);
139
+ ```
140
+
141
+ ### Hotwire Location
142
+
143
+ ```tsx
144
+ import '@bagisto-native/core';
145
+
146
+ <hotwire-location style="display: none;"></hotwire-location>
147
+ ```
148
+
149
+ Listen to location events:
150
+
151
+ ```ts
152
+ // Listen for a custom event from Turbo.
153
+ const handleLocationEvent = (e: Event) => {
154
+ const customEvent = e as CustomEvent<{ data: any }>;
155
+ const data = customEvent.detail.data;
156
+ console.log(data);
157
+
158
+ // Here, data include the current address of the user's device.
159
+ };
160
+
161
+ window.addEventListener("turbo:hotwire-app-fill-addresses", handleLocationEvent);
162
+ ```
163
+
164
+ ### Dynamic Button
165
+
166
+ ```tsx
167
+ import '@bagisto-native/core';
168
+
169
+ <dynamic-button data-page-type="home" style="display: none;"></dynamic-button>
170
+ ```
171
+
172
+ > Use `pageType='home'` on homepage, `pageType='product'` on product pages.
173
+
174
+ On both the home page and product page variants, the dynamic button component also shows the cart icon in the native app area. When we click on the cart icon, it emits the turbo:next-cart-modal event. You can open the cart modal or route to the cart page as per your requirement by listening to this event.
175
+
176
+ If you want to open the cart modal and also include the native modal close button on the screen, you can use the following configuration of DynamicButton:
177
+
178
+ ```tsx
179
+ <dynamic-button
180
+ data-action="click->bridge--dynamicbutton#sendModalOpenEvent"
181
+ style="display: none;"
182
+ >
183
+ </dynamic-button>
184
+
185
+ <dynamic-button
186
+ data-action="click->bridge--dynamicbutton#sendModalDismissEvent"
187
+ style="display: none;"
188
+ >
189
+ </dynamic-button>
190
+ ```
191
+
192
+ Now, you also have to send the event to the native side of the application when you open the modal, and also send the event when you close the modal via external sources.
193
+
194
+ For sending the modal open event, use the following method:
195
+
196
+ ```tsx
197
+ import { triggerDynamicButtonModalOpenEvent } from '@bagisto-native/core';
198
+
199
+ triggerDynamicButtonModalOpenEvent();
200
+ ```
201
+
202
+ For sending the modal dismiss event, use the following method:
203
+
204
+ ```tsx
205
+ import { triggerDynamicButtonModalDismissEvent } from '@bagisto-native/core';
206
+
207
+ triggerDynamicButtonModalDismissEvent();
208
+ ```
209
+
210
+ However, without the native cart modal close icon, you can handle the cart modal or any other modal by only listening to the turbo:next-cart-modal event.
211
+
212
+ For using the cart count variant, you need to add the following configuration of DynamicButton:
213
+
214
+ ```tsx
215
+ <dynamic-button
216
+ data-action="click->bridge--dynamicbutton#sendCartCount"
217
+ style="display: none;"
218
+ >
219
+ </dynamic-button>
220
+ ```
221
+ Now, whenever the cart count changes, you also need to notify the native app. For this, you can use the following method from `@bagisto-native/core`:
222
+
223
+ ```tsx
224
+ import { triggerCartCountValue } from '@bagisto-native/core';
225
+
226
+ triggerCartCountValue(5);
227
+ ```
228
+
229
+ ##### Dynamic Button Home Page Variant
230
+
231
+ It basically shows three icons on the home page. The first one is for the Image Search feature, the second one is for the Barcode/QR code feature, and the third one is for the Cart and cart count feature.
232
+
233
+ When you click on the Image Search icon or the Barcode/QR code icon, the corresponding native component opens and sends the processed data via the `turbo:next-search` event. You can listen to this event and handle the search process.
234
+
235
+ ```tsx
236
+ const handleTurboSearch = (e: Event) => {
237
+ const customEvent = e as CustomEvent<{ query?: string; code?: string }>;
238
+ const query = customEvent.detail.query || customEvent.detail.code;
239
+ if (!query) return;
240
+ window.location.href = `/search?q=${encodeURIComponent(query)}`;
241
+ };
242
+
243
+ window.addEventListener("turbo:next-search", handleTurboSearch);
244
+ ```
245
+
246
+ ##### Dynamic Button Product Page Variant
247
+
248
+ In this variant, the dynamic button component shows the product share icon and cart icon in the application’s native area. The cart icon works the same as in the home page variant of the dynamic button component. When you click on the product share icon, it will open the native share modal, and you can handle the share process.
249
+
250
+
251
+ ##### Dynamic Button Emtpy Variant
252
+
253
+ When we use the home or product variant of the dynamic button and then navigate to the auth pages, the previous-page icon remains visible. To hide this icon on the auth pages, use the empty variant of the dynamic button.
254
+
255
+ ```ts
256
+ <dynamic-button data-page-type="empty" style="display: none;"></dynamic-button>
257
+ ```
258
+
259
+
260
+ ## ⚑ Utility Functions
261
+
262
+ `@bagisto-native/core` provides several **helper functions**:
263
+
264
+ ### `triggerHotwireNativeToast(message: string)`
265
+
266
+ Trigger a native toast message (hotwire-toast component must be present in the DOM):
267
+
268
+ ```ts
269
+ import { triggerHotwireNativeToast } from "@bagisto-native/core";
270
+
271
+ triggerHotwireNativeToast("Hello World!");
272
+ ```
273
+
274
+
275
+ ### `triggerHistorySyncEvent(url: URL, tabTitle: string)`
276
+
277
+ Send the current URL and tab-title to the native app (hotwire-history-sync component must be present in the DOM):
278
+
279
+ ```ts
280
+ import { triggerHistorySyncEvent } from "@bagisto-native/core";
281
+
282
+ const url = new URL(window.location.href);
283
+ const tabTitle = document?.title || "";
284
+ triggerHistorySyncEvent(url, tabTitle);
285
+ ```
286
+
287
+
288
+ ### `triggerThemeModeEvent(mode: "light" | "dark")`
289
+
290
+ Send the current theme mode (hotwire-theme-mode component must be present in the DOM):
291
+
292
+ ```ts
293
+ import { triggerThemeModeEvent } from "@bagisto-native/core";
294
+
295
+ triggerThemeModeEvent("light");
296
+ ```
297
+
298
+
299
+ ### `triggerCartCountValue(cartCount: number)`
300
+
301
+ Send the cart count to the native app (the dynamic-button click->bridge--dynamicbutton#sendCartCount action variant must be present in the DOM):
302
+
303
+ ```ts
304
+ import { triggerCartCountValue } from "@bagisto-native/core";
305
+
306
+ triggerCartCountValue(3);
307
+ ```
308
+
309
+
310
+ ### `isTurboNativeUserAgent(userAgent?: string): boolean`
311
+
312
+ Check if the current environment is a Turbo Native app:
313
+
314
+ ```ts
315
+ import { isTurboNativeUserAgent } from "@bagisto-native/core";
316
+
317
+ // Client-side
318
+ if (isTurboNativeUserAgent()) {
319
+ console.log("Running in Turbo Native");
320
+ }
321
+
322
+ // Server-side
323
+ isTurboNativeUserAgent(req.headers['user-agent']);
324
+
325
+ ```
326
+
327
+ ### `triggerDynamicButtonModalOpenEvent()`
328
+
329
+ Trigger a modal open event (the dynamic-button click->bridge--dynamicbutton#sendModalOpenEvent action variant must be present in the DOM):
330
+
331
+ ```ts
332
+ import { triggerDynamicButtonModalOpenEvent } from "@bagisto-native/core";
333
+
334
+ triggerDynamicButtonModalOpenEvent();
335
+ ```
336
+
337
+ ### `triggerDynamicButtonModalDismissEvent()`
338
+
339
+ Trigger a modal dismiss event (the dynamic-button click->bridge--dynamicbutton#sendModalDismissEvent action variant must be present in the DOM):
340
+
341
+ ```ts
342
+ import { triggerDynamicButtonModalDismissEvent } from "@bagisto-native/core";
343
+
344
+ triggerDynamicButtonModalDismissEvent();
345
+ ```
346
+
347
+ ## Build the Native iOS App
348
+
349
+ After completing the setup of **Bagisto Native**, you can proceed to build the native iOS application using the official open-source repository.
350
+
351
+ πŸ”— Bagisto Native iOS App:
352
+ https://github.com/SocialMobikul/BagistoNative_iOS
353
+
354
+
355
+ ## πŸ“‹ Requirements
356
+
357
+ * Node.js >= 18
358
+ * Works with any web project; for React/Next.js, use `@bagisto-native/react` for wrappers
359
+
360
+
361
+ ## πŸ†˜ Support
362
+
363
+ Open an issue or discussion in the repository if you need help.
364
+
365
+
366
+ ## πŸ“„ License
367
+
368
+ MIT License