Introduction

As an HTML-based technology, Dojo makes use of CSS for styling elements across the framework and for applications developed with it.

Dojo promotes encapsulated structural styling of individual widgets for maximum reuse, as well as simplified presentational theming across all widgets within an application. This pattern gives users a predictable way to style and theme their applications, even when using a mixture of widgets from Dojo’s @dojo/widgets library, a third-party provider, or any that may be developed in-house for a particular application.

FeatureDescription
Per-widget stylesheetsCSS Modules can be used to define stylesheets that are scoped to individual widgets, avoiding potential cross-contamination and style name clashes. A widget can refer to its exact CSS class names via typed CSS module imports and IDE autocompletion.
Robust theming supportThemeable widgets can easily be developed that allow for simplified and centralized whole-application theming, as well as single-instance, targeted style tweaking and overriding, if required. CLI tooling is available to support distributing custom themes.
Reactive theme change responseSimilar to other reactive state changes within a Dojo application, only the affected widgets will be re-rendered when a theme change is made at either a widget level or across an entire application.
Abstracted CSS propertiesIndividual CSS modules can refer to a set of centrally-defined :root style variables through CSS custom properties and var().
Simplified third-party widget themingApplications can easily extend their themes to cover third-party widgets, such as those from Dojo’s native widget library, and Dojo also provides out-the-box themes which applications can base their own on. CLI tooling is available to streamline theme creation and composition.

Basic usage

Note: The following examples build upon each other in a linear order. Individual examples are kept brief to only highlight relevant changes from any previous examples.

These examples assume an application with the following name:

package.json

  1. {
  2. "name": "my-app"
  3. }

The application name becomes relevant when specifying widget theme keys.

Styling a widget

  • Defining a CSS module for a widget
  • Using the corresponding typed style classes within the widget’s TypeScript code

src/styles/MyWidget.m.css

  1. .root {
  2. font-family: sans-serif;
  3. }

src/widgets/MyWidget.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import * as css from '../styles/MyWidget.m.css';
  3. const factory = create();
  4. export default factory(function MyWidget() {
  5. return <div classes={[css.root]}>My Widget</div>;
  6. });

Making a widget themeable

src/widgets/MyWidget.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import theme from '@dojo/framework/core/middleware/theme';
  3. import * as css from '../styles/MyWidget.m.css';
  4. const factory = create({ theme });
  5. export default factory(function MyWidget({ middleware: { theme } }) {
  6. const { root } = theme.classes(css);
  7. return <div classes={[root]}>My Widget</div>;
  8. });

Creating a theme

  • Overriding a widget’s default CSS class with custom theme style properties
  • Linking one or more overrides via the appropriate widget theme keys into a theme structure

src/themes/MyTheme/MyWidget.m.css

  1. .root {
  2. color: hotpink;
  3. background-color: slategray;
  4. }

src/themes/MyTheme/theme.ts

  1. import * as myWidgetCss from './MyWidget.m.css';
  2. export default {
  3. 'my-app/MyWidget': myWidgetCss
  4. };

Abstracting common theme properties

  • Importing a central variables.css regular CSS file that defines CSS custom properties
  • Referring to the custom properties via var()

src/themes/variables.css

  1. :root {
  2. --foreground: hotpink;
  3. --background: slategray;
  4. }

src/themes/MyTheme/MyWidget.m.css

  1. @import '../variables.css';
  2. .root {
  3. color: var(--foreground);
  4. background-color: var(--background);
  5. }

Specifying a default application theme

The theme middleware can be used to set the application theme. To set a “default” or initial theme, the theme.set function can be used with the theme.get function to determine if the theme needs to be set. Setting the default theme should be done in the application’s top level widget.

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import theme from '@dojo/framework/core/middleware/theme';
  3. import myTheme from '../themes/MyTheme/theme';
  4. const factory = create({ theme });
  5. export default factory(function App({ middleware: { theme }}) {
  6. // if the theme isn't set, set the default theme
  7. if (!theme.get()) {
  8. theme.set(myTheme);
  9. }
  10. return (
  11. // the application's widgets
  12. );
  13. });

Note: When using both function-based and class-based widgets, the theme needs to be registered with the application registry. This is true when using any class-based widget dependencies such as @dojo/widgets. Please see the class-based theming section for more details.

Changing the theme within an application

src/widgets/ThemeSwitcher.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import theme from '@dojo/framework/core/middleware/theme';
  3. import myTheme from '../themes/MyTheme/theme';
  4. import alternativeTheme from '../themes/MyAlternativeTheme/theme';
  5. const factory = create({ theme });
  6. export default factory(function ThemeSwitcher({ middleware: { theme } }) {
  7. return (
  8. <div>
  9. <button
  10. onclick={() => {
  11. theme.set(myTheme);
  12. }}
  13. >
  14. Use Default Theme
  15. </button>
  16. <button
  17. onclick={() => {
  18. theme.set(alternativeTheme);
  19. }}
  20. >
  21. Use Alternative Theme
  22. </button>
  23. </div>
  24. );
  25. });