Unverified Commit b31ee944 authored by PapatMayuri's avatar PapatMayuri Committed by GitHub

updated with-styletron example to utilize the app router. (#73252)

This PR updates the with-react-foundation example for using the App
Router.
Here are the changes that have been made:

- I renamed the `pages` folder and moved it to the `app` folder.
- Added the layout.tsx file as part of the App Router.
- Updated the package.json file.

CC: @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 1efa3c2a
"use client";
import { Provider as StyletronProvider } from "styletron-react"; import { Provider as StyletronProvider } from "styletron-react";
import { styletron } from "../styletron"; import { styletron } from "../styletron";
export default function App({ Component, pageProps }) { export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return ( return (
<StyletronProvider value={styletron}> <html lang="en">
<Component {...pageProps} /> <StyletronProvider value={styletron}>
</StyletronProvider> <body>{children}</body>
</StyletronProvider>
</html>
); );
} }
// DOCUMENTATION: http://styletron.org // DOCUMENTATION: http://styletron.org
"use client";
import { styled, useStyletron } from "styletron-react"; import { styled, useStyletron } from "styletron-react";
// statically styled component // Statically styled component
const Title = styled("h1", { const Title = styled("h1", {
color: "red", color: "red",
fontSize: "82px", fontSize: "82px",
}); });
// dynamically styled component // Dynamically styled component
const SubTitle = styled("h2", ({ $size }) => ({ const SubTitle = styled("h2", ({ $size }: { $size: number }) => ({
color: "blue", color: "blue",
fontSize: `${$size}px`, fontSize: `${$size}px`,
})); }));
export default function Home() { export default function Home() {
// an alternative hook based API // An alternative hook-based API
const [css] = useStyletron(); const [css] = useStyletron();
return ( return (
<div> <div>
<Title>Title</Title> <Title>Title</Title>
......
{ {
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.3.1",
"react-dom": "^18.2.0", "react-dom": "^18.3.1",
"styletron-engine-atomic": "1.4.6", "styletron-engine-atomic": "^1.6.2",
"styletron-react": "6.1.0" "styletron-react": "^6.1.1"
},
"devDependencies": {
"@types/node": "^22.10.0",
"@types/react": "^18.3.12",
"typescript": "^5.7.2"
} }
} }
import Document, { Html, Head, Main, NextScript } from "next/document";
import { Provider as StyletronProvider } from "styletron-react";
import { styletron } from "../styletron";
class MyDocument extends Document {
static async getInitialProps(context) {
const renderPage = () =>
context.renderPage({
enhanceApp: (App) => (props) => (
<StyletronProvider value={styletron}>
<App {...props} />
</StyletronProvider>
),
});
const initialProps = await Document.getInitialProps({
...context,
renderPage,
});
const stylesheets = styletron.getStylesheets() || [];
return { ...initialProps, stylesheets };
}
render() {
return (
<Html>
<Head>
{this.props.stylesheets.map((sheet, i) => (
<style
className="_styletron_hydrate_"
dangerouslySetInnerHTML={{ __html: sheet.css }}
media={sheet.attrs.media}
data-hydrate={sheet.attrs["data-hydrate"]}
key={i}
/>
))}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment