Unverified Commit 6d692576 authored by Manoj M S's avatar Manoj M S Committed by GitHub

chore(examples): updated the example of with-styled-components-rtl to utlize the…

chore(examples): updated the example of with-styled-components-rtl to utlize the App router (#74180)

This PR updates the with-styled-components-rtl example to use the App
Router. Here are the changes that have been made:

- Renamed the `pages` folder to the `app` folder.
- Converted the `index.tsx` to `page.tsx` file as part of the App
Router.
- Created `layout.tsx` in app directory, merging functionality from both
` _app.tsx` and `_document.tsx`:
  -  `ThemeProvider` setup from `_app.tsx`
  -   `RTL` and `styled-components` setup from `_document.tsx`
- Created `lib/registry.tsx` for styled-components server-side rendering
and moved styled-components server-side rendering logic from
`_document.tsx`
- Updated the package.json file.

The following actions were performed as part of this PR:

- Ran `pnpm prettier-check` with no issues found.
- Executed the `pnpm check-examples` script.

CC: @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 9b79c118
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# Example with styled-components RTL
This example shows how to use nextjs with right to left (RTL) styles using styled-components.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-styled-components-rtl)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-styled-components-rtl&project-name=with-styled-components-rtl&repository-name=with-styled-components-rtl)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example with-styled-components-rtl with-styled-components-rtl-app
```
```bash
yarn create next-app --example with-styled-components-rtl with-styled-components-rtl-app
```
```bash
pnpm create next-app --example with-styled-components-rtl with-styled-components-rtl-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
## Notes
Right to left allows to "flip" every element in your site to fit the needs of the cultures that are read from right to left (like arabic for example).
This example shows how to enable right to left styles using `styled-components`.
The good news, is there is no need of doing it manually anymore. `stylis-plugin-rtl` makes the transformation automatic.
From `pages/index.js` you can see, styles are `text-align: left;`, but what is actually applied is `text-align: right;`.
/** @type {import('next').NextConfig} */
const nextConfig = {
compiler: {
styledComponents: true,
},
};
module.exports = nextConfig;
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^6.1.0",
"stylis-plugin-rtl": "^2.1.1"
},
"devDependencies": {
"@types/node": "^18.18.3",
"@types/react": "^18.2.33",
"@types/react-dom": "^18.2.14",
"typescript": "^5.2.2"
}
}
import type { AppProps } from "next/app";
import type { DefaultTheme } from "styled-components";
import { ThemeProvider } from "styled-components";
const theme: DefaultTheme = {
colors: {
primary: "#0070f3",
},
};
export default function App({ Component, pageProps }: AppProps) {
return (
<ThemeProvider theme={theme}>
<Component {...pageProps} />
</ThemeProvider>
);
}
import type { DocumentContext, DocumentInitialProps } from "next/document";
import Document from "next/document";
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
import stylisRTLPlugin from "stylis-plugin-rtl";
export default class MyDocument extends Document {
static async getInitialProps(
ctx: DocumentContext,
): Promise<DocumentInitialProps> {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(
<StyleSheetManager stylisPlugins={[stylisRTLPlugin]}>
<App {...props} />
</StyleSheetManager>,
),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: [initialProps.styles, sheet.getStyleElement()],
};
} finally {
sheet.seal();
}
}
}
import styled from "styled-components";
const Title = styled.h1`
font-size: 50px;
color: ${({ theme }) => theme.colors.primary};
text-align: left;
`;
export default function Home() {
return <Title>My page</Title>;
}
import "styled-components";
declare module "styled-components" {
export interface DefaultTheme {
colors: {
primary: string;
};
}
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "styled.d.ts"],
"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