Merge claude/cookie-button-bottom: the cookie button stays in the bottom corner of the window
Conflicts in the 1.17.2 changelog draft and the upgrade skill: both sides kept (rewritten into one entry at release). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -24,10 +24,12 @@ export {
|
||||
ConsentProvider,
|
||||
ConsentScript,
|
||||
COOKIE_BUTTON_AVOID,
|
||||
COOKIE_BUTTON_CLEAR,
|
||||
CookieBanner,
|
||||
CookieButton,
|
||||
readEffectiveConsent,
|
||||
signalVendors,
|
||||
stepAbove,
|
||||
useConsent,
|
||||
useConsentContext,
|
||||
} from '../modules/consent/client.js'
|
||||
|
||||
@@ -6,17 +6,30 @@ import { useEffect, useRef, useState } from 'react'
|
||||
|
||||
import { useConsentContext } from './ConsentContext.js'
|
||||
|
||||
/** Elements the button moves up for, so it never covers footer links. */
|
||||
/**
|
||||
* The site footer: the selector of the placement before 1.17.2, when the button moved up by the visible part of
|
||||
* the footer. `<CookieButton avoid={COOKIE_BUTTON_AVOID} />` brings that placement back.
|
||||
*/
|
||||
export const COOKIE_BUTTON_AVOID = 'footer, [role="contentinfo"]'
|
||||
|
||||
/** Controls the button never stays on top of once the page cannot be scrolled any further. */
|
||||
export const COOKIE_BUTTON_CLEAR = 'a[href], button, input, select, textarea, summary, [role="button"], [role="link"]'
|
||||
|
||||
export type CookieButtonProps = {
|
||||
/**
|
||||
* CSS selector of elements the button lifts above while they are on screen
|
||||
* (default: the site footer). `false` = never move.
|
||||
* Placement before 1.17.2: a CSS selector of elements the button lifts above while they are on screen
|
||||
* (`COOKIE_BUTTON_AVOID` = the site footer). Default: none — the button stays in the bottom corner of the
|
||||
* window. `false` = never move at all (also turns `clear` off).
|
||||
*/
|
||||
avoid?: false | string
|
||||
/** Replaces the default classes (look and position). The lift above `avoid` still applies. */
|
||||
/** Replaces the default classes (look and position). The lift above `avoid` and the step of `clear` still apply. */
|
||||
className?: string
|
||||
/**
|
||||
* CSS selector of controls the button must not keep covered. Only at the very end of the page (where a visitor
|
||||
* cannot scroll a covered link out from under it) the button steps up just far enough to free them. Default
|
||||
* `COOKIE_BUTTON_CLEAR` (links, buttons, form fields). `false` = stay in the corner whatever is under it.
|
||||
*/
|
||||
clear?: false | string
|
||||
/** Corner. Default 'left'. */
|
||||
side?: 'left' | 'right'
|
||||
}
|
||||
@@ -26,15 +39,20 @@ export type CookieButtonProps = {
|
||||
* after deciding (GDPR: withdrawing consent must be as easy as giving it).
|
||||
* Hidden while the banner is showing.
|
||||
*
|
||||
* Placement (1.17.2): fixed in the bottom corner of the window, on every page and at every scroll position — it
|
||||
* does not climb above the footer any more. At the very end of the page, if it would lie on a link or a button
|
||||
* there (a footer link in that corner), it steps up just above it, so nothing stays unreachable. The placement
|
||||
* before 1.17.2 (riding on the footer's top edge): `avoid={COOKIE_BUTTON_AVOID}`.
|
||||
*
|
||||
* Colours follow the same design tokens as CookieBanner (--ipal-surface,
|
||||
* --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.
|
||||
* side, default 1rem), both plus the device's safe-area inset; z-index --ipal-z-header (below the banner and dialogs).
|
||||
*/
|
||||
export function CookieButton({ avoid = COOKIE_BUTTON_AVOID, className, side = 'left' }: CookieButtonProps = {}) {
|
||||
export function CookieButton({ avoid, className, clear = COOKIE_BUTTON_CLEAR, side = 'left' }: CookieButtonProps = {}) {
|
||||
const { decided, reopen, texts } = useConsentContext()
|
||||
const lift = useLiftAbove(avoid, decided)
|
||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||
const lift = useLift({ active: decided, avoid: avoid ?? false, buttonRef, clear: avoid === undefined ? clear : false })
|
||||
|
||||
// `decided` is the server's value while hydrating (no decision on a static page, so nothing is rendered — or the
|
||||
// cookie the server read, <ConsentProvider cookieValue>, and then the button is in the HTML), the browser's after.
|
||||
@@ -50,7 +68,7 @@ export function CookieButton({ avoid = COOKIE_BUTTON_AVOID, className, side = 'l
|
||||
: {
|
||||
bottom: 'calc(var(--ipal-cookie-button-offset, 1rem) + env(safe-area-inset-bottom, 0px))',
|
||||
position: 'fixed',
|
||||
[side]: 'var(--ipal-cookie-button-inset, 1rem)',
|
||||
[side]: `calc(var(--ipal-cookie-button-inset, 1rem) + env(safe-area-inset-${side}, 0px))`,
|
||||
zIndex: 'var(--ipal-z-header, 40)' as unknown as number,
|
||||
}
|
||||
|
||||
@@ -61,6 +79,7 @@ export function CookieButton({ avoid = COOKIE_BUTTON_AVOID, className, side = 'l
|
||||
className={cls}
|
||||
data-ipal="cookie-button"
|
||||
onClick={reopen}
|
||||
ref={buttonRef}
|
||||
style={{ ...position, ...(lift ? { transform: `translateY(-${lift}px)` } : {}) }}
|
||||
type="button"
|
||||
>
|
||||
@@ -79,16 +98,67 @@ export function bottomOverlap(tops: number[], viewportHeight: number): number {
|
||||
return Math.max(0, Math.min(viewportHeight - top, viewportHeight - 96))
|
||||
}
|
||||
|
||||
function useLiftAbove(selector: false | string, active: boolean): number {
|
||||
type Box = { bottom: number; left: number; right: number; top: number }
|
||||
|
||||
/** Space kept between the button and a control it stepped above. */
|
||||
const CLEAR_GAP = 8
|
||||
|
||||
/**
|
||||
* How far the button must move up so that it lies on none of `obstacles` (boxes in viewport pixels; `button` is the
|
||||
* button's box in its resting place). 0 when nothing is under it — and 0 when no free place exists within `max`:
|
||||
* better in its corner than wandering over the page.
|
||||
*/
|
||||
export function stepAbove(button: Box, obstacles: Box[], max: number): number {
|
||||
let lift = 0
|
||||
// each pass clears the lowest control still under the button; a control is passed at most once
|
||||
for (let pass = 0; pass <= obstacles.length; pass++) {
|
||||
const top = button.top - lift
|
||||
const bottom = button.bottom - lift
|
||||
const hit = obstacles.filter((o) => o.right > button.left && o.left < button.right && o.bottom > top && o.top < bottom)
|
||||
if (!hit.length) {return lift}
|
||||
lift = button.bottom - Math.min(...hit.map((o) => o.top)) + CLEAR_GAP
|
||||
if (lift > max) {return 0}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type LiftOptions = {
|
||||
active: boolean
|
||||
avoid: false | string
|
||||
buttonRef: { current: HTMLButtonElement | null }
|
||||
clear: false | string
|
||||
}
|
||||
|
||||
function useLift({ active, avoid, buttonRef, clear }: LiftOptions): number {
|
||||
const [lift, setLift] = useState(0)
|
||||
const applied = useRef(0)
|
||||
const frame = useRef(0)
|
||||
useEffect(() => {
|
||||
if (!selector || !active) {return}
|
||||
if (!active || (!avoid && !clear)) {return}
|
||||
const set = (value: number) => {
|
||||
applied.current = value
|
||||
setLift(value)
|
||||
}
|
||||
const measure = () => {
|
||||
cancelAnimationFrame(frame.current)
|
||||
frame.current = requestAnimationFrame(() => {
|
||||
const tops = [...document.querySelectorAll(selector)].map((el) => el.getBoundingClientRect().top)
|
||||
setLift(bottomOverlap(tops, window.innerHeight))
|
||||
if (avoid) {
|
||||
const tops = [...document.querySelectorAll(avoid)].map((el) => el.getBoundingClientRect().top)
|
||||
return set(bottomOverlap(tops, window.innerHeight))
|
||||
}
|
||||
const button = buttonRef.current
|
||||
const page = document.documentElement
|
||||
// Anywhere else a covered control is one scroll away; at the end of the page it would stay covered.
|
||||
const atEnd = window.innerHeight + window.scrollY >= page.scrollHeight - 2
|
||||
if (!button || !clear || !atEnd || getComputedStyle(button).position !== 'fixed') {return set(0)}
|
||||
const now = button.getBoundingClientRect()
|
||||
// the resting place: the measured box without the step the button has already taken
|
||||
const rest = { bottom: now.bottom + applied.current, left: now.left, right: now.right, top: now.top + applied.current }
|
||||
const obstacles = [...document.querySelectorAll(clear)]
|
||||
.filter((el) => el !== button && !button.contains(el))
|
||||
.map((el) => el.getBoundingClientRect())
|
||||
.filter((box) => box.width > 0 && box.height > 0)
|
||||
set(stepAbove(rest, obstacles, window.innerHeight / 2))
|
||||
})
|
||||
}
|
||||
measure()
|
||||
@@ -102,6 +172,6 @@ function useLiftAbove(selector: false | string, active: boolean): number {
|
||||
window.removeEventListener('resize', measure)
|
||||
observer?.disconnect()
|
||||
}
|
||||
}, [selector, active])
|
||||
return selector ? lift : 0
|
||||
}, [active, avoid, buttonRef, clear])
|
||||
return active && (avoid || clear) ? lift : 0
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ export { ConsentScript } from './ConsentScript.js'
|
||||
export { readEffectiveConsent } from './consentStore.js'
|
||||
export { CookieBanner } from './CookieBanner.js'
|
||||
export type { CookieBannerAction, CookieBannerClassNames, CookieSettingsAction } from './CookieBanner.js'
|
||||
export { bottomOverlap, COOKIE_BUTTON_AVOID, CookieButton } from './CookieButton.js'
|
||||
export { bottomOverlap, COOKIE_BUTTON_AVOID, COOKIE_BUTTON_CLEAR, CookieButton, stepAbove } 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'
|
||||
|
||||
Reference in New Issue
Block a user