Skip to content

Commit

Permalink
chore: update components to use mixinFocusable
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 576982507
  • Loading branch information
asyncLiz authored and copybara-github committed Oct 26, 2023
1 parent f94de5d commit f5daadc
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 69 deletions.
4 changes: 2 additions & 2 deletions chips/internal/chip-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {html, isServer, LitElement} from 'lit';
import {queryAssignedElements} from 'lit/decorators.js';

import {
polyfillARIAMixin,
polyfillElementInternalsAria,
setupHostAria,
} from '../../internal/aria/aria.js';

import {Chip} from './chip.js';
Expand All @@ -19,7 +19,7 @@ import {Chip} from './chip.js';
*/
export class ChipSet extends LitElement {
static {
setupHostAria(ChipSet, {focusable: false});
polyfillARIAMixin(ChipSet);
}

get chips() {
Expand Down
53 changes: 0 additions & 53 deletions internal/aria/aria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,59 +315,6 @@ export function polyfillARIAMixin(ctor: typeof ReactiveElement) {
ctor.createProperty('role', {reflect: true});
}

/**
* Enables a host custom element to be the target for aria roles and attributes.
* Components should set the `elementInternals.role` property.
*
* By default, aria components are tab focusable. Provide a `focusable: false`
* option for components that should not be tab focusable, such as
* `role="listbox"`.
*
* This function will also polyfill aria `ElementInternals` properties for
* Firefox.
*
* @param ctor The `ReactiveElement` constructor to set up.
* @param options Options to configure the element's host aria.
* @deprecated use `mixinFocusable()` and `polyfillARIAMixin()`
* TODO(b/307785469): remove after updating components to use mixinFocusable
*/
export function setupHostAria(
ctor: typeof ReactiveElement,
{focusable}: SetupHostAriaOptions = {},
) {
if (focusable !== false) {
ctor.addInitializer((host) => {
host.addController({
hostConnected() {
if (host.hasAttribute('tabindex')) {
return;
}

host.tabIndex = 0;
},
});
});
}

polyfillARIAMixin(ctor);
}

/**
* Options for setting up a host element as an aria target.
* @deprecated use `mixinFocusable()` and `polyfillARIAMixin()`
* TODO(b/307785469): remove after updating components to use mixinFocusable
*/
export interface SetupHostAriaOptions {
/**
* Whether or not the element can be focused with the tab key. Defaults to
* true.
*
* Set this to false for aria roles that should not be tab focusable, such as
* `role="listbox"`.
*/
focusable?: boolean;
}

/**
* Polyfills an element and its `ElementInternals` to support `ARIAMixin`
* properties on internals. This is needed for Firefox.
Expand Down
4 changes: 2 additions & 2 deletions list/internal/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {html, isServer, LitElement} from 'lit';
import {queryAssignedElements} from 'lit/decorators.js';

import {
polyfillARIAMixin,
polyfillElementInternalsAria,
setupHostAria,
} from '../../internal/aria/aria.js';

import {ListController, NavigableKeys} from './list-controller.js';
Expand All @@ -24,7 +24,7 @@ interface ListItem extends SharedListItem {
// tslint:disable-next-line:enforce-comments-on-exported-symbols
export class List extends LitElement {
static {
setupHostAria(List, {focusable: false});
polyfillARIAMixin(List);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions menu/internal/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {classMap} from 'lit/directives/class-map.js';
import {styleMap} from 'lit/directives/style-map.js';

import {
polyfillARIAMixin,
polyfillElementInternalsAria,
setupHostAria,
} from '../../internal/aria/aria.js';
import {createAnimationSignal, EASING} from '../../internal/motion/animation.js';
import {
Expand Down Expand Up @@ -91,8 +91,7 @@ function getFocusedElement(
*/
export abstract class Menu extends LitElement {
static {
// We want to manage tabindex ourselves.
setupHostAria(Menu, {focusable: false});
polyfillARIAMixin(Menu);
}

@query('.menu') private readonly surfaceEl!: HTMLElement | null;
Expand Down
12 changes: 8 additions & 4 deletions radio/internal/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ import {property} from 'lit/decorators.js';
import {classMap} from 'lit/directives/class-map.js';

import {
polyfillARIAMixin,
polyfillElementInternalsAria,
setupHostAria,
} from '../../internal/aria/aria.js';
import {isActivationClick} from '../../internal/controller/events.js';
import {mixinFocusable} from '../../labs/behaviors/focusable.js';

import {SingleSelectionController} from './single-selection-controller.js';

const CHECKED = Symbol('checked');
let maskId = 0;

// Separate variable needed for closure.
const radioBaseClass = mixinFocusable(LitElement);

/**
* A radio component.
*
Expand All @@ -30,9 +34,9 @@ let maskId = 0;
* @fires change Dispatched when the value changes from user interaction.
* --bubbles --composed
*/
export class Radio extends LitElement {
export class Radio extends radioBaseClass {
static {
setupHostAria(Radio);
polyfillARIAMixin(Radio);
}

/** @nocollapse */
Expand Down Expand Up @@ -116,7 +120,7 @@ export class Radio extends LitElement {
}

protected override render() {
const classes = {checked: this.checked};
const classes = {'checked': this.checked};
return html`
<div class="container ${classMap(classes)}" aria-hidden="true">
<md-ripple
Expand Down
10 changes: 7 additions & 3 deletions tabs/internal/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import {
import {classMap} from 'lit/directives/class-map.js';

import {
polyfillARIAMixin,
polyfillElementInternalsAria,
setupHostAria,
} from '../../internal/aria/aria.js';
import {EASING} from '../../internal/motion/animation.js';
import {mixinFocusable} from '../../labs/behaviors/focusable.js';

/**
* Symbol for tabs to use to animate their indicators based off another tab's
Expand All @@ -36,12 +37,15 @@ const INDICATOR = Symbol('indicator');
*/
export const ANIMATE_INDICATOR = Symbol('animateIndicator');

// Separate variable needed for closure.
const tabBaseClass = mixinFocusable(LitElement);

/**
* Tab component.
*/
export class Tab extends LitElement {
export class Tab extends tabBaseClass {
static {
setupHostAria(Tab);
polyfillARIAMixin(Tab);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tabs/internal/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {html, isServer, LitElement} from 'lit';
import {property, query, queryAssignedElements} from 'lit/decorators.js';

import {
polyfillARIAMixin,
polyfillElementInternalsAria,
setupHostAria,
} from '../../internal/aria/aria.js';

import {ANIMATE_INDICATOR, Tab} from './tab.js';
Expand Down Expand Up @@ -42,7 +42,7 @@ import {ANIMATE_INDICATOR, Tab} from './tab.js';
*/
export class Tabs extends LitElement {
static {
setupHostAria(Tabs, {focusable: false});
polyfillARIAMixin(Tabs);
}

/**
Expand Down

0 comments on commit f5daadc

Please sign in to comment.