Closes #18006: Dispatch event when toggling color mode & document for plugin use

This commit is contained in:
Jeremy Stretch 2025-08-06 09:45:28 -04:00
parent 3ecb904e37
commit 059fee5f06
6 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,14 @@
# User Interface
## Light & Dark Mode
The NetBox user interface supports toggling between light and dark versions of the theme. If needed, a plugin can determine the currently active color theme by inspecting `window.localStorage['netbox-color-mode']`, which will indicate either `light` or `dark`.
Additionally, when the color scheme is toggled by the user, a custom event `netbox.colorModeChanged` indicating the new scheme is dispatched. A plugin can listen for this event if needed to react to the change:
```typescript
window.addEventListener('netbox.colorModeChanged', e => {
const customEvent = e as CustomEvent<ColorModeData>;
console.log('New color mode:', customEvent.detail.netboxColorMode);
});
```

View File

@ -144,6 +144,7 @@ nav:
- Search: 'plugins/development/search.md'
- Event Types: 'plugins/development/event-types.md'
- Data Backends: 'plugins/development/data-backends.md'
- User Interface: 'plugins/development/user-interface.md'
- REST API: 'plugins/development/rest-api.md'
- GraphQL API: 'plugins/development/graphql-api.md'
- Background Jobs: 'plugins/development/background-jobs.md'

Binary file not shown.

Binary file not shown.

View File

@ -43,6 +43,11 @@ function updateElements(targetMode: ColorMode): void {
export function setColorMode(mode: ColorMode): void {
storeColorMode(mode);
updateElements(mode);
window.dispatchEvent(
new CustomEvent<ColorModeData>('netbox.colorModeChanged', {
detail: { netboxColorMode: mode },
}),
);
}
/**

View File

@ -79,3 +79,6 @@ type FormControls = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
type ColorMode = 'light' | 'dark';
type ColorModePreference = ColorMode | 'none';
type ColorModeData = {
netboxColorMode: ColorMode;
};