UNPKG

easy-aria

Version:

The easiest (and safest) way to manipulate ARIA attributes in HTML.

701 lines (685 loc) 28.1 kB
/** * Creates a new `EasyAria` object which wraps the given Element. * * @param selector A CSS selector or HTML Element. * @throws {DOMException} If the syntax of a string selector is invalid. * @returns The wrapped Element in an `EasyAria` object, or `null` if the * element does not exist. */ declare function aria(selector: Element): EasyAria; /** * Creates a new `EasyAria` object which wraps the given Element. * * @param selector A CSS selector or HTML Element. * @throws {DOMException} If the syntax of a string selector is invalid. * @returns The wrapped Element in an `EasyAria` object, or `null` if the * element does not exist. */ declare function aria(selector: string | Element | null): EasyAria | null; export default aria; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#state_prop_def} * * All WAI-ARIA 1.2 attributes, without their `aria-` prefix. */ declare interface AriaAttributes { /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-activedescendant} * * Identifies the currently active element when DOM focus is on a `composite` widget, `combobox`, `textbox`, `group`, or `application`. */ activedescendant: idref; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-atomic} * * Indicates whether assistive technologies will present all, or only parts of, the changed region based on the change notifications defined by the `aria-relevant` attribute. */ atomic: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-autocomplete} * * Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for a `combobox`, `searchbox`, or `textbox` and specifies how predictions would be presented if they were made. */ autocomplete: 'inline' | 'list' | 'both' | 'none'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-busy} * * Indicates an element is being modified and that assistive technologies MAY want to wait until the modifications are complete before exposing them to the user. */ busy: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-checked} * * Indicates the current "checked" state of checkboxes, radio buttons, and other widgets. * @see pressed @see selected. */ checked: boolean | 'mixed' | 'undefined'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-colcount} * * Defines the total number of columns in a `table`, `grid`, or `treegrid`. * @see colindex. */ colcount: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-colindex} * * Defines an element's column index or position with respect to the total number of columns within a `table`, `grid`, or `treegrid`. * @see colcount @see colspan. */ colindex: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-colspan} * * Defines the number of columns spanned by a cell or gridcell within a `table`, `grid`, or `treegrid`. * @see colindex @see rowspan. */ colspan: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-controls} * * Identifies the element (or elements) whose contents or presence are controlled by the current element. * @see owns. */ controls: idref_list; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-current} * * Indicates the element that represents the current item within a container or set of related elements. */ current: boolean | 'page' | 'step' | 'location' | 'date' | 'time'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-describedby} * * Identifies the element (or elements) that describes the object. * @see labelledby. */ describedby: idref_list; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-details} * * Identifies the element that provides a detailed, extended description for the object. * @see describedby. */ details: idref; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-disabled} * * Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable. * @see hidden @see readonly. */ disabled: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-dropeffect} * * Indicates what functions can be performed when a dragged object is released on the drop target. * @deprecated in ARIA 1.1 */ dropeffect: 'copy' | 'execute' | 'link' | 'move' | 'none' | 'popup'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-errormessage} * * Identifies the element that provides an error message for an object. * @see invalid @see describedby. */ errormessage: idref; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-expanded} * * Indicates whether a grouping element owned or controlled by this element is expanded or collapsed. */ expanded: boolean | 'undefined'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-flowto} * * Identifies the next element (or elements) in an alternate reading order of content which, at the user's discretion, allows assistive technology to override the general default of reading in document source order. */ flowto: idref_list; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-grabbed} * * Indicates an element's "grabbed" state in a drag-and-drop operation. * @deprecated in ARIA 1.1 */ grabbed: boolean | 'undefined'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-haspopup} * * Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. */ haspopup: boolean | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-hidden} * * Indicates whether the element is exposed to an accessibility API. * @see disabled. */ hidden: boolean | 'undefined'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-invalid} * * Indicates the entered value does not conform to the format expected by the application. * @see errormessage. */ invalid: boolean | 'grammar' | 'spelling'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-keyshortcuts} * * Indicates keyboard shortcuts that an author has implemented to activate or give focus to an element. */ keyshortcuts: string; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-label} * * Defines a string value that labels the current element. * @see labelledby. */ label: string; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-labelledby} * * Identifies the element (or elements) that labels the current element. * @see describedby. */ labelledby: idref_list; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-level} * * Defines the hierarchical level of an element within a structure. */ level: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-live} * * Indicates that an element will be updated, and describes the types of updates the user agents, assistive technologies, and user can expect from the live region. */ live: 'assertive' | 'off' | 'polite'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-modal} * * Indicates whether an element is modal when displayed. */ modal: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-multiline} * * Indicates whether a text box accepts multiple lines of input or only a single line. */ multiline: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-multiselectable} * * Indicates that the user may select more than one item from the current selectable descendants. */ multiselectable: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-orientation} * * Indicates whether the element's orientation is horizontal, vertical, or unknown/ambiguous. */ orientation: 'horizontal' | 'undefined' | 'vertical'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-owns} * * Identifies an element (or elements) in order to define a visual, functional, or contextual parent/child relationship between DOM elements where the DOM hierarchy cannot be used to represent the relationship. * @see controls. */ owns: idref_list; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-placeholder} * * Defines a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value. A hint could be a sample value or a brief description of the expected format. */ placeholder: string; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-posinset} * * Defines an element's number or position in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM. * @see setsize. */ posinset: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-pressed} * * Indicates the current "pressed" state of toggle buttons. * @see checked @see selected. */ pressed: boolean | 'mixed' | 'undefined'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-readonly} * * Indicates that the element is not editable, but is otherwise operable. * @see disabled. */ readonly: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-relevant} * * Indicates what notifications the user agent will trigger when the accessibility tree within a live region is modified. * @see atomic. */ relevant: 'additions' | 'additions text' | 'all' | 'removals' | 'text'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-required} * * Indicates that user input is required on the element before a form may be submitted. */ required: boolean; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-roledescription} * * Defines a human-readable, author-localized description for the role of an element. */ roledescription: string; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-rowcount} * * Defines the total number of rows in a `table`, `grid`, or `treegrid`. * @see rowindex. */ rowcount: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-rowindex} * * Defines an element's row index or position with respect to the total number of rows within a table, grid, or treegrid. * @see rowcount @see rowspan. */ rowindex: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-rowspan} * * Defines the number of rows spanned by a cell or gridcell within a `table`, `grid`, or `treegrid`. * @see rowindex @see colspan. */ rowspan: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-selected} * * Indicates the current "selected" state of various widgets. * @see checked @see pressed. */ selected: boolean | 'undefined'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-setsize} * * Defines the number of items in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM. * @see posinset. */ setsize: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-sort} * * Indicates if items in a table or grid are sorted in ascending or descending order. */ sort: 'ascending' | 'descending' | 'none' | 'other'; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-valuemax} * * Defines the maximum allowed value for a range widget. */ valuemax: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-valuemin} * * Defines the minimum allowed value for a range widget. */ valuemin: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-valuenow} * * Defines the current value for a range widget. * @see valuetext. */ valuenow: number; /** * {@link https://www.w3.org/TR/wai-aria-1.2/#aria-valuetext} * * Defines the human readable text alternative of `aria-valuenow` for a range widget. */ valuetext: string; } /** * {@link https://www.w3.org/TR/wai-aria-1.2/#role_definitions} * * All WAI-ARIA 1.2 roles. */ declare type AriaRoles = 'alert' | 'alertdialog' | 'application' | 'article' | 'banner' | 'blockquote' | 'button' | 'caption' | 'cell' | 'checkbox' | 'code' | 'columnheader' | 'combobox' | 'complementary' | 'contentinfo' | 'definition' | 'deletion' | 'dialog' | 'directory' | 'document' | 'emphasis' | 'feed' | 'figure' | 'form' | 'generic' | 'grid' | 'gridcell' | 'group' | 'heading' | 'img' | 'insertion' | 'link' | 'list' | 'listbox' | 'listitem' | 'log' | 'main' | 'marquee' | 'math' | 'meter' | 'menu' | 'menubar' | 'menuitem' | 'menuitemcheckbox' | 'menuitemradio' | 'navigation' | 'none' | 'note' | 'option' | 'paragraph' | 'presentation' | 'progressbar' | 'radio' | 'radiogroup' | 'region' | 'row' | 'rowgroup' | 'rowheader' | 'scrollbar' | 'search' | 'searchbox' | 'separator' | 'slider' | 'spinbutton' | 'status' | 'strong' | 'subscript' | 'superscript' | 'switch' | 'tab' | 'table' | 'tablist' | 'tabpanel' | 'term' | 'textbox' | 'time' | 'timer' | 'toolbar' | 'tooltip' | 'tree' | 'treegrid' | 'treeitem' | (string & {}); /** Attributes which support only boolean values. */ declare type BooleanOnlyAttributes = FilterKeysWithValueStrict<AriaAttributes, boolean>; /** Attributes which accept booleans. */ declare type BooleanSupportedAttributes = FilterKeysWithValue<AriaAttributes, boolean>; /** * A utility class which wraps an HTML Element and exposes methods * for manipulating ARIA attributes in a type-safe way. */ declare class EasyAria { #private; /** HTML Element wrapped by `EasyAria` object. */ readonly el: Element; constructor(el: Element); /** * @returns The *explicit* role defined on the wrapped Element. */ getRole(): AriaRoles | null; /** * Sets the `role` attribute on the wrapped Element to the given value. * * @param role The value to be assigned to the `role` attribute. */ setRole(role: AriaRoles): EasyAria; /** * Takes a callback function which runs immediately, and is passed the * wrapped Element. The callback's return value is discarded, and the `EasyAria` * instance is returned. * * @param callback The callback function which is passed the wrapped Element. */ call(callback: (this: EasyAria, el: Element) => void): EasyAria; /** * Sets the given aria attribute with the given value on * the wrapped Element. * * @param attribute The aria attribute to be applied, without the `aria-` prefix. * @param value The value to be assigned to the given attribute. * If a string, assigns it as the ID reference. * If an Element, retrieves its ID first and assigns it. */ set<K extends keyof IDREFAttributes>(attribute: K, value: IDREFAttributeValue): EasyAria; /** * Sets the given aria attribute with the given value on * the wrapped Element. * * @param attribute The aria attribute to be applied, without the `aria-` prefix. * @param value The value to be assigned to the given attribute. * If a string, assigns it as the ID reference list. * If an Element, retrieves its ID first and assigns it. * If a an array or NodeList of Elements, retrieves their IDs and assigns them * as a space-separated string of IDs. */ set<K extends keyof IDREFListAttributes>(attribute: K, value: IDREFListAttributeValue): EasyAria; /** * Sets the given aria attribute with the given value on * the wrapped Element. * * @param attribute The aria attribute to be applied, without the `aria-` prefix. * @param value The value to be assigned to the given attribute. * The value can be omitted if the given attribute accepts a boolean. * An empty value defaults to `true`. */ set<K extends keyof BooleanOnlyAttributes>(attribute: K, value?: BooleanOnlyAttributes[K]): EasyAria; /** * Sets the given aria attribute with the given value on * the wrapped Element. * * @param attribute The aria attribute to be applied, without the `aria-` prefix. * @param value The value to be assigned to the given attribute. * The value can be omitted if the given attribute accepts a boolean. * An empty value defaults to `true`. */ set<K extends keyof BooleanSupportedAttributes>(attribute: K, value?: BooleanSupportedAttributes[K]): EasyAria; /** * Sets the given aria attribute with the given value on * the wrapped Element. * * @param attribute The aria attribute to be applied, without the `aria-` prefix. * @param value The value to be assigned to the given attribute. */ set<K extends keyof AriaAttributes>(attribute: K, value: AriaAttributes[K]): EasyAria; /** * Removes the given aria attribute from the wrapped Element. * * @param attribute The aria attribute to remove, without the `aria-` prefix. */ unset<K extends keyof AriaAttributes>(attribute: K): EasyAria; /** * Retrieves the value of the given aria attribute on the wrapped Element. * * @param attribute The aria attribute (without the `aria-` prefix) whose value to return. */ get<K extends keyof (IDREFAttributes & IDREFListAttributes)>(attribute: K): string | null; /** * Retrieves the value of the given aria attribute on the wrapped Element. * * @param attribute The aria attribute (without the `aria-` prefix) whose value to return. */ get<K extends keyof AriaAttributes>(attribute: K): AriaAttributes[K] | null; /** Sets `aria-checked` to `true`. */ check(): EasyAria; /** Sets `aria-checked` to `false`. */ uncheck(): EasyAria; /** * Toggles the state of `aria-checked` between `true` and `false`. * Sets it to `true` if its current value is neither. */ toggleChecked(): EasyAria; /** * Sets `aria-controls` to the given value. * * @param value A space-separated string of ID references, an Element, * or array/NodeList of Elements whose ID(s) to use for the attribute. */ control(value: IDREFListAttributeValue): EasyAria; /** * Sets `aria-describedby` to the given value. * * @param value A space-separated string of ID references, an Element, * or array/NodeList of Elements whose ID(s) to use for the attribute. */ describeWith(value: IDREFListAttributeValue): EasyAria; /** Sets `aria-disabled` to `true`. */ disable(): EasyAria; /** Sets `aria-disabled` to `false`. */ enable(): EasyAria; /** * Toggles the state of `aria-disabled` between `true` and `false`. * Sets it to `true` if its current value is neither. */ toggleDisabled(): EasyAria; /** Sets `aria-expanded` to `true`. */ expand(): EasyAria; /** Sets `aria-expanded` to `false`. */ collapse(): EasyAria; /** * Toggles the state of `aria-expanded` between `true` and `false`. * Sets it to `true` if its current value is neither. */ toggleExpanded(): EasyAria; /** * Sets `aria-flowto` to the given value. * * @param value A space-separated string of ID references, an Element, * or array/NodeList of Elements whose ID(s) to use for the attribute. */ flowTo(value: IDREFListAttributeValue): EasyAria; /** * Sets `aria-grabbed` to `true`. * * @deprecated in ARIA 1.1. * The `aria-grabbed` state is expected to be replaced by a new feature * in a future version of WAI-ARIA. Authors are therefore advised to treat * `aria-grabbed` as deprecated. */ grab(): EasyAria; /** * Sets `aria-grabbed` to `false`. * * @deprecated in ARIA 1.1. * The `aria-grabbed` state is expected to be replaced by a new feature * in a future version of WAI-ARIA. Authors are therefore advised to treat * `aria-grabbed` as deprecated. */ ungrab(): EasyAria; /** * Toggles the state of `aria-grabbed` between `true` and `false`. * Sets it to `true` if its current value is neither. * * @deprecated in ARIA 1.1. * The `aria-grabbed` state is expected to be replaced by a new feature * in a future version of WAI-ARIA. Authors are therefore advised to treat * `aria-grabbed` as deprecated. */ toggleGrabbed(): EasyAria; /** Sets `aria-hidden` to `true`. */ hide(): EasyAria; /** Sets `aria-hidden` to `false`. */ unhide(): EasyAria; /** * Toggles the state of `aria-expanded` between `true` and `false`. * Sets it to `true` if its current value is neither. */ toggleHidden(): EasyAria; /** Sets `aria-label` to the given string. */ label(value: string): EasyAria; /** * Sets `aria-labelledby` to the given value. * * @param value A space-separated string of ID references, an Element, * or array/NodeList of Elements whose ID(s) to use for the attribute. */ labelWith(value: IDREFListAttributeValue): EasyAria; /** * Sets `aria-owns` to the given value. * * @param value A space-separated string of ID references, an Element, * or array/NodeList of Elements whose ID(s) to use for the attribute. */ own(value: IDREFListAttributeValue): EasyAria; /** Sets `aria-pressed` to `true`. */ press(): EasyAria; /** Sets `aria-pressed` to `false`. */ unpress(): EasyAria; /** * Toggles the state of `aria-pressed` between `true` and `false`. * Sets it to `true` if its current value is neither. */ togglePressed(): EasyAria; /** Sets `aria-required` to `true`. */ require(): EasyAria; /** Sets `aria-required` to `false`. */ unrequire(): EasyAria; /** * Toggles the state of `aria-required` between `true` and `false`. */ toggleRequired(): EasyAria; /** Sets `aria-roledescription` to the given string. */ describeRole(value: string): EasyAria; /** Sets `aria-selected` to `true`. */ select(): EasyAria; /** Sets `aria-selected` to `false`. */ unselect(): EasyAria; /** * Toggles the state of `aria-selected` between `true` and `false`. * Sets it to `true` if its current value is neither. */ toggleSelected(): EasyAria; /** * @returns `true` if `aria-atomic` is set to `true`, otherwise returns `false`. */ isAtomic(): boolean; /** * @returns `true` if `aria-busy` is set to `true`, otherwise returns `false`. */ isBusy(): boolean; /** * @returns `true` if `aria-checked` is set to `true`, otherwise returns `false`. */ isChecked(): boolean; /** * @param value Any of the tokens supported by `aria-current`, except `true` or `false`. * @returns `true` if `aria-current` is set to the given value, * or any of its supported values if none is provided, except `false`, * in which case it returns `false`. */ isCurrent(value?: 'page' | 'step' | 'location' | 'date' | 'time'): boolean; /** * @returns `true` if `aria-disabled` is set to `true`, otherwise returns `false`. */ isDisabled(): boolean; /** * @returns `true` if `aria-expanded` is set to `true`, otherwise returns `false`. */ isExpanded(): boolean; /** * @returns `true` if `aria-grabbed` is set to `true`, otherwise returns `false`. * @deprecated in ARIA 1.1. * The `aria-grabbed` state is expected to be replaced by a new feature * in a future version of WAI-ARIA. Authors are therefore advised to treat * `aria-grabbed` as deprecated. */ isGrabbed(): boolean; /** * @param value Any of the tokens supported by `aria-haspopup`, except `true` or `false`. * @returns `true` if `aria-haspopup` is set to the given value, * or any of its supported values if none is provided, except `false`, * in which case it returns `false`. */ hasPopup(value?: 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog'): boolean; /** * @returns `true` if `aria-hidden` is set to `true` only on the wrapped Element. * This method does not consider whether an ancestor has `aria-hidden` set to `true`. */ isHidden(): boolean; /** * @param value Any of the tokens supported by `aria-invalid`, except `true` or `false`. * @returns `true` if `aria-invalid` is set to the given value, * or any of its supported values if none is provided, except `false`, * in which case it returns `false`. */ isInvalid(value?: 'grammar' | 'spelling'): boolean; /** * @returns `true` if `aria-modal` is set to `true`, otherwise returns `false`. */ isModal(): boolean; /** * @returns `true` if `aria-multiline` is set to `true`, otherwise returns `false`. */ isMultiline(): boolean; /** * @returns `true` if `aria-multiselectable` is set to `true`, otherwise returns `false`. */ isMultiselectable(): boolean; /** * @returns `true` if `aria-pressed` is set to `true`, otherwise returns `false`. */ isPressed(): boolean; /** * @returns `true` if `aria-readonly` is set to `true`, otherwise returns `false`. */ isReadonly(): boolean; /** * @returns `true` if `aria-required` is set to `true`, otherwise returns `false`. */ isRequired(): boolean; /** * @returns `true` if `aria-selected` is set to `true`, otherwise returns `false`. */ isSelected(): boolean; } /** Filters keys from `T` which include `V` possibly in a mixed union type. */ declare type FilterKeysWithValue<T, V> = { [K in keyof T as V extends T[K] ? K : never]: T[K]; }; /** * Filters keys from `T` which have `V` as their only type * or in a homogeneous union type. */ declare type FilterKeysWithValueStrict<T, V> = { [K in keyof T as T[K] extends V ? K : never]: T[K]; }; /** Denotes a reference to the `id` of a single element. */ declare type idref = { readonly __tag: unique symbol; }; /** Denotes a list of {@link idref}s. */ declare type idref_list = { readonly __tag: unique symbol; }; /** Attributes which accept IDREFs. */ declare type IDREFAttributes = FilterKeysWithValue<AriaAttributes, idref>; /** Values supported by `.set()` for IDREF attributes. */ declare type IDREFAttributeValue = string | Element | null; /** Attributes which accept a list of IDREFs. */ declare type IDREFListAttributes = FilterKeysWithValue<AriaAttributes, idref_list>; /** Values supported by `.set()` for IDREFList attributes. */ declare type IDREFListAttributeValue = IDREFAttributeValue | Element[] | NodeListOf<Element>; export { }