-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathModelViewer.tsx
More file actions
221 lines (209 loc) · 10.9 KB
/
ModelViewer.tsx
File metadata and controls
221 lines (209 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {useState, useEffect, useCallback} from 'react';
import {useLoadScript} from './load-script.js';
import type {Model3d} from './storefront-api-types.js';
import type {PartialDeep} from 'type-fest';
import type {ModelViewerElement} from '@google/model-viewer/lib/model-viewer.js';
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace JSX {
interface IntrinsicElements {
'model-viewer': PartialDeep<
ModelViewerElement,
{recurseIntoArrays: true}
>;
}
}
}
type ModelViewerProps = Omit<
PartialDeep<JSX.IntrinsicElements['model-viewer'], {recurseIntoArrays: true}>,
'src'
> &
ModelViewerBaseProps;
/**
* The `ModelViewer` component renders a 3D model (with the `model-viewer` custom element) for the Storefront API's [Model3d object](https://shopify.dev/api/storefront/reference/products/model3d). The `model-viewer` custom element is lazily downloaded through a dynamically-injected `<script type='module'>` tag when the `<ModelViewer />` component is rendered. ModelViewer is using version `1.21.1` of the `@google/model-viewer` library.
* @publicDocs
*/
export type ModelViewerBaseProps = {
/** An object with fields that correspond to the Storefront API's [Model3D object](https://shopify.dev/api/storefront/2025-07/objects/model3d). */
data: PartialDeep<Model3d, {recurseIntoArrays: true}>;
/** The callback to invoke when the 'error' event is triggered. Refer to [error in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-loading-events-error). */
onError?: (event: Event) => void;
/** The callback to invoke when the `load` event is triggered. Refer to [load in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-loading-events-load). */
onLoad?: (event: Event) => void;
/** The callback to invoke when the 'preload' event is triggered. Refer to [preload in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-loading-events-preload). */
onPreload?: (event: Event) => void;
/** The callback to invoke when the 'model-visibility' event is triggered. Refer to [model-visibility in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-loading-events-modelVisibility). */
onModelVisibility?: (event: Event) => void;
/** The callback to invoke when the 'progress' event is triggered. Refer to [progress in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-loading-events-progress). */
onProgress?: (event: Event) => void;
/** The callback to invoke when the 'ar-status' event is triggered. Refer to [ar-status in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-augmentedreality-events-arStatus). */
onArStatus?: (event: Event) => void;
/** The callback to invoke when the 'ar-tracking' event is triggered. Refer to [ar-tracking in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-augmentedreality-events-arTracking). */
onArTracking?: (event: Event) => void;
/** The callback to invoke when the 'quick-look-button-tapped' event is triggered. Refer to [quick-look-button-tapped in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-augmentedreality-events-quickLookButtonTapped). */
onQuickLookButtonTapped?: (event: Event) => void;
/** The callback to invoke when the 'camera-change' event is triggered. Refer to [camera-change in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-stagingandcameras-events-cameraChange). */
onCameraChange?: (event: Event) => void;
/** The callback to invoke when the 'environment-change' event is triggered. Refer to [environment-change in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-lightingandenv-events-environmentChange). */
onEnvironmentChange?: (event: Event) => void;
/** The callback to invoke when the 'play' event is triggered. Refer to [play in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-animation-events-play). */
onPlay?: (event: Event) => void;
/** The callback to invoke when the 'pause' event is triggered. Refer to [pause in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-animation-events-pause). */
onPause?: (event: Event) => void;
/** The callback to invoke when the 'scene-graph-ready' event is triggered. Refer to [scene-graph-ready in the <model-viewer> documentation](https://modelviewer.dev/docs/index.html#entrydocs-scenegraph-events-sceneGraphReady). */
onSceneGraphReady?: (event: Event) => void;
};
/**
* The `ModelViewer` component renders a 3D model (with the `model-viewer` custom element) for
* the Storefront API's [Model3d object](https://shopify.dev/api/storefront/reference/products/model3d).
*
* The `model-viewer` custom element is lazily downloaded through a dynamically-injected `<script type="module">` tag when the `<ModelViewer />` component is rendered
*
* ModelViewer is using version `1.21.1` of the `@google/model-viewer` library.
* @publicDocs
*/
export function ModelViewer(props: ModelViewerProps): JSX.Element | null {
const [modelViewer, setModelViewer] = useState<undefined | HTMLElement>(
undefined,
);
const callbackRef = useCallback((node: HTMLElement) => {
setModelViewer(node);
}, []);
const {data, children, className, ...passthroughProps} = props;
const modelViewerLoadedStatus = useLoadScript(
'https://unpkg.com/@google/model-viewer@v1.12.1/dist/model-viewer.min.js',
{
module: true,
},
);
useEffect(() => {
const hydrogenEventListener = {
error: passthroughProps.onError,
load: passthroughProps.onLoad,
preload: passthroughProps.onPreload,
'model-visibility': passthroughProps.onModelVisibility,
progress: passthroughProps.onProgress,
'ar-status': passthroughProps.onArStatus,
'ar-tracking': passthroughProps.onArTracking,
'quick-look-button-tapped': passthroughProps.onQuickLookButtonTapped,
'camera-change': passthroughProps.onCameraChange,
'environment-change': passthroughProps.onEnvironmentChange,
play: passthroughProps.onPlay,
pause: passthroughProps.onPause,
'scene-graph-ready': passthroughProps.onSceneGraphReady,
};
if (!modelViewer) {
return;
}
Object.entries(hydrogenEventListener).forEach(
([eventName, callbackFunc]) => {
if (callbackFunc) {
modelViewer.addEventListener(eventName, callbackFunc);
}
},
);
return (): void => {
if (modelViewer == null) {
return;
}
Object.entries(hydrogenEventListener).forEach(
([eventName, callbackFunc]) => {
if (callbackFunc) {
modelViewer.removeEventListener(eventName, callbackFunc);
}
},
);
};
}, [
modelViewer,
passthroughProps.onArStatus,
passthroughProps.onArTracking,
passthroughProps.onCameraChange,
passthroughProps.onEnvironmentChange,
passthroughProps.onError,
passthroughProps.onLoad,
passthroughProps.onModelVisibility,
passthroughProps.onPause,
passthroughProps.onPlay,
passthroughProps.onPreload,
passthroughProps.onProgress,
passthroughProps.onQuickLookButtonTapped,
passthroughProps.onSceneGraphReady,
]);
if (modelViewerLoadedStatus !== 'done') {
// TODO: What do we want to display while the model-viewer library loads?
return null;
}
if (!data.sources?.[0]?.url) {
const sourcesUrlError = `<ModelViewer/> requires 'data.sources' prop to be an array, with an object that has a property 'url' on it. Rendering 'null'`;
if (__HYDROGEN_DEV__) {
throw new Error(sourcesUrlError);
} else {
console.error(sourcesUrlError);
return null;
}
}
if (__HYDROGEN_DEV__ && !data.alt) {
console.warn(
`<ModelViewer/> requires the 'data.alt' prop for accessibility`,
);
}
return (
<model-viewer
ref={callbackRef}
{...passthroughProps}
// @ts-expect-error src should exist
// @eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
class={className}
id={passthroughProps.id ?? data.id}
src={data.sources[0].url}
alt={data.alt ?? null}
camera-controls={passthroughProps.cameraControls ?? true}
poster={(passthroughProps.poster || data.previewImage?.url) ?? null}
autoplay={passthroughProps.autoplay ?? true}
loading={passthroughProps.loading}
reveal={passthroughProps.reveal}
ar={passthroughProps.ar}
ar-modes={passthroughProps.arModes}
ar-scale={passthroughProps.arScale}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
ar-placement={passthroughProps.arPlacement}
ios-src={passthroughProps.iosSrc}
touch-action={passthroughProps.touchAction}
disable-zoom={passthroughProps.disableZoom}
orbit-sensitivity={passthroughProps.orbitSensitivity}
auto-rotate={passthroughProps.autoRotate}
auto-rotate-delay={passthroughProps.autoRotateDelay}
// @ts-expect-error rotationPerSecond should exist as a type, not sure why it doesn't. https://modelviewer.dev/docs/index.html#entrydocs-stagingandcameras-attributes-rotationPerSecond
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
rotation-per-second={passthroughProps.rotationPerSecond}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
interaction-policy={(passthroughProps as any).interactionPolicy}
interaction-prompt={passthroughProps.interactionPrompt}
interaction-prompt-style={passthroughProps.interactionPromptStyle}
interaction-prompt-threshold={passthroughProps.interactionPromptThreshold}
camera-orbit={passthroughProps.cameraOrbit}
camera-target={passthroughProps.cameraTarget}
field-of-view={passthroughProps.fieldOfView}
max-camera-orbit={passthroughProps.maxCameraOrbit}
min-camera-orbit={passthroughProps.minCameraOrbit}
max-field-of-view={passthroughProps.maxFieldOfView}
min-field-of-view={passthroughProps.minFieldOfView}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
bounds={(passthroughProps as any).bounds}
interpolation-decay={passthroughProps.interpolationDecay ?? 100}
skybox-image={passthroughProps.skyboxImage}
environment-image={passthroughProps.environmentImage}
exposure={passthroughProps.exposure}
shadow-intensity={passthroughProps.shadowIntensity ?? 0}
shadow-softness={passthroughProps.shadowSoftness ?? 0}
animation-name={passthroughProps.animationName}
animation-crossfade-duration={passthroughProps.animationCrossfadeDuration}
variant-name={passthroughProps.variantName}
orientation={passthroughProps.orientation}
scale={passthroughProps.scale}
>
{children}
</model-viewer>
);
}