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:
Radosław Smoliński
2026-12-01 11:17:59 +01:00
co-authored by Claude Opus 5.5
15 changed files with 424 additions and 28 deletions
+189
View File
@@ -0,0 +1,189 @@
// @vitest-environment happy-dom
/**
* Where <CookieButton> sits (1.17.2): the bottom corner of the window, always — it does not climb above the footer.
* Only at the very end of the page it steps above a control it would keep covered. The placement before 1.17.2
* (above the footer) and "never move" stay available.
*/
import { act, createElement, type ReactElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { ConsentProvider } from '../src/modules/consent/ConsentContext.js'
import { resetConsentStore } from '../src/modules/consent/consentStore.js'
import { COOKIE_BUTTON_AVOID, COOKIE_BUTTON_CLEAR, CookieButton, stepAbove } from '../src/modules/consent/CookieButton.js'
import { CONSENT_FALLBACK } from '../src/modules/consent/getConsentTexts.js'
import { CONSENT_COOKIE, serializeConsent } from '../src/modules/consent/storage.js'
const h = createElement as unknown as (type: unknown, props?: null | object, ...children: unknown[]) => ReactElement
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
const box = (top: number, bottom: number, left = 16, right = 60) => ({ bottom, left, right, top })
/** The button's resting place in an 800 px high window: 44 px, 16 px from the bottom and the left. */
const REST = box(740, 784)
describe('stepAbove', () => {
test('nothing under the button: stays', () => {
expect(stepAbove(REST, [], 400)).toBe(0)
expect(stepAbove(REST, [box(700, 730), box(750, 780, 200, 300), box(790, 800)], 400)).toBe(0)
})
test('a link under it: just above the link, with a gap', () => {
expect(stepAbove(REST, [box(760, 780, 30, 120)], 400)).toBe(784 - 760 + 8)
})
test('two rows of links: above both, onto text', () => {
// social links at 760–780, legal links at 724–744: clearing the first lands on the second
expect(stepAbove(REST, [box(760, 780, 30, 120), box(724, 744, 30, 200)], 400)).toBe(784 - 724 + 8)
})
test('no free place within the limit: back to the corner', () => {
const wall = Array.from({ length: 20 }, (_, i) => box(780 - (i + 1) * 30, 780 - i * 30))
expect(stepAbove(REST, wall, 400)).toBe(0)
expect(stepAbove(REST, [box(300, 790)], 400)).toBe(0)
})
test('the default list of controls', () => {
for (const selector of ['a[href]', 'button', 'input', 'select', 'textarea', 'summary']) {expect(COOKIE_BUTTON_CLEAR).toContain(selector)}
})
})
describe('placement', () => {
let container: HTMLDivElement
let root: Root
let rects: Map<Element, ReturnType<typeof box>>
let page: { height: number; scrollY: number }
const setRect = (el: Element, r: ReturnType<typeof box>) => rects.set(el, r)
const button = () => document.querySelector<HTMLButtonElement>('[data-ipal="cookie-button"]')!
const lift = () => /translateY\(-(?<px>[\d.]+)px\)/.exec(button().style.transform)?.groups?.px ?? '0'
const frame = () => act(() => new Promise((resolve) => setTimeout(resolve, 40)))
const scrollTo = async (y: number) => {
page.scrollY = y
window.dispatchEvent(new Event('scroll'))
await frame()
}
const add = (html: string) => {
const el = document.createElement('div')
el.innerHTML = html
document.body.appendChild(el)
return el.firstElementChild!
}
const mount = async (props: object = {}) => {
act(() => root.render(h(ConsentProvider, { texts: CONSENT_FALLBACK }, h(CookieButton, props))))
setRect(button(), REST)
window.dispatchEvent(new Event('resize'))
await frame()
}
beforeEach(() => {
resetConsentStore()
document.cookie = `${CONSENT_COOKIE}=${serializeConsent({ analytics: false, functional: false, marketing: false, necessary: true })};path=/`
rects = new Map()
page = { height: 3000, scrollY: 0 }
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) {
const r = rects.get(this) ?? { bottom: 0, left: 0, right: 0, top: 0 }
// like the browser: the measured box includes the transform the button carries
const moved = this === document.querySelector('[data-ipal="cookie-button"]') ? Number(/translateY\(-([\d.]+)px\)/.exec((this as HTMLElement).style.transform)?.[1] ?? 0) : 0
return { ...r, bottom: r.bottom - moved, height: r.bottom - r.top, top: r.top - moved, width: r.right - r.left, x: r.left, y: r.top - moved } as DOMRect
})
vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(800)
vi.spyOn(window, 'scrollY', 'get').mockImplementation(() => page.scrollY)
vi.spyOn(document.documentElement, 'scrollHeight', 'get').mockImplementation(() => page.height)
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
})
afterEach(() => {
act(() => root.unmount())
document.body.innerHTML = ''
vi.restoreAllMocks()
})
test('default: fixed in the bottom corner with the safe-area insets, below dialogs', async () => {
await mount()
const style = button().style
expect(style.position).toBe('fixed')
expect(style.bottom).toBe('calc(var(--ipal-cookie-button-offset, 1rem) + env(safe-area-inset-bottom, 0px))')
expect(style.left).toBe('calc(var(--ipal-cookie-button-inset, 1rem) + env(safe-area-inset-left, 0px))')
expect(style.zIndex).toBe('var(--ipal-z-header, 40)')
expect(button().getAttribute('aria-label')).toBe(CONSENT_FALLBACK.settingsTitle)
expect(button().type).toBe('button')
})
test('side="right": the right corner and its safe-area inset', async () => {
await mount({ side: 'right' })
expect(button().style.right).toBe('calc(var(--ipal-cookie-button-inset, 1rem) + env(safe-area-inset-right, 0px))')
expect(button().style.left).toBe('')
})
test('default: the footer on screen does not move it (before 1.17.2 it rode up by 300 px here)', async () => {
setRect(add('<footer>company</footer>'), box(500, 1100, 0, 1280))
await mount()
await scrollTo(1500)
expect(lift()).toBe('0')
})
test('avoid={COOKIE_BUTTON_AVOID}: the placement before 1.17.2, above the footer', async () => {
setRect(add('<footer>company</footer>'), box(500, 1100, 0, 1280))
await mount({ avoid: COOKIE_BUTTON_AVOID })
expect(lift()).toBe('300')
})
test('end of the page, a footer link in the corner: steps just above it; scrolling back up: returns to the corner', async () => {
const link = add('<a href="/x">Instagram</a>')
setRect(link, box(760, 780, 30, 120))
await mount()
await scrollTo(1000) // mid-page: a covered link is one scroll away, the button stays
expect(lift()).toBe('0')
await scrollTo(2200) // 2200 + 800 = the end
expect(lift()).toBe('32')
await scrollTo(2200) // measured again while lifted: the same step, no creeping
expect(lift()).toBe('32')
await scrollTo(1000)
expect(lift()).toBe('0')
})
test('a page shorter than the window is always at its end', async () => {
page.height = 800
setRect(add('<button>Send</button>'), box(750, 790, 20, 140))
await mount()
expect(lift()).toBe('42')
})
test('end of the page, nothing under it (text only): stays in the corner', async () => {
setRect(add('<p>© Example</p>'), box(760, 780, 30, 400))
setRect(add('<a href="/y">Terms</a>'), box(760, 780, 900, 960))
await mount()
await scrollTo(2200)
expect(lift()).toBe('0')
})
test('clear={false} and avoid={false}: never moves', async () => {
setRect(add('<a href="/x">Instagram</a>'), box(760, 780, 30, 120))
setRect(add('<footer>company</footer>'), box(500, 1100, 0, 1280))
await mount({ clear: false })
await scrollTo(2200)
expect(lift()).toBe('0')
act(() => root.render(h(ConsentProvider, { texts: CONSENT_FALLBACK }, h(CookieButton, { avoid: false }))))
await scrollTo(2200)
expect(lift()).toBe('0')
})
test('a selector of your own in clear', async () => {
setRect(add('<a href="/x">Instagram</a>'), box(760, 780, 30, 120))
setRect(add('<div class="chat">chat</div>'), box(700, 790, 10, 70))
await mount({ clear: '.chat' })
await scrollTo(2200)
expect(lift()).toBe(String(784 - 700 + 8))
})
test('className (the project positions it, here not fixed): no step', async () => {
setRect(add('<a href="/x">Instagram</a>'), box(760, 780, 30, 120))
await mount({ className: 'in-the-footer' })
expect(button().style.position).toBe('')
await scrollTo(2200)
expect(lift()).toBe('0')
})
})