Mantle Initial HTML Theme Props Migration Guide
@ngrok/mantle/theme removes the useInitialHtmlThemeProps hook. getInitialHtmlThemeProps replaces it. It takes the same options (className, forceTheme, ssrCookie) and returns the same <html> props. It is a plain function in a module with no "use client", so a React Router Layout, a client component, and a server layout such as a React Router RSC ServerLayout can all call it. PreventWrongThemeFlashScript also moves out of the "use client" module, so in an RSC app it renders on the server and adds no client JavaScript. Its props do not change.
TL;DR: Rename useInitialHtmlThemeProps to getInitialHtmlThemeProps in the import from @ngrok/mantle/theme and at each call. Change nothing else: the options and the result are the same.
Codemod
From a mantle checkout:
In a project that installs @ngrok/mantle:
The rules apply steps 2 and 3 for a named import. They rename the import specifier from @ngrok/mantle/theme, and each reference that reads it under its own name, such as a call or renderHook(useInitialHtmlThemeProps). An aliased import (useInitialHtmlThemeProps as useHtmlThemeProps) keeps its alias, so its calls need no change. A call under a local name that shadows the import stays. These stay manual, and the type checker names each one: a re-export (export { useInitialHtmlThemeProps } from "@ngrok/mantle/theme"), a namespace import (theme.useInitialHtmlThemeProps), and a shorthand object key ({ useInitialHtmlThemeProps }). The codemod is idempotent. To preview the rewrites, pass --dry-run to the mantle command, or leave out --update-all in the ast-grep command. Run your formatter after.
You are an agent migrating a codebase. Do this:
- Search for
useInitialHtmlThemePropsin imports from@ngrok/mantle/theme, with any alias, and in re-exports of it. - In each import, rename
useInitialHtmlThemePropstogetInitialHtmlThemeProps. Keep an alias as it is. - At each call or other reference that reads the import under its own name, rename it to
getInitialHtmlThemeProps. Keep the arguments. - Rename a re-export, a namespace member, and a shorthand object key the same way.
- Optional: the function is not a hook, so you can call it inline (
<html {...getInitialHtmlThemeProps({ ssrCookie })}>) and remove the local variable. - In tests: a test that read the hook through
renderHooknow calls the function.- Before:
const { result } = renderHook(() => useInitialHtmlThemeProps({ ssrCookie })); expect(result.current["data-theme"]).toBe("dark"); - After:
const props = getInitialHtmlThemeProps({ ssrCookie }); expect(props["data-theme"]).toBe("dark");
- Before:
- Run your formatter, linter, type checker, and tests.
Why a function? The hook held no state: it read the cookie and the OS preference once. As a hook in a
"use client"module, a server layout could not call it, and an RSC app had to rebuild the<html>props by hand fromextractThemeCookieandgetStoredTheme. A plain function in a server module works in both places.
Before → after
A React Router root layout
An aliased import
Edge cases & gotchas
- The result does not change. On the server, the function reads only
ssrCookie, and a"system"theme renders as light. In the browser, it readsdocument.cookieand the OS preference.PreventWrongThemeFlashScriptstill corrects a"system"page before first paint. - When the file already has a local named
getInitialHtmlThemeProps, the renamed call reads that local. Import the function under an alias there instead. - An aliased import that starts with
usestill works, but a linter can read it as a hook. Rename the alias when you want the call to read as a function.
How to verify
Run your type checker: a reference that the codemod did not reach fails with a missing export. Then follow the In tests: step and run your tests.