Skip to content

feat(react): let setTheme(null) forget the theme choice on the web - #1043

Open
owjs3901 wants to merge 2 commits into
mainfrom
feat/set-theme-null
Open

owjs3901 wants to merge 2 commits into
mainfrom
feat/set-theme-null

Conversation

@owjs3901

Copy link
Copy Markdown
Contributor

Summary

setTheme(theme: ThemeName | null): null means "forget the user's explicit choice and follow the system again".

  • Removes the saved theme (__DF_THEME_SELECTED__) — other tabs follow through the native storage event.
  • Removes the explicit data-theme, then lets the running ThemeScript / initTheme re-apply the first-load theme with their own auto and default theme (a same-tab storage event for the theme key, which both already listen to). Their existing prefers-color-scheme listeners keep following the system afterwards, since no saved choice blocks them anymore.
  • Without ThemeScript or initTheme on the page, it applies the system preference once (dark for a dark system, else the default theme); there is nothing to keep listening in that setup, the same as before.
  • getTheme() / useTheme() keep their return types and return the theme in effect after a reset (never null on a page), so toggles like getTheme() === 'dark' keep working.
  • The ThemeScript inline script is unchanged: on first load with no saved choice it already picks the system theme before paint, so there is no flash after a reset either.
  • The shared type lives in @devup-ui/react, so the React Native runtime (feat(native): theme tokens, multiple themes and the React Native runtime #1042) gets the nullable argument too.

Shared helpers moved to utils/theme-system.ts (storage key, saved-theme read, system theme) so initTheme and setTheme(null) resolve the theme the same way.

Tests

  • theme-behavior.browser.test.ts: reset with initTheme(true) (back to system, then follows its changes), with initTheme(false) (default theme), without any runtime (system once), no matchMedia, blocked removeItem, StorageEvent unavailable, server no-op.
  • ThemeScript.behavior.browser.test.tsx: the inline script follows the reset (auto and non-auto).
  • use-theme.browser.test.ts: useTheme() returns the effective theme after setTheme(null).
  • Published type test: setTheme(null) type-checks.
  • Docs: landing colors page "Switching Themes" section, SKILL.md. Changepack (patch) for @devup-ui/react.

요약

setTheme(theme: ThemeName | null): null은 "사용자가 명시적으로 고른 테마를 잊고 다시 시스템을 따른다"는 뜻입니다.

  • 저장된 테마(__DF_THEME_SELECTED__)를 지웁니다. 다른 탭은 기본 storage 이벤트로 따라옵니다.
  • 명시적 data-theme을 지운 뒤, 실행 중인 ThemeScript / initTheme이 자신의 auto와 기본 테마로 첫 로드 테마를 다시 적용하게 합니다(둘 다 이미 듣고 있는 테마 키의 같은 탭 storage 이벤트). 저장된 선택이 없으므로 기존 prefers-color-scheme 리스너가 이후 시스템 변경을 계속 따릅니다.
  • ThemeScript나 initTheme이 없는 페이지에서는 시스템 설정을 한 번 적용합니다(다크면 dark, 아니면 기본 테마). 그 구성에는 계속 들을 주체가 없으며 이전과 같습니다.
  • getTheme() / useTheme()의 반환 타입은 그대로이며, 리셋 후에도 실제 적용된 테마를 반환합니다(페이지에서는 null이 아님). getTheme() === 'dark' 같은 토글이 그대로 동작합니다.
  • ThemeScript 인라인 스크립트는 변경이 없습니다. 저장된 선택이 없으면 첫 로드에서 이미 페인트 전에 시스템 테마를 고르므로 리셋 후에도 깜빡임이 없습니다.
  • 공용 타입이 @devup-ui/react에 있으므로 React Native 런타임(feat(native): theme tokens, multiple themes and the React Native runtime #1042)도 nullable 인자를 받게 됩니다.

공용 헬퍼(저장 키, 저장 테마 읽기, 시스템 테마)를 utils/theme-system.ts로 옮겨 initTheme과 setTheme(null)이 같은 방식으로 테마를 정합니다.

테스트

  • theme-behavior.browser.test.ts: initTheme(true)에서 리셋(시스템으로 복귀 후 변경 추적), initTheme(false)(기본 테마), 런타임 없음(시스템 1회), matchMedia 없음, removeItem 차단, StorageEvent 사용 불가, 서버 no-op.
  • ThemeScript.behavior.browser.test.tsx: 인라인 스크립트가 리셋을 따름(auto / non-auto).
  • use-theme.browser.test.ts: setTheme(null) 후 useTheme()이 실제 테마를 반환.
  • 배포 타입 테스트: setTheme(null) 타입 체크.
  • 문서: 랜딩 colors 페이지 "Switching Themes" 섹션, SKILL.md. @devup-ui/react changepack(patch).

🤖 Generated with Claude Code

owjs3901 and others added 2 commits November 21, 2026 09:57
setTheme(null) removes the saved theme in every tab and applies the theme
the page starts with when nothing was chosen, so ThemeScript and initTheme
follow prefers-color-scheme again. getTheme and useTheme keep returning the
theme in effect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…g example

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
packages/react/src/components/ThemeScript.tsx 100.00% <ø> (ø)
packages/react/src/utils/init-theme.ts 100.00% <100.00%> (ø)
packages/react/src/utils/set-theme.ts 100.00% <100.00%> (ø)
packages/react/src/utils/theme-system.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant