Under construction mode (developer tool)
Site Settings -> Under construction: a switch, 'Pages still available' and notice texts. When on, only the homepage and the chosen pages open (every language); other addresses redirect to the homepage with the notice (createPageRoute redirect option), and <ConstructionNotice> cancels clicks on links to blocked pages and shows the notice. getConstructionMode(locale). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
'use client'
|
||||
|
||||
import { useCallback, useEffect, useId, useRef, useState } from 'react'
|
||||
|
||||
import type { ConstructionMode } from './types.js'
|
||||
|
||||
import { isPathAllowed } from './logic.js'
|
||||
import { CONSTRUCTION_NOTICE_PARAM, CONSTRUCTION_NOTICE_VALUE } from './types.js'
|
||||
|
||||
export type ConstructionNoticeClassNames = {
|
||||
button?: string
|
||||
dialog?: string
|
||||
message?: string
|
||||
overlay?: string
|
||||
title?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The "site under construction" notice (Site Settings → Under construction).
|
||||
* Opens after a redirect from a blocked address (?notice=construction, then
|
||||
* removed from the URL) and when a visitor clicks a link to a page that isn't
|
||||
* available — the click is cancelled. Closes with its button or Escape.
|
||||
* Renders nothing while the mode is off.
|
||||
*
|
||||
* Unstyled (classNames, like CookieBanner); texts come from the panel.
|
||||
*
|
||||
* // [locale]/layout.tsx
|
||||
* <ConstructionNotice mode={await getConstructionMode(locale)} classNames={{ overlay: '…', dialog: '…' }} />
|
||||
*/
|
||||
export function ConstructionNotice({ classNames: cn = {}, mode }: { classNames?: ConstructionNoticeClassNames; mode: ConstructionMode }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||
const titleId = useId()
|
||||
const close = useCallback(() => setOpen(false), [])
|
||||
const { allowedPaths, enabled } = mode
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) {return}
|
||||
// After a redirect: show the notice once and clean the address bar
|
||||
const url = new URL(window.location.href)
|
||||
if (url.searchParams.get(CONSTRUCTION_NOTICE_PARAM) === CONSTRUCTION_NOTICE_VALUE) {
|
||||
setOpen(true)
|
||||
url.searchParams.delete(CONSTRUCTION_NOTICE_PARAM)
|
||||
window.history.replaceState(window.history.state, '', `${url.pathname}${url.search}${url.hash}`)
|
||||
}
|
||||
|
||||
// Capture phase, before Next's <Link> — a cancelled click doesn't navigate
|
||||
const onClick = (event: MouseEvent) => {
|
||||
if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {return}
|
||||
const anchor = event.target instanceof Element ? event.target.closest('a[href]') : null
|
||||
if (!(anchor instanceof HTMLAnchorElement) || anchor.hasAttribute('download')) {return}
|
||||
if (anchor.target && anchor.target !== '_self') {return}
|
||||
const target = new URL(anchor.href, window.location.href)
|
||||
if (target.origin !== window.location.origin || isPathAllowed(target.pathname, allowedPaths)) {return}
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
setOpen(true)
|
||||
}
|
||||
document.addEventListener('click', onClick, true)
|
||||
return () => document.removeEventListener('click', onClick, true)
|
||||
}, [enabled, allowedPaths])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {return}
|
||||
buttonRef.current?.focus()
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {close()}
|
||||
}
|
||||
document.addEventListener('keydown', onKey)
|
||||
return () => document.removeEventListener('keydown', onKey)
|
||||
}, [open, close])
|
||||
|
||||
if (!enabled || !open) {return null}
|
||||
return (
|
||||
<div className={cn.overlay} data-ipal="construction-notice">
|
||||
<div aria-labelledby={mode.texts.title ? titleId : undefined} aria-modal="true" className={cn.dialog} role="dialog">
|
||||
{mode.texts.title ? <p className={cn.title} id={titleId}>{mode.texts.title}</p> : null}
|
||||
{mode.texts.message ? <p className={cn.message}>{mode.texts.message}</p> : null}
|
||||
<button className={cn.button} onClick={close} ref={buttonRef} type="button">
|
||||
{mode.texts.button ?? '×'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user