@douyinfe/semi-foundation 2.11.2 → 2.12.0-beta.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.
- package/inputNumber/foundation.ts +4 -2
- package/lib/cjs/inputNumber/foundation.js +10 -2
- package/lib/cjs/tabs/foundation.d.ts +5 -0
- package/lib/cjs/tabs/foundation.js +108 -0
- package/lib/cjs/tabs/tabs.css +23 -0
- package/lib/cjs/tabs/tabs.scss +30 -1
- package/lib/cjs/tabs/variables.scss +5 -0
- package/lib/cjs/tag/tag.css +10 -0
- package/lib/cjs/tag/tag.scss +22 -7
- package/lib/cjs/tag/variables.scss +5 -0
- package/lib/cjs/utils/FocusHandle.d.ts +25 -0
- package/lib/cjs/utils/FocusHandle.js +161 -0
- package/lib/cjs/utils/a11y.d.ts +1 -0
- package/lib/cjs/utils/a11y.js +14 -0
- package/lib/es/inputNumber/foundation.js +10 -2
- package/lib/es/tabs/foundation.d.ts +5 -0
- package/lib/es/tabs/foundation.js +107 -0
- package/lib/es/tabs/tabs.css +23 -0
- package/lib/es/tabs/tabs.scss +30 -1
- package/lib/es/tabs/variables.scss +5 -0
- package/lib/es/tag/tag.css +10 -0
- package/lib/es/tag/tag.scss +22 -7
- package/lib/es/tag/variables.scss +5 -0
- package/lib/es/utils/FocusHandle.d.ts +25 -0
- package/lib/es/utils/FocusHandle.js +145 -0
- package/lib/es/utils/a11y.d.ts +1 -0
- package/lib/es/utils/a11y.js +4 -0
- package/package.json +3 -3
- package/tabs/foundation.ts +90 -1
- package/tabs/tabs.scss +30 -1
- package/tabs/variables.scss +5 -0
- package/tag/tag.scss +22 -7
- package/tag/variables.scss +5 -0
- package/utils/FocusHandle.ts +159 -0
- package/utils/a11y.ts +4 -0
package/tabs/foundation.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BaseFoundation, { DefaultAdapter } from '../base/foundation';
|
|
2
|
-
import { noop } from 'lodash';
|
|
2
|
+
import { get, noop } from 'lodash';
|
|
3
3
|
|
|
4
4
|
export interface TabsAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
|
|
5
5
|
collectPane: () => void;
|
|
@@ -72,6 +72,95 @@ class TabsFoundation<P = Record<string, any>, S = Record<string, any>> extends B
|
|
|
72
72
|
handleTabDelete(tabKey: string): void {
|
|
73
73
|
this._adapter.notifyTabDelete(tabKey);
|
|
74
74
|
}
|
|
75
|
+
|
|
76
|
+
handlePrevent = (event: any) => {
|
|
77
|
+
event.stopPropagation();
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
handleKeyDown = (event: any, itemKey: string, closable: boolean) => {
|
|
82
|
+
const tabs = [...event.target.parentNode.childNodes].filter(item => {
|
|
83
|
+
return get(item, 'attributes.data-tabkey.value', '').includes('semiTab') && item.ariaDisabled!=="true";
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
switch (event.key) {
|
|
87
|
+
case "ArrowLeft":
|
|
88
|
+
case "ArrowRight":
|
|
89
|
+
case "ArrowUp":
|
|
90
|
+
case "ArrowDown":
|
|
91
|
+
this.determineOrientation(event, tabs);
|
|
92
|
+
break;
|
|
93
|
+
case "Backspace":
|
|
94
|
+
case "Delete":
|
|
95
|
+
this.handleDeleteKeyDown(event, tabs, itemKey, closable);
|
|
96
|
+
break;
|
|
97
|
+
case "Enter":
|
|
98
|
+
case " ":
|
|
99
|
+
this.handleTabClick(itemKey, event);
|
|
100
|
+
this.handlePrevent(event);
|
|
101
|
+
break;
|
|
102
|
+
case "Home":
|
|
103
|
+
tabs[0].focus(); // focus first tab
|
|
104
|
+
this.handlePrevent(event);
|
|
105
|
+
break;
|
|
106
|
+
case "End":
|
|
107
|
+
tabs[tabs.length - 1].focus(); // focus last tab
|
|
108
|
+
this.handlePrevent(event);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
determineOrientation(event: any, tabs: HTMLElement[]): void {
|
|
114
|
+
const { tabPosition } = this.getProps();
|
|
115
|
+
const isVertical = tabPosition === 'left';
|
|
116
|
+
|
|
117
|
+
if (isVertical) {
|
|
118
|
+
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
|
119
|
+
this.switchTabOnArrowPress(event, tabs);
|
|
120
|
+
this.handlePrevent(event);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
|
|
124
|
+
this.switchTabOnArrowPress(event, tabs);
|
|
125
|
+
this.handlePrevent(event);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
handleDeleteKeyDown(event:any, tabs: HTMLElement[], itemKey: string, closable: boolean): void {
|
|
131
|
+
if (closable) {
|
|
132
|
+
this.handleTabDelete(itemKey);
|
|
133
|
+
const index = tabs.indexOf(event.target);
|
|
134
|
+
// Move focus to next element after deletion
|
|
135
|
+
// If the element is the last removable tab, focus to its previous tab
|
|
136
|
+
if (tabs.length !== 1 ){
|
|
137
|
+
tabs[index + 1 >= tabs.length ? index - 1 : index + 1].focus();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
switchTabOnArrowPress(event: any, tabs: HTMLElement[]): void {
|
|
143
|
+
const index = tabs.indexOf(event.target);
|
|
144
|
+
|
|
145
|
+
const direction = {
|
|
146
|
+
"ArrowLeft": -1,
|
|
147
|
+
"ArrowUp": -1,
|
|
148
|
+
"ArrowRight": 1,
|
|
149
|
+
"ArrowDown": 1,
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
if (direction[event.key]) {
|
|
153
|
+
if (index !== undefined) {
|
|
154
|
+
if (tabs[index + direction[event.key]]) {
|
|
155
|
+
tabs[index+ direction[event.key]].focus();
|
|
156
|
+
} else if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
|
|
157
|
+
tabs[tabs.length - 1].focus(); // focus last tab
|
|
158
|
+
} else if (event.key === "ArrowRight" || event.key == "ArrowDown") {
|
|
159
|
+
tabs[0].focus(); // focus first tab
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
75
164
|
}
|
|
76
165
|
|
|
77
166
|
export default TabsFoundation;
|
package/tabs/tabs.scss
CHANGED
|
@@ -124,6 +124,11 @@ $module: #{$prefix}-tabs;
|
|
|
124
124
|
width: 0;
|
|
125
125
|
height: 0;
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
&:focus-visible {
|
|
129
|
+
outline: $width-tabs-outline solid $color-tabs_tab-outline-focus;
|
|
130
|
+
outline-offset: $width-tabs-outline-offset;
|
|
131
|
+
}
|
|
127
132
|
}
|
|
128
133
|
}
|
|
129
134
|
|
|
@@ -164,6 +169,11 @@ $module: #{$prefix}-tabs;
|
|
|
164
169
|
&:hover {
|
|
165
170
|
border-bottom: $width-tabs_bar_line_tab-border solid $color-tabs_tab_line_default-border-hover;
|
|
166
171
|
}
|
|
172
|
+
|
|
173
|
+
&:focus-visible {
|
|
174
|
+
outline: $width-tabs-outline solid $color-tabs_tab-outline-focus;
|
|
175
|
+
outline-offset: $width-tabs_bar_line-outline-offset;
|
|
176
|
+
}
|
|
167
177
|
|
|
168
178
|
&:active {
|
|
169
179
|
border-bottom: $width-tabs_bar_line_tab-border solid $color-tabs_tab_line_default-border-active;
|
|
@@ -202,6 +212,11 @@ $module: #{$prefix}-tabs;
|
|
|
202
212
|
border-left: $width-tabs_bar_line_tab-border solid $color-tabs_tab_line_default-border-hover;
|
|
203
213
|
background-color: $color-tabs_tab_line_vertical-bg-hover;
|
|
204
214
|
}
|
|
215
|
+
|
|
216
|
+
&:focus-visible {
|
|
217
|
+
outline: $width-tabs-outline solid $color-tabs_tab-outline-focus;
|
|
218
|
+
outline-offset: $width-tabs-outline-offset;
|
|
219
|
+
}
|
|
205
220
|
|
|
206
221
|
&:active {
|
|
207
222
|
border-left: $width-tabs_bar_line_tab-border solid $color-tabs_tab_line_default-border-active;
|
|
@@ -327,6 +342,11 @@ $module: #{$prefix}-tabs;
|
|
|
327
342
|
&:hover {
|
|
328
343
|
background: $color-tabs_tab_card-bg-hover;
|
|
329
344
|
}
|
|
345
|
+
|
|
346
|
+
&:focus-visible {
|
|
347
|
+
outline: $width-tabs-outline solid $color-tabs_tab-outline-focus;
|
|
348
|
+
outline-offset: $width-tabs-outline-offset;
|
|
349
|
+
}
|
|
330
350
|
|
|
331
351
|
&:active {
|
|
332
352
|
background: $color-tabs_tab_card-bg-active;
|
|
@@ -365,6 +385,11 @@ $module: #{$prefix}-tabs;
|
|
|
365
385
|
border: none;
|
|
366
386
|
background-color: $color-tabs_tab_button-bg-hover;
|
|
367
387
|
}
|
|
388
|
+
|
|
389
|
+
&:focus-visible {
|
|
390
|
+
outline: $width-tabs-outline solid $color-tabs_tab-outline-focus;
|
|
391
|
+
outline-offset: $width-tabs-outline-offset;
|
|
392
|
+
}
|
|
368
393
|
|
|
369
394
|
&:active {
|
|
370
395
|
background-color: $color-tabs_tab_button-bg-active;
|
|
@@ -393,13 +418,17 @@ $module: #{$prefix}-tabs;
|
|
|
393
418
|
height: 100%;
|
|
394
419
|
padding: $spacing-tabs_content_left-paddingY $spacing-tabs_content_left-paddingX;
|
|
395
420
|
}
|
|
396
|
-
|
|
421
|
+
|
|
397
422
|
&-pane {
|
|
398
423
|
width: 100%;
|
|
399
424
|
overflow: hidden;
|
|
400
425
|
// position: absolute;
|
|
401
426
|
// flex-shrink: 0;
|
|
402
427
|
// position: absolute;
|
|
428
|
+
|
|
429
|
+
&:focus-visible {
|
|
430
|
+
outline: $width-tabs-outline solid $color-tabs_tab-outline-focus;
|
|
431
|
+
}
|
|
403
432
|
}
|
|
404
433
|
|
|
405
434
|
&-pane-inactive,
|
package/tabs/variables.scss
CHANGED
|
@@ -44,6 +44,8 @@ $color-tabs_tab-icon-hover: var(--semi-color-text-0); // 页签图标颜色 -
|
|
|
44
44
|
$color-tabs_tab-icon-active: var(--semi-color-text-0); // 页签图标颜色 - 按下
|
|
45
45
|
$color-tabs_tab_selected-icon-default: var(--semi-color-primary); // 页签图标颜色 - 选中
|
|
46
46
|
|
|
47
|
+
$color-tabs_tab-outline-focus: var(--semi-color-primary-light-active); // 页签轮廓 - 聚焦
|
|
48
|
+
|
|
47
49
|
|
|
48
50
|
$font-tabs_tab-fontWeight: $font-weight-regular; // 页签文本字重 - 默认
|
|
49
51
|
$font-tabs_tab_active-fontWeight: $font-weight-bold; // 页签文本字重 - 选中
|
|
@@ -54,6 +56,9 @@ $width-tabs_bar_line-border: $border-thickness-control; // 线条式页签底部
|
|
|
54
56
|
$width-tabs_bar_line_tab-border: 2px; // 页签标示线宽度
|
|
55
57
|
|
|
56
58
|
$width-tabs_bar_card-border: $border-thickness-control; // 卡片式页签底部分割线宽度
|
|
59
|
+
$width-tabs-outline: 2px; // 聚焦轮廓宽度
|
|
60
|
+
$width-tabs-outline-offset: -2px; // 聚焦轮廓偏移宽度
|
|
61
|
+
$width-tabs_bar_line-outline-offset: -1px; // 线条式页签聚焦轮廓偏移宽度
|
|
57
62
|
|
|
58
63
|
$height-tabs_bar_extra_large: 50px; // 大尺寸页签高度
|
|
59
64
|
$font-tabs_bar_extra_large-lineHeight: $height-tabs_bar_extra_large; // 大尺寸页签文字行高
|
package/tag/tag.scss
CHANGED
|
@@ -25,12 +25,18 @@ $types: "ghost", "solid", "light";
|
|
|
25
25
|
@include font-size-small;
|
|
26
26
|
height: $height-tag_small;
|
|
27
27
|
padding: $spacing-tag_small-paddingY $spacing-tag_small-paddingX;
|
|
28
|
+
&:focus-visible {
|
|
29
|
+
outline: $width-tag-outline solid $color-tag-outline-focus;
|
|
30
|
+
}
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
&-large {
|
|
31
34
|
@include font-size-small;
|
|
32
35
|
padding: $spacing-tag_large-paddingY $spacing-tag_large-paddingX;
|
|
33
36
|
height: $height-tag_large;
|
|
37
|
+
&:focus-visible {
|
|
38
|
+
outline: $width-tag-outline solid $color-tag-outline-focus;
|
|
39
|
+
}
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
&-invisible {
|
|
@@ -46,12 +52,13 @@ $types: "ghost", "solid", "light";
|
|
|
46
52
|
&-close {
|
|
47
53
|
@include all-center;
|
|
48
54
|
color: $color-tag_close-icon-default;
|
|
49
|
-
padding-left: $spacing-tag_close-paddingLeft;
|
|
55
|
+
padding-left: $spacing-tag_close-paddingLeft;
|
|
50
56
|
cursor: pointer;
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
&-closable {
|
|
54
|
-
padding: $spacing-tag_closable-paddingTop $spacing-tag_closable-paddingRight $spacing-tag_closable-paddingBottom
|
|
60
|
+
padding: $spacing-tag_closable-paddingTop $spacing-tag_closable-paddingRight $spacing-tag_closable-paddingBottom
|
|
61
|
+
$spacing-tag_closable-paddingLeft;
|
|
55
62
|
}
|
|
56
63
|
|
|
57
64
|
&-avatar-square,
|
|
@@ -62,10 +69,10 @@ $types: "ghost", "solid", "light";
|
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
&-avatar-square {
|
|
65
|
-
padding: $spacing-tag_avatar_square-paddingTop $spacing-tag_avatar_square-paddingRight
|
|
72
|
+
padding: $spacing-tag_avatar_square-paddingTop $spacing-tag_avatar_square-paddingRight
|
|
73
|
+
$spacing-tag_avatar_square-paddingBottom $spacing-tag_avatar_square-paddingLeft;
|
|
66
74
|
|
|
67
75
|
.#{$prefix}-avatar {
|
|
68
|
-
|
|
69
76
|
& > img {
|
|
70
77
|
background-color: $color-tag_avatar_square_img-bg-default;
|
|
71
78
|
}
|
|
@@ -73,10 +80,12 @@ $types: "ghost", "solid", "light";
|
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
&-avatar-circle {
|
|
76
|
-
padding: $spacing-tag_avatar_circle-paddingTop $spacing-tag_avatar_circle-paddingRight
|
|
83
|
+
padding: $spacing-tag_avatar_circle-paddingTop $spacing-tag_avatar_circle-paddingRight
|
|
84
|
+
$spacing-tag_avatar_circle-paddingBottom $spacing-tag_avatar_circle-paddingLeft;
|
|
77
85
|
}
|
|
78
86
|
|
|
79
|
-
&-avatar-square.#{$module}-default,
|
|
87
|
+
&-avatar-square.#{$module}-default,
|
|
88
|
+
&-avatar-square.#{$module}-small {
|
|
80
89
|
.#{$prefix}-avatar {
|
|
81
90
|
width: $height-tag_small;
|
|
82
91
|
height: $height-tag_small;
|
|
@@ -179,4 +188,10 @@ $types: "ghost", "solid", "light";
|
|
|
179
188
|
color: $color-tag_avatar-text-default;
|
|
180
189
|
}
|
|
181
190
|
|
|
182
|
-
|
|
191
|
+
.#{$module}-solid {
|
|
192
|
+
.#{$module}-close {
|
|
193
|
+
color: $color-tag_close-icon_deep-default;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
@import './rtl.scss';
|
package/tag/variables.scss
CHANGED
|
@@ -7,12 +7,16 @@ $color-tag_white-border-default: rgba(var(--semi-grey-2), 0.7); // 白色标签
|
|
|
7
7
|
$color-tag_white-text-default: var(--semi-color-text-0); // 白色标签文字颜色 - 默认
|
|
8
8
|
$color-tag_white-icon-default: var(--semi-color-text-2); // 白色标签图标颜色 - 默认
|
|
9
9
|
|
|
10
|
+
$color-tag-outline-focus: var(--semi-color-primary-light-active); // 标签轮廓 - 聚焦
|
|
11
|
+
|
|
10
12
|
$width-tag_avatar_circle_small: 16px; // 头像标签圆角 - 小尺寸
|
|
11
13
|
$width-tag_avatar_circle_large: 20px; // 头像标签圆角 - 大尺寸
|
|
12
14
|
|
|
13
15
|
$width-tag-border: 1px; // 标签描边宽度
|
|
14
16
|
$width-tag_avatar-border: $width-tag-border; // 头像标签描边宽度
|
|
15
17
|
|
|
18
|
+
$width-tag-outline: 2px; // 标签轮廓宽度
|
|
19
|
+
|
|
16
20
|
$height-tag_small: 20px; // 小尺寸标签高度
|
|
17
21
|
$height-tag_large: 24px; // 大尺寸标签高度
|
|
18
22
|
$radius-tag: var(--semi-border-radius-small); // 标签圆角大小
|
|
@@ -24,6 +28,7 @@ $spacing-tag_large-paddingY: 4px; // 大尺寸标签垂直方向内边距
|
|
|
24
28
|
$spacing-tag_large-paddingX: $spacing-tight; // 大尺寸标签水平方向内边距
|
|
25
29
|
|
|
26
30
|
$color-tag_close-icon-default: var(--semi-color-text-2); // 可删除的标签删除按钮颜色
|
|
31
|
+
$color-tag_close-icon_deep-default: var(--semi-color-white); // 深色模式下可删除的标签删除按钮颜色
|
|
27
32
|
$spacing-tag_close-paddingLeft: $spacing-extra-tight; // 可删除的标签删除按钮左侧内边距
|
|
28
33
|
$spacing-tag_closable-paddingTop: $spacing-extra-tight; // 可删除的标签删除按钮顶部内边距
|
|
29
34
|
$spacing-tag_closable-paddingRight: $spacing-extra-tight; // 可删除的标签删除按钮右侧内边距
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { isHTMLElement } from "@douyinfe/semi-foundation/utils/dom";
|
|
2
|
+
import { without } from "lodash-es";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
type FocusRedirectListener = (element: HTMLElement) => boolean;
|
|
6
|
+
|
|
7
|
+
interface HandleOptions {
|
|
8
|
+
enable?: boolean
|
|
9
|
+
onFocusRedirectListener?: FocusRedirectListener | FocusRedirectListener[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Usage:
|
|
14
|
+
* // Eg1: Pass a dom as the tab tarp container.
|
|
15
|
+
* const handle = new FocusTrapHandle(container, { enable: true });
|
|
16
|
+
*
|
|
17
|
+
* // Eg2: The focus redirect listener will be triggered when user pressed tab whiling last focusable dom is focusing in trap dom, return false to cancel redirect and use the browser normal tab focus index.
|
|
18
|
+
* handle.addFocusRedirectListener((e)=>{
|
|
19
|
+
* return true; // return false to prevent redirect on target DOM;
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Eg3: Set it to false in order to disable tab tarp at any moment;
|
|
23
|
+
* handle.enable = true;
|
|
24
|
+
*
|
|
25
|
+
* // Eg4: Destroy instance when component is unmounting for saving resource;
|
|
26
|
+
* handle.destroy();
|
|
27
|
+
*
|
|
28
|
+
* */
|
|
29
|
+
|
|
30
|
+
class FocusTrapHandle {
|
|
31
|
+
public container: HTMLElement;
|
|
32
|
+
private options: HandleOptions;
|
|
33
|
+
private focusRedirectListenerList: FocusRedirectListener[];
|
|
34
|
+
private _enable: boolean;
|
|
35
|
+
|
|
36
|
+
constructor(container: HTMLElement, options?: HandleOptions) {
|
|
37
|
+
Object.freeze(options); // prevent user to change options after init;
|
|
38
|
+
this.container = container;
|
|
39
|
+
this.options = options;
|
|
40
|
+
this.enable = options?.enable ?? true;
|
|
41
|
+
this.focusRedirectListenerList = (() => {
|
|
42
|
+
if (options?.onFocusRedirectListener) {
|
|
43
|
+
return Array.isArray(options.onFocusRedirectListener) ? [...options.onFocusRedirectListener] : [options.onFocusRedirectListener];
|
|
44
|
+
} else {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
})();
|
|
48
|
+
this.container.addEventListener('keydown', this.onKeyPress);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public addFocusRedirectListener = (listener: FocusRedirectListener) => {
|
|
52
|
+
this.focusRedirectListenerList.push(listener);
|
|
53
|
+
return () => this.removeFocusRedirectListener(listener);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public removeFocusRedirectListener = (listener: FocusRedirectListener) => {
|
|
57
|
+
this.focusRedirectListenerList = without(this.focusRedirectListenerList, listener);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public get enable() {
|
|
61
|
+
return this._enable;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public set enable(value) {
|
|
65
|
+
this._enable = value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public destroy = () => {
|
|
69
|
+
this.container?.removeEventListener('keydown', this.onKeyPress);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---- private func ----
|
|
73
|
+
|
|
74
|
+
private shouldFocusRedirect = (element: HTMLElement) => {
|
|
75
|
+
if (!this.enable) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
for (const listener of this.focusRedirectListenerList) {
|
|
79
|
+
const should = listener(element);
|
|
80
|
+
if (!should) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private focusElement = (element: HTMLElement, event: KeyboardEvent) => {
|
|
88
|
+
element?.focus();
|
|
89
|
+
event.preventDefault(); // prevent browser default tab move behavior
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
private onKeyPress = (event: KeyboardEvent) => {
|
|
94
|
+
if (event && event.key === 'Tab') {
|
|
95
|
+
const focusableElements = FocusTrapHandle.getFocusableElements(this.container);
|
|
96
|
+
const focusableNum = focusableElements.length;
|
|
97
|
+
if (focusableNum) {
|
|
98
|
+
// Shift + Tab will move focus backward
|
|
99
|
+
if (event.shiftKey) {
|
|
100
|
+
this.handleContainerShiftTabKeyDown(focusableElements, event);
|
|
101
|
+
} else {
|
|
102
|
+
this.handleContainerTabKeyDown(focusableElements, event);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private handleContainerTabKeyDown = (focusableElements: any[], event: any) => {
|
|
109
|
+
const activeElement = FocusTrapHandle.getActiveElement();
|
|
110
|
+
const isLastCurrentFocus = focusableElements[focusableElements.length - 1] === activeElement;
|
|
111
|
+
|
|
112
|
+
const redirectForcingElement = focusableElements[0];
|
|
113
|
+
if (isLastCurrentFocus && this.shouldFocusRedirect(redirectForcingElement)) {
|
|
114
|
+
this.focusElement(redirectForcingElement, event);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
private handleContainerShiftTabKeyDown = (focusableElements: any[], event: KeyboardEvent) => {
|
|
120
|
+
const activeElement = FocusTrapHandle.getActiveElement();
|
|
121
|
+
const isFirstCurrentFocus = focusableElements[0] === activeElement;
|
|
122
|
+
const redirectForcingElement = focusableElements[focusableElements.length - 1];
|
|
123
|
+
if (isFirstCurrentFocus && this.shouldFocusRedirect(redirectForcingElement)) {
|
|
124
|
+
this.focusElement(redirectForcingElement, event);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
// ---- static func ----
|
|
130
|
+
|
|
131
|
+
static getFocusableElements(node: HTMLElement) {
|
|
132
|
+
if (!isHTMLElement(node)) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
const focusableSelectorsList = [
|
|
136
|
+
"input:not([disabled]):not([tabindex='-1'])",
|
|
137
|
+
"textarea:not([disabled]):not([tabindex='-1'])",
|
|
138
|
+
"button:not([disabled]):not([tabindex='-1'])",
|
|
139
|
+
"a[href]:not([tabindex='-1'])",
|
|
140
|
+
"select:not([disabled]):not([tabindex='-1'])",
|
|
141
|
+
"area[href]:not([tabindex='-1'])",
|
|
142
|
+
"iframe:not([tabindex='-1'])",
|
|
143
|
+
"object:not([tabindex='-1'])",
|
|
144
|
+
"*[tabindex]:not([tabindex='-1'])",
|
|
145
|
+
"*[contenteditable]:not([tabindex='-1'])",
|
|
146
|
+
];
|
|
147
|
+
const focusableSelectorsStr = focusableSelectorsList.join(',');
|
|
148
|
+
// we are not filtered elements which are invisible
|
|
149
|
+
return Array.from(node.querySelectorAll<HTMLElement>(focusableSelectorsStr));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static getActiveElement(): HTMLElement | null {
|
|
153
|
+
return document ? document.activeElement as HTMLElement : null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export default FocusTrapHandle;
|
package/utils/a11y.ts
ADDED