React and apps
The same configurator in React, React Native, iOS and Android, with the phone’s own AR, share sheet and haptics, and your app’s cart.
React and Next.js
React 18 or 19. The player loads from huesix in the browser, so it adds almost nothing to your bundle and is always current.
npm install https://www.huesix.com/sdk/huesix-react-0.1.0.tgz
import { useRef, useState, type CSSProperties } from 'react';
import { HuesixConfigurator, type HuesixConfiguratorHandle } from '@huesix/react';
export function ProductPage() {
const configurator = useRef<HuesixConfiguratorHandle>(null);
const [total, setTotal] = useState('');
return (
<>
<HuesixConfigurator
ref={configurator}
src="https://www.huesix.com/studio/api/public/<workspace>/<product>/manifest.hxp"
cart
onChange={({ configuration }) => setTotal(configuration.price.formatted)}
// The Add to cart button waits for this promise; a rejection's message is shown.
onAddToCart={async ({ configuration }) => {
const response = await fetch('/api/cart', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
productId: configuration.productId,
shareCode: configuration.shareCode,
}),
});
if (!response.ok) throw new Error('We couldn’t add it to your cart. Try again.');
}}
style={{ '--huesix-height': '640px' } as CSSProperties}
/>
<p>Total: {total}</p>
<button onClick={() => configurator.current?.select('metal:rose-gold')}>
Rose gold
</button>
</>
);
}
-
Props:
src,language,config,cart(trueor'buy-now'),headless,stageOnly,view,shareUrl,loading,interaction,xr,analyticsConsent,className,style, andscriptUrl(another player’s address). They’re the element’s attributes. -
Callbacks:
onReady,onProgress,onChange,onBlocked,onAddToCart(return a promise and the button waits),onEnquiry,onCta,onShare,onXr,onCamera,onHotspot,onLanguage,onOpenGroup,onCollectionChange,onCollectionSelect(returnfalseto show it your own way) andonError. -
The ref has the
element’s methods, typed, and the element
itself (
ref.current.element). -
loadHuesix()loads the player ahead of time, say on a product list, so the configurator opens at once.
Next.js: the component is a client component (it says
'use client'), and renders on the server as the bare element with nothing
to run. A server component can render it with plain props; with callbacks, render it
from a client component of your own:
// app/products/[slug]/configurator.tsx
'use client';
import { HuesixConfigurator } from '@huesix/react';
import { addToCart } from './actions'; // a server action
export function Configurator({ src }: { src: string }) {
return (
<HuesixConfigurator
src={src}
cart
onAddToCart={({ configuration }) =>
addToCart(configuration.productId, configuration.shareCode)
}
/>
);
}
React Native
React Native 0.72 or later, Expo included, with react-native-webview 13 or later. The configurator runs in a WebView on huesix’s app page; the phone does AR and sharing (and haptics, on Android).
npm install https://www.huesix.com/sdk/huesix-react-native-0.1.0.tgz
# Expo
npx expo install react-native-webview
# bare React Native
npm install react-native-webview && cd ios && pod install
import { useRef, useState } from 'react';
import { Button, Text, View } from 'react-native';
import { HuesixConfigurator, type HuesixConfiguratorHandle } from '@huesix/react-native';
export function ProductScreen() {
const configurator = useRef<HuesixConfiguratorHandle>(null);
const [total, setTotal] = useState('');
return (
<View style={{ flex: 1 }}>
<HuesixConfigurator
ref={configurator}
src="https://www.huesix.com/studio/api/public/<workspace>/<product>/manifest.hxp"
shareUrl="https://www.huesix.com/<workspace>/<product>"
cart
onChange={(configuration) => setTotal(configuration.price.formatted)}
// Your app's cart: resolve once it's in, or throw with a message for the shopper.
onAddToCart={(configuration) => cart.add(configuration.productId, configuration.shareCode)}
style={{ flex: 1 }}
/>
<Text>{total}</Text>
<Button title="Rose gold" onPress={() => configurator.current?.select('metal:rose-gold')} />
</View>
);
}
-
Props:
src,shareUrl(the product’s page: shared designs and AR open there),language,config,cart,view,analyticsConsent,playerUrlandstyle. -
Callbacks:
onReady,onChange,onAddToCart(resolve when it’s in your cart; reject with a message for the shopper),onCollectionChange,onShare,onAr(returntrueto open your own AR screen),onSaveImage(Save image hands you the picture as base64),onEvent(the configurator’s other events, by name:enquiry,cta,xr,camera,hotspot,language,analytics,collection-select,back…) andonError. -
The ref:
select,deselect,setSelection,getConfiguration,setCamera,snapshot,addToCart,undo,redo,reset,setLanguage,showCollectionItem,enterAr, andcall(method, ...args)for the app page’s others:toggle,getState,getLanguage,collection,applyLook,setEnvironment,setAnalyticsConsentandtrackConversion. -
Permissions: for trying products on, the camera:
NSCameraUsageDescriptionin iOS’s Info.plist (the app is closed without it) andandroid.permission.CAMERAin AndroidManifest.xml (with Expo:ios.infoPlistandandroid.permissionsin app.json). For Android’s haptics,android.permission.VIBRATE.
iOS (Swift)
iOS 15 or later and Xcode 15 or later, SwiftUI or UIKit. Add HuesixKit either way:
-
Swift Package Manager: download
HuesixKit-0.1.0.tar.gz, open it,
and drag the
HuesixKitfolder into your project (or File → Add Package Dependencies → Add Local…). Then add the HuesixKit library to your app target. -
CocoaPods: in your Podfile,
pod 'HuesixKit', :podspec => 'https://www.huesix.com/sdk/HuesixKit.podspec', thenpod install.
import HuesixKit
import SwiftUI
struct ProductView: View {
@StateObject private var configurator = HuesixController()
var body: some View {
VStack(spacing: 0) {
HuesixConfigurator(
options: HuesixOptions(
src: URL(string: "https://www.huesix.com/studio/api/public/<workspace>/<product>/manifest.hxp")!,
shareURL: URL(string: "https://www.huesix.com/<workspace>/<product>"),
cart: .addToCart
),
controller: configurator,
onAddToCart: { configuration in
// Your app's cart: throw with a message for the shopper if it can't be added.
try await Cart.shared.add(configuration.productId, configuration.shareCode)
}
)
HStack {
Text(configurator.configuration?.price.formatted ?? "")
Spacer()
Button("Rose gold") {
Task { try? await configurator.select("metal:rose-gold") }
}
}
.padding()
}
}
}
In SwiftUI, HuesixConfigurator takes onChange,
onAddToCart, onCollectionChange, onEvent,
onError, onReady, onShare and
onAR (return true when your app opened its own AR screen, with
the USDZ file of the product as configured or the design’s link), in that order. Its
HuesixController publishes configuration and
isReady, and has select, setSelection,
getConfiguration, snapshot, addToCart,
undo, redo, reset, setLanguage,
showCollectionItem, enterAR and call(_:_:).
UIKit: HuesixConfiguratorView(options:) is a UIView with the
same closures (onReady, onChange, onAddToCart,
onCollectionChange, onShare, onAR,
onEvent and onError) and async methods, more of them:
select, deselect, setSelection,
getConfiguration, setCamera, snapshot (a
UIImage), addToCart, undo, redo,
reset, setLanguage, showCollectionItem,
enterAR, and call(_:_:) for the app page’s others (as in React
Native). Setting options with another src shows that product:
the design in its config, else the product’s defaults. The other options
are read when the view is made.
let configurator = HuesixConfiguratorView(options: HuesixOptions(src: manifestURL))
configurator.frame = view.bounds
configurator.autoresizingMask = [.flexibleWidth, .flexibleHeight]
configurator.onChange = { [weak self] configuration in
self?.totalLabel.text = configuration.price.formatted
}
view.addSubview(configurator)
In your Info.plist: NSCameraUsageDescription for trying products on with
the camera (“Lets you try products on with your camera”), and
NSPhotoLibraryAddUsageDescription for Save image’s Save to Photos (“Saves
your design to Photos”). iOS closes an app that uses either without it; leave
.save out of features if you don’t want the second.
Android (Kotlin)
Android 7 (API 24) or later, with Kotlin 2.0 or later and JDK 17. Add huesix’s Maven repository and the library:
// settings.gradle.kts
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://www.huesix.com/maven")
}
}
// app/build.gradle.kts
dependencies {
implementation("com.huesix:huesix-android:0.1.0")
}
import androidx.lifecycle.lifecycleScope
import com.huesix.android.HuesixConfiguratorView
import com.huesix.android.HuesixOptions
import kotlinx.coroutines.launch
// In your Activity's onCreate
val configurator = HuesixConfiguratorView(this)
setContentView(configurator)
configurator.onChange = { configuration -> total.text = configuration.formattedTotal }
configurator.onAddToCart = { configuration ->
// Your app's cart, in a suspend function: throw with a message for the shopper if it
// can't be added.
cart.add(configuration.productId, configuration.shareCode)
}
configurator.show(
HuesixOptions(
src = "https://www.huesix.com/studio/api/public/<workspace>/<product>/manifest.hxp",
shareUrl = "https://www.huesix.com/<workspace>/<product>",
cart = HuesixOptions.Cart.ADD_TO_CART,
),
)
lifecycleScope.launch { configurator.select("metal:rose-gold") }
In Jetpack Compose:
@Composable
fun ProductConfigurator(options: HuesixOptions, onTotal: (String) -> Unit) {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
HuesixConfiguratorView(context).apply {
onChange = { onTotal(it.formattedTotal) }
}
},
// Another product, when options change.
update = { it.show(options) },
)
}
Methods (suspend): select, deselect,
setSelection, getConfiguration, setCamera,
snapshot, addToCart, undo, redo,
reset, setLanguage, showCollectionItem,
enterAr, and call(method, arg…) for the app page’s others (as
in React Native). Listeners: onReady, onChange,
onAddToCart, onCollectionChange, onAr,
onCameraPermissionNeeded, onEvent and onError;
webView is the WebView itself. Calling show with another
src shows that product: the design in its config, else the
product’s defaults. The view can be taken off the screen and put back (a list’s row, a
reused Compose view); an add to cart still under way as it’s taken off isn’t added. For
trying products on, add the CAMERA permission; set
onCameraPermissionNeeded to ask for it, then call
cameraPermissionResult(granted).
What the phone does
| iOS | Android | React Native | |
|---|---|---|---|
| AR | AR Quick Look of the product as configured | The design in Chrome (WebXR), else the model in Scene Viewer | The design in the phone’s browser, else Scene Viewer |
| Share | The share sheet | The share sheet | The share sheet |
| Save image | The share sheet (Save Image, AirDrop) | Saved to Pictures (Android 10 and later) | onSaveImage |
| Haptics | A tick with each choice | A tick with each choice | A tick on Android |
| Links out | Safari over the app | A Custom Tab | The system browser |
Each SDK can hand AR to your own screen instead (onAR on iOS, SwiftUI and
UIKit alike; onAr on Android and in React Native); on iOS and Android,
HuesixOptions’s features (HuesixFeature: AR,
share, save, haptics) keeps any of these on the web’s side. AR offers the shared
design’s link, so give shareUrl (the product’s page from Studio → Publish).
Under the hood
The app SDKs load https://www.huesix.com/embed/app.html, with what to show in the
address’s # as JSON (src, language,
config, shareUrl, cart, view,
analyticsConsent, features). The page passes the
configurator’s events to the app as {type: "event", name, detail} (those
listed for onEvent, with ready, change,
add-to-cart, share, xr-support and
collection-change), runs its calls (huesixApp.call(id, method, args), the methods listed above, answered as {type: "result", id, ok, value}),
takes the cart’s answer (huesixApp.cartDone(id, ok, message); with no
answer within 30 seconds it wasn’t added, and the shopper is told so and can try again),
shows another product in the same page (huesixApp.show(options), the same
JSON: the design in its config, else the product’s defaults), and asks the
app for AR, sharing, saving and haptics ({type: "ar" | "share" | "save" | "haptic", …}). The SDKs keep the WebView on that page: anything else opens outside the app.