Aligning your custom widget to the style and theme of your App or Intranet gives brand consistency and your users the feeling that your custom widget is a part of their app and intranet. In this tutorial, you will learn how to align your custom widget to the same color scheme used in your App or Intranet.
This tutorial is for advanced developers.
You must have already created a custom widget before proceeding with this tutorial. For modern rendering compatibility, review Shadow DOM compatibility for New UX and Content Designer.
On completing this tutorial, you will be familiar with the properties and functions required to align with the text and the background color of your Staffbase App or Intranet.
- Pass the widget API element as a property to your custom widget component.
const factory = (Base, _widgetApi) => { return class CustomBlock extends BaseBlockClass implements BaseBlock { private _root: ReactDOM.Root | null = null;
constructor() { super(); } ... public renderBlock(container: HTMLElement): void { this._root ??= ReactDOM.createRoot(container); this._root.render(<CustomWidget {...this.props} widgetApi={_widgetApi} />); } ... };};In New UX, the Content Designer uses Shadow DOM. Keep styling scoped to your widget and avoid relying on host-page CSS. For host-page custom CSS selectors, see Overview of CSS Selectors for Content Designer and New UX.
For more information on branding, read branding for Staffbase App / Intranet.
- Apply the text and background color of the Staffbase App / Intranet to the main
divelement of your custom widget.
As this tutorial is designed to help you get started applying theme of your app or intranet, only changing text and background color is shown here. You can use this as a base and customize your widget further.
import React, { useEffect, useState, ReactElement } from "react";import { WidgetApi, ColorTheme } from "widget-sdk";
export interface CustomWidgetProps extends BlockAttributes { widgetApi: WidgetApi;}
export const CustomWidget = ({ widgetApi }: CustomWidgetProps): ReactElement => {
const [theme, setTheme] = useState<ColorTheme | null>(null);
useEffect(() => { setTheme(widgetApi.getLegacyAppTheme()); }, []);
return <> <div style={{ color: theme?.textColor, backgroundColor: theme?.bgColor, padding: "20px" }}> <div>Your widget content</div> </div> </>};You have applied Staffbase App or Intranet text and background color to your custom widget.