boot-theme.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Theme bootstrap row for the browser's pre-plugin interval. Each index
  3. * render embeds the current durable built-in preference; the browser resolves
  4. * only `system`, then writes the same DOM fields ui-layout's ThemePresenter
  5. * owns after the client plugin tree activates.
  6. */
  7. import type { IndexInjection } from '@deepseek-ai/dsh-host-webserver'
  8. import { DEFAULT_PREFERENCE, type ThemePreference } from './theme-settings.ts'
  9. /** Build the inline script body for one schema-validated built-in preference. */
  10. function bootThemeScript(preference: ThemePreference): string {
  11. return `(() => {
  12. const preference = ${JSON.stringify(preference)}
  13. const systemDark = preference === 'system'
  14. && typeof matchMedia !== 'undefined'
  15. && matchMedia('(prefers-color-scheme: dark)').matches
  16. const dark = preference === 'dark' || systemDark
  17. document.documentElement.style.colorScheme = dark ? 'dark' : 'light'
  18. document.body.toggleAttribute('data-ds-dark-theme', dark)
  19. })()`
  20. }
  21. /**
  22. * The theme bootstrap as an injection row: an inline script immediately after
  23. * the opening body tag, before the shell mount and module script.
  24. * @param preference - Current Host-backed built-in preference.
  25. * @returns the body script row.
  26. */
  27. export function bootThemeInjection(
  28. preference: ThemePreference = DEFAULT_PREFERENCE,
  29. ): IndexInjection {
  30. return { kind: 'script', placement: 'body', text: bootThemeScript(preference) }
  31. }