Open this example in the interactive library
Live preview
Source code
// components/ContrastPreviewSwitch.tsx
import { useState } from "react";
import type { ReactNode } from "react";
export default function ContrastPreviewSwitch({
children = "Preview text",
}: { children?: ReactNode } = {}) {
const [dark, set] = useState(false);
return (
<section
style={{
padding: "1rem",
color: dark ? "white" : "black",
background: dark ? "#111" : "white",
}}
>
<button aria-pressed={dark} onClick={() => set(!dark)}>
Dark preview
</button>
{children}
</section>
);
}
