Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/app/services/theme.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,33 @@ describe('ThemeService theme validation', () => {
expect(root.style.colorScheme).toBe('light');
});
});

describe('ThemeService selection in the URL', () => {
it('applies the theme and mode that the documentation passes when it opens the sample in its own tab', () => {
const document = window.document.implementation.createHTMLDocument();
// A test document has no location, so the query of the sample's URL is stubbed.
const withQuery = new Proxy(document, {
get: (target, key) => {
if (key === 'location') {
return { search: '?igd-theme=fluent&igd-mode=dark' };
}
const value = Reflect.get(target, key);
return typeof value === 'function' ? value.bind(target) : value;
}
});
TestBed.configureTestingModule({
providers: [
{ provide: DOCUMENT, useValue: withQuery },
{ provide: PLATFORM_ID, useValue: 'browser' },
{ provide: THEME_TOKEN, useValue: signal<IgxTheme>('material') }
]
});
const service = TestBed.inject(ThemeService);
service.init();

expect(service.theme()).toBe('fluent');
expect(service.mode()).toBe('dark');
expect(document.getElementById('igniteui-theme').getAttribute('href'))
.toBe('assets/themes/igniteui-fluent-dark.css');
});
});
7 changes: 7 additions & 0 deletions src/app/services/theme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export class ThemeService {
this.prefersDark = this.document.defaultView?.matchMedia('(prefers-color-scheme: dark)');
// Only relevant while the widget is on `system`; `apply` ignores it otherwise.
this.prefersDark?.addEventListener('change', () => this.apply());

// "Open in full screen" in the documentation opens the sample in its own
// tab, where no theme message arrives; the selection comes in the URL.
const params = new URLSearchParams(this.document.location?.search);
if (this.set(params.get('igd-theme'), params.get('igd-mode'))) {
return;
}
}

this.apply();
Expand Down