Adding showcase widgets to your iTwin Viewer

Introduction
This tutorial takes widgets from the sample showcase and adds them to your iTwin Viewer using the uiProviders
prop.
Prerequisites
This tutorial assumes that you already have:
- Explored the Sample Showcase.
- Created your own iTwin Web Viewer based on the Web Application Quick Start.
- Configured your iTwin Viewer with the House Model sample iModel.
- Instructions to use this sample iModel can be found at the create iModel sample tutorial.
- Understand the concept of a UiItemsProvider and how to add widgets on a UiItemsProvider to an iTwin Viewer.
- Completed the “Customizing the iTwin Viewer” tutorial.
1. Understanding the Sample Showcase
We hope you have given the Sample Showcase a tour and enjoyed the many samples provided. You may want to use some of these samples in your own iTwin Viewer. To do so you first need to understand how the showcase works.
If we take a closer look at the files involved in each sample, notice they all follow the same pattern containing a few important files:
[SampleName]App.tsx
- Corresponds toApp.tsx
in the iTwin Viewer template and provides the mainViewer
component.[SampleName]Widget.tsx
- Defines theUiItemsProvider
that will be passed into propuiProviders
for our sample widget component. This widget is the controller for our samples.[SampleName]Api.ts
- Defines widget functionality that uses the iTwin.js API being showcased.[SampleName].scss
- Defines the styles in our css classes used inside the widget.
Given this pattern, it’s simple to identify the parts required to bring our sample showcase to your iTwin Viewer. The component revolves around the [SampleName]Widget.tsx
file so we need to copy all the files associated with our custom widgets UiItemsProvider
and pass it in the Viewer
component.
2. Example using a sample
For this tutorial, we will be taking the widget from sample View Attributes and adding it into our iTwin Viewer.
Starting with our entry file ViewAttributesApp.tsx
, the Viewer
component is shown in a code snippet.
The Viewer component in ViewAttributesApp.tsx file
<Viewer iTwinId={iTwinId} iModelId={iModelId} authClient={authClient} viewportOptions={viewportOptions} defaultUiConfig={default3DSandboxUi} uiProviders={uiProviders} mapLayerOptions={mapLayerOptions} enablePerformanceMonitors={false} theme="dark" />
Prop uiProviders
is passed in as an array that contains the ViewAttributesWidgetProvider
, imported and defined at the top of the file.
Passing ViewAttributesWidgetProvider to uiProviders
import { ViewAttributesWidgetProvider } from "./ViewAttributesWidget"; ... const uiProviders = [new ViewAttributesWidgetProvider()];
To sum it up, the three lines shown in the code snippet are the only lines you will need to add in your iTwin Viewer in App.tsx
.
Lines you need to add in your iTwin Viewer in `App.tsx`
... // Import the widget provider import { ViewAttributesWidgetProvider } from "./ViewAttributesWidget"; ... // Construct the widget provider const uiProviders = [new ViewAttributesWidgetProvider()]; ... // A Prop passed into the <Viewer> component uiProviders={uiProviders}
We now have made all the necessary coding modifications to our iTwin Viewer. We’ll just need to copy the remaining three files to bring our widget over.
ViewAttributesApi.ts
ViewAttributesWidget.tsx
ViewAttributes.scss
For this tutorial, these files will be placed directly in our src directory so your file structure should look similar to this:
Running our iTwin Viewer, notice the same fully functional widget from the sample showcase in your iTwin Viewer.
Feel free to customize these widgets to your liking.
3. Multiple ways to extend uiProviders
If you already have a uiProviders
prop passed in or would like to add more widgets from the sample showcase, the uiProviders
prop takes in an array of providers. Extending the widget is as simple as appending to your array.
You can add to the uiProviders const variable, i.e.:
const uiProviders = [new ViewAttributesWidgetProvider(), new HyerModelingWidgetProvider(), ...]
or ignore the variable completely and pass the array in directly:
<Viewer
iTwinId={iTwinId}
iModelId={iModelId}
authClient={authClient}
viewportOptions={viewportOptions}
defaultUiConfig={default3DSandboxUi}
mapLayerOptions={mapLayerOptions}
theme="dark"
uiProviders={[new ViewAttributesWidgetProvider(), new HyperModelingWidgetProvider(), ...]}
/>
Just remember to copy corresponding files to your source.
If you would like to use an existing UiItemsProvider instead of passing in multiple new ones, just add the widget in your provideWigets()
function along with copying and pasting the react component to your desired location.
Using an existing UiItemsProvider instead of passing in multiple new ones
export class MyCustomUiProvider extends UiItemsProvider { ... // Your custom code public provideWidgets( _stageId: string, _stageUsage: string, location: StagePanelLocation, _section?: StagePanelSection ): ReadonlyArray<AbstractWidgetProps> { const widgets: AbstractWidgetProps[] = []; if (location === StagePanelLocation.Right) { widgets.push({ id: 'ViewAttributesWidget', label: 'View Attributes Controls', defaultState: WidgetState.Floating, getWidgetContent: () => <ViewAttributesWidget />, // Don't forget to copy code for the ViewAttributesWidget }); } } }
As you can see, extending your iTwin Viewer with multiple widgets is simple. It’s completely up to you on how you want to structure your directories and components. Feel free to extend as many widgets as you like.