mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 01:21:18 -08:00
fix: restore prism body theme classes
This commit is contained in:
2 files changed
+111
No files matched your search
@@ -0,0 +1,106 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { render } from '@testing-library/react';
|
||||
|
||||
import { ThemeKind, type Theme } from '$hooks/useTheme';
|
||||
import { AuthRouteThemeManager, UnAuthRouteThemeManager } from './ThemeManager';
|
||||
|
||||
const settings = {
|
||||
saturationLevel: 100,
|
||||
underlineLinks: false,
|
||||
reducedMotion: false,
|
||||
};
|
||||
|
||||
let systemThemeKind = ThemeKind.Light;
|
||||
let activeTheme: Theme = {
|
||||
id: 'test-light',
|
||||
kind: ThemeKind.Light,
|
||||
classNames: ['test-light-theme'],
|
||||
};
|
||||
|
||||
type ThemeContextProviderProps = {
|
||||
value: Theme;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
type ArboriumThemeBridgeProps = {
|
||||
kind: ThemeKind;
|
||||
children?: ReactNode;
|
||||
};
|
||||
|
||||
vi.mock('$hooks/useTheme', () => ({
|
||||
ThemeKind: {
|
||||
Light: 'light',
|
||||
Dark: 'dark',
|
||||
},
|
||||
DarkTheme: {
|
||||
classNames: ['test-dark-theme'],
|
||||
},
|
||||
LightTheme: {
|
||||
classNames: ['test-light-theme'],
|
||||
},
|
||||
ThemeContextProvider: ({ value, children }: ThemeContextProviderProps) =>
|
||||
value.kind === ThemeKind.Dark ? <>{children}</> : <>{children}</>,
|
||||
useActiveTheme: () => activeTheme,
|
||||
useSystemThemeKind: () => systemThemeKind,
|
||||
}));
|
||||
|
||||
vi.mock('$state/hooks/settings', () => ({
|
||||
useSetting: (_atom: unknown, key: keyof typeof settings) => [settings[key]],
|
||||
}));
|
||||
|
||||
vi.mock('$state/settings', () => ({
|
||||
settingsAtom: {},
|
||||
}));
|
||||
|
||||
vi.mock('$plugins/arborium', () => ({
|
||||
ArboriumThemeBridge: ({ kind, children }: ArboriumThemeBridgeProps) =>
|
||||
kind === ThemeKind.Dark ? <>{children}</> : <>{children}</>,
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
systemThemeKind = ThemeKind.Light;
|
||||
activeTheme = {
|
||||
id: 'test-light',
|
||||
kind: ThemeKind.Light,
|
||||
classNames: ['test-light-theme'],
|
||||
};
|
||||
settings.saturationLevel = 100;
|
||||
settings.underlineLinks = false;
|
||||
settings.reducedMotion = false;
|
||||
document.body.className = '';
|
||||
document.body.style.filter = '';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
document.body.className = '';
|
||||
document.body.style.filter = '';
|
||||
});
|
||||
|
||||
describe('ThemeManager', () => {
|
||||
it('keeps a Prism compatibility class on the body for unauthenticated routes', () => {
|
||||
systemThemeKind = ThemeKind.Dark;
|
||||
|
||||
render(<UnAuthRouteThemeManager />);
|
||||
|
||||
expect(document.body).toHaveClass('prism-dark');
|
||||
expect(document.body).not.toHaveClass('prism-light');
|
||||
});
|
||||
|
||||
it('keeps a Prism compatibility class on the body for authenticated routes', () => {
|
||||
activeTheme = {
|
||||
id: 'test-dark',
|
||||
kind: ThemeKind.Dark,
|
||||
classNames: ['test-dark-theme'],
|
||||
};
|
||||
|
||||
render(
|
||||
<AuthRouteThemeManager>
|
||||
<div>child</div>
|
||||
</AuthRouteThemeManager>
|
||||
);
|
||||
|
||||
expect(document.body).toHaveClass('prism-dark');
|
||||
expect(document.body).not.toHaveClass('prism-light');
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,9 @@ import { ArboriumThemeBridge } from '$plugins/arborium';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
|
||||
const getPrismCompatibilityClass = (kind: ThemeKind) =>
|
||||
kind === ThemeKind.Dark ? 'prism-dark' : 'prism-light';
|
||||
|
||||
export function UnAuthRouteThemeManager() {
|
||||
const systemThemeKind = useSystemThemeKind();
|
||||
|
||||
@@ -24,6 +27,7 @@ export function UnAuthRouteThemeManager() {
|
||||
if (systemThemeKind === ThemeKind.Light) {
|
||||
document.body.classList.add(...LightTheme.classNames);
|
||||
}
|
||||
document.body.classList.add(getPrismCompatibilityClass(systemThemeKind));
|
||||
}, [systemThemeKind]);
|
||||
|
||||
return <ArboriumThemeBridge kind={systemThemeKind} />;
|
||||
@@ -39,6 +43,7 @@ export function AuthRouteThemeManager({ children }: { children: ReactNode }) {
|
||||
document.body.className = '';
|
||||
document.body.classList.add(configClass, varsClass);
|
||||
document.body.classList.add(...activeTheme.classNames);
|
||||
document.body.classList.add(getPrismCompatibilityClass(activeTheme.kind));
|
||||
|
||||
if (underlineLinks) {
|
||||
document.body.classList.add('force-underline-links');
|
||||
|
||||
Reference in new issue
Block a user