@avlcodemonkey/nilla-components 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +219 -3
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/css/alert.css +48 -0
- package/css/autocomplete.css +38 -0
- package/css/dialog.css +61 -0
- package/css/file-to-textarea.css +8 -0
- package/css/html-editor.css +172 -0
- package/css/json.css +44 -0
- package/css/list.css +37 -0
- package/css/nav-toggle.css +92 -0
- package/css/nilla.css +16 -0
- package/css/pjax.css +58 -0
- package/css/slider-input.css +16 -0
- package/css/table.css +99 -0
- package/css/tokens.css +44 -0
- package/css/unsaved-changes.css +10 -0
- package/css/vendor/wysi.css +406 -0
- package/docs/llms.txt +319 -0
- package/package.json +121 -5
- package/src/components/Alert.js +24 -0
- package/src/components/Autocomplete.js +127 -0
- package/src/components/BaseComponent.js +78 -0
- package/src/components/BaseDialog.js +151 -0
- package/src/components/ConfirmDialog.js +34 -0
- package/src/components/CopyToClipboard.js +66 -0
- package/src/components/DateFormatter.js +51 -0
- package/src/components/DateTimeInput.js +63 -0
- package/src/components/DisplayToggle.js +53 -0
- package/src/components/FileToTextarea.js +81 -0
- package/src/components/HtmlEditor.js +309 -0
- package/src/components/InfoDialog.js +38 -0
- package/src/components/JsonFormatter.js +23 -0
- package/src/components/List.js +237 -0
- package/src/components/ListSortable.js +8 -0
- package/src/components/NavToggle.js +119 -0
- package/src/components/PJax.js +888 -0
- package/src/components/Share.js +41 -0
- package/src/components/SliderInput.js +26 -0
- package/src/components/Table.js +758 -0
- package/src/components/TimeOffsetSelect.js +29 -0
- package/src/components/UnsavedChanges.js +187 -0
- package/src/constants/Fetch.js +7 -0
- package/src/constants/HttpHeaders.js +19 -0
- package/src/constants/HttpMethods.js +14 -0
- package/src/constants/SortDirection.js +11 -0
- package/src/constants/TableElements.js +25 -0
- package/src/constants/TableSettings.js +14 -0
- package/src/errors/FetchError.js +7 -0
- package/src/events/ListReorderEvent.js +19 -0
- package/src/events/PJaxErrorEvent.js +38 -0
- package/src/events/PJaxNavigatingEvent.js +46 -0
- package/src/index.js +26 -0
- package/src/locales/en.json +55 -0
- package/src/locales/es.js +9 -0
- package/src/locales/es.json +55 -0
- package/src/models/TableSort.js +20 -0
- package/src/utils/ajaxHeaders.js +10 -0
- package/src/utils/announce.js +40 -0
- package/src/utils/defineElement.js +13 -0
- package/src/utils/editor/blockTools.js +256 -0
- package/src/utils/editor/elements.js +18 -0
- package/src/utils/editor/input.js +350 -0
- package/src/utils/editor/selection.js +125 -0
- package/src/utils/editor/tableTools.js +497 -0
- package/src/utils/escape.js +20 -0
- package/src/utils/fetchJsonArray.js +29 -0
- package/src/utils/filterArray.js +18 -0
- package/src/utils/formActionUrl.js +10 -0
- package/src/utils/formData.js +118 -0
- package/src/utils/index.js +18 -0
- package/src/utils/indexedCompare.js +18 -0
- package/src/utils/isSameOrigin.js +12 -0
- package/src/utils/locale.js +36 -0
- package/src/utils/notifyChange.js +10 -0
- package/src/utils/numberSetting.js +14 -0
- package/src/utils/pageLanguage.js +13 -0
- package/src/utils/parseUtcDate.js +22 -0
- package/src/utils/randomId.js +17 -0
- package/src/utils/registerTabTrap.js +60 -0
- package/src/utils/renderTemplate.js +44 -0
- package/src/utils/requestTimeout.js +12 -0
- package/src/utils/response.js +28 -0
- package/src/utils/sortedCompare.js +67 -0
- package/src/utils/storage.js +40 -0
- package/src/vendor/autocomplete.js +515 -0
- package/src/vendor/formatDate.js +236 -0
- package/src/vendor/licenses/autocomplete.LICENSE.md +21 -0
- package/src/vendor/licenses/fecha.LICENSE.md +21 -0
- package/src/vendor/licenses/pretty-print-json.LICENSE.md +21 -0
- package/src/vendor/licenses/wysi.LICENSE.md +21 -0
- package/src/vendor/prettyPrintJson.js +108 -0
- package/src/vendor/wyzi.js +2024 -0
package/docs/llms.txt
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
# Nilla Components
|
|
2
|
+
|
|
3
|
+
> Vanilla JavaScript web components that enhance plain, server-rendered HTML. No framework, and no build step required. npm package: `@avlcodemonkey/nilla-components`.
|
|
4
|
+
|
|
5
|
+
Instructions for AI tools that write code with this package. The full documentation, with every attribute and live demos, is at https://avlcodemonkey-industries.github.io/NillaComponents/. When something isn't here, look there or in the package's `src` folder rather than guessing.
|
|
6
|
+
|
|
7
|
+
## Rules
|
|
8
|
+
|
|
9
|
+
- Wrap working HTML. Forms, links, buttons and tables must make sense before the JavaScript loads.
|
|
10
|
+
- Don't build component markup in JavaScript. Render it on the server, or insert it whole with `innerHTML`. `document.createElement` followed by adding children doesn't work.
|
|
11
|
+
- Components find their parts by `data-*` attributes. Use only the ones documented, and don't invent attributes or use classes instead.
|
|
12
|
+
- Import only the components the page uses, once. Importing registers the element.
|
|
13
|
+
- Data comes from the same site only. Other origins aren't requested.
|
|
14
|
+
- Escape values on the server as usual. Sanitize the HTML editor's content on the server, every time.
|
|
15
|
+
- In table row templates, quote attributes that have placeholders, and check addresses before putting them in `href`.
|
|
16
|
+
- State changes go in forms, never in links.
|
|
17
|
+
- Don't add ARIA that the component sets itself, such as `aria-expanded`, `aria-sort` or `aria-busy`.
|
|
18
|
+
- Don't add other libraries for what a component does.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm install @avlcodemonkey/nilla-components
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import '@avlcodemonkey/nilla-components/table';
|
|
28
|
+
import '@avlcodemonkey/nilla-components/confirm-dialog';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`import '@avlcodemonkey/nilla-components';` registers everything. CSS, either all of it or `tokens.css` first and then one file for each component that has one:
|
|
32
|
+
|
|
33
|
+
```css
|
|
34
|
+
@import url("@avlcodemonkey/nilla-components/css");
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```css
|
|
38
|
+
@import url("@avlcodemonkey/nilla-components/css/tokens.css");
|
|
39
|
+
@import url("@avlcodemonkey/nilla-components/css/table.css");
|
|
40
|
+
@import url("@avlcodemonkey/nilla-components/css/dialog.css");
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
CSS files: `alert.css`, `autocomplete.css`, `dialog.css` (info, confirm and unsaved changes), `file-to-textarea.css`, `html-editor.css`, `json.css`, `list.css`, `nav-toggle.css`, `pjax.css`, `slider-input.css`, `table.css`, `unsaved-changes.css`. The other components have none.
|
|
44
|
+
|
|
45
|
+
Theme with `--nilla-*` custom properties on `:root`, such as `--nilla-primary`, `--nilla-background`, `--nilla-surface`, `--nilla-color`, `--nilla-border`, `--nilla-error` and `--nilla-radius`. Dark colors apply only when the page sets `color-scheme`.
|
|
46
|
+
|
|
47
|
+
Without a bundler, the page needs an import map for the package, and for `dompurify` if it uses the HTML editor.
|
|
48
|
+
|
|
49
|
+
Spanish text: `import '@avlcodemonkey/nilla-components/locales/es';` and set `lang="es"` on `<html>`. Other languages: `setLocale(lang, texts)` from `@avlcodemonkey/nilla-components/utils`.
|
|
50
|
+
|
|
51
|
+
## Server contract
|
|
52
|
+
|
|
53
|
+
- Every request has the header `X-Requested-With: XMLHttpRequest`.
|
|
54
|
+
- Table: returns a JSON array of objects.
|
|
55
|
+
- Autocomplete: gets `query` in the query string, returns a JSON array of `{ "label": "…", "value": "…" }`.
|
|
56
|
+
- PJax: requests also have `x-pjax: true` and `x-pjax-version`. Return the `text/html` for the target element only, not a whole page. Optional response headers: `x-pjax-title`, `x-pjax-description`, `x-pjax-canonical`, `x-pjax-og-type`, `x-pjax-push-url`, `x-pjax-refresh`. Send `Vary: X-PJax` if a shared cache is in front.
|
|
57
|
+
|
|
58
|
+
## Components
|
|
59
|
+
|
|
60
|
+
### Alert (`alert`)
|
|
61
|
+
|
|
62
|
+
A message the user can dismiss. `data-variant`: `success`, `error` or `warning`.
|
|
63
|
+
|
|
64
|
+
```html
|
|
65
|
+
<nilla-alert data-variant="success">
|
|
66
|
+
<div role="status">
|
|
67
|
+
<p>Saved.</p>
|
|
68
|
+
<button type="button" data-alert-dismiss aria-label="Dismiss">×</button>
|
|
69
|
+
</div>
|
|
70
|
+
</nilla-alert>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Autocomplete (`autocomplete`)
|
|
74
|
+
|
|
75
|
+
Suggestions from the server. The hidden input is submitted. Give it `value`, and the display input `value` and `data-label`, when a value is already chosen.
|
|
76
|
+
|
|
77
|
+
```html
|
|
78
|
+
<nilla-autocomplete data-src-url="/users/search" data-empty-message="No matches.">
|
|
79
|
+
<label>User <input type="text" autocomplete="off" data-autocomplete-display></label>
|
|
80
|
+
<input type="hidden" name="userId" data-autocomplete-value>
|
|
81
|
+
</nilla-autocomplete>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Confirm dialog (`confirm-dialog`)
|
|
85
|
+
|
|
86
|
+
Asks before a link or button does its normal thing. Only `data-dialog-content` is required.
|
|
87
|
+
|
|
88
|
+
```html
|
|
89
|
+
<form action="/files/3/delete" method="post">
|
|
90
|
+
<nilla-confirm>
|
|
91
|
+
<button type="submit" data-dialog-content="Delete this file?" data-dialog-ok="Delete" data-dialog-cancel="Keep it">Delete</button>
|
|
92
|
+
</nilla-confirm>
|
|
93
|
+
</form>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Copy to clipboard (`copy-to-clipboard`)
|
|
97
|
+
|
|
98
|
+
Needs HTTPS or localhost.
|
|
99
|
+
|
|
100
|
+
```html
|
|
101
|
+
<nilla-copy-to-clipboard>
|
|
102
|
+
<input type="text" value="https://example.com/invite" readonly data-copy-content aria-label="Invite link">
|
|
103
|
+
<button type="button" data-copy-button>Copy</button>
|
|
104
|
+
<small data-copy-status hidden>Copied</small>
|
|
105
|
+
</nilla-copy-to-clipboard>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Date formatter (`date-formatter`)
|
|
109
|
+
|
|
110
|
+
Shows a UTC date in the reader's time and the page's language. Without a format it's written the way the language does. A date without a time is that day everywhere.
|
|
111
|
+
|
|
112
|
+
```html
|
|
113
|
+
<nilla-date data-date-format="MMM D, YYYY [at] h:mm A">2026-11-29T15:30:00Z</nilla-date>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Date time input (`datetime-input`)
|
|
117
|
+
|
|
118
|
+
The user edits local time, and the hidden input submits UTC.
|
|
119
|
+
|
|
120
|
+
```html
|
|
121
|
+
<nilla-datetime-input>
|
|
122
|
+
<label>Starts <input type="datetime-local" data-datetime-local></label>
|
|
123
|
+
<input type="hidden" name="startsUtc" value="2026-12-01T14:00:00Z" data-datetime-hidden>
|
|
124
|
+
</nilla-datetime-input>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Display toggle (`display-toggle`)
|
|
128
|
+
|
|
129
|
+
Shows the fields that go with the selected option. Hidden fields still submit and validate, so don't make them `required`. One select for each component.
|
|
130
|
+
|
|
131
|
+
```html
|
|
132
|
+
<nilla-display-toggle>
|
|
133
|
+
<label>Type
|
|
134
|
+
<select name="type" data-display-toggle-trigger>
|
|
135
|
+
<option value="percent" data-display-toggle-target="percent">Percent</option>
|
|
136
|
+
<option value="users" data-display-toggle-target="users">Users</option>
|
|
137
|
+
</select>
|
|
138
|
+
</label>
|
|
139
|
+
<label data-display-toggle="percent">Percent <input type="number" name="percent"></label>
|
|
140
|
+
<label data-display-toggle="users">Users <input type="text" name="users"></label>
|
|
141
|
+
</nilla-display-toggle>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### File to textarea (`file-to-textarea`)
|
|
145
|
+
|
|
146
|
+
Reads a text file into a textarea. `data-max-bytes` is a plain number.
|
|
147
|
+
|
|
148
|
+
```html
|
|
149
|
+
<nilla-file-to-textarea data-max-bytes="20000">
|
|
150
|
+
<label>Import <input type="file" accept=".json" data-file-input></label>
|
|
151
|
+
<small data-file-error hidden></small>
|
|
152
|
+
<textarea name="content" aria-label="Content"></textarea>
|
|
153
|
+
</nilla-file-to-textarea>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### HTML editor (`html-editor`)
|
|
157
|
+
|
|
158
|
+
A rich text editor. The textarea is submitted, with HTML in it, so encode its content. The counter is optional, and needs `maxlength`.
|
|
159
|
+
|
|
160
|
+
```html
|
|
161
|
+
<nilla-html-editor>
|
|
162
|
+
<textarea name="body" maxlength="2000" aria-label="Body" data-html-input><p>Text</p></textarea>
|
|
163
|
+
<small data-html-counter data-over-text="{{count}} over"><span data-html-count></span> / <span data-html-max></span> <span data-html-over></span></small>
|
|
164
|
+
</nilla-html-editor>
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Info dialog (`info-dialog`)
|
|
168
|
+
|
|
169
|
+
Shows a message. With the attributes on the component itself, open it with `element.show()`.
|
|
170
|
+
|
|
171
|
+
```html
|
|
172
|
+
<nilla-info>
|
|
173
|
+
<button type="button" data-dialog-content="Reports update every hour." data-dialog-ok="Got it">What's this?</button>
|
|
174
|
+
</nilla-info>
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### JSON formatter (`json-formatter`)
|
|
178
|
+
|
|
179
|
+
HTML-encode the JSON.
|
|
180
|
+
|
|
181
|
+
```html
|
|
182
|
+
<nilla-json>{"name":"report","enabled":true}</nilla-json>
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### List (`list`, or `list-sortable` for dragging)
|
|
186
|
+
|
|
187
|
+
Adds, removes and reorders items. Arrow keys on the drag handle reorder. Dragging needs `npm install sortablejs` and the `list-sortable` import instead of `list`.
|
|
188
|
+
|
|
189
|
+
```html
|
|
190
|
+
<nilla-list data-index-placeholder="{{index}}">
|
|
191
|
+
<template data-list-template>
|
|
192
|
+
<div data-list-item>
|
|
193
|
+
<button type="button" data-list-drag-handle aria-label="Move">⠿</button>
|
|
194
|
+
<input type="email" name="emails[{{index}}]" aria-label="Email">
|
|
195
|
+
<button type="button" data-list-remove-button aria-label="Remove">×</button>
|
|
196
|
+
</div>
|
|
197
|
+
</template>
|
|
198
|
+
<div data-list-container></div>
|
|
199
|
+
<p data-list-empty-message>No email addresses yet.</p>
|
|
200
|
+
<button type="button" data-list-add-button>Add email</button>
|
|
201
|
+
</nilla-list>
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Nav toggle (`nav-toggle`)
|
|
205
|
+
|
|
206
|
+
A side nav: a drawer at 1024px and below, and collapsible to icons above that. The nav must be inside. Labels are plain spans; icons have `aria-hidden="true"`.
|
|
207
|
+
|
|
208
|
+
```html
|
|
209
|
+
<nilla-nav-toggle>
|
|
210
|
+
<button type="button" popovertarget="side-nav" data-nav-toggle-drawer>Menu</button>
|
|
211
|
+
<button type="button" data-nav-toggle-target="side-nav" aria-controls="side-nav">Menu</button>
|
|
212
|
+
<nav id="side-nav" aria-label="Main" popover>
|
|
213
|
+
<button type="button" popovertarget="side-nav" popovertargetaction="hide" data-nav-toggle-drawer>Close</button>
|
|
214
|
+
<ul>
|
|
215
|
+
<li><a href="/"><span aria-hidden="true">⌂</span> <span>Home</span></a></li>
|
|
216
|
+
</ul>
|
|
217
|
+
</nav>
|
|
218
|
+
</nilla-nav-toggle>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### PJax (`pjax`)
|
|
222
|
+
|
|
223
|
+
Loads same-origin links and forms into the target without a full page load. Usually wraps the page body. `data-scroll`: `top` (default), `target` or `none`. `data-pjax-no-follow` on a link or form leaves it to the browser. `data-pjax-method` on a form sends `PUT`, `DELETE` and so on. Scripts in responses don't run.
|
|
224
|
+
|
|
225
|
+
```html
|
|
226
|
+
<nilla-pjax data-version="1.0.0">
|
|
227
|
+
<nav aria-label="Pages"><a href="/reports">Reports</a></nav>
|
|
228
|
+
<div data-pjax-loading-indicator></div>
|
|
229
|
+
<main data-pjax-target>Content</main>
|
|
230
|
+
<nilla-info data-pjax-info-dialog data-dialog-content="That page couldn't be loaded."></nilla-info>
|
|
231
|
+
</nilla-pjax>
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Share (`share`)
|
|
235
|
+
|
|
236
|
+
Opens the share sheet where there is one. Otherwise it's an ordinary link.
|
|
237
|
+
|
|
238
|
+
```html
|
|
239
|
+
<nilla-share><a href="https://example.com/post" title="Post title">Share</a></nilla-share>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Slider input (`slider-input`)
|
|
243
|
+
|
|
244
|
+
Keeps a range and a number input in sync. Give only one a name.
|
|
245
|
+
|
|
246
|
+
```html
|
|
247
|
+
<nilla-slider-input>
|
|
248
|
+
<input type="range" min="0" max="100" value="25" data-slider-range aria-label="Volume slider">
|
|
249
|
+
<input type="number" name="volume" min="0" max="100" value="25" data-slider-text aria-label="Volume">
|
|
250
|
+
</nilla-slider-input>
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Table (`table`)
|
|
254
|
+
|
|
255
|
+
Loads all rows once, then searches, sorts and pages in the browser. `{{property}}` is escaped. `{{#if property}}…{{/if}}` and `{{#if !property}}…{{/if}}` can't be nested, and go inside a cell or around a whole row. `data-key` saves the user's settings. For a filter form, use `data-src-form` with the form's ID instead of `data-src-url`; the form must come before the table.
|
|
256
|
+
|
|
257
|
+
```html
|
|
258
|
+
<nilla-table data-key="people" data-src-url="/people/list" data-sort-property="name">
|
|
259
|
+
<input type="search" data-table-search aria-label="Search">
|
|
260
|
+
<table>
|
|
261
|
+
<thead><tr><th data-property="name">Name</th><th data-property="team">Team</th></tr></thead>
|
|
262
|
+
<tbody>
|
|
263
|
+
<template><tr><td>{{name}}</td><td>{{team}}</td></tr></template>
|
|
264
|
+
<tr data-table-status>
|
|
265
|
+
<td colspan="2">
|
|
266
|
+
<span data-table-loading>Loading…</span>
|
|
267
|
+
<span data-table-empty hidden>No people.</span>
|
|
268
|
+
<span data-table-error hidden>Couldn't load people. <button type="button" data-table-retry>Retry</button></span>
|
|
269
|
+
</td>
|
|
270
|
+
</tr>
|
|
271
|
+
</tbody>
|
|
272
|
+
</table>
|
|
273
|
+
<div data-table-pager>
|
|
274
|
+
<small><span data-table-start-row></span>–<span data-table-end-row></span> of <span data-table-filtered-rows></span></small>
|
|
275
|
+
<button type="button" data-table-previous-page>Previous</button>
|
|
276
|
+
<button type="button" data-table-next-page>Next</button>
|
|
277
|
+
</div>
|
|
278
|
+
</nilla-table>
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Time offset select (`time-offset-select`)
|
|
282
|
+
|
|
283
|
+
Selects the browser's UTC offset. Values are minutes east of UTC.
|
|
284
|
+
|
|
285
|
+
```html
|
|
286
|
+
<nilla-time-offset data-browser-default="true">
|
|
287
|
+
<label>Time zone
|
|
288
|
+
<select name="offset">
|
|
289
|
+
<option value="-300">UTC−05:00</option>
|
|
290
|
+
<option value="0" selected>UTC</option>
|
|
291
|
+
</select>
|
|
292
|
+
</label>
|
|
293
|
+
</nilla-time-offset>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Unsaved changes (`unsaved-changes`)
|
|
297
|
+
|
|
298
|
+
Warns before leaving a changed form. `data-unsaved="true"` treats it as changed from the start, such as after a failed save.
|
|
299
|
+
|
|
300
|
+
```html
|
|
301
|
+
<nilla-unsaved-changes>
|
|
302
|
+
<form action="/posts/3" method="post">
|
|
303
|
+
<label>Title <input type="text" name="title" value="Report"></label>
|
|
304
|
+
<button type="submit">Save</button>
|
|
305
|
+
<span data-unsaved-indicator hidden>Unsaved changes</span>
|
|
306
|
+
</form>
|
|
307
|
+
</nilla-unsaved-changes>
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## Events
|
|
311
|
+
|
|
312
|
+
- `nilla:pjax-navigating` on `document`, before PJax navigates. Cancelable: after `preventDefault()`, call the event's `proceed()` or `cancel()`.
|
|
313
|
+
- `nilla:pjax-error` on the form that was sent, or else on `nilla-pjax`, when a request fails. Bubbles. `event.aborted` is true for a form whose request was stopped by another.
|
|
314
|
+
- `nilla:list-reorder` on `nilla-list`, after an item moves. Bubbles.
|
|
315
|
+
- `input` and `change`, whenever a component changes form content.
|
|
316
|
+
|
|
317
|
+
## Your own components
|
|
318
|
+
|
|
319
|
+
Extend `BaseComponent` from `@avlcodemonkey/nilla-components/base-component`, or `BaseDialog` from `@avlcodemonkey/nilla-components/base-dialog`, and register with `defineElement` from `@avlcodemonkey/nilla-components/utils`. Find parts with `this.getElement('name')`, which looks for `data-{prefix}-name`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,122 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@avlcodemonkey/nilla-components",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vanilla JavaScript web components with semantic, themeable CSS. No framework, no build step required.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"web-components",
|
|
7
|
+
"custom-elements",
|
|
8
|
+
"vanilla-js",
|
|
9
|
+
"pjax",
|
|
10
|
+
"table",
|
|
11
|
+
"dialog",
|
|
12
|
+
"autocomplete",
|
|
13
|
+
"html-editor"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "avlcodemonkey",
|
|
17
|
+
"homepage": "https://avlcodemonkey-industries.github.io/NillaComponents/",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/AvlCodeMonkey-Industries/NillaComponents.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/AvlCodeMonkey-Industries/NillaComponents/issues"
|
|
24
|
+
},
|
|
25
|
+
"funding": {
|
|
26
|
+
"type": "github",
|
|
27
|
+
"url": "https://github.com/sponsors/avlcodemonkey"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22"
|
|
32
|
+
},
|
|
33
|
+
"sideEffects": [
|
|
34
|
+
"./src/index.js",
|
|
35
|
+
"./src/components/*.js",
|
|
36
|
+
"./src/vendor/wyzi.js",
|
|
37
|
+
"./src/locales/*.js",
|
|
38
|
+
"./css/*.css"
|
|
39
|
+
],
|
|
40
|
+
"main": "./src/index.js",
|
|
41
|
+
"exports": {
|
|
42
|
+
".": "./src/index.js",
|
|
43
|
+
"./alert": "./src/components/Alert.js",
|
|
44
|
+
"./autocomplete": "./src/components/Autocomplete.js",
|
|
45
|
+
"./base-component": "./src/components/BaseComponent.js",
|
|
46
|
+
"./base-dialog": "./src/components/BaseDialog.js",
|
|
47
|
+
"./confirm-dialog": "./src/components/ConfirmDialog.js",
|
|
48
|
+
"./copy-to-clipboard": "./src/components/CopyToClipboard.js",
|
|
49
|
+
"./date-formatter": "./src/components/DateFormatter.js",
|
|
50
|
+
"./datetime-input": "./src/components/DateTimeInput.js",
|
|
51
|
+
"./display-toggle": "./src/components/DisplayToggle.js",
|
|
52
|
+
"./file-to-textarea": "./src/components/FileToTextarea.js",
|
|
53
|
+
"./html-editor": "./src/components/HtmlEditor.js",
|
|
54
|
+
"./info-dialog": "./src/components/InfoDialog.js",
|
|
55
|
+
"./json-formatter": "./src/components/JsonFormatter.js",
|
|
56
|
+
"./list": "./src/components/List.js",
|
|
57
|
+
"./list-sortable": "./src/components/ListSortable.js",
|
|
58
|
+
"./nav-toggle": "./src/components/NavToggle.js",
|
|
59
|
+
"./pjax": "./src/components/PJax.js",
|
|
60
|
+
"./share": "./src/components/Share.js",
|
|
61
|
+
"./slider-input": "./src/components/SliderInput.js",
|
|
62
|
+
"./table": "./src/components/Table.js",
|
|
63
|
+
"./time-offset-select": "./src/components/TimeOffsetSelect.js",
|
|
64
|
+
"./unsaved-changes": "./src/components/UnsavedChanges.js",
|
|
65
|
+
"./utils": "./src/utils/index.js",
|
|
66
|
+
"./constants/*": "./src/constants/*.js",
|
|
67
|
+
"./events/*": "./src/events/*.js",
|
|
68
|
+
"./errors/*": "./src/errors/*.js",
|
|
69
|
+
"./locales/*.json": "./src/locales/*.json",
|
|
70
|
+
"./locales/*": "./src/locales/*.js",
|
|
71
|
+
"./css": "./css/nilla.css",
|
|
72
|
+
"./css/*": "./css/*",
|
|
73
|
+
"./package.json": "./package.json"
|
|
74
|
+
},
|
|
75
|
+
"files": [
|
|
76
|
+
"src",
|
|
77
|
+
"css",
|
|
78
|
+
"docs/llms.txt",
|
|
79
|
+
"THIRD_PARTY_NOTICES.md",
|
|
80
|
+
"CHANGELOG.md"
|
|
81
|
+
],
|
|
82
|
+
"publishConfig": {
|
|
83
|
+
"access": "public",
|
|
84
|
+
"provenance": true
|
|
85
|
+
},
|
|
86
|
+
"scripts": {
|
|
87
|
+
"test": "node --test --experimental-test-coverage --test-reporter spec \"tests/**/*.test.js\"",
|
|
88
|
+
"test:ci": "node --test --experimental-test-coverage --test-reporter junit --test-reporter-destination test-results.xml \"tests/**/*.test.js\"",
|
|
89
|
+
"test:browser": "node --test --test-timeout 180000 \"tests/browser/*.spec.js\"",
|
|
90
|
+
"js:lint": "eslint .",
|
|
91
|
+
"js:fix": "eslint --fix .",
|
|
92
|
+
"css:lint": "stylelint \"css/**/*.css\" \"docs/**/*.css\"",
|
|
93
|
+
"css:fix": "stylelint \"css/**/*.css\" \"docs/**/*.css\" --fix",
|
|
94
|
+
"lint": "npm run js:lint && npm run css:lint",
|
|
95
|
+
"docs": "node scripts/serve-docs.mjs",
|
|
96
|
+
"docs:vendor": "node scripts/vendor-docs.mjs"
|
|
97
|
+
},
|
|
98
|
+
"dependencies": {
|
|
99
|
+
"dompurify": "^3.5.0"
|
|
100
|
+
},
|
|
101
|
+
"peerDependencies": {
|
|
102
|
+
"sortablejs": "^1.15.0"
|
|
103
|
+
},
|
|
104
|
+
"peerDependenciesMeta": {
|
|
105
|
+
"sortablejs": {
|
|
106
|
+
"optional": true
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"devDependencies": {
|
|
110
|
+
"@eslint/js": "10.0.1",
|
|
111
|
+
"@stylistic/eslint-plugin": "6.0.1",
|
|
112
|
+
"axe-core": "4.14.1",
|
|
113
|
+
"eslint": "10.15.0",
|
|
114
|
+
"eslint-plugin-jsdoc": "65.5.3",
|
|
115
|
+
"globals": "17.14.0",
|
|
116
|
+
"jsdom": "31.0.1",
|
|
117
|
+
"puppeteer-core": "25.19.0",
|
|
118
|
+
"sortablejs": "1.15.7",
|
|
119
|
+
"stylelint": "18.1.0",
|
|
120
|
+
"stylelint-config-standard": "41.1.0"
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import BaseComponent from './BaseComponent.js';
|
|
2
|
+
import defineElement from '../utils/defineElement.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Dismissable alert web component.
|
|
6
|
+
*/
|
|
7
|
+
class Alert extends BaseComponent {
|
|
8
|
+
constructor() {
|
|
9
|
+
super('alert');
|
|
10
|
+
|
|
11
|
+
this.getElements('dismiss').forEach(btn => btn.addEventListener('click', this));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hide the alert when the dismiss button is clicked.
|
|
16
|
+
*/
|
|
17
|
+
handleEvent() {
|
|
18
|
+
this.hidden = true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
defineElement('nilla-alert', Alert);
|
|
23
|
+
|
|
24
|
+
export default Alert;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import notifyChange from '../utils/notifyChange.js';
|
|
2
|
+
import autocomplete from '../vendor/autocomplete.js';
|
|
3
|
+
import BaseComponent from './BaseComponent.js';
|
|
4
|
+
import defineElement from '../utils/defineElement.js';
|
|
5
|
+
import fetchJsonArray from '../utils/fetchJsonArray.js';
|
|
6
|
+
import numberSetting from '../utils/numberSetting.js';
|
|
7
|
+
import requestTimeout from '../utils/requestTimeout.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef AutocompleteItem
|
|
11
|
+
* @type {object}
|
|
12
|
+
* @property {string} label Label to display for the item.
|
|
13
|
+
* @property {string} value Value to use when selecting the item.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {import("autocompleter").AutocompleteResult} AutocompleteResult
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Web component for an input autocomplete.
|
|
22
|
+
*/
|
|
23
|
+
class Autocomplete extends BaseComponent {
|
|
24
|
+
/**
|
|
25
|
+
* Suggestion list for the input, while the element is in the page.
|
|
26
|
+
* @type {AutocompleteResult|undefined}
|
|
27
|
+
*/
|
|
28
|
+
#autocompleter;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Clear the selected value when the search text changes.
|
|
32
|
+
*/
|
|
33
|
+
constructor() {
|
|
34
|
+
super('autocomplete');
|
|
35
|
+
|
|
36
|
+
const inputs = this.#getInputs();
|
|
37
|
+
if (!inputs) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const { displayInput, valueInput } = inputs;
|
|
41
|
+
|
|
42
|
+
// clear out the value in the hidden input when changing the search value. it'll get re-set in onSelect
|
|
43
|
+
displayInput.addEventListener('change', () => {
|
|
44
|
+
if (displayInput.value !== displayInput.dataset.label) {
|
|
45
|
+
displayInput.value = '';
|
|
46
|
+
if (valueInput.value) {
|
|
47
|
+
valueInput.value = '';
|
|
48
|
+
notifyChange(valueInput);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Gets the inputs, when the component has everything it needs to suggest.
|
|
56
|
+
* @returns {{ displayInput: HTMLInputElement, valueInput: HTMLInputElement }|null} Input the user types in and the
|
|
57
|
+
* hidden input that's submitted, or null if either, or `data-src-url`, is missing.
|
|
58
|
+
*/
|
|
59
|
+
#getInputs() {
|
|
60
|
+
const displayInput = /** @type {HTMLInputElement|null|undefined} */ (this.getElement('display'));
|
|
61
|
+
const valueInput = /** @type {HTMLInputElement|null|undefined} */ (this.getElement('value'));
|
|
62
|
+
return this.dataset.srcUrl && displayInput && valueInput ? { displayInput, valueInput } : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Start suggesting. The autocompleter listens on the window and document, so it's created each time the element is
|
|
67
|
+
* added to the page, including when it's moved.
|
|
68
|
+
*/
|
|
69
|
+
connectedCallback() {
|
|
70
|
+
const inputs = this.#getInputs();
|
|
71
|
+
if (!inputs) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const { displayInput, valueInput } = inputs;
|
|
75
|
+
const { srcUrl, emptyMessage } = this.dataset;
|
|
76
|
+
|
|
77
|
+
const timeout = requestTimeout(this.dataset.timeout);
|
|
78
|
+
|
|
79
|
+
this.#autocompleter = autocomplete({
|
|
80
|
+
minLength: numberSetting(this.dataset.minLength, 2, { integer: true }),
|
|
81
|
+
preventSubmit: 2, // PreventSubmit.OnSelect
|
|
82
|
+
emptyMsg: emptyMessage,
|
|
83
|
+
input: displayInput,
|
|
84
|
+
debounceWaitMs: numberSetting(this.dataset.delay, 250, { allowZero: true }),
|
|
85
|
+
async fetch(query, update) {
|
|
86
|
+
if (!query) {
|
|
87
|
+
update([]);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let suggestions;
|
|
92
|
+
try {
|
|
93
|
+
const url = new URL(srcUrl, window.location.href);
|
|
94
|
+
url.searchParams.set('query', query);
|
|
95
|
+
suggestions = await fetchJsonArray(url, { timeout });
|
|
96
|
+
} catch (ex) {
|
|
97
|
+
console.error(ex);
|
|
98
|
+
suggestions = [];
|
|
99
|
+
}
|
|
100
|
+
update(suggestions);
|
|
101
|
+
},
|
|
102
|
+
onSelect(/** @type {AutocompleteItem} */ item) {
|
|
103
|
+
const previous = valueInput.value;
|
|
104
|
+
valueInput.value = item.value;
|
|
105
|
+
displayInput.value = item.label;
|
|
106
|
+
displayInput.dataset.label = item.label;
|
|
107
|
+
// not on the display input, since an input event there would search again
|
|
108
|
+
if (valueInput.value !== previous) {
|
|
109
|
+
notifyChange(valueInput);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Remove the autocompleter and its listeners.
|
|
117
|
+
*/
|
|
118
|
+
disconnectedCallback() {
|
|
119
|
+
this.#autocompleter?.destroy();
|
|
120
|
+
this.#autocompleter = undefined;
|
|
121
|
+
super.disconnectedCallback();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
defineElement('nilla-autocomplete', Autocomplete);
|
|
126
|
+
|
|
127
|
+
export default Autocomplete;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extend the HTMLElement class to provide common functionality for custom components.
|
|
3
|
+
*/
|
|
4
|
+
class BaseComponent extends HTMLElement {
|
|
5
|
+
/**
|
|
6
|
+
* Stores commonly queried DOM elements as kvp to improve performance.
|
|
7
|
+
* @type {object}
|
|
8
|
+
*/
|
|
9
|
+
#elementCache = {};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Prefix for finding elements via data attributes, and caching elements in memory.
|
|
13
|
+
* @type {string}
|
|
14
|
+
*/
|
|
15
|
+
#elementPrefix = '';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Initialize the component.
|
|
19
|
+
* @param {string} key Key for the component, used to identify it.
|
|
20
|
+
*/
|
|
21
|
+
constructor(key) {
|
|
22
|
+
super();
|
|
23
|
+
|
|
24
|
+
this.#elementPrefix = key ?? '';
|
|
25
|
+
this.#elementCache = {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Clean up when the component is removed from the document.
|
|
30
|
+
* Don't want to keep references to any elements that have been removed.
|
|
31
|
+
*/
|
|
32
|
+
disconnectedCallback() {
|
|
33
|
+
this.#elementCache = {};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets the specified DOM element from cache, or finds the first match and adds it to the cache.
|
|
38
|
+
* Will match element with attribute `data-{elementPrefix}-{elementKey}`, or `data-{elementKey}` if prefix is empty.
|
|
39
|
+
* @param {string} elementKey Key for the element to find.
|
|
40
|
+
* @returns {HTMLElement|null|undefined} Element to find, or nothing if not found.
|
|
41
|
+
*/
|
|
42
|
+
getElement(elementKey) {
|
|
43
|
+
const cached = this.#elementCache[elementKey];
|
|
44
|
+
// an element that has been removed or replaced is looked up again
|
|
45
|
+
if (cached && this.contains(cached)) {
|
|
46
|
+
return cached;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const [match] = this.getElements(elementKey);
|
|
50
|
+
this.#elementCache[elementKey] = match;
|
|
51
|
+
return match;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Finds every element in this component with attribute `data-{elementPrefix}-{elementKey}`, or `data-{elementKey}` if
|
|
56
|
+
* prefix is empty. Elements that belong to a nested component of the same kind are skipped.
|
|
57
|
+
* @param {string} elementKey Key for the elements to find.
|
|
58
|
+
* @returns {HTMLElement[]} Matching elements, in document order.
|
|
59
|
+
*/
|
|
60
|
+
getElements(elementKey) {
|
|
61
|
+
const key = this.#elementPrefix ? `${this.#elementPrefix}-${elementKey}` : elementKey;
|
|
62
|
+
return /** @type {HTMLElement[]} */ ([...this.querySelectorAll(`[data-${key}]`)])
|
|
63
|
+
.filter(x => x.closest(this.localName) === this);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Sets the prefix for finding elements via data attributes, and caching elements in memory.
|
|
68
|
+
* This is used for testing purposes only. Key should be set via the constructor.
|
|
69
|
+
* @param {string} key Key to set as the prefix for element queries.
|
|
70
|
+
*/
|
|
71
|
+
_setKey(key) {
|
|
72
|
+
if (key) {
|
|
73
|
+
this.#elementPrefix = key;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export default BaseComponent;
|