@answerdotai/answerui 0.0.0-stage → 0.0.4
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/LICENSE +201 -0
- package/README.md +144 -2
- package/answerui/assets/browser.js +150 -0
- package/answerui/assets/copy.js +7 -0
- package/answerui/assets/fit.js +98 -0
- package/answerui/assets/floating.js +78 -0
- package/answerui/assets/fonts/IBM-Plex.LICENSE +93 -0
- package/answerui/assets/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
- package/answerui/assets/fonts/ibm-plex-sans-latin-400-italic.woff2 +0 -0
- package/answerui/assets/fonts/ibm-plex-sans-latin-400-normal.woff2 +0 -0
- package/answerui/assets/fonts/ibm-plex-sans-latin-600-normal.woff2 +0 -0
- package/answerui/assets/fonts/ibm-plex-serif-latin-600-normal.woff2 +0 -0
- package/answerui/assets/fonts.css +7 -0
- package/answerui/assets/interactions.js +92 -0
- package/answerui/assets/styles.css +853 -0
- package/package.json +22 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2022, fastai
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,145 @@
|
|
|
1
|
-
#
|
|
1
|
+
# answerui
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
|
5
|
+
|
|
6
|
+
Use the `@answerdotai/answerui` npm package for the browser foundation: a stylesheet keyed on semantic HTML attributes and CSS custom properties, optional fonts, and vanilla JavaScript modules. Use the `answerui-py` Python package to add [FastHTML](https://fastht.ml) constructors and a registry for app-specific CSS and browser behavior on top of those same assets.
|
|
7
|
+
|
|
8
|
+
Python constructors emit marked HTML, such as `<section card>` from `Card()` and `<button btn>` from `Btn()`. AnswerUI does not restyle unmarked form elements. Frontend frameworks such as Svelte can emit the same markup and import the JavaScript modules directly without Python. The notebooks cover the generated OKLCH palette, typography and spacing scales, parent-driven layouts, UI components, and the Python registry.
|
|
9
|
+
|
|
10
|
+
Read `answerui.skill` with `doc(answerui.skill)` for an LLM guide to building FastHTML pages, inspecting and customising registered components, and previewing results in a persistent Python kernel.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
For a JavaScript app:
|
|
15
|
+
|
|
16
|
+
``` sh
|
|
17
|
+
npm install @answerdotai/answerui
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Import the stylesheet once:
|
|
21
|
+
|
|
22
|
+
``` js
|
|
23
|
+
import '@answerdotai/answerui/styles.css';
|
|
24
|
+
import {mountPopover} from '@answerdotai/answerui/floating.js';
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`@answerdotai/answerui/styles.css` works without JavaScript, including native dialogs, disclosures, and CSS tooltips. Import individual browser modules only for components that require JavaScript: `@answerdotai/answerui/floating.js` for popovers, `@answerdotai/answerui/fit.js` for responsive navbars, `@answerdotai/answerui/interactions.js` for tabs, toolbars, and hover cards, and `@answerdotai/answerui/copy.js` for clipboard actions. Load `@answerdotai/answerui/fonts.css` to use the bundled IBM Plex font faces.
|
|
28
|
+
|
|
29
|
+
For server-rendered or static HTML, `import '@answerdotai/answerui/browser.js'` mounts those behaviors automatically on marked elements and observes later DOM insertions. In Svelte or another component framework, import the individual modules inside your component lifecycle instead of loading `@answerdotai/answerui/browser.js`.
|
|
30
|
+
|
|
31
|
+
For FastHTML:
|
|
32
|
+
|
|
33
|
+
``` sh
|
|
34
|
+
pip install answerui-py
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
If you serve the browser assets directly from Python, `from answerui import assets` returns the packaged `Path` to the assets directory.
|
|
38
|
+
|
|
39
|
+
## Use from Python
|
|
40
|
+
|
|
41
|
+
`reg.hdrs()` returns the shared foundation stylesheet, browser scripts, and registered app rules. `reg.ftrs()` returns the `fastlucide` icon sprite sheet. Because FastHTML evaluates both on every response, newly registered rules take effect on the next request without a restart:
|
|
42
|
+
|
|
43
|
+
``` python
|
|
44
|
+
from fasthtml.common import *
|
|
45
|
+
from answerui.all import *
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
``` python
|
|
49
|
+
app = FastHTML(hdrs=reg.hdrs(), ftrs=reg.ftrs())
|
|
50
|
+
rt = app.route
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To serve cached assets instead of inlining them on every page, create the app without `reg.hdrs()` or `reg.ftrs()`, then call `reg.serve(app)`. That call registers the headers, icon sprite sheet, and content-hashed asset routes while preserving relative imports between the browser JavaScript modules.
|
|
54
|
+
|
|
55
|
+
``` python
|
|
56
|
+
served_app = FastHTML()
|
|
57
|
+
reg.serve(served_app);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Each Python constructor renders standard HTML with a semantic attribute. Below, `Card` renders `<section card>` and `Stack` wraps the children in `<div stack>` to space them vertically:
|
|
61
|
+
|
|
62
|
+
``` python
|
|
63
|
+
page = Card(Stack(H2('Profile'), Field('Name', Input(name='name')), Cluster(Btn('Save'), Btn('Cancel', variant='secondary'))))
|
|
64
|
+
demo(page)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
``` html
|
|
68
|
+
<section card>
|
|
69
|
+
<div stack>
|
|
70
|
+
<h2>Profile</h2>
|
|
71
|
+
<div field>
|
|
72
|
+
<label for="_d_HaIOHCRvCTv3Ba6WYo-A">Name</label> <div field-control>
|
|
73
|
+
<input input name="name" id="_d_HaIOHCRvCTv3Ba6WYo-A">
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
<div cluster>
|
|
77
|
+
<button type="button" btn btn-variant="primary">Save</button><button type="button" btn btn-variant="secondary">Cancel</button> </div>
|
|
78
|
+
</div>
|
|
79
|
+
</section>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`demo(page)` renders the Python markup with the same stylesheet and browser scripts used by JavaScript applications.
|
|
83
|
+
|
|
84
|
+
## Customise
|
|
85
|
+
|
|
86
|
+
Register standard CSS at the scope you want to change: `:root` custom properties via `reg.rule` for the whole app, a scoped selector for one region, or a component rule via `Btn.rule`. Because registered rules are placed in the `app` cascade layer after every framework layer except `utilities`, they override AnswerUI defaults without modifying the package:
|
|
87
|
+
|
|
88
|
+
``` python
|
|
89
|
+
reg.rule(':root { --hue: 150; --radius: 0; }', 'theme')
|
|
90
|
+
Btn.rule('caps', 'text-transform: uppercase;')
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Btn.caps [app] [btn]
|
|
94
|
+
|
|
95
|
+
Call `.variant()` on a component to declare a named style modifier. The component constructor then accepts that variant name as a keyword argument. Declared variants also appear in the component’s documentation:
|
|
96
|
+
|
|
97
|
+
``` python
|
|
98
|
+
Btn.variant('cta', '--ui-pad-block: var(--space); --ui-pad-inline: var(--space2); font-size: var(--text1);', doc='The landing page call to action.')
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Btn.cta [app] :is([btn])[cta]: The landing page call to action.
|
|
102
|
+
|
|
103
|
+
Declaring a variant registers an `app`-layer rule for that attribute on the component element without modifying the default styles in `styles.css`.
|
|
104
|
+
|
|
105
|
+
``` python
|
|
106
|
+
demo(Btn('Request access', cta=True))
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
``` html
|
|
110
|
+
<button type="button" btn btn-variant="primary" cta>Request access</button>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
``` python
|
|
114
|
+
Btn
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### `Btn(*c, variant='primary', size=None, href=None, type='button', icon=False, **attrs)`
|
|
118
|
+
|
|
119
|
+
A raised action, latched by aria-pressed and sunken while busy or expanded
|
|
120
|
+
|
|
121
|
+
Root selector: `[btn]`.
|
|
122
|
+
|
|
123
|
+
Declared variants:
|
|
124
|
+
- `cta`: The landing page call to action.
|
|
125
|
+
|
|
126
|
+
Rules:
|
|
127
|
+
- `base` (app)
|
|
128
|
+
- `caps` (app)
|
|
129
|
+
|
|
130
|
+
## Previews in notebooks
|
|
131
|
+
|
|
132
|
+
`demo(page)` returns a `Preview` with two MIME representations: an HTML iframe containing the full stylesheet and browser scripts, and a Markdown block of the HTML markup. Because each iframe embeds the entire stylesheet, committing them would bloat notebooks and churn diffs on every stylesheet edit. `Preview._repr_mimebundle_` sets nbdev’s `strip` metadata flag on the HTML output. When you run `nbdev-clean`, it strips the iframe and keeps the Markdown markup in git. In a fresh clone, run the notebook cells to display the iframes again.
|
|
133
|
+
|
|
134
|
+
For the docs, set `exec_all` in `pyproject.toml`:
|
|
135
|
+
|
|
136
|
+
``` toml
|
|
137
|
+
[tool.nbdev]
|
|
138
|
+
exec_all = true
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`nbdev-docs` then executes every notebook and renders each iframe from the current code.
|
|
142
|
+
|
|
143
|
+
## With an LLM
|
|
144
|
+
|
|
145
|
+
`answerui.skill` is a [pyskill](https://github.com/AnswerDotAI/pyskills) whose module docstring documents how to use AnswerUI in a Python kernel. Run `list_pyskills()` to discover it, `doc(answerui.skill)` to read the guide, and `doc(Btn)` to inspect a component’s signature, declared variants, and CSS rules.
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/* Generated by nbdev-export. Edit the source notebooks. */
|
|
2
|
+
|
|
3
|
+
import {mountPopover} from './floating.js';
|
|
4
|
+
import {mountTabs, mountToolbar, mountHoverPopover} from './interactions.js';
|
|
5
|
+
import {mountNavbar} from './fit.js';
|
|
6
|
+
import {copyText} from './copy.js';
|
|
7
|
+
|
|
8
|
+
function enable(form) {
|
|
9
|
+
if (!form) return;
|
|
10
|
+
const changed = [...form.elements].some(control => control.matches('input:not([type=hidden], [type=submit], [type=button], [type=reset]), select, textarea') &&
|
|
11
|
+
(control.type === 'checkbox' || control.type === 'radio' ? control.checked !== control.defaultChecked :
|
|
12
|
+
control.tagName === 'SELECT' ? [...control.options].some(option => option.selected !== option.defaultSelected) : control.value !== control.defaultValue));
|
|
13
|
+
const valid = [...form.elements].every(control => control.validity.valid);
|
|
14
|
+
for (const button of form.querySelectorAll('button[enable]'))
|
|
15
|
+
button.disabled = !button.getAttribute('enable').split(' ').every(want => want === 'changed' ? changed : want === 'valid' ? valid : true);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function range(input) {
|
|
19
|
+
const low = input.min === '' ? 0 : +input.min, high = input.max === '' ? 100 : +input.max;
|
|
20
|
+
input.style.setProperty('--ui-range-fill', `${high > low ? (input.value - low) / (high - low) * 100 : 0}%`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function validity(control, event) {
|
|
24
|
+
const field = control.closest('[field]');
|
|
25
|
+
if (!field) return;
|
|
26
|
+
let message = field.querySelector(':scope > small[validation]');
|
|
27
|
+
const ids = (control.getAttribute('aria-describedby') || '').split(' ').filter(id => id && id !== message?.id);
|
|
28
|
+
const show = event.type === 'invalid' || (event.type === 'focusout' ? control.matches(':user-invalid') : message && !control.validity.valid);
|
|
29
|
+
if (show) {
|
|
30
|
+
if (!message) {
|
|
31
|
+
message = document.createElement('small');
|
|
32
|
+
message.setAttribute('validation', '');
|
|
33
|
+
message.setAttribute('ui-tone', 'danger');
|
|
34
|
+
message.id = `validation-${Math.random().toString(36).slice(2, 10)}`;
|
|
35
|
+
field.append(message);
|
|
36
|
+
}
|
|
37
|
+
message.textContent = control.validationMessage;
|
|
38
|
+
ids.push(message.id);
|
|
39
|
+
} else message?.remove();
|
|
40
|
+
if (ids.length) control.setAttribute('aria-describedby', ids.join(' '));
|
|
41
|
+
else control.removeAttribute('aria-describedby');
|
|
42
|
+
if (event.type === 'invalid') {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
if ([...(control.form?.elements ?? [control])].find(item => !item.validity.valid) === control) control.focus();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function changed(event) {
|
|
49
|
+
const control = event.target;
|
|
50
|
+
if (!(control instanceof Element)) return;
|
|
51
|
+
if (control.form?.querySelector('button[enable]')) enable(control.form);
|
|
52
|
+
if (control.matches('input[range]')) range(control);
|
|
53
|
+
if (control.matches('input[reveals]')) document.getElementById(control.getAttribute('reveals')).hidden = !control.checked;
|
|
54
|
+
if (control.matches('input, select, textarea')) {
|
|
55
|
+
validity(control, event);
|
|
56
|
+
for (const message of control.form?.querySelectorAll('small[validation]') ?? []) if (!message.closest('[field]')) message.remove();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
document.addEventListener('input', changed);
|
|
60
|
+
document.addEventListener('change', changed);
|
|
61
|
+
document.addEventListener('reset', event => setTimeout(() => {
|
|
62
|
+
enable(event.target);
|
|
63
|
+
for (const input of event.target.querySelectorAll('input[range]')) range(input);
|
|
64
|
+
}));
|
|
65
|
+
for (const type of ['focusout', 'invalid']) document.addEventListener(type, event => {
|
|
66
|
+
if (event.target.matches('input, select, textarea')) validity(event.target, event);
|
|
67
|
+
}, {capture: type === 'invalid'});
|
|
68
|
+
|
|
69
|
+
document.addEventListener('click', async event => {
|
|
70
|
+
const button = event.target.closest('button');
|
|
71
|
+
if (!button) return;
|
|
72
|
+
if (button.hasAttribute('dismiss')) button.closest('[alert], [toast]')?.remove();
|
|
73
|
+
if (button.matches('[copy], [copyfor]')) {
|
|
74
|
+
const target = document.getElementById(button.getAttribute('copyfor'));
|
|
75
|
+
const text = button.hasAttribute('copy') ? button.getAttribute('copy') : target?.matches('input, textarea, select') ? target.value : target?.textContent;
|
|
76
|
+
if (text == null) return;
|
|
77
|
+
await copyText(text);
|
|
78
|
+
if (!button.isConnected) return;
|
|
79
|
+
button._label ??= button.getAttribute('aria-label') ?? '';
|
|
80
|
+
button.toggleAttribute('copied', true);
|
|
81
|
+
if (button._label) button.setAttribute('aria-label', 'Copied');
|
|
82
|
+
clearTimeout(button._copied);
|
|
83
|
+
button._copied = setTimeout(() => {
|
|
84
|
+
button.removeAttribute('copied');
|
|
85
|
+
if (button._label) button.setAttribute('aria-label', button._label);
|
|
86
|
+
}, 1500);
|
|
87
|
+
}
|
|
88
|
+
if (button.hasAttribute('commandfor') && !('commandForElement' in button)) {
|
|
89
|
+
const target = document.getElementById(button.getAttribute('commandfor')), command = button.getAttribute('command');
|
|
90
|
+
if (command === 'show-modal') target.showModal();
|
|
91
|
+
else if (command === 'close') target.close();
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
document.addEventListener('animationend', event => {
|
|
95
|
+
if (event.animationName === 'ui-toast-lifetime' && event.target.matches('[toast]')) event.target.remove();
|
|
96
|
+
});
|
|
97
|
+
for (const type of ['htmx:after-swap', 'htmx:after:swap', 'htmx-after-swap']) document.addEventListener(type, event => {
|
|
98
|
+
const dialog = event.target.closest('dialog[dialog]');
|
|
99
|
+
if (dialog && event.target !== dialog && !dialog.open) dialog.showModal();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const mounted = new WeakMap();
|
|
103
|
+
const tabs = '[tabs], [tabs] [role=tablist]';
|
|
104
|
+
const navbar = '[navbar], [navbar] > [navbar-row], [navbar-row] > nav, [navbar-row] > [navbar-toggle]';
|
|
105
|
+
const selector = `${tabs}, ${navbar}, [popup][popover], [popovertarget], [toolbar][role=toolbar], [toaster], input[range], button[enable], [copy], [copyfor]`;
|
|
106
|
+
function own(elements, dispose) {
|
|
107
|
+
const stop = () => { dispose(); for (const element of elements) mounted.delete(element); };
|
|
108
|
+
for (const element of elements) mounted.set(element, stop);
|
|
109
|
+
}
|
|
110
|
+
function mount(element) {
|
|
111
|
+
if (mounted.has(element)) return;
|
|
112
|
+
if (element.matches('[popovertarget]')) {
|
|
113
|
+
const panel = element.popoverTargetElement;
|
|
114
|
+
if (panel?.matches('[popup]')) mount(panel);
|
|
115
|
+
} else if (element.matches('[popup][popover]')) {
|
|
116
|
+
const trigger = document.querySelector(`[popovertarget="${CSS.escape(element.id)}"]`);
|
|
117
|
+
if (!trigger) return;
|
|
118
|
+
const stop = mountPopover(trigger, element, {side: element.dataset.side, align: element.dataset.align, sideOffset: +(element.dataset.sideOffset ?? 6)});
|
|
119
|
+
const hover = element.hasAttribute('hovercard') ? mountHoverPopover(trigger, element) : undefined;
|
|
120
|
+
own([element, trigger], () => { hover?.(); stop(); });
|
|
121
|
+
} else if (element.matches(tabs)) {
|
|
122
|
+
const root = element.closest('[tabs]'), list = root?.querySelector('[role=tablist]');
|
|
123
|
+
if (list && !mounted.has(root)) own([root, list], mountTabs(root, {activationMode: root.dataset.activationMode}).dispose);
|
|
124
|
+
} else if (element.matches('[toolbar][role=toolbar]')) own([element], mountToolbar(element));
|
|
125
|
+
else if (element.matches(navbar)) {
|
|
126
|
+
const root = element.closest('[navbar]'), row = root?.querySelector(':scope > [navbar-row]');
|
|
127
|
+
const nav = row?.querySelector(':scope > nav'), toggle = row?.querySelector(':scope > [navbar-toggle]');
|
|
128
|
+
if (nav && toggle || root?.hasAttribute('navbar-sticky')) {
|
|
129
|
+
mounted.get(root)?.();
|
|
130
|
+
own([root, row, nav, toggle].filter(Boolean), mountNavbar(root).dispose);
|
|
131
|
+
}
|
|
132
|
+
} else if (element.matches('[toaster]')) { if (!element.matches(':popover-open')) element.showPopover(); }
|
|
133
|
+
else if (element.matches('input[range]')) range(element);
|
|
134
|
+
else if (element.matches('button[enable]')) enable(element.form);
|
|
135
|
+
}
|
|
136
|
+
function cleanup(element) {
|
|
137
|
+
mounted.get(element)?.();
|
|
138
|
+
mounted.delete(element);
|
|
139
|
+
clearTimeout(element._copied);
|
|
140
|
+
}
|
|
141
|
+
function visit(root, action, match = selector) {
|
|
142
|
+
if (!(root instanceof Element)) return;
|
|
143
|
+
if (root.matches(match)) action(root);
|
|
144
|
+
root.querySelectorAll(match).forEach(action);
|
|
145
|
+
}
|
|
146
|
+
visit(document.body, mount);
|
|
147
|
+
new MutationObserver(records => {
|
|
148
|
+
for (const record of records) for (const node of record.removedNodes) if (!node.isConnected) visit(node, cleanup, '*');
|
|
149
|
+
for (const record of records) for (const node of record.addedNodes) if (node.isConnected) visit(node, mount);
|
|
150
|
+
}).observe(document.body, {childList: true, subtree: true});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export async function copyText(textOrPromise) {
|
|
2
|
+
if (typeof textOrPromise === 'string') return navigator.clipboard.writeText(textOrPromise);
|
|
3
|
+
const type = 'text/plain';
|
|
4
|
+
// Safari checks for user activation when write() is called, not when the data is ready.
|
|
5
|
+
const data = Promise.resolve(textOrPromise).then(text => new Blob([text], {type}));
|
|
6
|
+
return navigator.clipboard.write([new ClipboardItem({[type]: data})]);
|
|
7
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
function inlineSize(style, box) {
|
|
2
|
+
const edges = ['Start', 'End'].reduce((n, end) => n + parseFloat(style[`paddingInline${end}`]) +
|
|
3
|
+
parseFloat(style[`borderInline${end}Width`]), 0);
|
|
4
|
+
const size = parseFloat(style.writingMode.startsWith('horizontal') ? style.width : style.height);
|
|
5
|
+
if (box === 'content') return style.boxSizing === 'border-box' ? size - edges : size;
|
|
6
|
+
return style.boxSizing === 'border-box' ? size : size + edges;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function watchInlineFit(host, row, onCompact) {
|
|
10
|
+
const doc = host.ownerDocument, win = doc.defaultView;
|
|
11
|
+
const events = new win.AbortController(), {signal} = events;
|
|
12
|
+
let frame, compact;
|
|
13
|
+
function measure() {
|
|
14
|
+
frame = undefined;
|
|
15
|
+
if (!host.getClientRects().length) return;
|
|
16
|
+
const available = inlineSize(win.getComputedStyle(host), 'content');
|
|
17
|
+
host.setAttribute('data-fitting', '');
|
|
18
|
+
const needed = inlineSize(win.getComputedStyle(row), 'border');
|
|
19
|
+
host.removeAttribute('data-fitting');
|
|
20
|
+
if (!Number.isFinite(needed) || !Number.isFinite(available)) return;
|
|
21
|
+
const next = needed > available;
|
|
22
|
+
if (compact !== next) onCompact(compact = next);
|
|
23
|
+
}
|
|
24
|
+
function schedule() { frame ??= win.requestAnimationFrame(measure); }
|
|
25
|
+
const resize = new win.ResizeObserver(schedule);
|
|
26
|
+
resize.observe(host);
|
|
27
|
+
if (row !== host) resize.observe(row);
|
|
28
|
+
const children = new Set();
|
|
29
|
+
function observeChildren() {
|
|
30
|
+
const next = new Set(row.children);
|
|
31
|
+
for (const child of children) if (!next.has(child)) { resize.unobserve(child); children.delete(child); }
|
|
32
|
+
for (const child of next) if (!children.has(child)) { resize.observe(child); children.add(child); }
|
|
33
|
+
}
|
|
34
|
+
const mutations = new win.MutationObserver(records => {
|
|
35
|
+
if (records.some(r => r.type === 'childList' && r.target === row)) observeChildren();
|
|
36
|
+
if (records.some(r => r.target !== host || r.attributeName !== 'data-fitting')) schedule();
|
|
37
|
+
});
|
|
38
|
+
mutations.observe(row, {subtree: true, attributes: true, childList: true, characterData: true});
|
|
39
|
+
row.addEventListener('load', schedule, {capture: true, signal});
|
|
40
|
+
doc.fonts?.addEventListener('loadingdone', schedule, {signal});
|
|
41
|
+
observeChildren();
|
|
42
|
+
measure();
|
|
43
|
+
return () => {
|
|
44
|
+
events.abort();
|
|
45
|
+
win.cancelAnimationFrame(frame);
|
|
46
|
+
resize.disconnect();
|
|
47
|
+
mutations.disconnect();
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function mountNavbar(host, {open = false, onOpenChange} = {}) {
|
|
52
|
+
const doc = host.ownerDocument, root = doc.documentElement;
|
|
53
|
+
const sticky = host.hasAttribute('navbar-sticky') ? new doc.defaultView.ResizeObserver(() =>
|
|
54
|
+
root.style.setProperty('--navbar-size', `${host.offsetHeight}px`)) : null;
|
|
55
|
+
sticky?.observe(host);
|
|
56
|
+
function stopSticky() { sticky?.disconnect(); if (sticky) root.style.removeProperty('--navbar-size'); }
|
|
57
|
+
const row = host.querySelector(':scope > [navbar-row]'), nav = row?.querySelector(':scope > nav');
|
|
58
|
+
const toggle = row?.querySelector(':scope > [navbar-toggle]');
|
|
59
|
+
if (!nav || !toggle) return {setOpen() {}, dispose: stopSticky};
|
|
60
|
+
const events = new doc.defaultView.AbortController();
|
|
61
|
+
let compact, expanded = Boolean(open);
|
|
62
|
+
function setOpen(next) {
|
|
63
|
+
const changed = expanded !== Boolean(next);
|
|
64
|
+
expanded = Boolean(next);
|
|
65
|
+
host.toggleAttribute('data-open', compact && expanded);
|
|
66
|
+
toggle.setAttribute('aria-expanded', String(Boolean(compact && expanded)));
|
|
67
|
+
if (changed) onOpenChange?.(expanded);
|
|
68
|
+
}
|
|
69
|
+
const stop = watchInlineFit(host, row, next => {
|
|
70
|
+
const wasCompact = compact, focus = doc.activeElement;
|
|
71
|
+
compact = next;
|
|
72
|
+
if (compact && nav.contains(focus)) setOpen(true);
|
|
73
|
+
else if (wasCompact && !compact) setOpen(false);
|
|
74
|
+
else setOpen(expanded);
|
|
75
|
+
host.toggleAttribute('data-compact', compact);
|
|
76
|
+
if (wasCompact && !compact && toggle.contains(focus))
|
|
77
|
+
(nav.querySelector('a[href]') ?? row.querySelector('[navbar-brand]'))?.focus({preventScroll: true});
|
|
78
|
+
});
|
|
79
|
+
const {signal} = events;
|
|
80
|
+
toggle.addEventListener('click', () => setOpen(!expanded), {signal});
|
|
81
|
+
doc.addEventListener('click', event => {
|
|
82
|
+
if (compact && expanded && (!host.contains(event.target) || nav.contains(event.target.closest?.('a[href]') ?? null))) setOpen(false);
|
|
83
|
+
}, {signal});
|
|
84
|
+
host.addEventListener('keydown', event => {
|
|
85
|
+
if (!compact || !expanded || event.key !== 'Escape' || event.defaultPrevented) return;
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
setOpen(false);
|
|
88
|
+
toggle.focus({preventScroll: true});
|
|
89
|
+
}, {signal});
|
|
90
|
+
return {setOpen, dispose() {
|
|
91
|
+
events.abort();
|
|
92
|
+
stop();
|
|
93
|
+
stopSticky();
|
|
94
|
+
host.removeAttribute('data-compact');
|
|
95
|
+
host.removeAttribute('data-open');
|
|
96
|
+
toggle.setAttribute('aria-expanded', 'false');
|
|
97
|
+
}};
|
|
98
|
+
}
|