From 323ad19f967ae3657999510099f6e30325daf5a8 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Thu, 6 Oct 2022 09:11:23 +0200 Subject: [PATCH 01/21] Run `npx create-next-app --example with-react-native-web next-example` --- next-example/.gitignore | 36 ++++ next-example/README.md | 31 +++ next-example/app.json | 4 + next-example/babel.config.js | 4 + next-example/next.config.js | 18 ++ next-example/package.json | 17 ++ next-example/pages/_app.js | 15 ++ next-example/pages/_document.js | 37 ++++ next-example/pages/alternate.js | 31 +++ next-example/pages/index.js | 41 ++++ next-example/yarn.lock | 339 ++++++++++++++++++++++++++++++++ 11 files changed, 573 insertions(+) create mode 100644 next-example/.gitignore create mode 100644 next-example/README.md create mode 100644 next-example/app.json create mode 100644 next-example/babel.config.js create mode 100644 next-example/next.config.js create mode 100644 next-example/package.json create mode 100644 next-example/pages/_app.js create mode 100644 next-example/pages/_document.js create mode 100644 next-example/pages/alternate.js create mode 100644 next-example/pages/index.js create mode 100644 next-example/yarn.lock diff --git a/next-example/.gitignore b/next-example/.gitignore new file mode 100644 index 00000000000..c87c9b392c0 --- /dev/null +++ b/next-example/.gitignore @@ -0,0 +1,36 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts diff --git a/next-example/README.md b/next-example/README.md new file mode 100644 index 00000000000..317cbe78969 --- /dev/null +++ b/next-example/README.md @@ -0,0 +1,31 @@ +# React Native Web example + +This example features how to use [react-native-web](https://github.com/necolas/react-native-web) to bring the platform-agnostic Components and APIs of React Native to the web. + +> **High-quality user interfaces**: React Native for Web makes it easy to create fast, adaptive web UIs in JavaScript. It provides native-like interactions, support for multiple input modes (touch, mouse, keyboard), optimized vendor-prefixed styles, built-in support for RTL layout, built-in accessibility, and integrates with React Dev Tools. +> +> **Write once, render anywhere**: React Native for Web interoperates with existing React DOM components and is compatible with the majority of the React Native API. You can develop new components for native and web without rewriting existing code. React Native for Web can also render to HTML and critical CSS on the server using Node.js. + +## 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-react-native-web) + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-react-native-web&project-name=with-react-native-web&repository-name=with-react-native-web) + +## 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-react-native-web with-react-native-web-app +``` + +```bash +yarn create next-app --example with-react-native-web with-react-native-web-app +``` + +```bash +pnpm create next-app --example with-react-native-web with-react-native-web-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)). diff --git a/next-example/app.json b/next-example/app.json new file mode 100644 index 00000000000..fde4f1bb112 --- /dev/null +++ b/next-example/app.json @@ -0,0 +1,4 @@ +{ + "name": "with-react-native-web", + "displayName": "with-react-native-web" +} diff --git a/next-example/babel.config.js b/next-example/babel.config.js new file mode 100644 index 00000000000..4efe54659ea --- /dev/null +++ b/next-example/babel.config.js @@ -0,0 +1,4 @@ +module.exports = { + presets: ['next/babel'], + plugins: [['react-native-web', { commonjs: true }]], +} diff --git a/next-example/next.config.js b/next-example/next.config.js new file mode 100644 index 00000000000..b082f9ab9ff --- /dev/null +++ b/next-example/next.config.js @@ -0,0 +1,18 @@ +/** @type {import('next').NextConfig} */ +module.exports = { + webpack: (config) => { + config.resolve.alias = { + ...(config.resolve.alias || {}), + // Transform all direct `react-native` imports to `react-native-web` + 'react-native$': 'react-native-web', + } + config.resolve.extensions = [ + '.web.js', + '.web.jsx', + '.web.ts', + '.web.tsx', + ...config.resolve.extensions, + ] + return config + }, +} diff --git a/next-example/package.json b/next-example/package.json new file mode 100644 index 00000000000..6ceaf65d93f --- /dev/null +++ b/next-example/package.json @@ -0,0 +1,17 @@ +{ + "private": true, + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + }, + "dependencies": { + "next": "latest", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-native-web": "^0.18.9" + }, + "devDependencies": { + "babel-plugin-react-native-web": "^0.18.9" + } +} diff --git a/next-example/pages/_app.js b/next-example/pages/_app.js new file mode 100644 index 00000000000..556b4961bb5 --- /dev/null +++ b/next-example/pages/_app.js @@ -0,0 +1,15 @@ +import * as React from 'react' +import Head from 'next/head' + +function MyApp({ Component, pageProps }) { + return ( + <> + + + + + + ) +} + +export default MyApp diff --git a/next-example/pages/_document.js b/next-example/pages/_document.js new file mode 100644 index 00000000000..4030597dabd --- /dev/null +++ b/next-example/pages/_document.js @@ -0,0 +1,37 @@ +import { Children } from 'react' +import Document, { Html, Head, Main, NextScript } from 'next/document' +import { AppRegistry } from 'react-native' +import config from '../app.json' +// Force Next-generated DOM elements to fill their parent's height +const normalizeNextElements = ` + #__next { + display: flex; + flex-direction: column; + height: 100%; + } +` + +export default class MyDocument extends Document { + static async getInitialProps({ renderPage }) { + AppRegistry.registerComponent(config.name, () => Main) + const { getStyleElement } = AppRegistry.getApplication(config.name) + const page = await renderPage() + const styles = [ +