Skip to content

Commit a362b34

Browse files
authored
[devtools] port devtools-indicator position to dispatcher (#80536)
### Why? Since the DevTools Panel and the DevTools Indicator are decoupled, use the dispatcher to communicate the indicator position from the DevTools Panel (Settings tab).
1 parent dfbdfde commit a362b34

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

packages/next/src/next-devtools/dev-overlay/components/devtools-indicator/devtools-indicator.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
ACTION_ERROR_OVERLAY_CLOSE,
1616
STORAGE_KEY_POSITION,
1717
ACTION_DEVTOOLS_PANEL_CLOSE,
18+
ACTION_DEVTOOLS_POSITION,
1819
} from '../../shared'
19-
import { getInitialPosition } from '../errors/dev-tools-indicator/dev-tools-info/preferences'
2020
import { Draggable } from '../errors/dev-tools-indicator/draggable'
2121

2222
const INDICATOR_PADDING = 20
@@ -35,9 +35,8 @@ export function DevToolsIndicator({
3535
scale: DevToolsScale
3636
}) {
3737
const [open, setOpen] = useState(false)
38-
const [position, setPosition] = useState(getInitialPosition())
3938

40-
const [vertical, horizontal] = position.split('-', 2)
39+
const [vertical, horizontal] = state.devToolsPosition.split('-', 2)
4140

4241
const toggleErrorOverlay = () => {
4342
dispatch({ type: ACTION_DEVTOOLS_PANEL_CLOSE })
@@ -66,10 +65,13 @@ export function DevToolsIndicator({
6665
<Draggable
6766
padding={INDICATOR_PADDING}
6867
onDragStart={() => setOpen(false)}
69-
position={position}
68+
position={state.devToolsPosition}
7069
setPosition={(p) => {
70+
dispatch({
71+
type: ACTION_DEVTOOLS_POSITION,
72+
devToolsPosition: p,
73+
})
7174
localStorage.setItem(STORAGE_KEY_POSITION, p)
72-
setPosition(p)
7375
}}
7476
>
7577
{/* Trigger */}

packages/next/src/next-devtools/dev-overlay/components/errors/dev-tools-indicator/dev-tools-indicator.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const state: OverlayState = {
6161
isErrorOverlayOpen: false,
6262
// TODO: This will be handled on the next stack——with proper story.
6363
isDevToolsPanelOpen: false,
64+
devToolsPosition: 'bottom-left',
6465
}
6566

6667
export const StaticRoute: Story = {

packages/next/src/next-devtools/dev-overlay/dev-overlay.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import imgApp from './app.png'
77
import { useReducer } from 'react'
88
import { DevOverlay } from './dev-overlay'
99
import {
10+
ACTION_DEVTOOLS_POSITION,
11+
ACTION_DEVTOOLS_PANEL_CLOSE,
1012
ACTION_DEVTOOLS_PANEL_TOGGLE,
1113
ACTION_ERROR_OVERLAY_CLOSE,
1214
ACTION_ERROR_OVERLAY_OPEN,
@@ -96,6 +98,7 @@ const initialState: OverlayState = {
9698
},
9799
isErrorOverlayOpen: false,
98100
isDevToolsPanelOpen: false,
101+
devToolsPosition: 'bottom-left',
99102
}
100103

101104
function useOverlayReducer() {
@@ -114,6 +117,12 @@ function useOverlayReducer() {
114117
case ACTION_DEVTOOLS_PANEL_TOGGLE: {
115118
return { ...state, isDevToolsPanelOpen: !state.isDevToolsPanelOpen }
116119
}
120+
case ACTION_DEVTOOLS_PANEL_CLOSE: {
121+
return { ...state, isDevToolsPanelOpen: false }
122+
}
123+
case ACTION_DEVTOOLS_POSITION: {
124+
return { ...state, devToolsPosition: action.devToolsPosition }
125+
}
117126
default: {
118127
return state
119128
}

packages/next/src/next-devtools/dev-overlay/shared.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type { DevIndicatorServerState } from '../../server/dev/dev-indicator-ser
88
import { parseStack } from '../../server/lib/parse-stack'
99
import { isConsoleError } from '../shared/console-error'
1010

11+
export type Corners = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
12+
1113
type FastRefreshState =
1214
/** No refresh in progress. */
1315
| { type: 'idle' }
@@ -30,6 +32,7 @@ export interface OverlayState {
3032
routerType: 'pages' | 'app'
3133
isErrorOverlayOpen: boolean
3234
isDevToolsPanelOpen: boolean
35+
devToolsPosition: Corners
3336
}
3437
export type OverlayDispatch = React.Dispatch<DispatcherEvent>
3538

@@ -57,6 +60,8 @@ export const ACTION_DEVTOOLS_PANEL_OPEN = 'devtools-panel-open'
5760
export const ACTION_DEVTOOLS_PANEL_CLOSE = 'devtools-panel-close'
5861
export const ACTION_DEVTOOLS_PANEL_TOGGLE = 'devtools-panel-toggle'
5962

63+
export const ACTION_DEVTOOLS_POSITION = 'devtools-position'
64+
6065
export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme'
6166
export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position'
6267
export const STORAGE_KEY_SCALE = '__nextjs-dev-tools-scale'
@@ -138,6 +143,11 @@ export interface DevToolsPanelToggleAction {
138143
type: typeof ACTION_DEVTOOLS_PANEL_TOGGLE
139144
}
140145

146+
export interface DevToolsIndicatorPositionAction {
147+
type: typeof ACTION_DEVTOOLS_POSITION
148+
devToolsPosition: Corners
149+
}
150+
141151
export type DispatcherEvent =
142152
| BuildOkAction
143153
| BuildErrorAction
@@ -159,6 +169,7 @@ export type DispatcherEvent =
159169
| DevToolsPanelOpenAction
160170
| DevToolsPanelCloseAction
161171
| DevToolsPanelToggleAction
172+
| DevToolsIndicatorPositionAction
162173

163174
const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX =
164175
// 1st group: v8
@@ -198,6 +209,7 @@ export const INITIAL_OVERLAY_STATE: Omit<
198209
versionInfo: { installed: '0.0.0', staleness: 'unknown' },
199210
debugInfo: { devtoolsFrontendUrl: undefined },
200211
isDevToolsPanelOpen: false,
212+
devToolsPosition: 'bottom-left',
201213
}
202214

203215
function getInitialState(
@@ -369,6 +381,9 @@ export function useErrorOverlayReducer(
369381
case ACTION_DEVTOOLS_PANEL_TOGGLE: {
370382
return { ...state, isDevToolsPanelOpen: !state.isDevToolsPanelOpen }
371383
}
384+
case ACTION_DEVTOOLS_POSITION: {
385+
return { ...state, devToolsPosition: action.devToolsPosition }
386+
}
372387
default: {
373388
return state
374389
}

0 commit comments

Comments
 (0)