Theme switcher, table of contents, missing-page redirect, form placeholders and time field; audit and revalidate fixes
- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
23e033d7d7
commit
faa4ad679c
@@ -1,9 +1,12 @@
|
||||
'use client'
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
import { useTheme } from '../theme/useTheme.js'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
turnstile?: {
|
||||
remove?: (id: string) => void
|
||||
render: (el: HTMLElement, opts: Record<string, unknown>) => string
|
||||
reset: (id?: string) => void
|
||||
}
|
||||
@@ -32,9 +35,15 @@ export type TurnstileProps = {
|
||||
* client component and can't read Payload itself.
|
||||
*/
|
||||
siteKey: string
|
||||
theme?: 'auto' | 'dark' | 'light'
|
||||
/**
|
||||
* 'page' (default): the site's theme (data-theme on <html>, ThemeScript) — a dark
|
||||
* widget on a dark page; no site theme → 'auto' (the device's). Or fixed.
|
||||
*/
|
||||
theme?: TurnstileTheme
|
||||
}
|
||||
|
||||
export type TurnstileTheme = 'auto' | 'dark' | 'light' | 'page'
|
||||
|
||||
/**
|
||||
* Cloudflare Turnstile widget — reusable across any form. Renders the challenge
|
||||
* and reports the token via onToken. The token must then be verified
|
||||
@@ -45,9 +54,24 @@ export type TurnstileProps = {
|
||||
* The script is injected directly (no next/script dependency) so the widget
|
||||
* stays framework-agnostic.
|
||||
*/
|
||||
export function Turnstile({ onToken, siteKey, theme = 'auto' }: TurnstileProps) {
|
||||
export function Turnstile({ onToken, siteKey, theme = 'page' }: TurnstileProps) {
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const widgetId = useRef<null | string>(null)
|
||||
const pageTheme = useTheme().theme
|
||||
const widgetTheme = theme === 'page' ? (pageTheme ?? 'auto') : theme
|
||||
const renderedTheme = useRef(widgetTheme)
|
||||
|
||||
// The page switched theme: drop the widget so the effect below draws it again in the new one.
|
||||
// Declared first, so it runs before the render effect.
|
||||
useEffect(() => {
|
||||
if (renderedTheme.current === widgetTheme) {return}
|
||||
renderedTheme.current = widgetTheme
|
||||
if (widgetId.current && window.turnstile?.remove) {
|
||||
window.turnstile.remove(widgetId.current)
|
||||
widgetId.current = null
|
||||
onToken(null)
|
||||
}
|
||||
}, [widgetTheme, onToken])
|
||||
|
||||
useEffect(() => {
|
||||
if (!siteKey) {return}
|
||||
@@ -61,7 +85,7 @@ export function Turnstile({ onToken, siteKey, theme = 'auto' }: TurnstileProps)
|
||||
'error-callback': () => onToken(null),
|
||||
'expired-callback': () => onToken(null),
|
||||
sitekey: siteKey,
|
||||
theme,
|
||||
theme: widgetTheme,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -69,7 +93,7 @@ export function Turnstile({ onToken, siteKey, theme = 'auto' }: TurnstileProps)
|
||||
// Script may load after mount — retry briefly until ready.
|
||||
const interval = setInterval(render, 300)
|
||||
return () => clearInterval(interval)
|
||||
}, [siteKey, theme, onToken])
|
||||
}, [siteKey, widgetTheme, onToken])
|
||||
|
||||
if (!siteKey) {return null}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
import { createContext, use, useCallback, useState } from 'react'
|
||||
|
||||
import type { TurnstileTheme } from './Turnstile.js'
|
||||
|
||||
import { Turnstile } from './Turnstile.js'
|
||||
|
||||
type TurnstileContextValue = {
|
||||
@@ -58,7 +60,7 @@ export function useTurnstile(): {
|
||||
enabled: boolean
|
||||
reset: () => void
|
||||
token: null | string
|
||||
TurnstileWidget: (props?: { theme?: 'auto' | 'dark' | 'light' }) => React.ReactNode
|
||||
TurnstileWidget: (props?: { theme?: TurnstileTheme }) => React.ReactNode
|
||||
} {
|
||||
const ctx = use(TurnstileContext)
|
||||
if (!ctx) {
|
||||
@@ -84,7 +86,7 @@ function useTurnstileFrom(ctx: TurnstileContextValue): ReturnType<typeof useTurn
|
||||
const reset = useCallback(() => setToken(null), [setToken])
|
||||
|
||||
const TurnstileWidget = useCallback(
|
||||
(props?: { theme?: 'auto' | 'dark' | 'light' }) => {
|
||||
(props?: { theme?: TurnstileTheme }) => {
|
||||
if (!siteKey) {return null}
|
||||
return <Turnstile onToken={setToken} siteKey={siteKey} theme={props?.theme} />
|
||||
},
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
// Client-only exports — the Turnstile widget. Kept separate from index.ts so
|
||||
// the server-only verify never leaks into a browser bundle.
|
||||
export { Turnstile } from './Turnstile.js'
|
||||
export type { TurnstileProps } from './Turnstile.js'
|
||||
export type { TurnstileProps, TurnstileTheme } from './Turnstile.js'
|
||||
export { TurnstileProvider, useOptionalTurnstile, useTurnstile } from './TurnstileProvider.js'
|
||||
|
||||
Reference in New Issue
Block a user