Skip to content

Commit

Permalink
Replace initial props with app props
Browse files Browse the repository at this point in the history
  • Loading branch information
xeoneux committed Oct 10, 2022
1 parent 7371e83 commit ca4ac4a
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 382 deletions.
4 changes: 4 additions & 0 deletions example/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../.eslintrc",
"globals": { "intern": true }
}
4 changes: 0 additions & 4 deletions example/.eslintrc.js

This file was deleted.

12 changes: 6 additions & 6 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"dependencies": {
"next": "^12.1.6",
"next": "^12.3.1",
"next-dark-mode": "3.0.0",
"react": "^18.1.0",
"react-dom": "^18.1.0"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.4",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"intern": "^4.10.1",
"typescript": "^4.6.4"
"typescript": "^4.8.4"
},
"private": true,
"scripts": {
Expand Down
274 changes: 152 additions & 122 deletions example/yarn.lock

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
"description": "Enable dark mode for Next.js",
"devDependencies": {
"@types/cookie": "^0.5.1",
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.4",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"concurrently": "^7.2.0",
"eslint": "^8.16.0",
"eslint-config-alloy": "^4.5.1",
"eslint-plugin-react": "^7.30.0",
"next": "^12.1.6",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"@typescript-eslint/eslint-plugin": "^5.40.0",
"@typescript-eslint/parser": "^5.40.0",
"concurrently": "^7.4.0",
"eslint": "^8.25.0",
"eslint-config-alloy": "^4.7.0",
"eslint-plugin-react": "^7.31.9",
"next": "^12.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "3.0.2",
"typescript": "^4.6.4"
"typescript": "^4.8.4"
},
"homepage": "https://github.com/xeoneux/next-dark-mode",
"jsnext:main": "src/index.tsx",
Expand Down Expand Up @@ -51,5 +51,5 @@
},
"source": "src/index.tsx",
"types": "es6/index.d.ts",
"version": "3.0.0"
"version": "4.0.0-beta.0"
}
11 changes: 5 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { DarkModeContext, useDarkMode } from './darkModeContext'
interface AppProps {
autoMode?: boolean
darkMode?: boolean
initialProps: any
}

export default (App: NextComponentType | any, config?: Partial<Config>) => {
Expand All @@ -19,7 +18,7 @@ export default (App: NextComponentType | any, config?: Partial<Config>) => {
...config,
}

function DarkMode({ autoMode, darkMode, initialProps, ...props }: AppProps) {
function DarkMode({ autoMode, darkMode, ...props }: AppProps) {
const [state, setState] = useState({
autoModeActive: !!autoMode,
autoModeSupported: false,
Expand Down Expand Up @@ -98,13 +97,13 @@ export default (App: NextComponentType | any, config?: Partial<Config>) => {
return removeListeners
}, [])

const app = <App darkMode={state} {...props} {...initialProps} />
const app = <App darkMode={state} {...props} />

return <DarkModeContext.Provider value={state}>{app}</DarkModeContext.Provider>
}

DarkMode.getInitialProps = async (appContext: AppContext) => {
const initialProps = App.getInitialProps ? await App.getInitialProps(appContext) : {}
const appProps = App.getInitialProps ? await App.getInitialProps(appContext) : {}

if (typeof window === 'undefined') {
const cookies = parseCookies(appContext.ctx)
Expand All @@ -123,10 +122,10 @@ export default (App: NextComponentType | any, config?: Partial<Config>) => {
if (darkModeString !== darkModeCookie)
setCookie(appContext.ctx, darkModeCookieName, darkModeString, cookieOptions)

return { autoMode, darkMode, initialProps }
return { autoMode, darkMode, ...appProps }
}

return { initialProps }
return { ...appProps }
}

DarkMode.displayName = `withDarkMode(${App.displayName || App.name || 'App'})`
Expand Down
Loading

0 comments on commit ca4ac4a

Please sign in to comment.