SPFx accessible accordion web part example with keyboard navigation and screen reader focus features

The SharePoint Framework (SPFx) is the modern development model for building client-side solutions in SharePoint Online. SPFx allows developers to create responsive and reusable web parts using technologies such as React, TypeScript, and Fluent UI.

SPFx and PnP React Controls: Using EnhancedThemeProvider for Consistent Theming

Introduction

The SharePoint Framework (SPFx) is the modern development model for building client-side solutions in SharePoint Online. SPFx allows developers to create responsive and reusable web parts using technologies such as React, TypeScript, and Fluent UI.

One of the key challenges when building enterprise applications is maintaining visual consistency across different SharePoint sites. Organizations frequently customize their SharePoint themes to match corporate branding, and modern Microsoft 365 environments also support dark mode and accessibility-focused themes.

To help developers create theme-aware solutions, the PnP React Controls library provides the EnhancedThemeProvider control.

Official Documentation:

PnP EnhancedThemeProvider Documentation


What is EnhancedThemeProvider?

The EnhancedThemeProvider is a React provider that exposes SharePoint theme information to child components.

Instead of hardcoding colors, fonts, and styles, components can retrieve the active SharePoint theme and automatically adapt to:

  • Corporate branding
  • SharePoint site themes
  • Microsoft 365 dark mode
  • High contrast accessibility themes
  • Fluent UI design system updates

This allows SPFx solutions to behave like native SharePoint components.


Why Is Theming Important?

Many developers initially build components using fixed colors:

backgroundColor: "#0078d4"

Although this works, it creates several problems:

  • Colors do not match corporate branding.
  • Dark mode may become unreadable.
  • Accessibility requirements may be violated.
  • Future theme changes require code modifications.

Consider a company that uses:

  • Blue theme for HR
  • Green theme for Finance
  • Dark theme for IT Operations

A hardcoded web part will always look identical regardless of the site theme.

A theme-aware web part automatically adapts to each environment.


Benefits of Theme-Aware Components

Consistent User Experience

Users see web parts that look and feel like native SharePoint components.

Dark Mode Support

No additional development effort is required.

Corporate Branding

Organizations can update branding without modifying custom solutions.

Accessibility

Theme colors are designed to maintain proper contrast ratios.

Easier Maintenance

Visual changes can be controlled centrally through SharePoint themes.


Installing the PnP React Controls Library

If the library is not yet installed:

npm install @pnp/spfx-controls-react --save

Example from the Official Documentation

The following example demonstrates how to obtain the current theme using the useTheme() hook.

import * as React from 'react';
import {
EnhancedThemeProvider,
getDefaultTheme,
useTheme,
ThemeContext
} from "@pnp/spfx-controls-react/lib/EnhancedThemeProvider";
import { DefaultButton } from "@fluentui/react/lib/Button";
const ETP: React.FC = () => {
const theme = useTheme();
return (
<DefaultButton theme={theme}>
Example Child Control
</DefaultButton>
);
}
export default ETP;

Understanding the Code

Importing EnhancedThemeProvider

import {
EnhancedThemeProvider,
useTheme
} from "@pnp/spfx-controls-react/lib/EnhancedThemeProvider";

This imports the provider and the hook used to access the current SharePoint theme.


Accessing the Current Theme

const theme = useTheme();

The hook returns a Fluent UI theme object containing:

theme.palette
theme.fonts
theme.effects
theme.semanticColors

These properties contain all visual information about the active SharePoint theme.


Applying the Theme

<DefaultButton theme={theme}>

The button automatically inherits:

  • Primary color
  • Hover color
  • Pressed color
  • Disabled styling
  • Typography settings

from the current SharePoint theme.


Wrapping Components with EnhancedThemeProvider

A common pattern is wrapping an entire application.

import * as React from 'react';
import { EnhancedThemeProvider } from "@pnp/spfx-controls-react/lib/EnhancedThemeProvider";
import ETP from './ETP';
const App: React.FC = () => {
return (
<EnhancedThemeProvider>
<ETP />
</EnhancedThemeProvider>
);
};
export default App;

All child components can now access the active theme using the useTheme() hook.


Creating a Theme-Aware Component

The following example demonstrates how to use theme colors dynamically.

import * as React from 'react';
import { useTheme } from "@pnp/spfx-controls-react/lib/EnhancedThemeProvider";
const ThemeSample: React.FC = () => {
const theme = useTheme();
return (
<div
style={{
backgroundColor: theme.palette.themePrimary,
color: theme.palette.white,
padding: '20px',
borderRadius: '4px'
}}
>
Current SharePoint Theme
</div>
);
};
export default ThemeSample;

Whenever the SharePoint theme changes, this component updates automatically.


Useful Theme Properties

Primary Color

theme.palette.themePrimary

Used for buttons, highlights, and branding.


Secondary Color

theme.palette.themeSecondary

Used for complementary visual elements.


Text Color

theme.palette.neutralPrimary

Primary text color for the current theme.


Background Color

theme.palette.white

Default background color.


Accent Color

theme.palette.accent

Used for emphasis and interactive elements.


Hardcoded Styling vs Theme-Based Styling

Hardcoded

backgroundColor: "#0078d4"

Problems:

  • No dark mode support
  • Ignores branding
  • Difficult to maintain

Theme-Aware

backgroundColor: theme.palette.themePrimary

Benefits:

  • Automatic theme adaptation
  • Dark mode compatibility
  • Corporate branding support
  • Better accessibility

Real-World Use Cases

EnhancedThemeProvider is particularly useful when developing:

  • Corporate dashboards
  • Employee portals
  • Knowledge management systems
  • Intranet applications
  • Custom SharePoint web parts
  • Microsoft Teams SPFx applications

In large organizations with multiple departments and branding requirements, theme-aware development significantly improves user experience.


Best Practices

Use Theme Values Whenever Possible

Avoid hardcoded colors.

theme.palette.themePrimary

is preferable to

"#0078d4"

Support Accessibility

Use semantic colors provided by the theme rather than custom colors.


Test Different Themes

Validate your solution with:

  • Default SharePoint theme
  • Custom tenant theme
  • Dark mode
  • High contrast mode

Conclusion

The EnhancedThemeProvider is one of the most valuable infrastructure controls in the PnP React Controls library. While it does not create visible UI elements, it provides the foundation for building modern, accessible, and brand-consistent SPFx solutions.

By leveraging EnhancedThemeProvider and the useTheme() hook, developers can ensure that their web parts automatically adapt to SharePoint themes, dark mode, and future branding changes without requiring code modifications.

For enterprise SharePoint development, theme-aware components should be considered a standard best practice rather than an optional enhancement.

Official Documentation:

PnP EnhancedThemeProvider Control


PnP SPFx React Controls Roadmap Progress

WPControlStatus
WP01AccessibleAccordion
WP02Accordion
WP03AdaptiveCardHost
WP04AnimatedDialog
WP05Carousel
WP06ChartControl
WP07Dashboard
WP08DragDropFiles
WP09EnhancedThemeProvider
WP10FieldCollectionData

Edvaldo Guimrães Filho Avatar

Published by