@prairielearn/browser-utils 2.4.0 → 2.5.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/CHANGELOG.md +14 -0
- package/dist/downloads.d.ts +1 -0
- package/dist/downloads.d.ts.map +1 -0
- package/dist/encode-data.d.ts +1 -0
- package/dist/encode-data.d.ts.map +1 -0
- package/dist/focus.d.ts +1 -0
- package/dist/focus.d.ts.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.d.ts.map +1 -0
- package/dist/on-document-ready.d.ts +1 -0
- package/dist/on-document-ready.d.ts.map +1 -0
- package/dist/parse-html.d.ts +1 -0
- package/dist/parse-html.d.ts.map +1 -0
- package/dist/template-from-attributes.d.ts +1 -0
- package/dist/template-from-attributes.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/focus.ts +57 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @prairielearn/browser-utils
|
|
2
2
|
|
|
3
|
+
## 2.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c0b1c74: Enable `declarationMap`
|
|
8
|
+
- Updated dependencies [c0b1c74]
|
|
9
|
+
- @prairielearn/html@4.0.21
|
|
10
|
+
|
|
11
|
+
## 2.5.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- 19f0bf7: Improved support for focus on radio groups
|
|
16
|
+
|
|
3
17
|
## 2.4.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/dist/downloads.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"downloads.d.ts","sourceRoot":"","sources":["../src/downloads.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAW1F;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAGhE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAGzF"}
|
package/dist/encode-data.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-data.d.ts","sourceRoot":"","sources":["../src/encode-data.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,cAAc,EAAoB,MAAM,oBAAoB,CAAC;AAE3E;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,cAAc,CAKnF;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,CAAC,CAQ5D"}
|
package/dist/focus.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focus.d.ts","sourceRoot":"","sources":["../src/focus.ts"],"names":[],"mappings":"AA+DA,MAAM,WAAW,SAAS;IACxB,UAAU,IAAI,IAAI,CAAC;CACpB;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,CAsCrD;AAED,wBAAgB,wBAAwB,CAAC,EAAE,EAAE,WAAW,QAsBvD"}
|
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;IAC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KAAK,EAAE,CAAC;gBACtC,CAAC,CAAC,cAAc,EAAE,CAAC;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAE9C,OAAO;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,SAA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,CAAC;AAC9B,CAAC;AAED;;;GAGG;AACH,SAAS,kBAAkB,CAAC,QAAwB,EAAE,QAAwB;IAC5E,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IACzC,IAAI,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
|
@@ -4,3 +4,4 @@ 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
6
|
export { downloadAsCSV, downloadAsJSON } from './downloads.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"on-document-ready.d.ts","sourceRoot":"","sources":["../src/on-document-ready.ts"],"names":[],"mappings":"AAAA,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,IAAI,CAQpD"}
|
package/dist/parse-html.d.ts
CHANGED
|
@@ -6,3 +6,4 @@ export declare function parseHTML(document: Document, html: string | HtmlSafeStr
|
|
|
6
6
|
* exactly one element, an error is thrown.
|
|
7
7
|
*/
|
|
8
8
|
export declare function parseHTMLElement<T extends Element = Element>(document: Document, html: string | HtmlSafeString): T;
|
|
9
|
+
//# sourceMappingURL=parse-html.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-html.d.ts","sourceRoot":"","sources":["../src/parse-html.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEzD,wBAAgB,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,GAAG,gBAAgB,CAK7F;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAC1D,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,GAAG,cAAc,GAC5B,CAAC,CAMH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template-from-attributes.d.ts","sourceRoot":"","sources":["../src/template-from-attributes.ts"],"names":[],"mappings":"AAAA,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE3C;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,EACnB,UAAU,EAAE,YAAY,QAyCzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/browser-utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"dev": "tsc --watch --preserveWatchOutput"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@prairielearn/html": "^4.0.
|
|
16
|
+
"@prairielearn/html": "^4.0.21",
|
|
17
17
|
"csv-stringify": "^6.6.0",
|
|
18
18
|
"js-base64": "^3.7.8"
|
|
19
19
|
},
|
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
|
+
}
|