Skip to main content

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:

pnpm -w run migrate initial-html-theme-props <path>

In a project that installs @ngrok/mantle:

npx --yes --package=@ast-grep/cli@0.51.3 ast-grep scan --rule node_modules/@ngrok/mantle/dist/migrations/initial-html-theme-props.yml --update-all <path>

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:

  1. Search for useInitialHtmlThemeProps in imports from @ngrok/mantle/theme, with any alias, and in re-exports of it.
  2. In each import, rename useInitialHtmlThemeProps to getInitialHtmlThemeProps. Keep an alias as it is.
  3. At each call or other reference that reads the import under its own name, rename it to getInitialHtmlThemeProps. Keep the arguments.
  4. Rename a re-export, a namespace member, and a shorthand object key the same way.
  5. Optional: the function is not a hook, so you can call it inline (<html {...getInitialHtmlThemeProps({ ssrCookie })}>) and remove the local variable.
  6. In tests: a test that read the hook through renderHook now 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");
  7. 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 from extractThemeCookie and getStoredTheme. A plain function in a server module works in both places.

Before → after

A React Router root layout

// BEFORE
import {
	PreventWrongThemeFlashScript,
	ThemeProvider,
	useInitialHtmlThemeProps,
} from "@ngrok/mantle/theme";
 
export function Layout({ children }: PropsWithChildren) {
	const loaderData = useRouteLoaderData<typeof loader>("root");
	const initialHtmlThemeProps = useInitialHtmlThemeProps({
		className: "h-full",
		ssrCookie: loaderData?.ssrCookie,
	});
	return (
		<html lang="en-US" {...initialHtmlThemeProps}>
			{/* … */}
		</html>
	);
}
// AFTER
import {
	getInitialHtmlThemeProps,
	PreventWrongThemeFlashScript,
	ThemeProvider,
} from "@ngrok/mantle/theme";
 
export function Layout({ children }: PropsWithChildren) {
	const loaderData = useRouteLoaderData<typeof loader>("root");
	const initialHtmlThemeProps = getInitialHtmlThemeProps({
		className: "h-full",
		ssrCookie: loaderData?.ssrCookie,
	});
	return (
		<html lang="en-US" {...initialHtmlThemeProps}>
			{/* … */}
		</html>
	);
}

An aliased import

// BEFORE
import { useInitialHtmlThemeProps as useHtmlThemeProps } from "@ngrok/mantle/theme";
 
function useRootProps() {
	return useHtmlThemeProps({ forceTheme: "dark" });
}
// AFTER
import { getInitialHtmlThemeProps as useHtmlThemeProps } from "@ngrok/mantle/theme";
 
function useRootProps() {
	return useHtmlThemeProps({ forceTheme: "dark" });
}

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 reads document.cookie and the OS preference. PreventWrongThemeFlashScript still 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 use still 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.