Install the Widgets
Install the Widgets once per page before you place any widget or component. The Widgets are web components published as the vviinn-widgets package, so they work in plain HTML and in any front-end framework that allows custom elements.
Every snippet on this page uses the Search Modal (vviinn-vps-button), because it shows results on its own. Replace it with any widget or component from the widget catalog.
Before You Start
Section titled “Before You Start”You need:
- a site in Panel with an imported product feed and an active campaign for the feature the widget shows (the Quickstart sets this up)
- the site’s read-only token, described on Authentication. It can only read your catalog, so it’s safe in front-end code.
Load the Widgets from the CDN
Section titled “Load the Widgets from the CDN”-
Paste both scripts into the
headof your page:<scripttype="module"src="https://cdn.jsdelivr.net/npm/vviinn-widgets@latest/www/build/vviinn-widgets.esm.js"></script><scriptnomodulesrc="https://cdn.jsdelivr.net/npm/vviinn-widgets@latest/www/build/vviinn-widgets.js"></script>The
nomodulescript is only for browsers without JavaScript modules. To pin a version, replacelatestwith a version number (see Choose a Version). -
Place a widget where it should render, with your read-only token:
<vviinn-vps-buttontoken="YOUR_READ_ONLY_TOKEN"locale="en-US"add-style="true"></vviinn-vps-button>You can also create it with the DOM API:
const searchModal = document.createElement("vviinn-vps-button");searchModal.token = "YOUR_READ_ONLY_TOKEN";searchModal.locale = "en-US";searchModal.addStyle = true;document.body.append(searchModal);
Each widget page lists the attributes that matter, and the component reference lists all of them.
Install from npm
Section titled “Install from npm”-
Install the package:
Terminal window npm install vviinn-widgets -
Register the components once, when your app starts:
import { defineCustomElements } from "vviinn-widgets/dist/loader";defineCustomElements();
After that, use the tags in your HTML templates or create them with the DOM API. The framework sections below show where the call goes.
Add the Widgets to a Framework
Section titled “Add the Widgets to a Framework”Call defineCustomElements() once from your app’s entry point or a module it imports. This example puts the call in importVviinn.js and imports it in App.jsx.
importVviinn.js
import { defineCustomElements } from "vviinn-widgets/dist/loader";
defineCustomElements();App.jsx
import "./importVviinn";
function App() { return ( <div className="App"> <vviinn-vps-button token="YOUR_READ_ONLY_TOKEN" locale="en-US" add-style="true"></vviinn-vps-button> </div> );}
export default App;With TypeScript, also declare the vviinn tags for JSX, so the compiler knows their attributes:
importVviinn.ts
import { defineCustomElements } from "vviinn-widgets/dist/loader";import type { JSX as LocalJSX } from "vviinn-widgets/dist/loader";import type { HTMLAttributes } from "react";
type StencilToReact<T> = { [P in keyof T]?: T[P] & Omit<HTMLAttributes<Element>, "className"> & { class?: string; };};
declare module "react" { namespace JSX { interface IntrinsicElements extends StencilToReact<LocalJSX.IntrinsicElements> {} }}
defineCustomElements(window);App.tsx is the same as App.jsx above, importing ./importVviinn. React has known limits with custom elements, summarized on Custom Elements Everywhere. For more, see the Stencil documentation for React.
If another library on the page defines custom elements that clash with the Widgets, don’t call defineCustomElements(). Load the Widgets from the CDN instead, as in Load the Widgets from the CDN, adding the scripts from a hook:
import { useEffect } from "react";
const CDN_URL = "https://cdn.jsdelivr.net/npm/vviinn-widgets@latest/www/build/";
export const useVviinnWidgets = () => { useEffect(() => { const scripts = [ Object.assign(document.createElement("script"), { src: `${CDN_URL}vviinn-widgets.esm.js`, type: "module" }), Object.assign(document.createElement("script"), { src: `${CDN_URL}vviinn-widgets.js`, noModule: true }), ]; scripts.forEach((script) => document.body.appendChild(script));
return () => scripts.forEach((script) => document.body.removeChild(script)); }, []);};Call useVviinnWidgets() in the component that renders the widget, then render the tag as usual.
Angular
Section titled “Angular”Angular needs two changes: allow custom elements in the templates that use the widgets, and call defineCustomElements() once at startup.
In a project with standalone components (the default in recent Angular CLI versions), add CUSTOM_ELEMENTS_SCHEMA to each component that uses the widgets:
import { Component, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
@Component({ selector: "app-root", templateUrl: "./app.html", schemas: [CUSTOM_ELEMENTS_SCHEMA],})export class App {}Then call defineCustomElements() in main.ts after bootstrapApplication():
import { bootstrapApplication } from "@angular/platform-browser";import { appConfig } from "./app/app.config";import { App } from "./app/app";
import { defineCustomElements } from "vviinn-widgets/dist/loader";
bootstrapApplication(App, appConfig) .catch((err) => console.error(err));defineCustomElements();In a project with an AppModule, add CUSTOM_ELEMENTS_SCHEMA to the schemas of AppModule and of every other module that uses the widgets:
import { BrowserModule } from "@angular/platform-browser";import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from "@angular/core";import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
@NgModule({ declarations: [AppComponent], imports: [BrowserModule, FormsModule], bootstrap: [AppComponent], schemas: [CUSTOM_ELEMENTS_SCHEMA],})export class AppModule {}Then call defineCustomElements() in main.ts:
import { enableProdMode } from "@angular/core";import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./app/app.module";import { environment } from "./environments/environment";
import { defineCustomElements } from "vviinn-widgets/dist/loader";
if (environment.production) { enableProdMode();}
platformBrowserDynamic().bootstrapModule(AppModule) .catch((err) => console.error(err));defineCustomElements();For more, see the Stencil documentation for Angular.
Call defineCustomElements() in main.js or main.ts, before you mount the app:
import { createApp } from "vue";import App from "./App.vue";
import { defineCustomElements } from "vviinn-widgets/dist/loader";
defineCustomElements();
createApp(App).mount("#app");Then use the tags in any component:
<template> <vviinn-vps-button token="YOUR_READ_ONLY_TOKEN" locale="en-US" add-style="true"></vviinn-vps-button></template>Also tell the Vue compiler that vviinn- tags are custom elements, or it warns Failed to resolve component. In vite.config.js or vite.config.ts:
export default defineConfig({ plugins: [ vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.startsWith("vviinn-"), }, }, }), ],});For more, see the Stencil documentation for Vue.
To load the Widgets from the CDN, add the scripts with useHead:
<script setup>useHead({ script: [ { src: "https://cdn.jsdelivr.net/npm/vviinn-widgets@latest/www/build/vviinn-widgets.esm.js", type: "module", }, { src: "https://cdn.jsdelivr.net/npm/vviinn-widgets@latest/www/build/vviinn-widgets.js", nomodule: true, }, ],});</script>
<template> <vviinn-vps-button token="YOUR_READ_ONLY_TOKEN" locale="en-US" add-style="true"></vviinn-vps-button></template>To use the npm package instead, install it, then register the components in a client component and use that component in your pages:
<script>import { defineCustomElements } from "vviinn-widgets/dist/loader";
defineCustomElements();</script>
<template> <vviinn-vps-button token="YOUR_READ_ONLY_TOKEN" locale="en-US" add-style="true"></vviinn-vps-button></template>Like Vue, Nuxt needs to know that vviinn- tags are custom elements. In nuxt.config.js:
export default defineNuxtConfig({ vue: { compilerOptions: { isCustomElement: (tag) => tag.startsWith("vviinn-"), }, },});These steps were tested with Nuxt 3.13.
Choose a Version
Section titled “Choose a Version”The vviinn-widgets releases follow semantic versioning. The versions page on npm lists every version and tag:
latest: the current stable release, and the default when you give no versionbeta: may contain features that aren’t released yet- an exact version, such as
2.148.0: what you get never changes
The widget pages, their live previews, and the component reference all follow latest. If you pin a version, check the Widgets changelog to see when a feature was added.
Use the Widgets in a Mobile App
Section titled “Use the Widgets in a Mobile App”A native or cross-platform app shows the Widgets in a WebView: the system component that displays web content inside an app. See the WebView documentation for Android and iOS, or a library for your stack, such as React Native WebView, Flutter WebView or Ionic WebView.
- Build an HTML page with the widgets, as in Load the Widgets from the CDN, and host it on an HTTPS server. Recommendation widgets can also be passed to the
WebViewas an HTML string, but the Search Modal needs HTTPS to use the camera. - In the
WebView, turn on JavaScript and set the JavaScript mode to unrestricted. - Load the page URL, or pass the HTML.
- Give the app camera permission (see below).
- Listen for the widget events you need. To open your app’s own product page, listen for
vviinnProductClickand setproduct-details-redirect="false"on the widget, so it doesn’t follow the product link itself.
On Android, add these permissions to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.VIDEO_CAPTURE" /><uses-permission android:name="android.permission.AUDIO_CAPTURE" />On iOS, add the usage descriptions to Info.plist. Write your own text: iOS shows it to the shopper when it asks for access.
<key>NSCameraUsageDescription</key><string>Search for products with a photo.</string><key>NSMicrophoneUsageDescription</key><string>Search for products by voice.</string>Only the camera permission is required. The examples also ask for the microphone.
This Flutter example uses the webview_flutter and permission_handler packages. It asks for the permissions, loads the hosted page, and forwards vviinnProductClick to the app:
import 'package:flutter/material.dart';import 'package:webview_flutter/webview_flutter.dart';import 'package:permission_handler/permission_handler.dart';
Future main() async { WidgetsFlutterBinding.ensureInitialized(); await Permission.camera.request(); await Permission.microphone.request();
runApp(const MyApp());}
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return const MaterialApp(home: ShopPage()); }}
class ShopPage extends StatefulWidget { const ShopPage({super.key});
@override State<ShopPage> createState() => _ShopPageState();}
class _ShopPageState extends State<ShopPage> { late WebViewController controller;
@override void initState() { super.initState(); controller = WebViewController(onPermissionRequest: (request) => request.grant()) ..setJavaScriptMode(JavaScriptMode.unrestricted) ..addJavaScriptChannel( 'vviinnProductClick', onMessageReceived: (JavaScriptMessage message) { // message.message is the product ID: open your product page here. debugPrint("vviinnProductClick ${message.message}"); }, ) ..setNavigationDelegate(NavigationDelegate( onPageFinished: (String url) { controller.runJavaScript('document.addEventListener("vviinnProductClick", ({ detail }) => {vviinnProductClick.postMessage(detail.productId);});'); }, )) ..loadRequest(Uri.parse('URL_TO_YOUR_HTML_FILE')); }
@override Widget build(BuildContext context) { return Scaffold(body: WebViewWidget(controller: controller)); }}Check That It Works
Section titled “Check That It Works”- Open the page. The Search Modal button appears where you placed it.
- Select the button and upload a photo of a product you sell. Results from your catalog appear in the search window.
To try a widget and its attributes without your own page, paste your read-only token into the Widgets Playground.
Related
Section titled “Related”- Widgets: the catalog of widgets and components, and what each one needs
- Authentication: the read-only token and how to reset it
- Troubleshooting: Widgets: common problems with the Widgets, with their cause and fix
- vviinn/widgets on GitHub: the source code, and the issue tracker for bugs and feature requests. Questions about your account, token or product feed go to support@vviinn.com.