feat(theme): configurable dark mode with system preference support#10669
Open
remintroy wants to merge 1 commit intoswagger-api:masterfrom
Open
feat(theme): configurable dark mode with system preference support#10669remintroy wants to merge 1 commit intoswagger-api:masterfrom
remintroy wants to merge 1 commit intoswagger-api:masterfrom
Conversation
This PR implements a robust and configurable dark mode system with
three options: `light`, `dark`, and `system` (auto-detect).
## Key Changes
1. **New `theme` Configuration:**
- Added `theme: { defaultMode: '...' }` to Swagger UI config.
- `'system'` (Default): Automatically respects the user's OS/browser
preference.
- `'dark'`: Forces dark mode.
- `'light'`: Forces light mode.
2. **Core Plugin Implementation:**
- Logic moved to a new core plugin (`src/core/plugins/dark-mode`)
for better state management.
- `src/standalone/plugins/top-bar/components/TopBar.jsx` - Pass
dark mode props
- `docker/configurator/variables.js` - Helper for Docker env vars
- `test/unit/standalone/plugins/top-bar/DarkModeToggle.jsx` -
Updated test
- Ensures dark mode works uniformly across all layouts (Base,
Standalone, etc.).
3. **Docker Support:**
- Added `THEME_DEFAULT_MODE` environment variable support.
- Allows configuring the default theme mode directly in Docker
containers (e.g., `-e THEME_DEFAULT_MODE=dark`).
4. **CSS Isolation:**
- Dark mode styles are properly scoped to `.swagger-ui` to prevent
style leakage into host applications during embedded usage.
5. **Backward Compatibility:**
- Existing manual methods (toggling `dark-mode` class) still work.
- No breaking changes for existing users.
## Usage Example
```javascript
SwaggerUI({
url: 'https://petstore.swagger.io/v2/swagger.json',
theme: {
defaultMode: 'dark' // or 'light' or 'system'
}
});
```
## Documentation
- Added comprehensive guide: `docs/usage/dark-mode.md`
- Updated configuration docs: `docs/usage/configuration.md`
Closes swagger-api#10663
Contributor
|
Great stuff, fully agree that with the logic move over to a plugin. |
|
Can this get merged? the latest swagger ui is useless to me as dark mode cannot be disabled if my system is set to dark mode and I'm trying to render a light mode UI. |
|
Very happy with this feature! Hope it will be out soon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements a robust and configurable dark mode system with three options:
light,dark, andsystem(auto-detect).Key Changes
New
themeConfiguration:theme: { defaultMode: '...' }to Swagger UI config.'system'(Default): Automatically respects the user's OS/browser preference.'dark': Forces dark mode.'light': Forces light mode.Core Plugin Implementation:
src/core/plugins/dark-mode) for better state management.src/standalone/plugins/top-bar/components/TopBar.jsx- Pass dark mode propsdocker/configurator/variables.js- Helper for Docker env varstest/unit/standalone/plugins/top-bar/DarkModeToggle.jsx- Updated testDocker Support:
THEME_DEFAULT_MODEenvironment variable support.-e THEME_DEFAULT_MODE=dark).CSS Isolation:
.swagger-uito prevent style leakage into host applications during embedded usage.Backward Compatibility:
dark-modeclass) still work.Usage Example
Documentation
docs/usage/dark-mode.mddocs/usage/configuration.mdCloses #10663
Description
Implements configurable dark mode with
light,dark, andsystem(auto-detect) options, including Docker support viaTHEME_DEFAULT_MODEenv var.Motivation and Context
As a user, I wanted native support for system-preferred dark mode and easier configuration options to improve the overall viewing experience.
Fixes #10663
How Has This Been Tested?
Manually verified
theme.defaultModein browser (Chrome/Firefox), tested Docker environment variable injection, and verified CSS scoping prevents leakage.Screenshots (if appropriate):
Checklist
My PR contains...
src/is unmodified: changes to documentation, CI, metadata, etc.)package.json)My changes...
Documentation
Automated tests