Merge claude/totp-pl-cookie-button: plain en/pl 2FA texts, CookieButton above the footer

TOTP_TEXTS (en, pl) merged over payload-2fa's, project translations still win,
twoFactor.texts: false keeps the package's; tab titles on the 2FA views
(TOTP_VIEW_TITLE). CookieButton lifts above the footer (avoid, default
COOKIE_BUTTON_AVOID; side; bottomOverlap; --ipal-cookie-button-inset). Admin
language keeps following the browser (no fallbackLanguage change).

Merge addition: the branch had no CHANGELOG entry — both items added under New
in the one Unreleased section, plus Requires project changes item 6 (CookieButton).
This commit is contained in:
Radosław Smoliński
2026-10-29 15:46:09 +01:00
16 changed files with 291 additions and 11 deletions
+69 -5
View File
@@ -1,22 +1,42 @@
'use client'
import type { CSSProperties } from 'react'
import { Cookie } from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
import { useConsentContext } from './ConsentContext.js'
import { useIsMounted } from './useIsMounted.js'
/** Elements the button moves up for, so it never covers footer links. */
export const COOKIE_BUTTON_AVOID = 'footer, [role="contentinfo"]'
export type CookieButtonProps = {
/**
* CSS selector of elements the button lifts above while they are on screen
* (default: the site footer). `false` = never move.
*/
avoid?: false | string
/** Replaces the default classes (look and position). The lift above `avoid` still applies. */
className?: string
/** Corner. Default 'left'. */
side?: 'left' | 'right'
}
/**
* Floating "cookie settings" button — lets visitors reopen the consent panel
* after deciding (GDPR: withdrawing consent must be as easy as giving it).
* Hidden while the banner is showing.
*
* Colours follow the same design tokens as CookieBanner (--ipal-surface,
* --ipal-border, --ipal-hover, --ipal-text), so a client's palette applies to
* both without touching either component. Pass className to
* replace the styling outright.
* --ipal-border, --ipal-hover, --ipal-text). Position: --ipal-cookie-button-offset
* (distance from the bottom, default 1rem), --ipal-cookie-button-inset (from the
* side, default 1rem), z-index --ipal-z-header. When the footer scrolls into
* view the button moves up by the visible part of it, so footer links stay free.
*/
export function CookieButton({ className }: { className?: string } = {}) {
export function CookieButton({ avoid = COOKIE_BUTTON_AVOID, className, side = 'left' }: CookieButtonProps = {}) {
const isMounted = useIsMounted()
const { decided, reopen, texts } = useConsentContext()
const lift = useLiftAbove(avoid, isMounted && decided)
// Render nothing until mounted — prevents hydration mismatch (no project
// ConsentWrapper workaround needed).
@@ -25,9 +45,18 @@ export function CookieButton({ className }: { className?: string } = {}) {
const cls =
className ??
'fixed bottom-4 left-4 z-40 flex h-11 w-11 items-center justify-center rounded-full border shadow-md transition-colors ' +
'flex h-11 w-11 items-center justify-center rounded-full border shadow-md transition-[background-color,transform] ' +
'bg-[var(--ipal-surface,light-dark(#fff,var(--ipal-surface-dark,#171717)))] border-[var(--ipal-border,light-dark(#e5e5e5,var(--ipal-border-dark,#262626)))] hover:bg-[var(--ipal-hover,light-dark(#f5f5f5,var(--ipal-hover-dark,#262626)))]'
const position: CSSProperties = className
? {}
: {
bottom: 'calc(var(--ipal-cookie-button-offset, 1rem) + env(safe-area-inset-bottom, 0px))',
position: 'fixed',
[side]: 'var(--ipal-cookie-button-inset, 1rem)',
zIndex: 'var(--ipal-z-header, 40)' as unknown as number,
}
return (
<button
// a11y label per locale: the panel's "settings title" (Cookie Settings global)
@@ -35,6 +64,7 @@ export function CookieButton({ className }: { className?: string } = {}) {
className={cls}
data-ipal="cookie-button"
onClick={reopen}
style={{ ...position, ...(lift ? { transform: `translateY(-${lift}px)` } : {}) }}
type="button"
>
<Cookie
@@ -44,3 +74,37 @@ export function CookieButton({ className }: { className?: string } = {}) {
</button>
)
}
/** Pixels of the `avoid` elements visible at the bottom of the viewport (0 when none). */
export function bottomOverlap(tops: number[], viewportHeight: number): number {
const top = Math.min(...tops, viewportHeight)
// Never push the button off the top of the screen.
return Math.max(0, Math.min(viewportHeight - top, viewportHeight - 96))
}
function useLiftAbove(selector: false | string, active: boolean): number {
const [lift, setLift] = useState(0)
const frame = useRef(0)
useEffect(() => {
if (!selector || !active) {return}
const measure = () => {
cancelAnimationFrame(frame.current)
frame.current = requestAnimationFrame(() => {
const tops = [...document.querySelectorAll(selector)].map((el) => el.getBoundingClientRect().top)
setLift(bottomOverlap(tops, window.innerHeight))
})
}
measure()
window.addEventListener('scroll', measure, { passive: true })
window.addEventListener('resize', measure)
const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(measure)
observer?.observe(document.body)
return () => {
cancelAnimationFrame(frame.current)
window.removeEventListener('scroll', measure)
window.removeEventListener('resize', measure)
observer?.disconnect()
}
}, [selector, active])
return selector ? lift : 0
}
+2 -1
View File
@@ -3,7 +3,8 @@ export { ConsentProvider, useConsentContext } from './ConsentContext.js'
export { ConsentScript } from './ConsentScript.js'
export { CookieBanner } from './CookieBanner.js'
export type { CookieBannerAction, CookieBannerClassNames, CookieSettingsAction } from './CookieBanner.js'
export { CookieButton } from './CookieButton.js'
export { bottomOverlap, COOKIE_BUTTON_AVOID, CookieButton } from './CookieButton.js'
export type { CookieButtonProps } from './CookieButton.js'
// Client-only consent exports — hook, provider, and UI components.
export { DEFAULT_COOKIE_MAP, useConsent } from './useConsent.js'
export type { ConsentCookieMap } from './useConsent.js'
+51
View File
@@ -0,0 +1,51 @@
/**
* Texts of the 2FA screens and the user field (@clocklimited/payload-2fa reads
* them as `totpPlugin:*` through Payload's i18n). The package ships long,
* informal texts; these follow docs/dev/admin-ux.md. ipalKit merges them over
* the package's and under the project's own `i18n.translations`, so a project
* still overrides any key. `setup.description` is rendered as HTML by the package.
*/
const AUTHENTICATOR =
'<a href="https://support.google.com/accounts/answer/1066447" target="_blank" rel="nofollow noopener noreferrer">Google Authenticator</a>'
export const TOTP_TEXTS = {
en: {
totpPlugin: {
authApp: 'Authenticator app',
configured: 'Set up',
errors: { alreadySet: 'Two-step verification is already set up.' },
fieldDescription: 'Codes from an authenticator app protect this account.',
forceTotp: { description: 'The user must set up two-step verification.', label: 'Require 2FA' },
setup: {
addCodeManually: 'Enter the key manually',
button: 'Set up',
description: `Scan the QR code with an authenticator app, e.g. ${AUTHENTICATOR} or Microsoft Authenticator.`,
enterCode: 'Enter the {digits}-digit code from the app:',
incorrectCode: 'Incorrect code. Check the time on your phone or scan the QR code again.',
title: 'Set up two-step verification',
},
verify: { title: 'Two-step verification' },
},
},
pl: {
totpPlugin: {
authApp: 'Aplikacja uwierzytelniająca',
configured: 'Skonfigurowano',
errors: { alreadySet: 'Weryfikacja dwuetapowa jest już skonfigurowana.' },
fieldDescription: 'Kody z aplikacji uwierzytelniającej chronią to konto.',
forceTotp: { description: 'Użytkownik musi skonfigurować weryfikację dwuetapową.', label: 'Wymagaj 2FA' },
setup: {
addCodeManually: 'Wpisz klucz ręcznie',
button: 'Skonfiguruj',
description: `Zeskanuj kod QR aplikacją uwierzytelniającą, np. ${AUTHENTICATOR} lub Microsoft Authenticator.`,
enterCode: 'Wpisz {digits}-cyfrowy kod z aplikacji:',
incorrectCode: 'Nieprawidłowy kod. Sprawdź czas w telefonie lub zeskanuj kod QR ponownie.',
title: 'Konfiguracja weryfikacji dwuetapowej',
},
verify: { title: 'Weryfikacja dwuetapowa' },
},
},
}
/** Browser-tab title of the 2FA screens (Payload view meta is a plain string: the site's language). */
export const TOTP_VIEW_TITLE = { en: 'Two-step verification', pl: 'Weryfikacja dwuetapowa' }