@itwin/web-viewer-react-3.0 v0.0.2
iTwin Viewer for Web
The iTwin Web Viewer is a configurable iTwin.js viewer that offers basic tooling and widgets out-of-the-box and can be further extended through the use of iTwin.js UI Providers. This package should be used for web-based applications. For desktop applications, use @itwin/desktop-viewer-react-3.0.
Installation
yarn add @itwin/web-viewer-react-3.0
or
npm install @itwin/web-viewer-react-3.0
Dependencies
Currently, in order to use the iTwin Viewer, the consuming application would need to be compiled using Webpack with the IModeljsLibraryExportsPlugin that is in the @bentley/webpack-tools-core package:
In your webpack.config file:
plugins: [
// NOTE: iTwin.js specific plugin to allow exposing iTwin.js shared libraries into the global scope.
new IModeljsLibraryExportsPlugin(),
If you are creating a new application and are using React, it is advised that you use create-react-app with @bentley/react-scripts, which already include this plugin, as well as some other optimizations. There is also a predefined template that includes the iTwin Viewer package:
npx create-react-app my-app --scripts-version @bentley/react-scripts --template @itwin/web-viewer
React component
import React, { useState, useEffect } from "react";
import { Viewer } from "@itwin/web-viewer-react-3.0";
export const MyViewerComponent = () => {
const contextId = "myConnectProjectId";
const iModelId = "myIModelId";
// authorization client configuration
const authConfig: BrowserAuthorizationClientConfiguration = {
scope: "profile email",
clientId: "my-oidc-client",
redirectUri: "https://myredirecturi.com",
postSignoutRedirectUri: "https://mypostsignouturi.com",
responseType: "code",
};
return (
<Viewer
authConfig={{ config: authConfig }}
contextId={contextId}
iModelId={iModelId}
/>
);
};
Props
Required
contextId
- GUID for the context (project, asset, etc.) that contains the iModel that you wish to viewiModelId
- GUID for the iModel that you wish to viewauthConfig
- OIDC configuration or an instance of an iTwin.js BrowserAuthorizationClient
Optional
changeSetId
- changeset id to view if combined with the contextId and iModelId propsbackend
- backend connection info (defaults to the iTwin General Purpose backend)theme
- override the default themedefaultUiConfig
- hide or override default tooling and widgetscontentManipulationTools
- options for the content manipulation section (top left)cornerItem
- replace the default backstage navigation button with a new itemhideDefaultHorizontalItems
- hide all horizontal tools in the top left section of the viewerhideDefaultVerticalItems
- hide all vertical tools in the top left section of the viewerverticalItems
selectTool
- hide the select toolmeasureTools
- hide the measure toolssectionTools
- hide the section tools
horizontalItems
clearSelection
- hide the clear selection toolclearHideIsolateEmphasizeElements
- hide the clear hide/isolate/emphasize elements toolhideElements
- hide the hide elements toolisolateElements
- hide the isolate elements toolemphasizeElements
- hide the emphasize elements tool
navigationTools
- options for the navigation section (top right)hideDefaultHorizontalItems
- hide all horizontal tools in the top right section of the viewerhideDefaultVerticalItems
- hide all vertical tools in the top right section of the viewerverticalItems
walkView
- hide the walk toolcameraView
- hide the camera tool
horizontalItems
rotateView
- hide the rotate toolpanView
- hide the pan toolfitView
- hide the fit view toolwindowArea
- hide the window area toolundoView
- hide the undo view changes toolredoView
- hide the redo view changes tool
hideToolSettings
- hide the contextual tool settingshideTreeView
- hide the tree view widgethidePropertyGrid
- hide the property grid widgethideDefaultStatusBar
- hide the status bar
productId
- application's GPRIDappInsightsKey
- Application Insights key for telemetryonIModelConnected
- Callback function that executes after the iModel connection is successful and contains the iModel connection as a parameteri18nUrlTemplate
- Override the default url template where i18n resource files are queriedfrontstages
- Provide additional frontstages for the viewer to renderbackstageItems
- Provide additional backstage items for the viewer's backstage composeronIModelAppInit
- Callback function that executes after IModelApp.startup completesviewportOptions
- Additional options for the default frontstage's IModelViewportControladditionalI18nNamespaces
- Additional i18n namespaces to registeradditionalRpcInterfaces
- Additional rpc interfaces to register (assumes that they are supported in your backend)toolAdmin
- OptionalToolAdmin
to registerimodelClient
- provide a client other than the default iModelHub client to access iModels (i.e. iModelBankClient)loadingComponent
- provide a custom React component to override the spinner and text that displays when an iModel is loading
Typescript API
<html>
<div id="viewerRoot">
</html>
import { ItwinViewer } from "@itwin/web-viewer-react-3.0";
const contextId = "myConnectProjectId";
const iModelId = "myIModelId";
// authorization client configuration
const authConfig: BrowserAuthorizationClientConfiguration = {
scope: "profile email",
clientId: "my-oidc-client",
redirectUri: "https://myredirecturi.com",
postSignoutRedirectUri: "https://mypostsignouturi.com",
responseType: "code",
};
const viewer = new iTwinViewer({
elementId: "viewerRoot",
authConfig: {
config: authConfig,
},
});
if (viewer) {
viewer.load({ contextId, iModelId });
}
Blank Viewer
For cases where you would prefer to use a Blank iModelConnection, you should use the BlankViewer React component.
import React, { useState, useEffect } from "react";
import {
BlankConnectionViewState,
BlankViewer,
} from "@itwin/web-viewer-react-3.0";
import { Range3d } from "@itwin/core-geometry";
import { Cartographic, ColorDef } from "@itwin/core-common";
export const MyBlankViewerComponent = () => {
const blankConnection: BlankConnectionProps = {
name: "GeometryConnection",
location: Cartographic.fromDegrees(0, 0, 0),
extents: new Range3d(-30, -30, -30, 30, 30, 30),
};
const viewStateOptions: BlankConnectionViewState = {
displayStyle: {
backgroundColor: ColorDef.blue,
},
};
// authorization client configuration
const authConfig: BrowserAuthorizationClientConfiguration = {
scope: "profile email",
clientId: "my-oidc-client",
redirectUri: "https://myredirecturi.com",
postSignoutRedirectUri: "https://mypostsignouturi.com",
responseType: "code",
};
return (
<BlankViewer
authConfig={{ config: authConfig }}
blankConnection={blankConnection}
viewStateOptions={viewStateOptions}
/>
);
};
It allows for most of the same optional props as the Viewer component, with a few differences:
Required
blankConnection
- Data to use to create the BlankConnection (name, location, extents, etc.). Note that no contextId or iModelId is required for this component
Optional
viewStateOptions
- Override options for the ViewState that is generated for the BlankConnection
Development
When making changes to the src, run npm start
in the package's root folder to enable source watching and rebuild, so the dev-server will have access to updated code on succesful code compilation.
Next Steps
3 years ago