import { expect, fixture } from '@open-wc/testing'
import { sendMouse } from '@web/test-runner-commands'
import { css, html, LitElement, type TemplateResult } from 'lit'
import { ButtonStyles, ButtonContainerStyles, CommonHeaderStyles } from '@operato/styles'
import '@material/web/icon/icon.js'
import '@material/web/button/filled-button.js'
import '@material/web/button/filled-tonal-button.js'
import '@material/web/button/elevated-button.js'
import '@material/web/button/outlined-button.js'
import '@material/web/button/text-button.js'
import '@material/web/chips/assist-chip.js'
import '@operato/popup/ox-popup-actions.js'
import '../src/data-grid/data-grid-field.js'
import '../src/data-card/data-card-gutter.js'
import '../src/data-list/data-list-gutter.js'
class BodyProbe extends LitElement {
static properties = { content: { attribute: false } }
content?: TemplateResult
static styles = [ButtonStyles, css`:host { display: block }`]
render() { return this.content ?? html`` }
}
class BarProbe extends BodyProbe { static styles = [ButtonContainerStyles] }
class HeaderProbe extends BodyProbe {
static styles = [CommonHeaderStyles]
render() { return html`
` }
}
customElements.define('danger-body-probe', BodyProbe)
customElements.define('danger-bar-probe', BarProbe)
customElements.define('danger-header-probe', HeaderProbe)
const column = { type: 'gutter', name: 'delete', danger: true, record: { renderer: () => html`` } }
const cases = [
['form', html``],
['quiet form', html`deleteDelete`}>`],
['raised form', html`deleteDelete`}>`],
['toolbar', html``],
['header', html``],
['grid', html``],
['card', html``],
['mobile list', html``],
['filled', html`deleteDelete`}>`],
['tonal', html`deleteDelete`}>`],
['elevated', html`deleteDelete`}>`],
['outlined', html`deleteDelete`}>`],
['text', html`deleteDelete`}>`],
['assist chip', html`delete`}>`],
['popup native', html``],
['popup filled', html`deleteDelete`],
['popup text', html`deleteDelete`],
['popup outlined', html`deleteDelete`],
] as const
const themes = {
light: { error: '#ba1a1a', onError: '#ffffff', container: '#ffdad6', onContainer: '#410002', surface: '#ffffff' },
dark: { error: '#ffb4ab', onError: '#690005', container: '#93000a', onContainer: '#ffdad6', surface: '#121212' },
boardDark: { error: '#fbafa9', onError: '#a3000c', container: '#a3000c', onContainer: '#fbafa9', surface: '#121318' },
custom: { error: '#a00000', onError: '#ffffff', container: '#ffdad6', onContainer: '#410002', surface: '#ffffff' }
}
const settle = () => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))
function rgb(value: string) { const channels = value.match(/[\d.]+/g)!.slice(0, 3).map(Number); return value.startsWith('color(srgb') ? channels.map(n => n * 255) : channels }
function contrast(a: string, b: string) {
const light = (color: string) => rgb(color).map(n => n / 255).map(n => n <= .04045 ? n / 12.92 : ((n + .055) / 1.055) ** 2.4).reduce((sum, n, i) => sum + n * [.2126, .7152, .0722][i], 0)
const x = light(a), y = light(b)
return (Math.max(x, y) + .05) / (Math.min(x, y) + .05)
}
function background(node: Element): string {
for (let current: Element | null = node; current; current = current.parentElement ?? (current.getRootNode() as ShadowRoot).host ?? null) {
const color = getComputedStyle(current).backgroundColor
if (color !== 'rgba(0, 0, 0, 0)' && color !== 'transparent') return color
}
return 'rgb(255, 255, 255)'
}
for (const [themeName, theme] of Object.entries(themes)) {
describe(`destructive action contrast: ${themeName}`, () => {
for (const [kind, template] of cases) {
it(`${kind}: icon follows the label before hover, on hover, focus, press and disabled`, async () => {
await sendMouse({ type: 'move', position: [0, 0] })
const root = await fixture(html`${template}
`)
for (const [name, value] of Object.entries({
'--md-sys-color-error': theme.error, '--md-sys-color-on-error': theme.onError,
'--md-sys-color-error-container': theme.container, '--md-sys-color-on-error-container': theme.onContainer,
'--md-sys-color-surface-container-lowest': theme.surface, '--md-sys-color-on-surface': themeName === 'dark' || themeName === 'boardDark' ? '#ffffff' : '#111111',
'--md-sys-color-primary': '#005566', '--md-sys-color-secondary': '#006677', '--md-sys-color-secondary-container': '#aaffff', '--md-sys-color-on-secondary-container': '#001122'
})) root.style.setProperty(name, value)
if (themeName === 'custom') {
root.style.setProperty('--danger-button-background', '#520000')
root.style.setProperty('--danger-button-color', '#fff0f0')
root.style.setProperty('--danger-button-hover-background', '#fff0f0')
root.style.setProperty('--danger-button-hover-color', '#520000')
}
if (themeName === 'boardDark') {
root.style.setProperty('--danger-button-background', 'var(--md-sys-color-error-container)')
root.style.setProperty('--danger-button-color', 'var(--md-sys-color-on-error-container)')
root.style.setProperty('--danger-button-hover-background', 'color-mix(in srgb, var(--md-sys-color-error-container) 88%, black)')
root.style.setProperty('--danger-button-hover-color', 'var(--md-sys-color-on-error-container)')
}
root.style.backgroundColor = theme.surface
await settle()
const surface = root.firstElementChild as HTMLElement & { updateComplete?: Promise }
await surface.updateComplete
const slotted = surface.tagName.startsWith('OX-POPUP')
const control = (slotted ? surface.firstElementChild : surface.shadowRoot!.querySelector('button, md-filled-button, md-filled-tonal-button, md-elevated-button, md-outlined-button, md-text-button, md-assist-chip')) as HTMLElement & { disabled: boolean }
const icon = control.querySelector('md-icon')!
const label = (control.shadowRoot?.querySelector('.label') ?? control.shadowRoot?.querySelector('button') ?? control) as HTMLElement
const focusTarget = (control.shadowRoot?.querySelector('button') ?? control) as HTMLElement
label.style.transition = 'none'
control.style.transition = 'none'
const check = (disabled = false) => {
const ink = getComputedStyle(label).color
expect(getComputedStyle(icon).color, `${kind} icon/label`).to.equal(ink)
expect(icon.getBoundingClientRect().width).to.be.greaterThan(10)
if (!disabled) expect(contrast(ink, background(control)), `${kind} contrast`).to.be.at.least(4.5)
}
check()
const box = control.getBoundingClientRect()
const position: [number, number] = [Math.round(box.x + box.width / 2), Math.round(box.y + box.height / 2)]
await sendMouse({ type: 'move', position })
await settle()
expect(control.matches(':hover')).to.equal(true)
check()
await sendMouse({ type: 'move', position: [0, 0] })
control.focus()
await settle()
check()
await sendMouse({ type: 'move', position })
await sendMouse({ type: 'down' })
try { await settle(); check() } finally { await sendMouse({ type: 'up' }) }
focusTarget.blur(); control.blur()
control.disabled = true
await settle()
check(true)
})
}
})
}