@momentum-design/components 0.46.1 → 0.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ import ScreenreaderAnnouncer from './screenreaderannouncer.component';
2
+ declare global {
3
+ interface HTMLElementTagNameMap {
4
+ ['mdc-screenreaderannouncer']: ScreenreaderAnnouncer;
5
+ }
6
+ }
7
+ export default ScreenreaderAnnouncer;
@@ -0,0 +1,4 @@
1
+ import ScreenreaderAnnouncer from './screenreaderannouncer.component';
2
+ import { TAG_NAME } from './screenreaderannouncer.constants';
3
+ ScreenreaderAnnouncer.register(TAG_NAME);
4
+ export default ScreenreaderAnnouncer;
@@ -0,0 +1,110 @@
1
+ import type { CSSResult, PropertyValueMap } from 'lit';
2
+ import { Component } from '../../models';
3
+ import { AriaLive } from './screenreaderannouncer.types';
4
+ /**
5
+ * `mdc-screenreaderannouncer` can be used to announce messages with the screen reader.
6
+ *
7
+ * To make an announcement set `announcement` attribute on the `mdc-screenreaderannouncer` element.
8
+ *
9
+ * **Internal logic**
10
+ *
11
+ * When the screenreader announcer is connected to the DOM, if the `identity` attribute is not
12
+ * provided, it is set to `mdc-screenreaderannouncer-identity` and a `<div>` element with this id is created
13
+ * in the DOM. If the `identity` attribute is provided, the identity element is used and no new element
14
+ * is created in the DOM.
15
+ *
16
+ * When the `announcement` attribute is set, the screenreader announcer will create a `<div>` element with
17
+ * `aria-live` attribute set to the value of `data-aria-live` attribute and append it to the `identity` element.
18
+ * After delay of `delay` milliseconds, a <p> element with the announcement text is appended to the `<div>` element.
19
+ *
20
+ * The announcement `<div>` element is removed from the DOM after `timeout` milliseconds.
21
+ *
22
+ * When the screen announcer component is disconnected from the DOM, all the timeouts are cleared and
23
+ * all the announcement elements added are removed from the DOM and timeouts cleared.
24
+ *
25
+ * **Note**
26
+ * 1. The default delay of 150 miliseconds is used as we dynamically generate the
27
+ * aria-live region in the DOM and add the announcement text to it.
28
+ * 3. If no `identity` is provided, all the screen reader components will create and use only one
29
+ * `<div>` element with id `mdc-screenreaderannouncer-identity` in the DOM.
30
+ *
31
+ * Reference: https://patrickhlauke.github.io/aria/tests/live-regions/
32
+ *
33
+ * @tagname mdc-screenreaderannouncer
34
+ */
35
+ declare class ScreenreaderAnnouncer extends Component {
36
+ /**
37
+ * The announcement attribute is a string that is used to announce messages to the screen reader.
38
+ * The announcement is made when the announcement attribute is set to a non-empty string.
39
+ *
40
+ * @default ''
41
+ */
42
+ announcement: string;
43
+ /**
44
+ * The id of the element in the light dom, to which announcement elements will be appended.
45
+ *
46
+ * If id is not provided, it will be set to `mdc-screenreaderannouncer-identity` and
47
+ * a div element with this id will be created in the light dom.
48
+ */
49
+ identity: string;
50
+ /**
51
+ * Aria live value for announcement.
52
+ *
53
+ * @default 'polite'
54
+ */
55
+ dataAriaLive: AriaLive;
56
+ /**
57
+ * Milliseconds to wait before adding the announcement to the identiy region in
58
+ * DOM, which will announce the message to the screen reader.
59
+ *
60
+ * @default 150
61
+ */
62
+ delay: number;
63
+ /**
64
+ * Milliseconds to wait after which the announcement element will be removed from
65
+ * identity region in DOM, causing the screen reader to not announcing the message.
66
+ * @default 20_000
67
+ */
68
+ timeout: number;
69
+ /**
70
+ * Array to store timeOutIds for clearing timeouts later.
71
+ * @internal
72
+ */
73
+ private timeOutIds;
74
+ /**
75
+ * Array to store the ids of the announcement elements.
76
+ * @internal
77
+ */
78
+ private ariaLiveAnnouncementIds;
79
+ /**
80
+ * Announces the given announcement to the screen reader.
81
+ *
82
+ * A div element with aria-live attribute set to the given ariaLive value is created
83
+ * and a p element with the announcement text is appended to it.
84
+ *
85
+ * The div element is appended to the element in the DOM identified with id as
86
+ * identity attribute.
87
+ *
88
+ * @param announcement - The announcement to be made.
89
+ * @param delay - The delay in milliseconds before announcing the message.
90
+ * @param timeout - The timeout in milliseconds before removing the announcement.
91
+ * @param ariaLive - The aria live value for the announcement.
92
+ */
93
+ announce(announcement: string, delay: number, timeout: number, ariaLive: AriaLive): void;
94
+ /**
95
+ * Clears all timeouts and removes all announcements from the screen reader.
96
+ */
97
+ private clearTimeOutsAndAnnouncements;
98
+ /**
99
+ * Creates a div element with id as identity attribute in the DOM.
100
+ *
101
+ * If the identity attribute is not provided, it is set internally to
102
+ * `mdc-screenreaderannouncer-identity`.
103
+ */
104
+ private createAnnouncementAriaLiveRegion;
105
+ connectedCallback(): void;
106
+ disconnectedCallback(): void;
107
+ protected updated(changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void;
108
+ static styles: Array<CSSResult>;
109
+ }
110
+ export default ScreenreaderAnnouncer;
@@ -0,0 +1,212 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { property } from 'lit/decorators.js';
11
+ import { v4 as uuidv4 } from 'uuid';
12
+ import { Component } from '../../models';
13
+ import { DEFAULTS } from './screenreaderannouncer.constants';
14
+ import styles from './screenreaderannouncer.styles';
15
+ /**
16
+ * `mdc-screenreaderannouncer` can be used to announce messages with the screen reader.
17
+ *
18
+ * To make an announcement set `announcement` attribute on the `mdc-screenreaderannouncer` element.
19
+ *
20
+ * **Internal logic**
21
+ *
22
+ * When the screenreader announcer is connected to the DOM, if the `identity` attribute is not
23
+ * provided, it is set to `mdc-screenreaderannouncer-identity` and a `<div>` element with this id is created
24
+ * in the DOM. If the `identity` attribute is provided, the identity element is used and no new element
25
+ * is created in the DOM.
26
+ *
27
+ * When the `announcement` attribute is set, the screenreader announcer will create a `<div>` element with
28
+ * `aria-live` attribute set to the value of `data-aria-live` attribute and append it to the `identity` element.
29
+ * After delay of `delay` milliseconds, a <p> element with the announcement text is appended to the `<div>` element.
30
+ *
31
+ * The announcement `<div>` element is removed from the DOM after `timeout` milliseconds.
32
+ *
33
+ * When the screen announcer component is disconnected from the DOM, all the timeouts are cleared and
34
+ * all the announcement elements added are removed from the DOM and timeouts cleared.
35
+ *
36
+ * **Note**
37
+ * 1. The default delay of 150 miliseconds is used as we dynamically generate the
38
+ * aria-live region in the DOM and add the announcement text to it.
39
+ * 3. If no `identity` is provided, all the screen reader components will create and use only one
40
+ * `<div>` element with id `mdc-screenreaderannouncer-identity` in the DOM.
41
+ *
42
+ * Reference: https://patrickhlauke.github.io/aria/tests/live-regions/
43
+ *
44
+ * @tagname mdc-screenreaderannouncer
45
+ */
46
+ class ScreenreaderAnnouncer extends Component {
47
+ constructor() {
48
+ super(...arguments);
49
+ /**
50
+ * The announcement attribute is a string that is used to announce messages to the screen reader.
51
+ * The announcement is made when the announcement attribute is set to a non-empty string.
52
+ *
53
+ * @default ''
54
+ */
55
+ this.announcement = '';
56
+ /**
57
+ * The id of the element in the light dom, to which announcement elements will be appended.
58
+ *
59
+ * If id is not provided, it will be set to `mdc-screenreaderannouncer-identity` and
60
+ * a div element with this id will be created in the light dom.
61
+ */
62
+ this.identity = '';
63
+ /**
64
+ * Aria live value for announcement.
65
+ *
66
+ * @default 'polite'
67
+ */
68
+ this.dataAriaLive = DEFAULTS.ARIA_LIVE;
69
+ /**
70
+ * Milliseconds to wait before adding the announcement to the identiy region in
71
+ * DOM, which will announce the message to the screen reader.
72
+ *
73
+ * @default 150
74
+ */
75
+ this.delay = DEFAULTS.DELAY;
76
+ /**
77
+ * Milliseconds to wait after which the announcement element will be removed from
78
+ * identity region in DOM, causing the screen reader to not announcing the message.
79
+ * @default 20_000
80
+ */
81
+ this.timeout = DEFAULTS.TIMEOUT;
82
+ /**
83
+ * Array to store timeOutIds for clearing timeouts later.
84
+ * @internal
85
+ */
86
+ this.timeOutIds = [];
87
+ /**
88
+ * Array to store the ids of the announcement elements.
89
+ * @internal
90
+ */
91
+ this.ariaLiveAnnouncementIds = [];
92
+ }
93
+ /**
94
+ * Announces the given announcement to the screen reader.
95
+ *
96
+ * A div element with aria-live attribute set to the given ariaLive value is created
97
+ * and a p element with the announcement text is appended to it.
98
+ *
99
+ * The div element is appended to the element in the DOM identified with id as
100
+ * identity attribute.
101
+ *
102
+ * @param announcement - The announcement to be made.
103
+ * @param delay - The delay in milliseconds before announcing the message.
104
+ * @param timeout - The timeout in milliseconds before removing the announcement.
105
+ * @param ariaLive - The aria live value for the announcement.
106
+ */
107
+ announce(announcement, delay, timeout, ariaLive) {
108
+ var _a;
109
+ if (announcement.length > 0) {
110
+ const announcementId = `mdc-screenreaderannouncer-announcement-${uuidv4()}`;
111
+ const announcementContainer = document.createElement('div');
112
+ announcementContainer.id = announcementId;
113
+ announcementContainer.ariaLive = ariaLive;
114
+ (_a = document.getElementById(this.identity)) === null || _a === void 0 ? void 0 : _a.appendChild(announcementContainer);
115
+ const timeOutId = window.setTimeout(() => {
116
+ const announcementElement = document.createElement('p');
117
+ announcementElement.textContent = announcement;
118
+ announcementContainer.appendChild(announcementElement);
119
+ this.ariaLiveAnnouncementIds.push(announcementId);
120
+ const timeOutId = window.setTimeout(() => {
121
+ var _a;
122
+ (_a = document.getElementById(announcementId)) === null || _a === void 0 ? void 0 : _a.remove();
123
+ }, timeout);
124
+ this.timeOutIds.push(timeOutId);
125
+ }, delay);
126
+ this.timeOutIds.push(timeOutId);
127
+ }
128
+ }
129
+ /**
130
+ * Clears all timeouts and removes all announcements from the screen reader.
131
+ */
132
+ clearTimeOutsAndAnnouncements() {
133
+ this.timeOutIds.forEach((timeOutId) => {
134
+ window.clearTimeout(timeOutId);
135
+ });
136
+ this.ariaLiveAnnouncementIds.forEach((announcementId) => {
137
+ var _a;
138
+ (_a = document.getElementById(announcementId)) === null || _a === void 0 ? void 0 : _a.remove();
139
+ });
140
+ }
141
+ /**
142
+ * Creates a div element with id as identity attribute in the DOM.
143
+ *
144
+ * If the identity attribute is not provided, it is set internally to
145
+ * `mdc-screenreaderannouncer-identity`.
146
+ */
147
+ createAnnouncementAriaLiveRegion() {
148
+ let liveRegionLightDom = document.getElementById(this.identity);
149
+ if (!liveRegionLightDom) {
150
+ liveRegionLightDom = document.createElement('div');
151
+ liveRegionLightDom.id = this.identity;
152
+ const styleElement = document.createElement('style');
153
+ styleElement.textContent = `
154
+ .mdc-screenreaderannouncer__visually-hidden {
155
+ clip: rect(0 0 0 0);
156
+ clip-path: inset(50%);
157
+ height: 1px;
158
+ overflow: hidden;
159
+ position: absolute;
160
+ white-space: nowrap;
161
+ width: 1px;
162
+ }
163
+ `;
164
+ liveRegionLightDom.appendChild(styleElement);
165
+ liveRegionLightDom.classList.add('mdc-screenreaderannouncer__visually-hidden');
166
+ document.body.appendChild(liveRegionLightDom);
167
+ }
168
+ }
169
+ connectedCallback() {
170
+ super.connectedCallback();
171
+ if (this.identity.length === 0) {
172
+ this.identity = DEFAULTS.IDENTITY;
173
+ }
174
+ this.createAnnouncementAriaLiveRegion();
175
+ }
176
+ disconnectedCallback() {
177
+ super.disconnectedCallback();
178
+ this.clearTimeOutsAndAnnouncements();
179
+ }
180
+ updated(changedProperties) {
181
+ if (changedProperties.has('identity') && this.identity.length === 0) {
182
+ this.identity = DEFAULTS.IDENTITY;
183
+ this.createAnnouncementAriaLiveRegion();
184
+ }
185
+ if (changedProperties.has('announcement') && this.announcement.length > 0) {
186
+ this.announce(this.announcement, this.delay, this.timeout, this.dataAriaLive);
187
+ this.announcement = '';
188
+ }
189
+ }
190
+ }
191
+ ScreenreaderAnnouncer.styles = [...Component.styles, ...styles];
192
+ __decorate([
193
+ property({ type: String, reflect: true }),
194
+ __metadata("design:type", String)
195
+ ], ScreenreaderAnnouncer.prototype, "announcement", void 0);
196
+ __decorate([
197
+ property({ type: String, reflect: true }),
198
+ __metadata("design:type", String)
199
+ ], ScreenreaderAnnouncer.prototype, "identity", void 0);
200
+ __decorate([
201
+ property({ type: String, reflect: true, attribute: 'data-aria-live' }),
202
+ __metadata("design:type", String)
203
+ ], ScreenreaderAnnouncer.prototype, "dataAriaLive", void 0);
204
+ __decorate([
205
+ property({ type: Number, reflect: true }),
206
+ __metadata("design:type", Number)
207
+ ], ScreenreaderAnnouncer.prototype, "delay", void 0);
208
+ __decorate([
209
+ property({ type: Number, reflect: true }),
210
+ __metadata("design:type", Number)
211
+ ], ScreenreaderAnnouncer.prototype, "timeout", void 0);
212
+ export default ScreenreaderAnnouncer;
@@ -0,0 +1,13 @@
1
+ declare const TAG_NAME: "mdc-screenreaderannouncer";
2
+ declare const ARIA_LIVE_VALUES: {
3
+ readonly ASSERTIVE: "assertive";
4
+ readonly POLITE: "polite";
5
+ readonly OFF: "off";
6
+ };
7
+ declare const DEFAULTS: {
8
+ readonly ARIA_LIVE: "polite";
9
+ readonly DELAY: 150;
10
+ readonly IDENTITY: "mdc-screenreaderannouncer-identity";
11
+ readonly TIMEOUT: 20000;
12
+ };
13
+ export { ARIA_LIVE_VALUES, DEFAULTS, TAG_NAME };
@@ -0,0 +1,14 @@
1
+ import utils from '../../utils/tag-name';
2
+ const TAG_NAME = utils.constructTagName('screenreaderannouncer');
3
+ const ARIA_LIVE_VALUES = {
4
+ ASSERTIVE: 'assertive',
5
+ POLITE: 'polite',
6
+ OFF: 'off',
7
+ };
8
+ const DEFAULTS = {
9
+ ARIA_LIVE: ARIA_LIVE_VALUES.POLITE,
10
+ DELAY: 150,
11
+ IDENTITY: 'mdc-screenreaderannouncer-identity',
12
+ TIMEOUT: 20000,
13
+ };
14
+ export { ARIA_LIVE_VALUES, DEFAULTS, TAG_NAME };
@@ -0,0 +1,2 @@
1
+ declare const styles: import("lit").CSSResult[];
2
+ export default styles;
@@ -0,0 +1,8 @@
1
+ import { css } from 'lit';
2
+ const styles = [css `
3
+ :host {
4
+ display: none;
5
+ visibility: hidden;
6
+ }
7
+ `];
8
+ export default styles;
@@ -0,0 +1,3 @@
1
+ import { ValueOf } from '../../utils/types';
2
+ import { ARIA_LIVE_VALUES } from './screenreaderannouncer.constants';
3
+ export type AriaLive = ValueOf<typeof ARIA_LIVE_VALUES>;
@@ -78,7 +78,11 @@ class Searchfield extends Input {
78
78
  })}" part="input-container">
79
79
  ${this.renderLeadingIcon()}
80
80
  <div part='scrollable-container'>
81
- <div part="filters-container"><slot name="filters" @slotchange=${this.renderInputChips}></slot></div>
81
+ <div part="filters-container"
82
+ @click=${() => this.inputElement.focus()}
83
+ @keydown=${(e) => e.key === 'Enter' ? this.inputElement.focus() : null}
84
+ @keyup=${(e) => e.key === ' ' ? this.inputElement.focus() : null}>
85
+ <slot name="filters" @slotchange=${this.renderInputChips}></slot></div>
82
86
  ${this.renderInputElement(DEFAULTS.TYPE)}
83
87
  </div>
84
88
  ${this.renderTrailingButton()}