Elizabeth Mitchell c390291687 chore: format files with prettier
PiperOrigin-RevId: 576601342
2023-10-25 11:59:00 -07:00

59 lines
1.5 KiB
TypeScript

/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {ReactiveElement} from 'lit';
import {ARIA_PROPERTIES, ariaPropertyToAttribute} from './aria.js';
/**
* Sets up a `ReactiveElement` constructor to enable updates when delegating
* aria attributes. Elements may bind `this.aria*` properties to `aria-*`
* attributes in their render functions.
*
* This function will:
* - Call `requestUpdate()` when an aria attribute changes.
* - Add `role="presentation"` to the host.
*
* NOTE: The following features are not currently supported:
* - Delegating IDREF attributes (ex: `aria-labelledby`, `aria-controls`)
* - Delegating the `role` attribute
*
* @example
* class XButton extends LitElement {
* static {
* requestUpdateOnAriaChange(XButton);
* }
*
* protected override render() {
* return html`
* <button aria-label=${this.ariaLabel || nothing}>
* <slot></slot>
* </button>
* `;
* }
* }
*
* @param ctor The `ReactiveElement` constructor to patch.
*/
export function requestUpdateOnAriaChange(ctor: typeof ReactiveElement) {
for (const ariaProperty of ARIA_PROPERTIES) {
ctor.createProperty(ariaProperty, {
attribute: ariaPropertyToAttribute(ariaProperty),
reflect: true,
});
}
ctor.addInitializer((element) => {
const controller = {
hostConnected() {
element.setAttribute('role', 'presentation');
},
};
element.addController(controller);
});
}