Unverified Commit 91e1ee04 authored by rauno's avatar rauno Committed by GitHub

[dev-overlay] Add `size` setting to preferences (#77027)

This PR is a follow up to https://github.com/vercel/next.js/pull/76969
and adds a size setting to the Dev Tools preferences.

Right now the **Small**, **Medium** (default), and **Large** variants
scale the UI as if the root font size were **14px**, **16px**, **18px**.
We can tweak these values as a follow-up.


https://github.com/user-attachments/assets/43055cae-1e14-4e52-94cc-3ad1b8cae424

---
- Closes NDX-971
parent d0dfc63c
...@@ -40,6 +40,7 @@ export const ACTION_DEV_INDICATOR = 'dev-indicator' ...@@ -40,6 +40,7 @@ export const ACTION_DEV_INDICATOR = 'dev-indicator'
export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme' export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme'
export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position' export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position'
export const STORAGE_KEY_SCALE = '__nextjs-dev-tools-scale'
interface StaticIndicatorAction { interface StaticIndicatorAction {
type: typeof ACTION_STATIC_INDICATOR type: typeof ACTION_STATIC_INDICATOR
......
import type { CSSProperties, Dispatch, SetStateAction } from 'react' import type { CSSProperties, Dispatch, SetStateAction } from 'react'
import { STORAGE_KEY_POSITION, type OverlayState } from '../../../../shared' import type { OverlayState } from '../../../../shared'
import { useState, useEffect, useRef, createContext, useContext } from 'react' import { useState, useEffect, useRef, createContext, useContext } from 'react'
import { Toast } from '../../toast' import { Toast } from '../../toast'
...@@ -17,21 +17,19 @@ import { ...@@ -17,21 +17,19 @@ import {
useClickOutside, useClickOutside,
useFocusTrap, useFocusTrap,
} from './utils' } from './utils'
import {
getInitialPosition,
type DevToolsScale,
} from './dev-tools-info/preferences'
// TODO: add E2E tests to cover different scenarios // TODO: add E2E tests to cover different scenarios
const INDICATOR_POSITION =
(process.env
.__NEXT_DEV_INDICATOR_POSITION as typeof window.__NEXT_DEV_INDICATOR_POSITION) ||
'bottom-left'
export type DevToolsIndicatorPosition = typeof INDICATOR_POSITION
export function DevToolsIndicator({ export function DevToolsIndicator({
state, state,
errorCount, errorCount,
isBuildError, isBuildError,
setIsErrorOverlayOpen, setIsErrorOverlayOpen,
...props
}: { }: {
state: OverlayState state: OverlayState
errorCount: number errorCount: number
...@@ -39,6 +37,8 @@ export function DevToolsIndicator({ ...@@ -39,6 +37,8 @@ export function DevToolsIndicator({
setIsErrorOverlayOpen: ( setIsErrorOverlayOpen: (
isErrorOverlayOpen: boolean | ((prev: boolean) => boolean) isErrorOverlayOpen: boolean | ((prev: boolean) => boolean)
) => void ) => void
scale: DevToolsScale
setScale: (value: DevToolsScale) => void
}) { }) {
const [isDevToolsIndicatorVisible, setIsDevToolsIndicatorVisible] = const [isDevToolsIndicatorVisible, setIsDevToolsIndicatorVisible] =
useState(true) useState(true)
...@@ -59,6 +59,7 @@ export function DevToolsIndicator({ ...@@ -59,6 +59,7 @@ export function DevToolsIndicator({
isTurbopack={!!process.env.TURBOPACK} isTurbopack={!!process.env.TURBOPACK}
disabled={state.disableDevIndicator || !isDevToolsIndicatorVisible} disabled={state.disableDevIndicator || !isDevToolsIndicatorVisible}
isBuildError={isBuildError} isBuildError={isBuildError}
{...props}
/> />
) )
} }
...@@ -73,19 +74,6 @@ interface C { ...@@ -73,19 +74,6 @@ interface C {
const Context = createContext({} as C) const Context = createContext({} as C)
function getInitialPosition() {
if (
typeof localStorage !== 'undefined' &&
localStorage.getItem(STORAGE_KEY_POSITION)
) {
return localStorage.getItem(
STORAGE_KEY_POSITION
) as DevToolsIndicatorPosition
}
return INDICATOR_POSITION
}
const OVERLAYS = { const OVERLAYS = {
Root: 'root', Root: 'root',
Turbo: 'turbo', Turbo: 'turbo',
...@@ -104,6 +92,8 @@ function DevToolsPopover({ ...@@ -104,6 +92,8 @@ function DevToolsPopover({
isBuildError, isBuildError,
hide, hide,
setIsErrorOverlayOpen, setIsErrorOverlayOpen,
scale,
setScale,
}: { }: {
routerType: 'pages' | 'app' routerType: 'pages' | 'app'
disabled: boolean disabled: boolean
...@@ -116,6 +106,8 @@ function DevToolsPopover({ ...@@ -116,6 +106,8 @@ function DevToolsPopover({
setIsErrorOverlayOpen: ( setIsErrorOverlayOpen: (
isOverlayOpen: boolean | ((prev: boolean) => boolean) isOverlayOpen: boolean | ((prev: boolean) => boolean)
) => void ) => void
scale: DevToolsScale
setScale: (value: DevToolsScale) => void
}) { }) {
const menuRef = useRef<HTMLDivElement>(null) const menuRef = useRef<HTMLDivElement>(null)
const triggerRef = useRef<HTMLButtonElement | null>(null) const triggerRef = useRef<HTMLButtonElement | null>(null)
...@@ -289,6 +281,7 @@ function DevToolsPopover({ ...@@ -289,6 +281,7 @@ function DevToolsPopover({
isDevBuilding={useIsDevBuilding()} isDevBuilding={useIsDevBuilding()}
isDevRendering={useIsDevRendering()} isDevRendering={useIsDevRendering()}
isBuildError={isBuildError} isBuildError={isBuildError}
scale={scale}
/> />
{/* Route Info */} {/* Route Info */}
...@@ -318,6 +311,8 @@ function DevToolsPopover({ ...@@ -318,6 +311,8 @@ function DevToolsPopover({
hide={handleHideDevtools} hide={handleHideDevtools}
setPosition={setPosition} setPosition={setPosition}
position={position} position={position}
scale={scale}
setScale={setScale}
/> />
{/* Dropdown Menu */} {/* Dropdown Menu */}
......
import { useState } from 'react'
import {
STORAGE_KEY_POSITION,
STORAGE_KEY_SCALE,
STORAGE_KEY_THEME,
} from '../../../../../shared'
const INDICATOR_POSITION =
(process.env
.__NEXT_DEV_INDICATOR_POSITION as typeof window.__NEXT_DEV_INDICATOR_POSITION) ||
'bottom-left'
export type DevToolsIndicatorPosition = typeof INDICATOR_POSITION
export function getInitialPosition() {
if (
typeof localStorage !== 'undefined' &&
localStorage.getItem(STORAGE_KEY_POSITION)
) {
return localStorage.getItem(
STORAGE_KEY_POSITION
) as DevToolsIndicatorPosition
}
return INDICATOR_POSITION
}
//////////////////////////////////////////////////////////////////////////////////////
const BASE_SIZE = 16
export const NEXT_DEV_TOOLS_SCALE = {
Small: BASE_SIZE / 14,
Medium: BASE_SIZE / 16,
Large: BASE_SIZE / 18,
} as const
export type DevToolsScale =
(typeof NEXT_DEV_TOOLS_SCALE)[keyof typeof NEXT_DEV_TOOLS_SCALE]
function getInitialScale() {
if (
typeof localStorage !== 'undefined' &&
localStorage.getItem(STORAGE_KEY_SCALE)
) {
return Number(localStorage.getItem(STORAGE_KEY_SCALE)) as DevToolsScale
}
return NEXT_DEV_TOOLS_SCALE.Medium
}
export function useDevToolsScale(): [
DevToolsScale,
(value: DevToolsScale) => void,
] {
const [scale, setScale] = useState<DevToolsScale>(getInitialScale())
function set(value: DevToolsScale) {
setScale(value)
localStorage.setItem(STORAGE_KEY_SCALE, String(value))
}
return [scale, set]
}
//////////////////////////////////////////////////////////////////////////////////////
export function getInitialTheme() {
if (typeof localStorage === 'undefined') {
return 'system'
}
const theme = localStorage.getItem(STORAGE_KEY_THEME)
return theme === 'dark' || theme === 'light' ? theme : 'system'
}
...@@ -7,30 +7,30 @@ import DarkIcon from '../../../../icons/dark-icon' ...@@ -7,30 +7,30 @@ import DarkIcon from '../../../../icons/dark-icon'
import SystemIcon from '../../../../icons/system-icon' import SystemIcon from '../../../../icons/system-icon'
import type { DevToolsInfoPropsCore } from './dev-tools-info' import type { DevToolsInfoPropsCore } from './dev-tools-info'
import { DevToolsInfo } from './dev-tools-info' import { DevToolsInfo } from './dev-tools-info'
import type { DevToolsIndicatorPosition } from '../dev-tools-indicator' import {
getInitialTheme,
function getInitialPreference() { NEXT_DEV_TOOLS_SCALE,
if (typeof localStorage === 'undefined') { type DevToolsIndicatorPosition,
return 'system' type DevToolsScale,
} } from './preferences'
const theme = localStorage.getItem(STORAGE_KEY_THEME)
return theme === 'dark' || theme === 'light' ? theme : 'system'
}
export function UserPreferences({ export function UserPreferences({
setPosition, setPosition,
position, position,
hide, hide,
scale,
setScale,
...props ...props
}: { }: {
setPosition: (position: DevToolsIndicatorPosition) => void setPosition: (position: DevToolsIndicatorPosition) => void
position: DevToolsIndicatorPosition position: DevToolsIndicatorPosition
scale: DevToolsScale
setScale: (value: DevToolsScale) => void
hide: () => void hide: () => void
} & DevToolsInfoPropsCore & } & DevToolsInfoPropsCore &
HTMLProps<HTMLDivElement>) { Omit<HTMLProps<HTMLDivElement>, 'size'>) {
// derive initial theme from system preference // derive initial theme from system preference
const [theme, setTheme] = useState(getInitialPreference()) const [theme, setTheme] = useState(getInitialTheme())
const handleThemeChange = (e: React.ChangeEvent<HTMLSelectElement>) => { const handleThemeChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const portal = document.querySelector('nextjs-portal') const portal = document.querySelector('nextjs-portal')
...@@ -61,6 +61,11 @@ export function UserPreferences({ ...@@ -61,6 +61,11 @@ export function UserPreferences({
localStorage.setItem(STORAGE_KEY_POSITION, e.target.value) localStorage.setItem(STORAGE_KEY_POSITION, e.target.value)
} }
function handleSizeChange({ target }: React.ChangeEvent<HTMLSelectElement>) {
const value = Number(target.value) as DevToolsScale
setScale(value)
}
return ( return (
<DevToolsInfo title="Preferences" {...props}> <DevToolsInfo title="Preferences" {...props}>
<div className="preferences-container"> <div className="preferences-container">
...@@ -104,6 +109,29 @@ export function UserPreferences({ ...@@ -104,6 +109,29 @@ export function UserPreferences({
</Select> </Select>
</div> </div>
<div className="preference-section">
<div className="preference-header">
<label htmlFor="size">Size</label>
<p className="preference-description">
Adjust the size of your dev tools.
</p>
</div>
<Select
id="size"
name="size"
value={scale}
onChange={handleSizeChange}
>
{Object.entries(NEXT_DEV_TOOLS_SCALE).map(([key, value]) => {
return (
<option value={value} key={key}>
{key}
</option>
)
})}
</Select>
</div>
<div className="preference-section"> <div className="preference-section">
<div className="preference-header"> <div className="preference-header">
<label htmlFor="hide-dev-tools"> <label htmlFor="hide-dev-tools">
......
...@@ -2,6 +2,7 @@ import { forwardRef, useEffect, useMemo, useRef, useState } from 'react' ...@@ -2,6 +2,7 @@ import { forwardRef, useEffect, useMemo, useRef, useState } from 'react'
import { css } from '../../../../utils/css' import { css } from '../../../../utils/css'
import mergeRefs from '../../../utils/merge-refs' import mergeRefs from '../../../utils/merge-refs'
import { useMinimumLoadingTimeMultiple } from './use-minimum-loading-time-multiple' import { useMinimumLoadingTimeMultiple } from './use-minimum-loading-time-multiple'
import type { DevToolsScale } from './dev-tools-info/preferences'
interface Props extends React.ComponentProps<'button'> { interface Props extends React.ComponentProps<'button'> {
issueCount: number issueCount: number
...@@ -10,9 +11,9 @@ interface Props extends React.ComponentProps<'button'> { ...@@ -10,9 +11,9 @@ interface Props extends React.ComponentProps<'button'> {
isBuildError: boolean isBuildError: boolean
onTriggerClick: () => void onTriggerClick: () => void
toggleErrorOverlay: () => void toggleErrorOverlay: () => void
scale: DevToolsScale
} }
const SIZE = 36
const SHORT_DURATION_MS = 150 const SHORT_DURATION_MS = 150
export const NextLogo = forwardRef(function NextLogo( export const NextLogo = forwardRef(function NextLogo(
...@@ -24,10 +25,13 @@ export const NextLogo = forwardRef(function NextLogo( ...@@ -24,10 +25,13 @@ export const NextLogo = forwardRef(function NextLogo(
isBuildError, isBuildError,
onTriggerClick, onTriggerClick,
toggleErrorOverlay, toggleErrorOverlay,
scale,
...props ...props
}: Props, }: Props,
propRef: React.Ref<HTMLButtonElement> propRef: React.Ref<HTMLButtonElement>
) { ) {
const SIZE = 36 / scale
const hasError = issueCount > 0 const hasError = issueCount > 0
const [isErrorExpanded, setIsErrorExpanded] = useState(hasError) const [isErrorExpanded, setIsErrorExpanded] = useState(hasError)
const [dismissed, setDismissed] = useState(false) const [dismissed, setDismissed] = useState(false)
...@@ -50,7 +54,7 @@ export const NextLogo = forwardRef(function NextLogo( ...@@ -50,7 +54,7 @@ export const NextLogo = forwardRef(function NextLogo(
if (pristine && hasError) width = 'auto' if (pristine && hasError) width = 'auto'
// Default state, collapsed // Default state, collapsed
return { width } return { width }
}, [measuredWidth, pristine, hasError]) }, [measuredWidth, pristine, hasError, SIZE])
useEffect(() => { useEffect(() => {
setIsErrorExpanded(hasError) setIsErrorExpanded(hasError)
......
...@@ -9,6 +9,7 @@ import { ErrorOverlay } from './components/errors/error-overlay/error-overlay' ...@@ -9,6 +9,7 @@ import { ErrorOverlay } from './components/errors/error-overlay/error-overlay'
import { DevToolsIndicator } from './components/errors/dev-tools-indicator/dev-tools-indicator' import { DevToolsIndicator } from './components/errors/dev-tools-indicator/dev-tools-indicator'
import { RenderError } from './container/runtime-error/render-error' import { RenderError } from './container/runtime-error/render-error'
import { DarkTheme } from './styles/dark-theme' import { DarkTheme } from './styles/dark-theme'
import { useDevToolsScale } from './components/errors/dev-tools-indicator/dev-tools-info/preferences'
export function DevOverlay({ export function DevOverlay({
state, state,
...@@ -21,10 +22,11 @@ export function DevOverlay({ ...@@ -21,10 +22,11 @@ export function DevOverlay({
isErrorOverlayOpen: boolean | ((prev: boolean) => boolean) isErrorOverlayOpen: boolean | ((prev: boolean) => boolean)
) => void ) => void
}) { }) {
const [scale, setScale] = useDevToolsScale()
return ( return (
<ShadowPortal> <ShadowPortal>
<CssReset /> <CssReset />
<Base /> <Base scale={scale} />
<Colors /> <Colors />
<ComponentStyles /> <ComponentStyles />
<DarkTheme /> <DarkTheme />
...@@ -35,6 +37,8 @@ export function DevOverlay({ ...@@ -35,6 +37,8 @@ export function DevOverlay({
return ( return (
<> <>
<DevToolsIndicator <DevToolsIndicator
scale={scale}
setScale={setScale}
state={state} state={state}
errorCount={totalErrorCount} errorCount={totalErrorCount}
isBuildError={isBuildError} isBuildError={isBuildError}
......
import { css } from '../../utils/css' import { css } from '../../utils/css'
import type { DevToolsScale } from '../components/errors/dev-tools-indicator/dev-tools-info/preferences'
export function Base() { export function Base({ scale = 1 }: { scale?: DevToolsScale }) {
return ( return (
<style> <style>
{css` {css`
...@@ -70,51 +71,51 @@ export function Base() { ...@@ -70,51 +71,51 @@ export function Base() {
--rounded-full: 9999px; --rounded-full: 9999px;
/* /*
These used to be in rem units but we have realised This value gets set from the Dev Tools preferences,
that using rem to style the Dev Overlays is not a and we use the following --size-* variables to
good idea because if the user sets their root font size scale the relevant elements.
to something tiny, they will not have a great time using
the Overlays because they will appear unexpectedly tiny.
In the future, we want to use these variables to offer The reason why we don't rely on rem values is because
a custom preference in the dropdown menu to alter the size if an app sets their root font size to something tiny,
of the Overlays. it feels unexpected to have the app root size leak
into a Next.js surface.
Ref: https://github.com/vercel/next.js/discussions/76812 https://github.com/vercel/next.js/discussions/76812
*/ */
--size-1: 1px; --nextjs-dev-tools-scale: ${String(scale)};
--size-2: 2px; --size-1: calc(1px / var(--nextjs-dev-tools-scale));
--size-3: 3px; --size-2: calc(2px / var(--nextjs-dev-tools-scale));
--size-4: 4px; --size-3: calc(3px / var(--nextjs-dev-tools-scale));
--size-5: 5px; --size-4: calc(4px / var(--nextjs-dev-tools-scale));
--size-6: 6px; --size-5: calc(5px / var(--nextjs-dev-tools-scale));
--size-7: 7px; --size-6: calc(6px / var(--nextjs-dev-tools-scale));
--size-8: 8px; --size-7: calc(7px / var(--nextjs-dev-tools-scale));
--size-9: 9px; --size-8: calc(8px / var(--nextjs-dev-tools-scale));
--size-10: 10px; --size-9: calc(9px / var(--nextjs-dev-tools-scale));
--size-11: 11px; --size-10: calc(10px / var(--nextjs-dev-tools-scale));
--size-12: 12px; --size-11: calc(11px / var(--nextjs-dev-tools-scale));
--size-13: 13px; --size-12: calc(12px / var(--nextjs-dev-tools-scale));
--size-14: 14px; --size-13: calc(13px / var(--nextjs-dev-tools-scale));
--size-15: 15px; --size-14: calc(14px / var(--nextjs-dev-tools-scale));
--size-16: 16px; --size-15: calc(15px / var(--nextjs-dev-tools-scale));
--size-17: 17px; --size-16: calc(16px / var(--nextjs-dev-tools-scale));
--size-18: 18px; --size-17: calc(17px / var(--nextjs-dev-tools-scale));
--size-20: 20px; --size-18: calc(18px / var(--nextjs-dev-tools-scale));
--size-22: 22px; --size-20: calc(20px / var(--nextjs-dev-tools-scale));
--size-24: 24px; --size-22: calc(22px / var(--nextjs-dev-tools-scale));
--size-26: 26px; --size-24: calc(24px / var(--nextjs-dev-tools-scale));
--size-28: 28px; --size-26: calc(26px / var(--nextjs-dev-tools-scale));
--size-30: 30px; --size-28: calc(28px / var(--nextjs-dev-tools-scale));
--size-32: 32px; --size-30: calc(30px / var(--nextjs-dev-tools-scale));
--size-34: 34px; --size-32: calc(32px / var(--nextjs-dev-tools-scale));
--size-36: 36px; --size-34: calc(34px / var(--nextjs-dev-tools-scale));
--size-38: 38px; --size-36: calc(36px / var(--nextjs-dev-tools-scale));
--size-40: 40px; --size-38: calc(38px / var(--nextjs-dev-tools-scale));
--size-42: 42px; --size-40: calc(40px / var(--nextjs-dev-tools-scale));
--size-44: 44px; --size-42: calc(42px / var(--nextjs-dev-tools-scale));
--size-46: 46px; --size-44: calc(44px / var(--nextjs-dev-tools-scale));
--size-48: 48px; --size-46: calc(46px / var(--nextjs-dev-tools-scale));
--size-48: calc(48px / var(--nextjs-dev-tools-scale));
@media print { @media print {
display: none; display: none;
......
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