@prairielearn/browser-utils 2.3.0 → 2.5.0
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/CHANGELOG.md +12 -0
- package/dist/downloads.d.ts +23 -0
- package/dist/downloads.js +43 -0
- package/dist/downloads.js.map +1 -0
- package/dist/focus.js +52 -6
- package/dist/focus.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/src/downloads.ts +44 -0
- package/src/focus.ts +57 -6
- package/src/index.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @prairielearn/browser-utils
|
|
2
2
|
|
|
3
|
+
## 2.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 19f0bf7: Improved support for focus on radio groups
|
|
8
|
+
|
|
9
|
+
## 2.4.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 5b46852: Add `downloadTextFile`, `downloadAsJSON` and `downloadAsCSV` utilities
|
|
14
|
+
|
|
3
15
|
## 2.3.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Triggers a browser download of a text-based file.
|
|
3
|
+
*
|
|
4
|
+
* @param content The content of the file.
|
|
5
|
+
* @param filename The desired filename.
|
|
6
|
+
* @param mimeType The MIME type of the file.
|
|
7
|
+
*/
|
|
8
|
+
export declare function downloadTextFile(content: string, filename: string, mimeType: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Triggers a browser download of a JSON file.
|
|
11
|
+
*
|
|
12
|
+
* @param data The data to be included in the JSON file.
|
|
13
|
+
* @param filename The desired filename.
|
|
14
|
+
*/
|
|
15
|
+
export declare function downloadAsJSON(data: any, filename: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Triggers a browser download of a CSV file.
|
|
18
|
+
*
|
|
19
|
+
* @param header The header row of the CSV.
|
|
20
|
+
* @param data The data rows of the CSV.
|
|
21
|
+
* @param filename The desired filename.
|
|
22
|
+
*/
|
|
23
|
+
export declare function downloadAsCSV(header: string[], data: unknown[][], filename: string): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { stringify } from 'csv-stringify/browser/esm/sync';
|
|
2
|
+
/**
|
|
3
|
+
* Triggers a browser download of a text-based file.
|
|
4
|
+
*
|
|
5
|
+
* @param content The content of the file.
|
|
6
|
+
* @param filename The desired filename.
|
|
7
|
+
* @param mimeType The MIME type of the file.
|
|
8
|
+
*/
|
|
9
|
+
export function downloadTextFile(content, filename, mimeType) {
|
|
10
|
+
if (typeof window === 'undefined')
|
|
11
|
+
return;
|
|
12
|
+
const blob = new Blob([content], { type: mimeType });
|
|
13
|
+
const url = URL.createObjectURL(blob);
|
|
14
|
+
const a = document.createElement('a');
|
|
15
|
+
a.href = url;
|
|
16
|
+
a.download = filename;
|
|
17
|
+
document.body.append(a);
|
|
18
|
+
a.click();
|
|
19
|
+
a.remove();
|
|
20
|
+
URL.revokeObjectURL(url);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Triggers a browser download of a JSON file.
|
|
24
|
+
*
|
|
25
|
+
* @param data The data to be included in the JSON file.
|
|
26
|
+
* @param filename The desired filename.
|
|
27
|
+
*/
|
|
28
|
+
export function downloadAsJSON(data, filename) {
|
|
29
|
+
const jsonContent = JSON.stringify(data, null, 2);
|
|
30
|
+
downloadTextFile(jsonContent, filename, 'application/json');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Triggers a browser download of a CSV file.
|
|
34
|
+
*
|
|
35
|
+
* @param header The header row of the CSV.
|
|
36
|
+
* @param data The data rows of the CSV.
|
|
37
|
+
* @param filename The desired filename.
|
|
38
|
+
*/
|
|
39
|
+
export function downloadAsCSV(header, data, filename) {
|
|
40
|
+
const csvContent = stringify(data, { header: true, columns: header });
|
|
41
|
+
downloadTextFile(csvContent, filename, 'text/csv');
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=downloads.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"downloads.js","sourceRoot":"","sources":["../src/downloads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;AAE3D;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe,EAAE,QAAgB,EAAE,QAAgB;IAClF,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO;IAC1C,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;IACrD,MAAM,GAAG,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC;IACb,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACtB,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACxB,CAAC,CAAC,KAAK,EAAE,CAAC;IACV,CAAC,CAAC,MAAM,EAAE,CAAC;IACX,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAS,EAAE,QAAgB;IACxD,MAAM,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAClD,gBAAgB,CAAC,WAAW,EAAE,QAAQ,EAAE,kBAAkB,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAgB,EAAE,IAAiB,EAAE,QAAgB;IACjF,MAAM,UAAU,GAAG,SAAS,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IACtE,gBAAgB,CAAC,UAAU,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import { stringify } from 'csv-stringify/browser/esm/sync';\n\n/**\n * Triggers a browser download of a text-based file.\n *\n * @param content The content of the file.\n * @param filename The desired filename.\n * @param mimeType The MIME type of the file.\n */\nexport function downloadTextFile(content: string, filename: string, mimeType: string): void {\n if (typeof window === 'undefined') return;\n const blob = new Blob([content], { type: mimeType });\n const url = URL.createObjectURL(blob);\n const a = document.createElement('a');\n a.href = url;\n a.download = filename;\n document.body.append(a);\n a.click();\n a.remove();\n URL.revokeObjectURL(url);\n}\n\n/**\n * Triggers a browser download of a JSON file.\n *\n * @param data The data to be included in the JSON file.\n * @param filename The desired filename.\n */\nexport function downloadAsJSON(data: any, filename: string): void {\n const jsonContent = JSON.stringify(data, null, 2);\n downloadTextFile(jsonContent, filename, 'application/json');\n}\n\n/**\n * Triggers a browser download of a CSV file.\n *\n * @param header The header row of the CSV.\n * @param data The data rows of the CSV.\n * @param filename The desired filename.\n */\nexport function downloadAsCSV(header: string[], data: unknown[][], filename: string): void {\n const csvContent = stringify(data, { header: true, columns: header });\n downloadTextFile(csvContent, filename, 'text/csv');\n}\n"]}
|
package/dist/focus.js
CHANGED
|
@@ -60,15 +60,15 @@ export function trapFocus(element) {
|
|
|
60
60
|
const lastFocusable = focusable[focusable.length - 1];
|
|
61
61
|
if (e.shiftKey) {
|
|
62
62
|
// Tabbing backwards
|
|
63
|
-
if (document.activeElement
|
|
64
|
-
focusable[focusable.length - 1]
|
|
63
|
+
if (isSameFocusContext(document.activeElement, firstFocusable)) {
|
|
64
|
+
focusElementOrCheckedOption(focusable[focusable.length - 1]);
|
|
65
65
|
e.preventDefault();
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
else {
|
|
69
69
|
// Tabbing forwards
|
|
70
|
-
if (document.activeElement
|
|
71
|
-
focusable[0]
|
|
70
|
+
if (isSameFocusContext(document.activeElement, lastFocusable)) {
|
|
71
|
+
focusElementOrCheckedOption(focusable[0]);
|
|
72
72
|
e.preventDefault();
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -80,7 +80,7 @@ export function trapFocus(element) {
|
|
|
80
80
|
// Restore focus to the previously active element, but only if focus is
|
|
81
81
|
// currently inside the trap container.
|
|
82
82
|
if (element.contains(document.activeElement)) {
|
|
83
|
-
previousActiveElement
|
|
83
|
+
focusElementOrCheckedOption(previousActiveElement, { preventScroll: true });
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
};
|
|
@@ -100,10 +100,56 @@ export function focusFirstFocusableChild(el) {
|
|
|
100
100
|
}
|
|
101
101
|
const focusablePopoverChildren = focusableChildren(el);
|
|
102
102
|
if (focusablePopoverChildren.length > 0) {
|
|
103
|
-
focusablePopoverChildren[0]
|
|
103
|
+
focusElementOrCheckedOption(focusablePopoverChildren[0]);
|
|
104
104
|
return;
|
|
105
105
|
}
|
|
106
106
|
// If we still couldn't find a child element, focus the container itself.
|
|
107
107
|
el.focus();
|
|
108
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Focus on the element, or if it's a radio button, focus on the checked radio button in the same group.
|
|
111
|
+
*/
|
|
112
|
+
function focusElementOrCheckedOption(element, focusOptions) {
|
|
113
|
+
// If the element receiving focus is a radio button, and there is another
|
|
114
|
+
// radio button in the same group that is currently checked, focus on that one
|
|
115
|
+
// instead.
|
|
116
|
+
// https://www.w3.org/WAI/ARIA/apg/patterns/radio/
|
|
117
|
+
if (element.tagName === 'INPUT') {
|
|
118
|
+
const inputElement = element;
|
|
119
|
+
if (inputElement.type === 'radio' && inputElement.name) {
|
|
120
|
+
const checkedRadio = (inputElement.form ?? document).querySelector(`input[type="radio"][name="${CSS.escape(inputElement.name)}"]:checked`);
|
|
121
|
+
if (checkedRadio) {
|
|
122
|
+
checkedRadio.focus(focusOptions);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Otherwise, just focus on the element itself.
|
|
128
|
+
element.focus(focusOptions);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Check if two elements are in the same focus context. This is true if they are the
|
|
132
|
+
* same element, or if they are radio buttons in the same group.
|
|
133
|
+
*/
|
|
134
|
+
function isSameFocusContext(element1, element2) {
|
|
135
|
+
if (!element1 || !element2)
|
|
136
|
+
return false;
|
|
137
|
+
if (element1 === element2)
|
|
138
|
+
return true;
|
|
139
|
+
if (element1.tagName === 'INPUT' && element2.tagName === 'INPUT') {
|
|
140
|
+
const input1 = element1;
|
|
141
|
+
const input2 = element2;
|
|
142
|
+
// https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
|
|
143
|
+
if (input1.type === 'radio' &&
|
|
144
|
+
input2.type === 'radio' &&
|
|
145
|
+
// radios without a name are not in the same group, even if they share the same form
|
|
146
|
+
input1.name &&
|
|
147
|
+
input1.name === input2.name &&
|
|
148
|
+
// either both have no form, or both have the same form
|
|
149
|
+
input1.form === input2.form) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
109
155
|
//# sourceMappingURL=focus.js.map
|
package/dist/focus.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"focus.js","sourceRoot":"","sources":["../src/focus.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,oHAAoH;AACpH,MAAM,kBAAkB,GAAG;IACzB,GAAG;IACH,QAAQ;IACR,OAAO;IACP,UAAU;IACV,QAAQ;IACR,SAAS;IACT,YAAY;IACZ,0BAA0B;CAC3B;KACE,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,GAAG,QAAQ,uCAAuC,CAAC;KACrE,IAAI,CAAC,GAAG,CAAC,CAAC;AAEb,SA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AO;QACL,UAAU;YACR,QAAQ,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YACjD,uEAAuE;YACvE,uCAAuC;YACvC,IAAI,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;gBAC5C,qBAAqC,EAAE,KAAK,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;YACzE,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,wBAAwB,CAAC,EAAe;IACtD,2EAA2E;IAC3E,gFAAgF;IAChF,gCAAgC;IAChC,IAAI,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC;QAAE,OAAO;IAEhD,2EAA2E;IAC3E,+DAA+D;IAC/D,MAAM,gBAAgB,GAAG,EAAE,CAAC,aAAa,CAAc,aAAa,CAAC,CAAC;IACtE,IAAI,gBAAgB,EAAE,CAAC;QACrB,gBAAgB,CAAC,KAAK,EAAE,CAAC;QACzB,OAAO;IACT,CAAC;IAED,MAAM,wBAAwB,GAAG,iBAAiB,CAAC,EAAE,CAAC,CAAC;IACvD,IAAI,wBAAwB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxC,wBAAwB,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;QACpC,OAAO;IACT,CAAC;IAED,yEAAyE;IACzE,EAAE,CAAC,KAAK,EAAE,CAAC;AACb,CAAC","sourcesContent":["// Borrowed from Bootstrap:\n// https://github.com/twbs/bootstrap/blob/5f75413735d8779aeefe0097af9dc5a416208ae5/js/src/dom/selector-engine.js#L67\nconst FOCUSABLE_SELECTOR = [\n 'a',\n 'button',\n 'input',\n 'textarea',\n 'select',\n 'details',\n '[tabindex]',\n '[contenteditable=\"true\"]',\n]\n .map((selector) => `${selector}:not([tabindex^=\"-\"]):not(.btn-close)`)\n .join(',');\n\nfunction isElement(object: Element) {\n return object?.nodeType !== undefined;\n}\n\nfunction isVisible(element: Element) {\n if (!isElement(element) || element.getClientRects().length === 0) {\n return false;\n }\n\n const elementIsVisible = getComputedStyle(element).getPropertyValue('visibility') === 'visible';\n // Handle `details` element as its content may falsly appear visible when it is closed\n const closedDetails = element.closest('details:not([open])');\n\n if (!closedDetails) {\n return elementIsVisible;\n }\n\n if (closedDetails !== element) {\n const summary = element.closest('summary');\n if (summary && summary.parentNode !== closedDetails) {\n return false;\n }\n\n if (summary === null) {\n return false;\n }\n }\n\n return elementIsVisible;\n}\n\nfunction isDisabled(element: Element) {\n if (!element || element.nodeType !== Node.ELEMENT_NODE) {\n return true;\n }\n\n if ((element as any).disabled !== undefined) {\n return (element as any).disabled;\n }\n\n return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false';\n}\n\nfunction focusableChildren(element: Element): HTMLElement[] {\n const focusableChildren = element.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR);\n return Array.from(focusableChildren).filter((child) => !isDisabled(child) && isVisible(child));\n}\n\nexport interface FocusTrap {\n deactivate(): void;\n}\n\nexport function trapFocus(element: Element): FocusTrap {\n // Store the previous active element so we can restore it later.\n const previousActiveElement = document.activeElement ?? document.body;\n\n function keyDown(e: KeyboardEvent) {\n if (e.key !== 'Tab') return;\n\n const focusable = focusableChildren(element);\n const firstFocusable = focusable[0];\n const lastFocusable = focusable[focusable.length - 1];\n\n if (e.shiftKey) {\n // Tabbing backwards\n if (document.activeElement === firstFocusable) {\n (focusable[focusable.length - 1] as HTMLElement).focus();\n e.preventDefault();\n }\n } else {\n // Tabbing forwards\n if (document.activeElement === lastFocusable) {\n (focusable[0] as HTMLElement).focus();\n e.preventDefault();\n }\n }\n }\n\n document.addEventListener('keydown', keyDown);\n\n return {\n deactivate() {\n document.removeEventListener('keydown', keyDown);\n // Restore focus to the previously active element, but only if focus is\n // currently inside the trap container.\n if (element.contains(document.activeElement)) {\n (previousActiveElement as HTMLElement)?.focus({ preventScroll: true });\n }\n },\n };\n}\n\nexport function focusFirstFocusableChild(el: HTMLElement) {\n // In case the user (or more frequently, Cypress) is too fast and focuses a\n // specific element inside the container before this script runs, don't transfer\n // focus to a different element.\n if (el.contains(document.activeElement)) return;\n\n // Escape hatch: if the first element isn't the one that should be focused,\n // add the `autofocus` attribute to the element that should be.\n const autofocusElement = el.querySelector<HTMLElement>('[autofocus]');\n if (autofocusElement) {\n autofocusElement.focus();\n return;\n }\n\n const focusablePopoverChildren = focusableChildren(el);\n if (focusablePopoverChildren.length > 0) {\n focusablePopoverChildren[0].focus();\n return;\n }\n\n // If we still couldn't find a child element, focus the container itself.\n el.focus();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"focus.js","sourceRoot":"","sources":["../src/focus.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,oHAAoH;AACpH,MAAM,kBAAkB,GAAG;IACzB,GAAG;IACH,QAAQ;IACR,OAAO;IACP,UAAU;IACV,QAAQ;IACR,SAAS;IACT,YAAY;IACZ,0BAA0B;CAC3B;KACE,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,GAAG,QAAQ,uCAAuC,CAAC;KACrE,IAAI,CAAC,GAAG,CAAC,CAAC;AAEb,SAAS,SAAS,CAAC,MAAe;IAChC,OAAO,MAAM,EAAE,QAAQ,KAAK,SAAS,CAAC;AACxC,CAAC;AAED,SAAS,SAAS,CAAC,OAAgB;IACjC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjE,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC,gBAAgB,CAAC,YAAY,CAAC,KAAK,SAAS,CAAC;IAChG,sFAAsF;IACtF,MAAM,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAE7D,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,gBAAgB,CAAC;IAC1B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QAAQ,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACvC,IAAI,QAAQ,CAAC,OAAO,KAAK,OAAO,IAAI,QAAQ,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC;QACjE,MAAM,MAAM,GAAG,QAA4B,CAAC;QAC5C,MAAM,MAAM,GAAG,QAA4B,CAAC;QAC5C,oFAAoF;QACpF,IACE,MAAM,CAAC,IAAI,KAAK,OAAO;YACvB,MAAM,CAAC,IAAI,KAAK,OAAO;YACvB,oFAAoF;YACpF,MAAM,CAAC,IAAI;YACX,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI;YAC3B,uDAAuD;YACvD,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,EAC3B,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["// Borrowed from Bootstrap:\n// https://github.com/twbs/bootstrap/blob/5f75413735d8779aeefe0097af9dc5a416208ae5/js/src/dom/selector-engine.js#L67\nconst FOCUSABLE_SELECTOR = [\n 'a',\n 'button',\n 'input',\n 'textarea',\n 'select',\n 'details',\n '[tabindex]',\n '[contenteditable=\"true\"]',\n]\n .map((selector) => `${selector}:not([tabindex^=\"-\"]):not(.btn-close)`)\n .join(',');\n\nfunction isElement(object: Element) {\n return object?.nodeType !== undefined;\n}\n\nfunction isVisible(element: Element) {\n if (!isElement(element) || element.getClientRects().length === 0) {\n return false;\n }\n\n const elementIsVisible = getComputedStyle(element).getPropertyValue('visibility') === 'visible';\n // Handle `details` element as its content may falsly appear visible when it is closed\n const closedDetails = element.closest('details:not([open])');\n\n if (!closedDetails) {\n return elementIsVisible;\n }\n\n if (closedDetails !== element) {\n const summary = element.closest('summary');\n if (summary && summary.parentNode !== closedDetails) {\n return false;\n }\n\n if (summary === null) {\n return false;\n }\n }\n\n return elementIsVisible;\n}\n\nfunction isDisabled(element: Element) {\n if (!element || element.nodeType !== Node.ELEMENT_NODE) {\n return true;\n }\n\n if ((element as any).disabled !== undefined) {\n return (element as any).disabled;\n }\n\n return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false';\n}\n\nfunction focusableChildren(element: Element): HTMLElement[] {\n const focusableChildren = element.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR);\n return Array.from(focusableChildren).filter((child) => !isDisabled(child) && isVisible(child));\n}\n\nexport interface FocusTrap {\n deactivate(): void;\n}\n\nexport function trapFocus(element: Element): FocusTrap {\n // Store the previous active element so we can restore it later.\n const previousActiveElement = document.activeElement ?? document.body;\n\n function keyDown(e: KeyboardEvent) {\n if (e.key !== 'Tab') return;\n\n const focusable = focusableChildren(element);\n const firstFocusable = focusable[0];\n const lastFocusable = focusable[focusable.length - 1];\n\n if (e.shiftKey) {\n // Tabbing backwards\n if (isSameFocusContext(document.activeElement, firstFocusable)) {\n focusElementOrCheckedOption(focusable[focusable.length - 1]);\n e.preventDefault();\n }\n } else {\n // Tabbing forwards\n if (isSameFocusContext(document.activeElement, lastFocusable)) {\n focusElementOrCheckedOption(focusable[0]);\n e.preventDefault();\n }\n }\n }\n\n document.addEventListener('keydown', keyDown);\n\n return {\n deactivate() {\n document.removeEventListener('keydown', keyDown);\n // Restore focus to the previously active element, but only if focus is\n // currently inside the trap container.\n if (element.contains(document.activeElement)) {\n focusElementOrCheckedOption(previousActiveElement as HTMLElement, { preventScroll: true });\n }\n },\n };\n}\n\nexport function focusFirstFocusableChild(el: HTMLElement) {\n // In case the user (or more frequently, Cypress) is too fast and focuses a\n // specific element inside the container before this script runs, don't transfer\n // focus to a different element.\n if (el.contains(document.activeElement)) return;\n\n // Escape hatch: if the first element isn't the one that should be focused,\n // add the `autofocus` attribute to the element that should be.\n const autofocusElement = el.querySelector<HTMLElement>('[autofocus]');\n if (autofocusElement) {\n autofocusElement.focus();\n return;\n }\n\n const focusablePopoverChildren = focusableChildren(el);\n if (focusablePopoverChildren.length > 0) {\n focusElementOrCheckedOption(focusablePopoverChildren[0]);\n return;\n }\n\n // If we still couldn't find a child element, focus the container itself.\n el.focus();\n}\n\n/**\n * Focus on the element, or if it's a radio button, focus on the checked radio button in the same group.\n */\nfunction focusElementOrCheckedOption(element: HTMLElement, focusOptions?: FocusOptions) {\n // If the element receiving focus is a radio button, and there is another\n // radio button in the same group that is currently checked, focus on that one\n // instead.\n // https://www.w3.org/WAI/ARIA/apg/patterns/radio/\n if (element.tagName === 'INPUT') {\n const inputElement = element as HTMLInputElement;\n if (inputElement.type === 'radio' && inputElement.name) {\n const checkedRadio = (inputElement.form ?? document).querySelector<HTMLInputElement>(\n `input[type=\"radio\"][name=\"${CSS.escape(inputElement.name)}\"]:checked`,\n );\n if (checkedRadio) {\n checkedRadio.focus(focusOptions);\n return;\n }\n }\n }\n\n // Otherwise, just focus on the element itself.\n element.focus(focusOptions);\n}\n\n/**\n * Check if two elements are in the same focus context. This is true if they are the\n * same element, or if they are radio buttons in the same group.\n */\nfunction isSameFocusContext(element1: Element | null, element2: Element | null) {\n if (!element1 || !element2) return false;\n if (element1 === element2) return true;\n if (element1.tagName === 'INPUT' && element2.tagName === 'INPUT') {\n const input1 = element1 as HTMLInputElement;\n const input2 = element2 as HTMLInputElement;\n // https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)\n if (\n input1.type === 'radio' &&\n input2.type === 'radio' &&\n // radios without a name are not in the same group, even if they share the same form\n input1.name &&\n input1.name === input2.name &&\n // either both have no form, or both have the same form\n input1.form === input2.form\n ) {\n return true;\n }\n }\n return false;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { parseHTML, parseHTMLElement } from './parse-html.js';
|
|
|
3
3
|
export { EncodedData, decodeData } from './encode-data.js';
|
|
4
4
|
export { templateFromAttributes } from './template-from-attributes.js';
|
|
5
5
|
export { trapFocus, focusFirstFocusableChild } from './focus.js';
|
|
6
|
+
export { downloadAsCSV, downloadAsJSON } from './downloads.js';
|
package/dist/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export { parseHTML, parseHTMLElement } from './parse-html.js';
|
|
|
3
3
|
export { EncodedData, decodeData } from './encode-data.js';
|
|
4
4
|
export { templateFromAttributes } from './template-from-attributes.js';
|
|
5
5
|
export { trapFocus, focusFirstFocusableChild } from './focus.js';
|
|
6
|
+
export { downloadAsCSV, downloadAsJSON } from './downloads.js';
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { onDocumentReady } from './on-document-ready.js';\nexport { parseHTML, parseHTMLElement } from './parse-html.js';\nexport { EncodedData, decodeData } from './encode-data.js';\nexport { templateFromAttributes } from './template-from-attributes.js';\nexport { trapFocus, focusFirstFocusableChild } from './focus.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { onDocumentReady } from './on-document-ready.js';\nexport { parseHTML, parseHTMLElement } from './parse-html.js';\nexport { EncodedData, decodeData } from './encode-data.js';\nexport { templateFromAttributes } from './template-from-attributes.js';\nexport { trapFocus, focusFirstFocusableChild } from './focus.js';\nexport { downloadAsCSV, downloadAsJSON } from './downloads.js';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/browser-utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@prairielearn/html": "^4.0.20",
|
|
17
|
+
"csv-stringify": "^6.6.0",
|
|
17
18
|
"js-base64": "^3.7.8"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
package/src/downloads.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { stringify } from 'csv-stringify/browser/esm/sync';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Triggers a browser download of a text-based file.
|
|
5
|
+
*
|
|
6
|
+
* @param content The content of the file.
|
|
7
|
+
* @param filename The desired filename.
|
|
8
|
+
* @param mimeType The MIME type of the file.
|
|
9
|
+
*/
|
|
10
|
+
export function downloadTextFile(content: string, filename: string, mimeType: string): void {
|
|
11
|
+
if (typeof window === 'undefined') return;
|
|
12
|
+
const blob = new Blob([content], { type: mimeType });
|
|
13
|
+
const url = URL.createObjectURL(blob);
|
|
14
|
+
const a = document.createElement('a');
|
|
15
|
+
a.href = url;
|
|
16
|
+
a.download = filename;
|
|
17
|
+
document.body.append(a);
|
|
18
|
+
a.click();
|
|
19
|
+
a.remove();
|
|
20
|
+
URL.revokeObjectURL(url);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Triggers a browser download of a JSON file.
|
|
25
|
+
*
|
|
26
|
+
* @param data The data to be included in the JSON file.
|
|
27
|
+
* @param filename The desired filename.
|
|
28
|
+
*/
|
|
29
|
+
export function downloadAsJSON(data: any, filename: string): void {
|
|
30
|
+
const jsonContent = JSON.stringify(data, null, 2);
|
|
31
|
+
downloadTextFile(jsonContent, filename, 'application/json');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Triggers a browser download of a CSV file.
|
|
36
|
+
*
|
|
37
|
+
* @param header The header row of the CSV.
|
|
38
|
+
* @param data The data rows of the CSV.
|
|
39
|
+
* @param filename The desired filename.
|
|
40
|
+
*/
|
|
41
|
+
export function downloadAsCSV(header: string[], data: unknown[][], filename: string): void {
|
|
42
|
+
const csvContent = stringify(data, { header: true, columns: header });
|
|
43
|
+
downloadTextFile(csvContent, filename, 'text/csv');
|
|
44
|
+
}
|
package/src/focus.ts
CHANGED
|
@@ -78,14 +78,14 @@ export function trapFocus(element: Element): FocusTrap {
|
|
|
78
78
|
|
|
79
79
|
if (e.shiftKey) {
|
|
80
80
|
// Tabbing backwards
|
|
81
|
-
if (document.activeElement
|
|
82
|
-
(focusable[focusable.length - 1]
|
|
81
|
+
if (isSameFocusContext(document.activeElement, firstFocusable)) {
|
|
82
|
+
focusElementOrCheckedOption(focusable[focusable.length - 1]);
|
|
83
83
|
e.preventDefault();
|
|
84
84
|
}
|
|
85
85
|
} else {
|
|
86
86
|
// Tabbing forwards
|
|
87
|
-
if (document.activeElement
|
|
88
|
-
(focusable[0]
|
|
87
|
+
if (isSameFocusContext(document.activeElement, lastFocusable)) {
|
|
88
|
+
focusElementOrCheckedOption(focusable[0]);
|
|
89
89
|
e.preventDefault();
|
|
90
90
|
}
|
|
91
91
|
}
|
|
@@ -99,7 +99,7 @@ export function trapFocus(element: Element): FocusTrap {
|
|
|
99
99
|
// Restore focus to the previously active element, but only if focus is
|
|
100
100
|
// currently inside the trap container.
|
|
101
101
|
if (element.contains(document.activeElement)) {
|
|
102
|
-
(previousActiveElement as HTMLElement
|
|
102
|
+
focusElementOrCheckedOption(previousActiveElement as HTMLElement, { preventScroll: true });
|
|
103
103
|
}
|
|
104
104
|
},
|
|
105
105
|
};
|
|
@@ -121,10 +121,61 @@ export function focusFirstFocusableChild(el: HTMLElement) {
|
|
|
121
121
|
|
|
122
122
|
const focusablePopoverChildren = focusableChildren(el);
|
|
123
123
|
if (focusablePopoverChildren.length > 0) {
|
|
124
|
-
focusablePopoverChildren[0]
|
|
124
|
+
focusElementOrCheckedOption(focusablePopoverChildren[0]);
|
|
125
125
|
return;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
// If we still couldn't find a child element, focus the container itself.
|
|
129
129
|
el.focus();
|
|
130
130
|
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Focus on the element, or if it's a radio button, focus on the checked radio button in the same group.
|
|
134
|
+
*/
|
|
135
|
+
function focusElementOrCheckedOption(element: HTMLElement, focusOptions?: FocusOptions) {
|
|
136
|
+
// If the element receiving focus is a radio button, and there is another
|
|
137
|
+
// radio button in the same group that is currently checked, focus on that one
|
|
138
|
+
// instead.
|
|
139
|
+
// https://www.w3.org/WAI/ARIA/apg/patterns/radio/
|
|
140
|
+
if (element.tagName === 'INPUT') {
|
|
141
|
+
const inputElement = element as HTMLInputElement;
|
|
142
|
+
if (inputElement.type === 'radio' && inputElement.name) {
|
|
143
|
+
const checkedRadio = (inputElement.form ?? document).querySelector<HTMLInputElement>(
|
|
144
|
+
`input[type="radio"][name="${CSS.escape(inputElement.name)}"]:checked`,
|
|
145
|
+
);
|
|
146
|
+
if (checkedRadio) {
|
|
147
|
+
checkedRadio.focus(focusOptions);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Otherwise, just focus on the element itself.
|
|
154
|
+
element.focus(focusOptions);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Check if two elements are in the same focus context. This is true if they are the
|
|
159
|
+
* same element, or if they are radio buttons in the same group.
|
|
160
|
+
*/
|
|
161
|
+
function isSameFocusContext(element1: Element | null, element2: Element | null) {
|
|
162
|
+
if (!element1 || !element2) return false;
|
|
163
|
+
if (element1 === element2) return true;
|
|
164
|
+
if (element1.tagName === 'INPUT' && element2.tagName === 'INPUT') {
|
|
165
|
+
const input1 = element1 as HTMLInputElement;
|
|
166
|
+
const input2 = element2 as HTMLInputElement;
|
|
167
|
+
// https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
|
|
168
|
+
if (
|
|
169
|
+
input1.type === 'radio' &&
|
|
170
|
+
input2.type === 'radio' &&
|
|
171
|
+
// radios without a name are not in the same group, even if they share the same form
|
|
172
|
+
input1.name &&
|
|
173
|
+
input1.name === input2.name &&
|
|
174
|
+
// either both have no form, or both have the same form
|
|
175
|
+
input1.form === input2.form
|
|
176
|
+
) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { parseHTML, parseHTMLElement } from './parse-html.js';
|
|
|
3
3
|
export { EncodedData, decodeData } from './encode-data.js';
|
|
4
4
|
export { templateFromAttributes } from './template-from-attributes.js';
|
|
5
5
|
export { trapFocus, focusFirstFocusableChild } from './focus.js';
|
|
6
|
+
export { downloadAsCSV, downloadAsJSON } from './downloads.js';
|