semantic-forms 5.3.15 → 5.3.17
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 +21 -0
- package/dist/semantic-forms.cjs +38 -18
- package/dist/semantic-forms.cjs.map +1 -1
- package/dist/semantic-forms.css +403 -342
- package/dist/semantic-forms.css.map +1 -1
- package/dist/semantic-forms.js +38 -18
- 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 +38 -18
- package/dist/semantic-forms.mjs.map +1 -1
- package/docs/statics/js/semantic-forms-demo.js +0 -4
- package/docs/statics/pages/buttons.html +8 -0
- package/docs/statics/pages/inputs.html +5 -3
- package/docs/statics/pages/layout.html +3 -8
- package/docs/statics/pages/theming.html +11 -1
- package/docs/statics/pages/usage.html +9 -0
- package/package.json +3 -3
- package/semanticForms.js +18 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## 5.3.17
|
|
2
|
+
|
|
3
|
+
- Added `semanticForms.setInvalid(field, message, options)` and `semanticForms.clearInvalid(field)`, which mark a field invalid from your own code with a message saying why.
|
|
4
|
+
- Added a `semanticFormsButton` class, which styles a button outside a form like the ones in one.
|
|
5
|
+
- Added a `--semanticFormsCursor` variable, for the pointer over buttons, checkboxes, selects and the like.
|
|
6
|
+
- 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.
|
|
7
|
+
- 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.
|
|
8
|
+
- 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.
|
|
9
|
+
- 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.
|
|
10
|
+
- Fixed a form inside a `<dialog>` or a popover, or one holding only a button, shrinking to nothing.
|
|
11
|
+
- Fixed a field without an id not having its control enhanced, so a switch without one wasn't given its role or dragging.
|
|
12
|
+
- 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.
|
|
13
|
+
- Fixed a help icon or required asterisk whose title holds quotes or markup breaking the label.
|
|
14
|
+
- Fixed `data-auto-grow` dropping the field's name, class, and other attributes when it turns an input into a textarea.
|
|
15
|
+
- Fixed enhancing a textarea firing an `input` event on it, which an app would take as the user having changed it.
|
|
16
|
+
- Updated dependencies.
|
|
17
|
+
|
|
18
|
+
## 5.3.16
|
|
19
|
+
|
|
20
|
+
- 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.
|
|
21
|
+
|
|
1
22
|
## 5.3.15
|
|
2
23
|
|
|
3
24
|
- Fixed the search icon looking jagged on displays scaled by a fraction, such as 125%. Its image said it was 14px square, but it's drawn at 18px, so browsers drew it at 14px and then stretched it.
|
package/dist/semantic-forms.cjs
CHANGED
|
@@ -58,7 +58,9 @@ input.addEventListener("click",clickEvent=>clickEvent.preventDefault(),{once:tru
|
|
|
58
58
|
// returns the field, which is the new textarea when one was made
|
|
59
59
|
const enhanceAutoGrow=(input,form)=>{if(input.getAttribute("data-auto-grow")===null)return input;
|
|
60
60
|
// progressively enhance inputs into textareas
|
|
61
|
-
if(input.nodeName==="INPUT"&&input.type==="text"){
|
|
61
|
+
if(input.nodeName==="INPUT"&&input.type==="text"){
|
|
62
|
+
// 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
|
|
63
|
+
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
64
|
// when pressing enter while this input is focused, we want to submit
|
|
63
65
|
input.addEventListener("keypress",e=>{if(e.key!=="Enter"||e.key==="Enter"&&e.shiftKey)return;e.preventDefault();form.requestSubmit()});return input};
|
|
64
66
|
// 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
|
|
@@ -85,8 +87,8 @@ input.style.removeProperty("width");input.style.removeProperty("box-sizing");inp
|
|
|
85
87
|
const rows=Math.round((scrollHeight-blockPadding)/lineHeight);
|
|
86
88
|
// set the calculated rows attribute (limited by rowLimit)
|
|
87
89
|
if(maxRows)input.setAttribute("rows",""+Math.min(rows,Number(maxRows)));else input.setAttribute("rows",""+rows)};input.addEventListener("input",handleInput);
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
+
// 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
|
|
91
|
+
handleInput()}
|
|
90
92
|
// progressively enhance textarea for Firefox and Safari
|
|
91
93
|
// this may be removed once fully supported in Firefox and Safari: https://caniuse.com/wf-field-sizing
|
|
92
94
|
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 +97,33 @@ input.style.height=window.getComputedStyle(form).getPropertyValue("--semanticFor
|
|
|
95
97
|
// shifts the clear button to the right if a scrollbar is present
|
|
96
98
|
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
99
|
// ignore input if it has previously been formatted
|
|
98
|
-
if(input.classList.contains("semanticform")
|
|
100
|
+
if(input.classList.contains("semanticform"))return;const type=input.getAttribute("type");if(!lookups.nodeName.includes(input.nodeName)&&!lookups.inputType.includes(type))return;
|
|
101
|
+
// 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
|
|
102
|
+
if(!input.id){input.classList.add("semanticform");enhanceControl(input,type,form);return}
|
|
99
103
|
// 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");
|
|
104
|
+
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
105
|
// recursively find <dl> element
|
|
102
106
|
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
107
|
// 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(
|
|
108
|
+
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
109
|
// an input outside a <dd> is reported by the checks further down, so tolerate it here
|
|
106
110
|
const isWrapped=input.closest("dd")?.parentElement.nodeName==="DIV";
|
|
107
111
|
// #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
|
|
112
|
+
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)}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
113
|
// if the user did not wrap the input in a div, do it for them
|
|
110
114
|
if(!isWrapped){
|
|
111
115
|
// 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
116
|
dt.before(div);div.append(dt,dd);
|
|
113
117
|
// 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"))
|
|
118
|
+
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);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
119
|
// #endregion
|
|
116
120
|
// #region standard inputs
|
|
117
121
|
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
122
|
// 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
|
-
|
|
123
|
+
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)
|
|
124
|
+
clearBtn.id=`semanticFormsClearButton_${input.id}`;clearBtn.addEventListener("click",()=>{input.previousValue=input.value;input.value="";input.focus();lastClearFieldPressed=input.id;
|
|
125
|
+
// the field has been changed, as typing in it and leaving it would change it, so it says so the same way
|
|
126
|
+
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
127
|
// 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
128
|
for(const utility of dd.className.match(/colspan-(?:[0-9]|full)|col-[0-9]/g)||[]){dd.classList.remove(utility);div.classList.add(utility)}
|
|
124
129
|
// check for max-content attribute
|
|
@@ -134,9 +139,20 @@ if(dt.style.display==="none"&&dd.style.display==="none")div.style.display="none"
|
|
|
134
139
|
// handle file input clear btn, cannot be handled with CSS
|
|
135
140
|
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
141
|
// 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")))
|
|
142
|
+
if(type==="range"&&(input.getAttribute("data-display-value")!==null||input.classList.contains("displayValue")))displayRangeValue(input,input.parentNode.parentNode.querySelector("dd label"));
|
|
138
143
|
// 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)};
|
|
144
|
+
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)};
|
|
145
|
+
// 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
|
|
146
|
+
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)
|
|
147
|
+
label.append(" ",badge)};
|
|
148
|
+
// 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}%"
|
|
149
|
+
|
|
150
|
+
// 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
|
|
151
|
+
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()})};
|
|
152
|
+
// 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
|
|
153
|
+
|
|
154
|
+
// calling it again replaces the message, and clearInvalid undoes it
|
|
155
|
+
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")};const handleUndoRedo=event=>{
|
|
140
156
|
// handle undo/redo
|
|
141
157
|
if((event.ctrlKey||event.metaKey)&&event.key==="z"&&!event.shiftKey){
|
|
142
158
|
// undo clearing a field
|
|
@@ -146,7 +162,7 @@ event.preventDefault();lastFocusedInput.redoValue=lastFocusedInput.value;lastFoc
|
|
|
146
162
|
// redo clearing a field
|
|
147
163
|
if(lastFocusedInput)if(lastFocusedInput?.parentNode?.querySelector("button.clear")?.id===`semanticFormsClearButton_${lastClearFieldPressed}`||lastFocusedInput?.parentNode?.querySelector("button.clear")?.name===`semanticFormsClearButton_${lastClearFieldPressed}`)if(lastFocusedInput.redoValue){
|
|
148
164
|
// 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}
|
|
165
|
+
event.preventDefault();lastFocusedInput.previousValue=lastFocusedInput.value;lastFocusedInput.value=lastFocusedInput.redoValue}};module.exports={enhanceInput,handleUndoRedo,setInvalid,clearInvalid}
|
|
150
166
|
/***/},
|
|
151
167
|
/***/"./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
168
|
// handles keyboard shortcut events
|
|
@@ -202,9 +218,11 @@ tabList.addEventListener("keydown",event=>{const current=tabs.indexOf(document.a
|
|
|
202
218
|
// 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
219
|
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
220
|
/***/},
|
|
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");
|
|
221
|
+
/***/"./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
222
|
// one observer per tree that has been handed to semanticForms, so that a shadow root gets its own without replacing the document's
|
|
207
223
|
const observers=new WeakMap;
|
|
224
|
+
// 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
|
|
225
|
+
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
226
|
// progressively enhances every semanticForms form in a tree
|
|
209
227
|
|
|
210
228
|
// root: document or shadow root to search; defaults to the main document
|
|
@@ -212,7 +230,7 @@ const semanticForms=(root=document)=>{
|
|
|
212
230
|
// do some feature detection so none of the JS executes if the browser is too old
|
|
213
231
|
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
232
|
// custom keyboard shortcut listener
|
|
215
|
-
|
|
233
|
+
if(!listeningForShortcuts){document.addEventListener("keydown",e=>shortcutListener(e,keyboardShortcuts));listeningForShortcuts=true}pruneShortcuts();
|
|
216
234
|
// progressively enhance form elements that have the semanticForms class
|
|
217
235
|
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
236
|
// 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 +238,7 @@ setInkOffset(form,form);
|
|
|
220
238
|
// update each input in the semantic form
|
|
221
239
|
const inputs=Array.from(form.querySelectorAll("input, textarea, select"));for(const input of inputs){enhanceInput(input,form);
|
|
222
240
|
// handle keyboard shortcuts
|
|
223
|
-
if(input.getAttribute("data-focus-key")!==null){const shortcut=createKeyboardShortcut(input,keyboardShortcuts);keyboardShortcuts.push(shortcut)}}
|
|
241
|
+
if(input.getAttribute("data-focus-key")!==null&&!keyboardShortcuts.some(shortcut=>shortcut.input===input)){const shortcut=createKeyboardShortcut(input,keyboardShortcuts);keyboardShortcuts.push(shortcut)}}
|
|
224
242
|
// done after the inputs so that anything sizing itself measures while it is still on screen
|
|
225
243
|
enhanceTabs(form)}
|
|
226
244
|
// prevents multiple listeners
|
|
@@ -234,7 +252,9 @@ if(root===document)window.semanticFormsObserver=observer}semanticForms.reinitial
|
|
|
234
252
|
// with no form given this just rescans for forms that are not active yet
|
|
235
253
|
if(!form)return semanticForms(root);form.classList.remove("semanticFormsActive");
|
|
236
254
|
// the form knows which tree it belongs to, so a form in a shadow root reinitializes there
|
|
237
|
-
semanticForms(form.getRootNode())}};
|
|
255
|
+
semanticForms(form.getRootNode())}};
|
|
256
|
+
// marks a field invalid with a message saying why, or clears that: see the docs for setInvalid
|
|
257
|
+
semanticForms.setInvalid=setInvalid;semanticForms.clearInvalid=clearInvalid;module.exports=semanticForms
|
|
238
258
|
/***/}
|
|
239
259
|
/******/};
|
|
240
260
|
/************************************************************************/
|