nbhi 0.6.16 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,133 +0,0 @@
1
- export { getAttributeMapping, updateAttributes };
2
-
3
- const mapping = {
4
- boolean: {
5
- autofocus: ['button', 'input', 'select', 'textarea'],
6
- autoplay: ['audio', 'video'],
7
- checked: ['input'],
8
- controls: ['audio', 'video'],
9
- default: ['track'],
10
- defer: ['script'],
11
- disabled: ['button', 'input', 'optgroup', 'option', 'select', 'textarea', 'fieldset'],
12
- formnovalidate: ['button', 'input'],
13
- ismap: ['img'],
14
- loop: ['audio', 'video'],
15
- multiple: ['input', 'select'],
16
- muted: ['audio', 'video'],
17
- novalidate: ['form'],
18
- open: ['details', 'dialog'],
19
- playsinline: ['video'],
20
- readonly: ['input', 'textarea'],
21
- required: ['input', 'select', 'textarea'],
22
- reversed: ['ol'],
23
- },
24
- keyValue: {
25
- accept: ['input'],
26
- autocomplete: ['input', 'select', 'textarea'],
27
- cols: ['textarea'],
28
- dirname: ['input', 'textarea'],
29
- max: ['input'],
30
- maxlength: ['input', 'textarea'],
31
- min: ['input'],
32
- minlength: ['input', 'textarea'],
33
- name: ['input', 'select', 'textarea'],
34
- pattern: ['input'],
35
- placeholder: ['input', 'textarea'],
36
- rows: ['textarea'],
37
- size: ['input', 'select'],
38
- step: ['input'],
39
- type: ['input'],
40
- value: ['input', 'select'],
41
- width: ['input'],
42
- wrap: ['textarea'],
43
- htmlFor: ['label'],
44
- for: ['label'],
45
- href: ['a'],
46
- target: ['a'],
47
- src: ['audio', 'img', 'input', 'video'],
48
- 'aria-invalid': ['input', 'textarea', 'select'],
49
- },
50
- excludedAttributes: ['class', 'extends', 'id', 'part', 'data-slot', 'child'],
51
- excludedElements: ['slot', 'template', 'style', 'script'],
52
- };
53
-
54
- function getAttributeMapping(element) {
55
- const { keyValue, excludedAttributes, excludedElements, boolean: binary } = mapping;
56
- const results = {};
57
- traverse(element);
58
- return results;
59
-
60
- function traverse(element) {
61
- const tagName = element.localName;
62
- if (
63
- element &&
64
- typeof tagName === 'string' &&
65
- !excludedElements.includes(element.localName)
66
- ) {
67
- Object
68
- .entries(binary)
69
- .filter(([key]) => !excludedAttributes.includes(key))
70
- .filter(([, tagNames]) => tagNames.includes(tagName))
71
- .forEach(([key, tagNames]) => results[key] = { type: 'boolean', tagNames });
72
-
73
- Object
74
- .entries(keyValue)
75
- .filter(([key]) => !excludedAttributes.includes(key))
76
- .filter(([, tagNames]) => tagNames.includes(tagName))
77
- .forEach(([key, tagNames]) => results[key] = { type: 'keyValue', tagNames });
78
-
79
- if (element.hasAttributes()) {
80
- [...element.attributes].forEach(attribute => {
81
- const name = attribute.name;
82
- if (!excludedAttributes.includes(name)) {
83
- if (!results[name]) {
84
- results[name] = { type: 'userDefined', tagNames: [] };
85
- }
86
-
87
- if (!results[name].tagNames.includes(tagName)) {
88
- results[name].tagNames.push(tagName);
89
- }
90
- }
91
- });
92
- }
93
- }
94
- [...element.children].forEach(child => traverse(child));
95
- }
96
- };
97
-
98
- function updateAttributes({ name, value, attributeMapping, root }) {
99
- const { type, tagNames = [] } = attributeMapping[name] ?? [];
100
-
101
- if (type === 'userDefined') {
102
- if (root.hasAttribute && root.hasAttribute(name)) {
103
- process(root);
104
- } else {
105
- process(root.querySelector(`[${ name }]`));
106
- }
107
- } else {
108
- tagNames.forEach(tagName => {
109
- root.querySelectorAll(tagName).forEach(process);
110
- if (tagName === root?.localName) {
111
- process(root);
112
- }
113
- });
114
- }
115
-
116
- function process(element) {
117
- if (element) {
118
- if (
119
- value === null ||
120
- value === undefined ||
121
- (type === 'boolean' && value === false)
122
- ) {
123
- element.removeAttribute(name);
124
- } else {
125
- element.setAttribute(name, value);
126
- // Triggers <select> to update the selected <option>
127
- if (name === 'value' && element.localName === 'select') {
128
- element.value = value;
129
- }
130
- }
131
- }
132
- }
133
- }
@@ -1,11 +0,0 @@
1
- export default selector => {
2
- if (typeof selector === 'string') {
3
- const instance = document.querySelector(selector);
4
- if (!instance) {
5
- throw new Error(`No elements found for selector ${ selector }`);
6
- }
7
- return instance;
8
- }
9
-
10
- return selector;
11
- };
@@ -1,11 +0,0 @@
1
- import element from './element.js';
2
-
3
- export default (selector, callback) => {
4
- const observer = new IntersectionObserver(entries => {
5
- if (entries[0].isIntersecting) {
6
- callback();
7
- observer.disconnect();
8
- }
9
- });
10
- observer.observe(element(selector));
11
- };
@@ -1,220 +0,0 @@
1
- import { getAttributeMapping, updateAttributes } from './attributes.js';
2
- import element from './element.js';
3
-
4
- export default selector => {
5
- const instance = element(selector);
6
- const idCache = new WeakMap();
7
- const mappingCache = new WeakMap();
8
- let renderedChildren = new Map();
9
- let idCount = 1;
10
- let templateChild;
11
- let templateParent;
12
-
13
- return data => {
14
- if (isDate(data)) {
15
- throw new Error('Please convert date to string, ie. toISOString()');
16
- }
17
-
18
- if (isArray(data)) {
19
- updateList(data);
20
- } else {
21
- if (isPrimitive(data)) {
22
- updateText(instance, data);
23
- } else if (isObject(data)) {
24
- updateFromObject(instance, data);
25
- }
26
- }
27
-
28
- function isDate(input) {
29
- return input?.getTime !== undefined;
30
- }
31
-
32
- function isPrimitive(input) {
33
- return !isArray(input) && !isObject(input);
34
- }
35
-
36
- function isObject(input) {
37
- return !isArray(input) && input !== null && typeof input === 'object' && Object.keys(input).length;
38
- }
39
-
40
- function isArray(input) {
41
- return Array.isArray(input);
42
- }
43
-
44
- function updateFromObject(element, data) {
45
- Object.entries(data).forEach(([key, value]) => {
46
- if (key.startsWith('$')) {
47
- const name = key.slice(1);
48
- if (element.shadowRoot) {
49
- if (value === false) {
50
- element.removeAttribute(name);
51
- } else if (value === true) {
52
- element.setAttribute(name, '');
53
- } else {
54
- element.setAttribute(name, value);
55
- }
56
- } else {
57
- if (!mappingCache.has(element)) {
58
- mappingCache.set(element, getAttributeMapping(element));
59
- }
60
- const attributeMapping = mappingCache.get(element);
61
- updateAttributes({ name, value, attributeMapping, root: element });
62
- }
63
- } else {
64
- updateText(element, value, key);
65
- }
66
- });
67
- }
68
-
69
- function updateText(element, value, name) {
70
- if (element.localName === 'option') {
71
- element.textContent = value;
72
- } else if (
73
- element.localName === 'tr' ||
74
- element.localName === 'li' ||
75
- (element.hasAttribute('child') && !element.shadowRoot)) {
76
- if (element.hasAttribute('data-slot') && element.dataset.slot === name) {
77
- element.innerHTML = value;
78
- } else {
79
- const match = element.querySelector(`[data-slot="${ name }"]`);
80
- if (match) {
81
- match.innerHTML = value;
82
- }
83
- }
84
- } else if (!name && element.innerHTML !== value) {
85
- element.innerHTML = value;
86
- } else {
87
- let slot = element.querySelector(`[slot="${ name }"]`);
88
- if (!slot) {
89
- slot = document.createElement('span');
90
- slot.setAttribute('slot', name);
91
- element.append(slot);
92
- }
93
-
94
- if (slot.innerHTML !== value) {
95
- slot.innerHTML = value;
96
- }
97
- }
98
- }
99
-
100
- function updateList(records) {
101
- const parent = getParent();
102
- const newChildren = new Map();
103
-
104
- records.forEach((record, index) => {
105
- const id = getId(record, index);
106
- let child = renderedChildren.get(id);
107
-
108
- if (!child) {
109
- child = templateChild.cloneNode(true);
110
- parent.append(child);
111
- }
112
-
113
- if (child.cachedData !== record) {
114
- if (isObject(record)) {
115
- updateFromObject(child, record);
116
- } else {
117
- updateText(child, record);
118
- }
119
- child.cachedData = record;
120
- }
121
-
122
- newChildren.set(id, child);
123
- });
124
-
125
- renderedChildren.forEach((child, id) => {
126
- if (!newChildren.has(id)) {
127
- child.remove();
128
- }
129
- });
130
-
131
- // Order children
132
- let lastChild;
133
- newChildren.forEach(child => {
134
- if (child !== lastChild?.nextSibling) {
135
- parent.insertBefore(child, lastChild ? lastChild.nextSibling : parent.firstChild);
136
- }
137
- lastChild = child;
138
- });
139
-
140
- renderedChildren = newChildren;
141
-
142
- function getId(record, index) {
143
- if (record && typeof record === 'object') {
144
- const recordId = record.id;
145
- if (['string', 'number'].includes(typeof recordId) && !newChildren.has(`id:${ recordId }`)) {
146
- return `id:${ recordId }`;
147
- }
148
- if (!idCache.has(record)) {
149
- idCache.set(record, idCount++);
150
- }
151
- return idCache.get(record);
152
- }
153
- return `p:${ record }:${ index }`;
154
- };
155
-
156
- function getParent() {
157
- if (templateParent) {
158
- return templateParent;
159
- }
160
- const topLevel = getTopLevel();
161
- if (topLevel.localName === 'table') {
162
- return process('tbody', 'tr');
163
- } else if (topLevel.localName === 'ol') {
164
- return process('ol', 'li');
165
- } else if (topLevel.localName === 'ul') {
166
- return process('ul', 'li');
167
- } else if (topLevel.localName === 'select') {
168
- return process('select', 'option');
169
- } else if (topLevel.querySelector('[child]')) {
170
- const parent = getParent(topLevel.querySelector('[child]')).parentElement;
171
- templateParent = parent;
172
- setTemplateChild('[child]', parent);
173
- if (parent.localName === 'slot' && parent.hasAttribute('name')) {
174
- templateChild.slot = parent.getAttribute('name');
175
- }
176
- return parent.localName === 'slot' ? instance : parent;
177
- }
178
-
179
- throw new Error(`Could not find a parent to assign children to, valid
180
- options are table, ul, ol, select or a structure with a child attribute`);
181
-
182
- function process(parentName, childName) {
183
- const parent = getParent(parentName);
184
- setTemplateChild(childName, parent);
185
- templateParent = parent;
186
- return parent;
187
- }
188
-
189
- function setTemplateChild(childName, parent) {
190
- if (!templateChild) {
191
- templateChild = parent.querySelector(childName).cloneNode(true);
192
- parent.replaceChildren();
193
- }
194
- }
195
-
196
- function getParent(parentName) {
197
- if (parentName === topLevel.localName) {
198
- return topLevel;
199
- }
200
- const parent = typeof parentName === 'string'
201
- ? topLevel.querySelector(parentName) : parentName;
202
- if (!parent) {
203
- console.log({ topLevel, parentName });
204
- throw new Error('Cannot find parent');
205
- }
206
- return parent;
207
- }
208
-
209
- function getTopLevel() {
210
- if (instance.tagName.includes('-')) {
211
- return [...instance.shadowRoot.children].find(child => {
212
- return child.localName !== 'script' && child.localName !== 'style';
213
- });
214
- }
215
- return instance;
216
- }
217
- }
218
- }
219
- };
220
- };
@@ -1,39 +0,0 @@
1
- import element from './element.js';
2
-
3
- // Too long and too short is only set when the actual input is changed, so we do it here
4
- export default selector => {
5
- const instance = element(selector);
6
- if (instance.elementInternals) {
7
- const element = instance.formElement;
8
- const value = element.value;
9
- const minlength = element.getAttribute('minlength');
10
- const maxlength = element.getAttribute('maxlength');
11
- const validity = {
12
- valueMissing: element.validity.valueMissing,
13
- typeMismatch: element.validity.typeMismatch,
14
- patternMismatch: element.validity.patternMismatch,
15
- rangeUnderflow: element.validity.rangeUnderflow,
16
- rangeOverflow: element.validity.rangeOverflow,
17
- stepMismatch: element.validity.stepMismatch,
18
- badInput: element.validity.badInput,
19
- customError: element.validity.customError,
20
- tooShort: minlength ? value.length < parseInt(minlength) : false,
21
- tooLong: maxlength ? value.length > parseInt(maxlength) : false,
22
- };
23
- instance.elementInternals.setFormValue(value);
24
- instance.elementInternals.setValidity(validity, message(), element);
25
-
26
- return instance.elementInternals;
27
-
28
- function message() {
29
- if (validity.tooShort) {
30
- return `The text needs to be at least ${ minlength } characters long`;
31
- } else if (validity.tooLong) {
32
- return `The text needs to be at most ${ maxlength } characters long`;
33
- } else if (element.validationMessage) {
34
- return element.validationMessage;
35
- }
36
- return 'The value is invalid';
37
- }
38
- }
39
- };