semantic-forms 5.3.16 → 5.3.18
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 +29 -0
- package/dist/semantic-forms.cjs +59 -21
- package/dist/semantic-forms.cjs.map +1 -1
- package/dist/semantic-forms.css +597 -357
- package/dist/semantic-forms.css.map +1 -1
- package/dist/semantic-forms.js +59 -21
- package/dist/semantic-forms.js.map +1 -1
- package/dist/semantic-forms.min.js +1 -1
- package/dist/semantic-forms.min.js.map +1 -1
- package/dist/semantic-forms.min.mjs +1 -1
- package/dist/semantic-forms.min.mjs.map +1 -1
- package/dist/semantic-forms.mjs +59 -21
- package/dist/semantic-forms.mjs.map +1 -1
- package/docs/statics/css/semantic-forms-styles.scss +6 -2
- package/docs/statics/js/semantic-forms-demo.js +0 -4
- package/docs/statics/js/semantic-forms-main.js +13 -3
- package/docs/statics/pages/buttons.html +8 -0
- package/docs/statics/pages/fullDemo.html +89 -0
- package/docs/statics/pages/inputs.html +5 -3
- package/docs/statics/pages/layout.html +77 -8
- package/docs/statics/pages/layouts/main.html +2 -0
- package/docs/statics/pages/theming.html +17 -1
- package/docs/statics/pages/usage.html +19 -0
- package/package.json +7 -5
- package/semanticForms.js +18 -4
- package/semanticForms.scss +19 -0
- package/styles/colspan.scss +316 -0
- package/styles/globals.scss +202 -0
- package/styles/inputs.scss +473 -0
- package/styles/labels.scss +316 -0
- package/styles/layoutScope.scss +4 -0
- package/styles/lowFlow.scss +166 -0
- package/styles/tabs.scss +181 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
## 5.3.18
|
|
2
|
+
|
|
3
|
+
- Added vertical tabs. Put the `vertical` class on a `tabs` group to show the tabs in a column beside the panels.
|
|
4
|
+
- Fixed `--semanticFormsFocusColor` to meet the 3:1 contrast WCAG asks of a focus indicator.
|
|
5
|
+
- Fixed every animation to respect `prefers-reduced-motion`, rather than only the switch.
|
|
6
|
+
- Fixed autofilled fields to stay marked as autofilled.
|
|
7
|
+
- Fixed `aria-invalid` not being set on a field the stylesheet outlines on `:user-invalid`, so a field the browser considers invalid said nothing to a screen reader unless `setInvalid` had marked it.
|
|
8
|
+
- Fixed a help icon's text not reaching a screen reader on a group of checkboxes or radios, where the title sits on the group's label and describes none of the fields.
|
|
9
|
+
- Fixed the documentation site having no `color-scheme` meta tag, which made a visitor whose system is dark get a white page before the stylesheets arrived, and not following the system's light or dark setting when it changed while a page was open.
|
|
10
|
+
- Fixed `semanticForms.scss` not compiling from the npm package, since the `styles` folder it pulls its partials from was left out of the package. It's now included, and the package declares its Sass entry point, so a Sass stylesheet can `@use "pkg:semantic-forms"` with Sass's Node.js package importer.
|
|
11
|
+
- Updated dependencies.
|
|
12
|
+
|
|
13
|
+
## 5.3.17
|
|
14
|
+
|
|
15
|
+
- Added `semanticForms.setInvalid(field, message, options)` and `semanticForms.clearInvalid(field)`, which mark a field invalid from your own code with a message saying why.
|
|
16
|
+
- Added a `semanticFormsButton` class, which styles a button outside a form like the ones in one.
|
|
17
|
+
- Added a `--semanticFormsCursor` variable, for the pointer over buttons, checkboxes, selects and the like.
|
|
18
|
+
- Added a `data-display-format` attribute for ranges with `data-display-value`, with `{value}` standing for the value, such as `{value}%`. In a `customLayout` element, a range with `data-display-value` now keeps the `<output>` for it up to date.
|
|
19
|
+
- Altered `--semanticFormsPageBgColor` to now set on the page's root element too, so the page can be painted with it. Forms in dark mode now set `color-scheme: dark` so things like date pickers and scrollbars are drawn dark.
|
|
20
|
+
- Added a `data-semantic-forms-clear` attribute for the clear button. And clearing a field now fires a `change` event as well as an `input` event.
|
|
21
|
+
- Fixed a keyboard listener being added every time a form was added to the page, since the enhancement runs again for each one. There's one for all of them now.
|
|
22
|
+
- Fixed a form inside a `<dialog>` or a popover, or one holding only a button, shrinking to nothing.
|
|
23
|
+
- Fixed a field without an id not having its control enhanced, so a switch without one wasn't given its role or dragging.
|
|
24
|
+
- Fixed a field whose id has characters a selector gives a meaning to, such as `tag:title`, stopping the enhancement of that field and every field after it.
|
|
25
|
+
- Fixed a help icon or required asterisk whose title holds quotes or markup breaking the label.
|
|
26
|
+
- Fixed `data-auto-grow` dropping the field's name, class, and other attributes when it turns an input into a textarea.
|
|
27
|
+
- Fixed enhancing a textarea firing an `input` event on it, which an app would take as the user having changed it.
|
|
28
|
+
- Updated dependencies.
|
|
29
|
+
|
|
1
30
|
## 5.3.16
|
|
2
31
|
|
|
3
32
|
- Fixed the search icon still looking jagged at 100% scale. It was drawn on a 24 unit grid but shown at 18px, so its 1.5px lines and its circle fell between pixels. It's now drawn on an 18 unit grid, one unit to a pixel, with 2px lines placed on whole pixels, which makes it crisp and a little bolder.
|
package/dist/semantic-forms.cjs
CHANGED
|
@@ -45,6 +45,18 @@ const waiting=new Map;let observer;const setInkOffset=(form,label)=>{if(!window.
|
|
|
45
45
|
/***/"./lib/inputEnhancements.js"(module,__unused_webpack_exports,__webpack_require__){const{insertAfter}=__webpack_require__("./lib/helpers.js");const icons={passwordShow:'<svg fill="none" height="256" viewBox="0 0 24 24" width="256" xmlns="http://www.w3.org/2000/svg"><g stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"><path d="m1 12s4-8 11-8 11 8 11 8"/><path d="m1 12s4 8 11 8 11-8 11-8"/><circle cx="12" cy="12" r="3"/></g></svg>',passwordHide:'<svg fill="none" height="256" viewBox="0 0 24 24" width="256" xmlns="http://www.w3.org/2000/svg"><g stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"><path d="m2 2 20 20"/><path d="m6.71277 6.7226c-3.04798 2.07267-4.71277 5.2774-4.71277 5.2774s3.63636 7 10 7c2.0503 0 3.8174-.7266 5.2711-1.7116m-6.2711-12.23018c.3254-.03809.6588-.05822 1-.05822 6.3636 0 10 7 10 7s-.6918 1.3317-2 2.8335"/><path d="m14 14.2362c-.5308.475-1.2316.7639-2 .7639-1.6569 0-3-1.3431-3-3 0-.8237.33193-1.5698.86932-2.11192"/></g></svg>',helpIcon:'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16" version="1.1" style="overflow:visible;"><rect width="16" height="16" fill="none"/><path fill="currentColor" d="M8,10.5c-0.552,0-1,0.448-1,1c0,0.552,0.448,1,1,1c0.552,0,1-0.448,1-1C9,10.948,8.552,10.5,8,10.5z M8,0 C3.582,0,0,3.582,0,8c0,4.418,3.582,8,8,8s8-3.582,8-8C16,3.582,12.418,0,8,0z M12.243,12.243C11.109,13.376,9.603,14,8,14 s-3.109-0.624-4.243-1.757C2.624,11.109,2,9.603,2,8s0.624-3.109,1.757-4.243C4.891,2.624,6.397,2,8,2s3.109,0.624,4.243,1.757 C13.376,4.891,14,6.397,14,8C14,9.603,13.376,11.109,12.243,12.243z M7.927,3.755c-2.248,0-2.76,1.695-2.802,2.906h1.571 c0.028-0.256,0.101-1.418,1.221-1.418c0.672,0,1.206,0.406,1.206,1.175c0,0.695-0.724,1.155-1.303,1.711 C7.241,8.685,7.168,9.325,7.168,9.824V10c0.146,0,1.615,0,1.615,0V9.824c0-1.256,1.967-1.594,1.967-3.541 C10.75,5.059,9.874,3.755,7.927,3.755z"/></svg>'};const lookups={nodeName:["TEXTAREA","SELECT"],inputType:["checkbox","color","date","datetime-local","email","file","image","month","number","password","radio","range","search","tel","text","time","url","week"]};let lastFocusedInput;let lastClearFieldPressed;
|
|
46
46
|
// browsers that implement the switch attribute expose it on the input prototype, render the control as a switch and report the switch role themselves, so nothing below is needed there
|
|
47
47
|
const nativeSwitchSupport="switch"in window.HTMLInputElement.prototype;
|
|
48
|
+
// the stylesheet outlines a field on :user-invalid, which is the browser saying the visitor has had their turn with the field and what they left behind does not pass. aria-invalid has to say the same thing, or the outline is a message only someone looking at the screen receives
|
|
49
|
+
|
|
50
|
+
// it is read back from that selector rather than worked out again from blur events, which is how it is usually done and gets it wrong: tabbing straight through an empty required field is not a mistake the visitor has made yet, and :user-invalid already knows that
|
|
51
|
+
const supportsUserInvalid=(()=>{try{return window.CSS.supports("selector(:user-invalid)")}catch{return false}})();
|
|
52
|
+
// while setInvalid has marked a field, its message owns aria-invalid and this leaves the attribute alone
|
|
53
|
+
const syncAriaInvalid=input=>{if(!supportsUserInvalid||input.classList.contains("invalid"))return;if(input.matches(":user-invalid"))input.setAttribute("aria-invalid","true");else input.removeAttribute("aria-invalid")};
|
|
54
|
+
// blur and the invalid event are when the browser's answer can start being yes; input and change are when the visitor puts it right again
|
|
55
|
+
const trackUserInvalid=input=>{if(!supportsUserInvalid)return;const sync=()=>syncAriaInvalid(input);for(const name of["blur","input","change","invalid"])input.addEventListener(name,sync)};
|
|
56
|
+
// puts a field's help text where a screen reader will read it, which a title attribute does not do for a group of checkboxes or radios, and points the field at it
|
|
57
|
+
|
|
58
|
+
// the text is not drawn on screen: it stands in for the title the help icon carries, which only a mouse hovering the icon can reach
|
|
59
|
+
let helpTextCount=0;const describe=(inputs,text)=>{const host=inputs[0]?.closest("dd");if(!host)return;const note=document.createElement("span");note.className="semanticFormsVisuallyHidden";note.id=`semanticFormsHelpText_${++helpTextCount}`;note.textContent=text;host.append(note);for(const input of inputs){const describedBy=(input.getAttribute("aria-describedby")||"").split(" ").filter(Boolean);if(!describedBy.includes(note.id))input.setAttribute("aria-describedby",[...describedBy,note.id].join(" "))}};
|
|
48
60
|
// lets a polyfilled switch be toggled by dragging its thumb, which is what the native control does. a pointer that never travels far enough is left alone so it still lands as a click.
|
|
49
61
|
const enableSwitchDragging=input=>{input.addEventListener("pointerdown",event=>{if(input.disabled)return;const startX=event.clientX;const rightToLeft=window.getComputedStyle(input).direction==="rtl";
|
|
50
62
|
// half the track is the distance the thumb travels between the two positions
|
|
@@ -58,11 +70,15 @@ input.addEventListener("click",clickEvent=>clickEvent.preventDefault(),{once:tru
|
|
|
58
70
|
// returns the field, which is the new textarea when one was made
|
|
59
71
|
const enhanceAutoGrow=(input,form)=>{if(input.getAttribute("data-auto-grow")===null)return input;
|
|
60
72
|
// progressively enhance inputs into textareas
|
|
61
|
-
if(input.nodeName==="INPUT"&&input.type==="text"){
|
|
73
|
+
if(input.nodeName==="INPUT"&&input.type==="text"){
|
|
74
|
+
// the textarea takes over everything the input had except what only an input has, so its name, its class, whether it's required, and so on all carry over
|
|
75
|
+
const newInput=document.createElement("textarea");for(const attribute of input.attributes)if(!["type","value","size","list","pattern"].includes(attribute.name))newInput.setAttribute(attribute.name,attribute.value);newInput.value=input.value;input.replaceWith(newInput);input=newInput}if(input.nodeName==="TEXTAREA")
|
|
62
76
|
// when pressing enter while this input is focused, we want to submit
|
|
63
77
|
input.addEventListener("keypress",e=>{if(e.key!=="Enter"||e.key==="Enter"&&e.shiftKey)return;e.preventDefault();form.requestSubmit()});return input};
|
|
64
78
|
// the behavior of the control itself, as opposed to the labels and buttons placed around it: switches and textareas. this is all a field in a customLayout element gets, and every other field gets it too
|
|
65
79
|
const enhanceControl=(input,type,form)=>{
|
|
80
|
+
// what a screen reader is told about the field matches what the stylesheet draws on it
|
|
81
|
+
trackUserInvalid(input);
|
|
66
82
|
// a checkbox with the switch attribute is a switch; the stylesheet draws one in every browser, and where the attribute is not implemented natively the role and dragging are supplied here
|
|
67
83
|
if(type==="checkbox"&&input.hasAttribute("switch")&&!nativeSwitchSupport){input.setAttribute("role","switch");enableSwitchDragging(input)}
|
|
68
84
|
// add listener to shift clear button when scrollbar present
|
|
@@ -85,8 +101,8 @@ input.style.removeProperty("width");input.style.removeProperty("box-sizing");inp
|
|
|
85
101
|
const rows=Math.round((scrollHeight-blockPadding)/lineHeight);
|
|
86
102
|
// set the calculated rows attribute (limited by rowLimit)
|
|
87
103
|
if(maxRows)input.setAttribute("rows",""+Math.min(rows,Number(maxRows)));else input.setAttribute("rows",""+rows)};input.addEventListener("input",handleInput);
|
|
88
|
-
//
|
|
89
|
-
|
|
104
|
+
// set the initial rows value. this calls the sizing directly rather than firing an input event, which would tell the app's own listeners the user had changed the field
|
|
105
|
+
handleInput()}
|
|
90
106
|
// progressively enhance textarea for Firefox and Safari
|
|
91
107
|
// this may be removed once fully supported in Firefox and Safari: https://caniuse.com/wf-field-sizing
|
|
92
108
|
if(input.getAttribute("data-auto-grow")!==null)if(!("fieldSizing"in document.createElement("input").style)){const adjustHeight=()=>{if(input.value.length){const borderWidth=parseInt(window.getComputedStyle(input).getPropertyValue("border-width"));input.style.height=input.scrollHeight+borderWidth*2+"px"}else input.style.height=window.getComputedStyle(form).getPropertyValue("--semanticFormsInputHeight")};
|
|
@@ -95,30 +111,33 @@ input.style.height=window.getComputedStyle(form).getPropertyValue("--semanticFor
|
|
|
95
111
|
// shifts the clear button to the right if a scrollbar is present
|
|
96
112
|
const shiftClearBtn=()=>{const clearBtn=input.parentElement?.querySelector("button.clear");if(clearBtn)clearBtn.style.marginRight=input.clientHeight<input.scrollHeight?"15px":""};input.addEventListener("input",shiftClearBtn);input.addEventListener("mouseup",shiftClearBtn);shiftClearBtn()}};const enhanceInput=(input,form)=>{
|
|
97
113
|
// ignore input if it has previously been formatted
|
|
98
|
-
if(input.classList.contains("semanticform")
|
|
114
|
+
if(input.classList.contains("semanticform"))return;const type=input.getAttribute("type");if(!lookups.nodeName.includes(input.nodeName)&&!lookups.inputType.includes(type))return;
|
|
115
|
+
// a field without an id has no label to float or place buttons beside, but the control itself is still enhanced, such as a switch being given its role and dragging
|
|
116
|
+
if(!input.id){input.classList.add("semanticform");enhanceControl(input,type,form);return}
|
|
99
117
|
// inside a customLayout element the app lays out the <dl> itself, so nothing is added, moved or rewritten around the field: no float label, no clear or show password button, no wrapping <div>, no required asterisk or help icon, and no value appended to the label of a range. the control itself is still enhanced. it is marked like any other enhanced field so that reinitializing does not enhance it twice
|
|
100
|
-
if(input.closest(".customLayout")){input.classList.add("semanticform");
|
|
118
|
+
if(input.closest(".customLayout")){input.classList.add("semanticform");input=enhanceAutoGrow(input,form);enhanceControl(input,type,form);if(type==="range"&&input.getAttribute("data-display-value")!==null)displayRangeValue(input,null);return}
|
|
101
119
|
// recursively find <dl> element
|
|
102
120
|
let dl=input.parentNode;while(dl&&dl.nodeName!=="DL")dl=dl.parentNode;if(!dl)return;if(!dl.classList.contains("floatLabelForm"))dl.classList.add("floatLabelForm");
|
|
103
121
|
// the label is looked up in the input's own tree rather than the document, so that a form inside a shadow root finds its labels instead of coming up empty
|
|
104
|
-
const tree=input.getRootNode();const label=input.parentNode.parentNode.id&&(type==="checkbox"||type==="radio")?tree.querySelector(
|
|
122
|
+
const tree=input.getRootNode();const label=input.parentNode.parentNode.id&&(type==="checkbox"||type==="radio")?tree.querySelector(`label[data-for="${window.CSS.escape(input.parentNode.parentNode.id)}"]`):tree.querySelector(`label[for="${window.CSS.escape(input.id)}"]`);if(!label){console.error(`semantic-forms: Found an input (${input.id||input.getAttribute("name")}) without a properly associated label. Make sure there is a label for it and the label has a matching "for" attribute.`);return}input.classList.add("semanticform");
|
|
105
123
|
// an input outside a <dd> is reported by the checks further down, so tolerate it here
|
|
106
124
|
const isWrapped=input.closest("dd")?.parentElement.nodeName==="DIV";
|
|
107
125
|
// #region create labels
|
|
108
|
-
const newLabel=document.createElement("label");newLabel.className="floatLabelFormAnimatedLabel";if(type==="checkbox"||type==="radio"){const div=isWrapped?input.closest("dd").parentElement:document.createElement("div");const dt=label.closest("dt");const dd=input.closest("dd");if(!dt||!dd){console.error(`semantic-forms: Found an input (${input.id||input.getAttribute("name")}) that is not inside a <dd> element.`);return}if(dd.firstChild.nodeName!=="LABEL"){if(type==="checkbox"&&input.parentNode.nodeName==="DD"){newLabel.setAttribute("for",input.id);input.parentNode.classList.add("singleCheckbox");newLabel.className="";label.setAttribute("hidden","hidden");insertAfter(newLabel,input)}if(type==="radio"&&input.parentNode.nodeName==="DD"){newLabel.setAttribute("for",input.id);input.parentNode.classList.add("singleRadio");newLabel.className="";label.setAttribute("hidden","hidden");insertAfter(newLabel,input)}newLabel.innerHTML=label.innerHTML;if(label.hasAttribute("title")&&label.getAttribute("data-show-help-icon")!==null&&!label.querySelector("span.help")){const text=label.getAttribute("title");label
|
|
126
|
+
const newLabel=document.createElement("label");newLabel.className="floatLabelFormAnimatedLabel";if(type==="checkbox"||type==="radio"){const div=isWrapped?input.closest("dd").parentElement:document.createElement("div");const dt=label.closest("dt");const dd=input.closest("dd");if(!dt||!dd){console.error(`semantic-forms: Found an input (${input.id||input.getAttribute("name")}) that is not inside a <dd> element.`);return}if(dd.firstChild.nodeName!=="LABEL"){if(type==="checkbox"&&input.parentNode.nodeName==="DD"){newLabel.setAttribute("for",input.id);input.parentNode.classList.add("singleCheckbox");newLabel.className="";label.setAttribute("hidden","hidden");insertAfter(newLabel,input)}if(type==="radio"&&input.parentNode.nodeName==="DD"){newLabel.setAttribute("for",input.id);input.parentNode.classList.add("singleRadio");newLabel.className="";label.setAttribute("hidden","hidden");insertAfter(newLabel,input)}newLabel.innerHTML=label.innerHTML;if(label.hasAttribute("title")&&label.getAttribute("data-show-help-icon")!==null&&!label.querySelector("span.help")){const text=label.getAttribute("title");appendBadge(label,"help",text,icons.helpIcon);appendBadge(newLabel,"help",text,icons.helpIcon);describe(dd.querySelectorAll("input"),text)}if(dd.querySelector(":required")&&label.getAttribute("data-no-asterisk")===null&&!label.querySelector("span.required")){const text=label.getAttribute("data-asterisk-text")||"This field is required.";appendBadge(label,"required",text,"*");appendBadge(newLabel,"required",text,"*")}if(!dd.querySelector("label"))dd.append(newLabel)}
|
|
109
127
|
// if the user did not wrap the input in a div, do it for them
|
|
110
128
|
if(!isWrapped){
|
|
111
129
|
// the wrapper goes where the <dt> was, so the field keeps its place in the markup. a div the author wrote is already in the right place and is deliberately left where it is: moving it would push the field past anything the enhancer does not touch, such as a <dd> holding only a button
|
|
112
130
|
dt.before(div);div.append(dt,dd);
|
|
113
131
|
// determine visibility of newly created <div>
|
|
114
|
-
if(dt.style.display==="none"&&dd.style.display==="none")div.style.display="none"}}else{newLabel.setAttribute("for",input.id);newLabel.innerHTML=label.innerHTML;if(input.hasAttribute("title")&&label.getAttribute("data-show-help-icon")!==null&&!label.querySelector("span.help")){
|
|
132
|
+
if(dt.style.display==="none"&&dd.style.display==="none")div.style.display="none"}}else{newLabel.setAttribute("for",input.id);newLabel.innerHTML=label.innerHTML;if(input.hasAttribute("title")&&label.getAttribute("data-show-help-icon")!==null&&!label.querySelector("span.help")){appendBadge(newLabel,"help",input.getAttribute("title"),icons.helpIcon);describe([input],input.getAttribute("title"))}if(input.hasAttribute("required")&&label.getAttribute("data-no-asterisk")===null&&!label.querySelector("span.required"))appendBadge(newLabel,"required",label.getAttribute("data-asterisk-text")||"This field is required.","*");label.setAttribute("hidden","hidden");insertAfter(newLabel,input)}
|
|
115
133
|
// #endregion
|
|
116
134
|
// #region standard inputs
|
|
117
135
|
input=enhanceAutoGrow(input,form);if(type!=="checkbox"&&type!=="radio"){if(!input.getAttribute("placeholder"))input.setAttribute("placeholder"," ");const div=isWrapped?input.closest("dd").parentElement:document.createElement("div");const dt=label.closest("dt");const dd=input.closest("dd");if(!dt||!dd){console.error(`semantic-forms: Found an input (${input.id||input.getAttribute("name")}) that does not have a corresponding ${!dt?"<dt>":"<dd>"} element.`);return}
|
|
118
136
|
// input clear button. a field the user cannot edit does not get one, since the button would empty it anyway. a field that only becomes uneditable later is handled in css
|
|
119
|
-
if(input.nodeName!=="SELECT"&&type!=="range"&&!input.readOnly&&!input.disabled){const clearBtn=document.createElement("button");clearBtn.type="button";clearBtn.title=input.getAttribute("data-clear-field-text")||"Clear field";clearBtn.ariaLabel=input.getAttribute("data-clear-field-text")||"Clear field";clearBtn.tabIndex=-1;clearBtn.innerHTML='<svg viewBox="0 0 16 16" width="18" height="18"><path d="M 1 1 L 15 15 M 1 15 L 15 1" fill="none" stroke-width="2" stroke="currentColor" />';clearBtn.classList.add("clear");clearBtn.
|
|
120
|
-
|
|
121
|
-
|
|
137
|
+
if(input.nodeName!=="SELECT"&&type!=="range"&&!input.readOnly&&!input.disabled){const clearBtn=document.createElement("button");clearBtn.type="button";clearBtn.title=input.getAttribute("data-clear-field-text")||"Clear field";clearBtn.ariaLabel=input.getAttribute("data-clear-field-text")||"Clear field";clearBtn.tabIndex=-1;clearBtn.innerHTML='<svg viewBox="0 0 16 16" width="18" height="18"><path d="M 1 1 L 15 15 M 1 15 L 15 1" fill="none" stroke-width="2" stroke="currentColor" />';clearBtn.classList.add("clear");clearBtn.setAttribute("data-semantic-forms-clear","");// (what marks it as a clear button, whatever its title says)
|
|
138
|
+
clearBtn.id=`semanticFormsClearButton_${input.id}`;clearBtn.addEventListener("click",()=>{input.previousValue=input.value;input.value="";input.focus();lastClearFieldPressed=input.id;
|
|
139
|
+
// the field has been changed, as typing in it and leaving it would change it, so it says so the same way
|
|
140
|
+
input.dispatchEvent(new Event("input",{bubbles:true}));input.dispatchEvent(new Event("change",{bubbles:true}))});insertAfter(clearBtn,dd.querySelector("label"))}input.addEventListener("focus",event=>{if(event.target.nodeName==="INPUT")lastFocusedInput=event.target});
|
|
122
141
|
// the colspan- and col- utility classes are documented as going on the dd, but they size and place the grid item, which is the div wrapping it, so move any of them across
|
|
123
142
|
for(const utility of dd.className.match(/colspan-(?:[0-9]|full)|col-[0-9]/g)||[]){dd.classList.remove(utility);div.classList.add(utility)}
|
|
124
143
|
// check for max-content attribute
|
|
@@ -134,9 +153,22 @@ if(dt.style.display==="none"&&dd.style.display==="none")div.style.display="none"
|
|
|
134
153
|
// handle file input clear btn, cannot be handled with CSS
|
|
135
154
|
if(type==="file"){const clearBtn=input.parentElement.querySelector(".clear");input.addEventListener("input",event=>{clearBtn.style.display=event.target.files.length?"flex":"none"});clearBtn.addEventListener("click",()=>{clearBtn.style.display="none"})}
|
|
136
155
|
// handle range inputs set to display the value. every other per-field flag in the library is a data attribute, so that is the documented way to ask for this, but the original `displayValue` class is still honored
|
|
137
|
-
if(type==="range"&&(input.getAttribute("data-display-value")!==null||input.classList.contains("displayValue")))
|
|
156
|
+
if(type==="range"&&(input.getAttribute("data-display-value")!==null||input.classList.contains("displayValue")))displayRangeValue(input,input.parentNode.parentNode.querySelector("dd label"));
|
|
138
157
|
// show password button
|
|
139
|
-
if(type==="password"&&input.getAttribute("data-no-reveal")===null){const showBtn=document.createElement("button");showBtn.type="button";showBtn.title=input.getAttribute("data-show-password-text")||"Show password";showBtn.ariaLabel=input.getAttribute("data-show-password-text")||"Show password";showBtn.tabIndex=-1;showBtn.innerHTML=icons.passwordShow;showBtn.classList.add("show");showBtn.id=`semanticFormsShowButton_${input.id}`;const dd=input.closest("dd");if(!dd){console.error(`semantic-forms: Found an input (${input.id||input.getAttribute("name")}) that is not inside a <dd> element.`);return}showBtn.addEventListener("click",()=>{if(input.type==="password"){showBtn.innerHTML=icons.passwordHide;showBtn.title=input.getAttribute("data-hide-password-text")||"Hide password";showBtn.ariaLabel=input.getAttribute("data-hide-password-text")||"Hide password";input.type="text"}else{showBtn.innerHTML=icons.passwordShow;showBtn.title=input.getAttribute("data-show-password-text")||"Show password";showBtn.ariaLabel=input.getAttribute("data-show-password-text")||"Show password";input.type="password"}input.focus()});insertAfter(showBtn,dd.querySelector("label"))}enhanceControl(input,type,form)};
|
|
158
|
+
if(type==="password"&&input.getAttribute("data-no-reveal")===null){const showBtn=document.createElement("button");showBtn.type="button";showBtn.title=input.getAttribute("data-show-password-text")||"Show password";showBtn.ariaLabel=input.getAttribute("data-show-password-text")||"Show password";showBtn.tabIndex=-1;showBtn.innerHTML=icons.passwordShow;showBtn.classList.add("show");showBtn.id=`semanticFormsShowButton_${input.id}`;const dd=input.closest("dd");if(!dd){console.error(`semantic-forms: Found an input (${input.id||input.getAttribute("name")}) that is not inside a <dd> element.`);return}showBtn.addEventListener("click",()=>{if(input.type==="password"){showBtn.innerHTML=icons.passwordHide;showBtn.title=input.getAttribute("data-hide-password-text")||"Hide password";showBtn.ariaLabel=input.getAttribute("data-hide-password-text")||"Hide password";input.type="text"}else{showBtn.innerHTML=icons.passwordShow;showBtn.title=input.getAttribute("data-show-password-text")||"Show password";showBtn.ariaLabel=input.getAttribute("data-show-password-text")||"Show password";input.type="password"}input.focus()});insertAfter(showBtn,dd.querySelector("label"))}enhanceControl(input,type,form)};
|
|
159
|
+
// appends a help icon or a required asterisk to a label, with its title set as text, so a title holding quotes or markup is shown as it is rather than breaking the label
|
|
160
|
+
const appendBadge=(label,className,title,content)=>{const badge=document.createElement("span");badge.className=className;badge.title=title;badge.innerHTML=content;// (the library's own icon, or an asterisk)
|
|
161
|
+
label.append(" ",badge)};
|
|
162
|
+
// shows a range's value as it moves, formatted by its data-display-format attribute, in which {value} stands for the value, such as "{value}%"
|
|
163
|
+
|
|
164
|
+
// in a float label form the value goes after the label; in a customLayout element, where nothing is added around a field, it goes in the <output> for the range, if the page has one
|
|
165
|
+
const displayRangeValue=(input,label)=>{const format=()=>(input.getAttribute("data-display-format")||"{value}").replaceAll("{value}",input.value);let output;if(label){const separator=document.createElement("span");separator.className="seperator";separator.textContent=": ";output=document.createElement("output");label.append(separator,output)}else{output=input.getRootNode().querySelector(`output[for="${window.CSS.escape(input.id)}"]`);if(!output)return}output.textContent=format();input.addEventListener("input",()=>{output.textContent=format()})};
|
|
166
|
+
// marks a field invalid, which the stylesheet outlines, with a message saying why, which screen readers read along with the field. options.messageIn is the element to put the message in; by default it goes after the field, where the stylesheet shows it
|
|
167
|
+
|
|
168
|
+
// calling it again replaces the message, and clearInvalid undoes it
|
|
169
|
+
const setInvalid=(input,message,options={})=>{input.classList.add("invalid");input.setAttribute("aria-invalid","true");const id=`${input.id||"semanticFormsField"}SemanticFormsInvalid`;let text=input.getRootNode().getElementById?.(id)||document.getElementById(id);if(!text){text=document.createElement("p");text.id=id;text.className="semanticFormsInvalidText";if(options.messageIn)options.messageIn.append(text);else input.parentNode.append(text)}text.textContent=message;const describedBy=(input.getAttribute("aria-describedby")||"").split(" ").filter(token=>token&&token!==id);input.setAttribute("aria-describedby",[...describedBy,id].join(" "))};const clearInvalid=input=>{input.classList.remove("invalid");input.removeAttribute("aria-invalid");const id=`${input.id||"semanticFormsField"}SemanticFormsInvalid`;const text=input.getRootNode().getElementById?.(id)||document.getElementById(id);text?.remove();const describedBy=(input.getAttribute("aria-describedby")||"").split(" ").filter(token=>token&&token!==id);if(describedBy.length)input.setAttribute("aria-describedby",describedBy.join(" "));else input.removeAttribute("aria-describedby");
|
|
170
|
+
// the field may still be one the browser considers invalid, in which case the stylesheet is still outlining it and aria-invalid goes back to saying so
|
|
171
|
+
syncAriaInvalid(input)};const handleUndoRedo=event=>{
|
|
140
172
|
// handle undo/redo
|
|
141
173
|
if((event.ctrlKey||event.metaKey)&&event.key==="z"&&!event.shiftKey){
|
|
142
174
|
// undo clearing a field
|
|
@@ -146,7 +178,7 @@ event.preventDefault();lastFocusedInput.redoValue=lastFocusedInput.value;lastFoc
|
|
|
146
178
|
// redo clearing a field
|
|
147
179
|
if(lastFocusedInput)if(lastFocusedInput?.parentNode?.querySelector("button.clear")?.id===`semanticFormsClearButton_${lastClearFieldPressed}`||lastFocusedInput?.parentNode?.querySelector("button.clear")?.name===`semanticFormsClearButton_${lastClearFieldPressed}`)if(lastFocusedInput.redoValue){
|
|
148
180
|
// as above, the browser's own redo would otherwise apply on top of this
|
|
149
|
-
event.preventDefault();lastFocusedInput.previousValue=lastFocusedInput.value;lastFocusedInput.value=lastFocusedInput.redoValue}};module.exports={enhanceInput,handleUndoRedo}
|
|
181
|
+
event.preventDefault();lastFocusedInput.previousValue=lastFocusedInput.value;lastFocusedInput.value=lastFocusedInput.redoValue}};module.exports={enhanceInput,handleUndoRedo,setInvalid,clearInvalid}
|
|
150
182
|
/***/},
|
|
151
183
|
/***/"./lib/keyboardShortcuts.js"(module,__unused_webpack_exports,__webpack_require__){const{getOS,insertAfter}=__webpack_require__("./lib/helpers.js");const specialCharMap={Minus:"-",Equal:"=",BracketLeft:"[",BracketRight:"]",Backslash:"\\",Semicolon:";",Quote:"'",Comma:",",Period:".",Slash:"/",Backquote:"`"};const shiftSpecialCharMap={1:"!",2:"@",3:"#",4:"$",5:"%",6:"^",7:"&",8:"*",9:"(",0:")",Minus:"_",Equal:"+",BracketLeft:"{",BracketRight:"}",Backslash:"|",Semicolon:":",Quote:'"',Comma:"<",Period:">",Slash:"?",Backquote:"~"};
|
|
152
184
|
// handles keyboard shortcut events
|
|
@@ -187,24 +219,28 @@ if(input.getAttribute("title"))input.setAttribute("title",input.getAttribute("ti
|
|
|
187
219
|
|
|
188
220
|
// the tab that opens first is the one whose fieldset carries `data-selected`, or the first fieldset when none do
|
|
189
221
|
|
|
222
|
+
// adding the `vertical` class to the group puts the tabs in a column beside the panels, and the up and down arrow keys move between them in place of left and right
|
|
223
|
+
|
|
190
224
|
// form: form element to search for tab groups
|
|
191
225
|
const enhanceTabs=form=>{for(const group of form.querySelectorAll(".tabs:not(.semanticFormsTabsActive)")){const panels=Array.from(group.children).filter(child=>child.nodeName==="FIELDSET");
|
|
192
226
|
// one fieldset is not a set of tabs, and no fieldsets means the class is on the wrong element
|
|
193
|
-
if(panels.length<2){if(panels.length===0)console.error("semantic-forms: Found a .tabs element with no <fieldset> children. Each tab needs to be a <fieldset> with a <legend> naming it.",group);continue}group.classList.add("semanticFormsTabsActive");const groupId=++tabGroupCount;const tabList=document.createElement("div");const tabs=[];tabList.classList.add("tabList");tabList.setAttribute("role","tablist");
|
|
227
|
+
if(panels.length<2){if(panels.length===0)console.error("semantic-forms: Found a .tabs element with no <fieldset> children. Each tab needs to be a <fieldset> with a <legend> naming it.",group);continue}group.classList.add("semanticFormsTabsActive");const groupId=++tabGroupCount;const vertical=group.classList.contains("vertical");const tabList=document.createElement("div");const tabs=[];tabList.classList.add("tabList");tabList.setAttribute("role","tablist");tabList.setAttribute("aria-orientation",vertical?"vertical":"horizontal");
|
|
194
228
|
// shows one panel and marks its tab as the selected one
|
|
195
229
|
const selectTab=(index,moveFocus)=>{for(const[i,panel]of panels.entries())panel.hidden=i!==index;for(const[i,tab]of tabs.entries()){const selected=i===index;tab.setAttribute("aria-selected",selected?"true":"false");
|
|
196
230
|
// only the selected tab is in the tab order; the arrow keys reach the others
|
|
197
231
|
tab.tabIndex=selected?0:-1}if(moveFocus)tabs[index].focus()};for(const[index,panel]of panels.entries()){const legend=panel.querySelector("legend");const tab=document.createElement("button");tab.type="button";tab.setAttribute("role","tab");tab.id=`semanticFormsTab_${groupId}_${index}`;tab.textContent=legend?legend.textContent:`Tab ${index+1}`;if(!panel.id)panel.id=`semanticFormsTabPanel_${groupId}_${index}`;panel.setAttribute("role","tabpanel");panel.setAttribute("aria-labelledby",tab.id);tab.setAttribute("aria-controls",panel.id);
|
|
198
232
|
// the tab now names the panel, so the legend would be read out twice
|
|
199
233
|
if(legend)legend.hidden=true;tab.addEventListener("click",()=>selectTab(index));tabs.push(tab);tabList.append(tab)}
|
|
200
|
-
// the arrow keys move between tabs, which is what a tablist is expected to do
|
|
201
|
-
tabList.addEventListener("keydown",event=>{const current=tabs.indexOf(document.activeElement);if(current===-1)return;const keys={
|
|
234
|
+
// the arrow keys along the strip move between tabs, which is what a tablist is expected to do
|
|
235
|
+
const nextKey=vertical?"ArrowDown":"ArrowRight";const previousKey=vertical?"ArrowUp":"ArrowLeft";tabList.addEventListener("keydown",event=>{const current=tabs.indexOf(document.activeElement);if(current===-1)return;const keys={[nextKey]:(current+1)%tabs.length,[previousKey]:(current-1+tabs.length)%tabs.length,Home:0,End:tabs.length-1};if(!(event.key in keys))return;event.preventDefault();selectTab(keys[event.key],true)});
|
|
202
236
|
// the panel carrying [data-selected] is the one that opens, defaulting to the first. without javascript every panel is on the page at once, so the attribute only means anything once the tabs have been built
|
|
203
237
|
const marked=panels.filter(panel=>panel.hasAttribute("data-selected"));if(marked.length>1)console.error('semantic-forms: Found more than one <fieldset> with a "data-selected" attribute in a tab group. Only the first one will be selected.',group);group.prepend(tabList);selectTab(marked.length?panels.indexOf(marked[0]):0)}};module.exports={enhanceTabs}
|
|
204
238
|
/***/},
|
|
205
|
-
/***/"./semanticForms.js"(module,__unused_webpack_exports,__webpack_require__){const{createKeyboardShortcut,shortcutListener}=__webpack_require__("./lib/keyboardShortcuts.js");const{enhanceInput,handleUndoRedo}=__webpack_require__("./lib/inputEnhancements.js");const{enhanceTabs}=__webpack_require__("./lib/tabs.js");const{setInkOffset}=__webpack_require__("./lib/helpers.js");
|
|
239
|
+
/***/"./semanticForms.js"(module,__unused_webpack_exports,__webpack_require__){const{createKeyboardShortcut,shortcutListener}=__webpack_require__("./lib/keyboardShortcuts.js");const{enhanceInput,handleUndoRedo,setInvalid,clearInvalid}=__webpack_require__("./lib/inputEnhancements.js");const{enhanceTabs}=__webpack_require__("./lib/tabs.js");const{setInkOffset}=__webpack_require__("./lib/helpers.js");
|
|
206
240
|
// one observer per tree that has been handed to semanticForms, so that a shadow root gets its own without replacing the document's
|
|
207
241
|
const observers=new WeakMap;
|
|
242
|
+
// the keyboard shortcuts every form has asked for, kept in one list with one listener for them all, since semanticForms runs again for every form added to the page. a shortcut whose field has left the page is dropped
|
|
243
|
+
const keyboardShortcuts=[];let listeningForShortcuts=false;const pruneShortcuts=()=>{for(let i=keyboardShortcuts.length-1;i>=0;i--)if(!keyboardShortcuts[i].input.isConnected)keyboardShortcuts.splice(i,1)};
|
|
208
244
|
// progressively enhances every semanticForms form in a tree
|
|
209
245
|
|
|
210
246
|
// root: document or shadow root to search; defaults to the main document
|
|
@@ -212,7 +248,7 @@ const semanticForms=(root=document)=>{
|
|
|
212
248
|
// do some feature detection so none of the JS executes if the browser is too old
|
|
213
249
|
if(typeof document.getElementsByClassName!=="function"||typeof document.querySelector!=="function"||!document.body.classList||!window.MutationObserver){console.warn("semantic-forms was loaded into an unsupported browser and will not execute.");return}
|
|
214
250
|
// custom keyboard shortcut listener
|
|
215
|
-
|
|
251
|
+
if(!listeningForShortcuts){document.addEventListener("keydown",e=>shortcutListener(e,keyboardShortcuts));listeningForShortcuts=true}pruneShortcuts();
|
|
216
252
|
// progressively enhance form elements that have the semanticForms class
|
|
217
253
|
const forms=root.querySelectorAll("form.semanticForms:not(.semanticFormsActive), table.semanticForms:not(.semanticFormsActive)");for(const form of forms){form.classList.add("semanticFormsActive");if(form.classList.contains("lowFlow"))continue;
|
|
218
254
|
// how far this font's letters sit from the middle of their own line box, which the stylesheet subtracts wherever it centers text. it is measured per form rather than per field, so that a form holding nothing but buttons is corrected too
|
|
@@ -220,7 +256,7 @@ setInkOffset(form,form);
|
|
|
220
256
|
// update each input in the semantic form
|
|
221
257
|
const inputs=Array.from(form.querySelectorAll("input, textarea, select"));for(const input of inputs){enhanceInput(input,form);
|
|
222
258
|
// handle keyboard shortcuts
|
|
223
|
-
if(input.getAttribute("data-focus-key")!==null){const shortcut=createKeyboardShortcut(input,keyboardShortcuts);keyboardShortcuts.push(shortcut)}}
|
|
259
|
+
if(input.getAttribute("data-focus-key")!==null&&!keyboardShortcuts.some(shortcut=>shortcut.input===input)){const shortcut=createKeyboardShortcut(input,keyboardShortcuts);keyboardShortcuts.push(shortcut)}}
|
|
224
260
|
// done after the inputs so that anything sizing itself measures while it is still on screen
|
|
225
261
|
enhanceTabs(form)}
|
|
226
262
|
// prevents multiple listeners
|
|
@@ -234,7 +270,9 @@ if(root===document)window.semanticFormsObserver=observer}semanticForms.reinitial
|
|
|
234
270
|
// with no form given this just rescans for forms that are not active yet
|
|
235
271
|
if(!form)return semanticForms(root);form.classList.remove("semanticFormsActive");
|
|
236
272
|
// the form knows which tree it belongs to, so a form in a shadow root reinitializes there
|
|
237
|
-
semanticForms(form.getRootNode())}};
|
|
273
|
+
semanticForms(form.getRootNode())}};
|
|
274
|
+
// marks a field invalid with a message saying why, or clears that: see the docs for setInvalid
|
|
275
|
+
semanticForms.setInvalid=setInvalid;semanticForms.clearInvalid=clearInvalid;module.exports=semanticForms
|
|
238
276
|
/***/}
|
|
239
277
|
/******/};
|
|
240
278
|
/************************************************************************/
|