Skip to content

Commit

Permalink
-document.tsx + AppRegistry.getApplication() TS support
Browse files Browse the repository at this point in the history
  • Loading branch information
LunatiqueCoder committed Dec 19, 2022
1 parent 90223e9 commit 27a503a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
6 changes: 5 additions & 1 deletion template/react-native-web.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// This file is adapted from: https://github.com/necolas/react-native-web/issues/832#issuecomment-1027393818

import {MouseEvent, UIEvent, FocusEvent} from 'react';
import {MouseEvent, UIEvent, FocusEvent, ReactNode} from 'react';
export * from 'react-native';

declare module 'react-native' {
namespace AppRegistry {
function getApplication(string): {getStyleElement: () => ReactNode};
}

// The following list is sourced from:
// - https://github.com/necolas/react-native-web/blob/0.17.5/packages/react-native-web/src/types/styles.js#L76
type CursorValue =
Expand Down
37 changes: 0 additions & 37 deletions template/src/pages/_document.js

This file was deleted.

46 changes: 46 additions & 0 deletions template/src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {Children} from 'react';
import {
Html,
Head,
Main,
NextScript,
DocumentContext,
DocumentInitialProps,
} 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%;
}
`;

function MyDocument() {
return (
<Html style={{height: '100%'}}>
<Head />
<body style={{height: '100%', overflow: 'hidden'}}>
<Main />
<NextScript />
</body>
</Html>
);
}

MyDocument.getInitialProps = async ({
renderPage,
}: DocumentContext): Promise<DocumentInitialProps> => {
AppRegistry.registerComponent(config.name, () => Main);
const {getStyleElement} = AppRegistry.getApplication(config.name);
const page = await renderPage();
const styles = [
<style dangerouslySetInnerHTML={{__html: normalizeNextElements}} />,
getStyleElement(),
];
return {...page, styles: Children.toArray(styles)};
};

export default MyDocument;

1 comment on commit 27a503a

@vercel
Copy link

@vercel vercel bot commented on 27a503a Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

luna – ./

luna-criszz77.vercel.app
luna-git-nextjs-criszz77.vercel.app
luna-gamma.vercel.app

Please sign in to comment.