Skip to content

Commit

Permalink
Fix build activity indicator position (vercel#36208)
Browse files Browse the repository at this point in the history
* Fix build activity indicator position

`devIndicators.buildActivityPosition` introduced in
vercel#30109 needed more tweaks to properly
position the build indicator container. The build indicator was being rendered
off screen when set to a non-default position.

* Refactor stuff for smaller diff

* add config validation

* Apply suggestions from code review

Co-authored-by: Martin Šťovíček <martin.stovicek@monitora.cz>
  • Loading branch information
2 people authored and SukkaW committed Apr 18, 2022
1 parent c04aebd commit f7963f2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
29 changes: 11 additions & 18 deletions packages/next/client/dev/dev-build-watcher.js
Expand Up @@ -5,21 +5,14 @@ export default function initializeBuildWatcher(
position = 'bottom-right'
) {
const shadowHost = document.createElement('div')
const [verticalProperty, horizontalProperty] = position.split('-')
shadowHost.id = '__next-build-watcher'
// Make sure container is fixed and on a high zIndex so it shows
shadowHost.style.position = 'fixed'
// Ensure container's position to be top or bottom (default)
if (['top-left', 'top-right'].indexOf(position) > -1) {
shadowHost.style.top = '10px'
} else {
shadowHost.style.bottom = '10px'
}
shadowHost.style[verticalProperty] = '10px'
// Ensure container's position to be left or right (default)
if (['bottom-left', 'top-left'].indexOf(position) > -1) {
shadowHost.style.left = '20px'
} else {
shadowHost.style.right = '20px'
}
shadowHost.style[horizontalProperty] = '20px'
shadowHost.style.width = 0
shadowHost.style.height = 0
shadowHost.style.zIndex = 99999
Expand All @@ -43,7 +36,7 @@ export default function initializeBuildWatcher(
shadowRoot.appendChild(container)

// CSS
const css = createCss(prefix)
const css = createCss(prefix, { horizontalProperty, verticalProperty })
shadowRoot.appendChild(css)

// State
Expand Down Expand Up @@ -134,13 +127,13 @@ function createContainer(prefix) {
return container
}

function createCss(prefix) {
function createCss(prefix, { horizontalProperty, verticalProperty }) {
const css = document.createElement('style')
css.textContent = `
#${prefix}container {
position: absolute;
bottom: 10px;
right: 30px;
${verticalProperty}: 10px;
${horizontalProperty}: 30px;
border-radius: 3px;
background: #000;
Expand All @@ -158,7 +151,7 @@ function createCss(prefix) {
display: none;
opacity: 0;
transition: opacity 0.1s ease, bottom 0.1s ease;
transition: opacity 0.1s ease, ${verticalProperty} 0.1s ease;
animation: ${prefix}fade-in 0.1s ease-in-out;
}
Expand All @@ -167,7 +160,7 @@ function createCss(prefix) {
}
#${prefix}container.${prefix}building {
bottom: 20px;
${verticalProperty}: 20px;
opacity: 1;
}
Expand All @@ -187,11 +180,11 @@ function createCss(prefix) {
@keyframes ${prefix}fade-in {
from {
bottom: 10px;
${verticalProperty}: 10px;
opacity: 0;
}
to {
bottom: 20px;
${verticalProperty}: 20px;
opacity: 1;
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/next/server/config.ts
Expand Up @@ -616,6 +616,24 @@ function assignDefaults(userConfig: { [key: string]: any }) {
result.pageExtensions = pageExtensions
}

if (result.devIndicators?.buildActivityPosition) {
const { buildActivityPosition } = result.devIndicators
const allowedValues = [
'top-left',
'top-right',
'bottom-left',
'bottom-right',
]

if (!allowedValues.includes(buildActivityPosition)) {
throw new Error(
`Invalid "devIndicator.buildActivityPosition" provided, expected one of ${allowedValues.join(
', '
)}, received ${buildActivityPosition}`
)
}
}

return result
}

Expand Down
31 changes: 31 additions & 0 deletions test/integration/build-indicator/test/index.test.js
Expand Up @@ -24,6 +24,37 @@ const installCheckVisible = (browser) => {
}

describe('Build Activity Indicator', () => {
it('should validate buildActivityPosition config', async () => {
let stderr = ''
const configPath = join(appDir, 'next.config.js')
await fs.writeFile(
configPath,
`
module.exports = {
devIndicators: {
buildActivityPosition: 'ttop-leff'
}
}
`
)
const app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg
},
}).catch((err) => {
console.error('got err', err)
})
await fs.remove(configPath)

expect(stderr).toContain(
`Invalid "devIndicator.buildActivityPosition" provided, expected one of top-left, top-right, bottom-left, bottom-right, received ttop-leff`
)

if (app) {
await killApp(app)
}
})

describe('Enabled', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
Expand Down

0 comments on commit f7963f2

Please sign in to comment.