answerui-py 0.0.2__py3-none-any.whl

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.
answerui/__init__.py ADDED
@@ -0,0 +1,20 @@
1
+ """A semantic HTML UI framework for FastHTML apps
2
+
3
+ Modules:
4
+
5
+ - `answerui.base`: The reset, the document on the tokens, and prose
6
+ - `answerui.behaviors`: The client runtime: delegated event rules that survive htmx swaps
7
+ - `answerui.controls`: Native form controls styled through their own states, raised to press and sunken to type into
8
+ - `answerui.data`: Tables and other displays of structured data
9
+ - `answerui.demo`: Isolated rendering of markup with current styles and behaviors, for notebooks and docs
10
+ - `answerui.hdrs`: App integration: the headers that carry the registry's assets
11
+ - `answerui.layout`: Stack, cluster, grid, and document flow
12
+ - `answerui.navigation`: The navbar
13
+ - `answerui.overlays`: Popovers, menus, dialogs, drawers, and tooltips
14
+ - `answerui.registry`: Defining hyperelements and collecting their styles and behaviors
15
+ - `answerui.skill`: Style a FastHTML app with answerui: build pages from its components, change how they look through the registry, and check the result
16
+ - `answerui.status`: Feedback, loading, and empty states
17
+ - `answerui.surfaces`: Containers and small pieces of visual information
18
+ - `answerui.tokens`: The styling surface: colour, space, radius, type, motion, and elevation"""
19
+
20
+ __version__ = "0.0.2"
answerui/_modidx.py ADDED
@@ -0,0 +1,143 @@
1
+ # Autogenerated by nbdev
2
+
3
+ d = { 'settings': { 'branch': 'main',
4
+ 'doc_baseurl': '/answerui',
5
+ 'doc_host': 'https://AnswerDotAI.github.io',
6
+ 'git_url': 'https://github.com/AnswerDotAI/answerui',
7
+ 'lib_path': 'answerui'},
8
+ 'syms': { 'answerui.behaviors': { 'answerui.behaviors.Registry.behaviors': ('behaviors.html#registry.behaviors', 'answerui/behaviors.py'),
9
+ 'answerui.behaviors.Registry.build_js': ('behaviors.html#registry.build_js', 'answerui/behaviors.py'),
10
+ 'answerui.behaviors._entry': ('behaviors.html#_entry', 'answerui/behaviors.py'),
11
+ 'answerui.behaviors._event': ('behaviors.html#_event', 'answerui/behaviors.py'),
12
+ 'answerui.behaviors._spellings': ('behaviors.html#_spellings', 'answerui/behaviors.py')},
13
+ 'answerui.controls': { 'answerui.controls.Checkbox': ('controls.html#checkbox', 'answerui/controls.py'),
14
+ 'answerui.controls.Copy': ('controls.html#copy', 'answerui/controls.py'),
15
+ 'answerui.controls.Field': ('controls.html#field', 'answerui/controls.py'),
16
+ 'answerui.controls.Group': ('controls.html#group', 'answerui/controls.py'),
17
+ 'answerui.controls.Radio': ('controls.html#radio', 'answerui/controls.py'),
18
+ 'answerui.controls.Range': ('controls.html#range', 'answerui/controls.py'),
19
+ 'answerui.controls.Switch': ('controls.html#switch', 'answerui/controls.py'),
20
+ 'answerui.controls.Toolbar': ('controls.html#toolbar', 'answerui/controls.py'),
21
+ 'answerui.controls._button_example': ('controls.html#_button_example', 'answerui/controls.py'),
22
+ 'answerui.controls._checkbox_example': ('controls.html#_checkbox_example', 'answerui/controls.py'),
23
+ 'answerui.controls._copy_example': ('controls.html#_copy_example', 'answerui/controls.py'),
24
+ 'answerui.controls._field_example': ('controls.html#_field_example', 'answerui/controls.py'),
25
+ 'answerui.controls._fieldset_example': ('controls.html#_fieldset_example', 'answerui/controls.py'),
26
+ 'answerui.controls._group_example': ('controls.html#_group_example', 'answerui/controls.py'),
27
+ 'answerui.controls._input_example': ('controls.html#_input_example', 'answerui/controls.py'),
28
+ 'answerui.controls._radio_example': ('controls.html#_radio_example', 'answerui/controls.py'),
29
+ 'answerui.controls._range_example': ('controls.html#_range_example', 'answerui/controls.py'),
30
+ 'answerui.controls._range_parts': ('controls.html#_range_parts', 'answerui/controls.py'),
31
+ 'answerui.controls._select_example': ('controls.html#_select_example', 'answerui/controls.py'),
32
+ 'answerui.controls._switch_example': ('controls.html#_switch_example', 'answerui/controls.py'),
33
+ 'answerui.controls._textarea_example': ('controls.html#_textarea_example', 'answerui/controls.py'),
34
+ 'answerui.controls._toolbar_example': ('controls.html#_toolbar_example', 'answerui/controls.py')},
35
+ 'answerui.data': { 'answerui.data.DataTable': ('data.html#datatable', 'answerui/data.py'),
36
+ 'answerui.data._datatable_example': ('data.html#_datatable_example', 'answerui/data.py')},
37
+ 'answerui.demo': { 'answerui.demo.Hyperelement.demo': ('demo.html#hyperelement.demo', 'answerui/demo.py'),
38
+ 'answerui.demo.Preview': ('demo.html#preview', 'answerui/demo.py'),
39
+ 'answerui.demo.Preview.__init__': ('demo.html#preview.__init__', 'answerui/demo.py'),
40
+ 'answerui.demo.Preview.__repr__': ('demo.html#preview.__repr__', 'answerui/demo.py'),
41
+ 'answerui.demo.Preview._repr_mimebundle_': ('demo.html#preview._repr_mimebundle_', 'answerui/demo.py'),
42
+ 'answerui.demo._as_served': ('demo.html#_as_served', 'answerui/demo.py'),
43
+ 'answerui.demo.demo': ('demo.html#demo', 'answerui/demo.py'),
44
+ 'answerui.demo.demo_html': ('demo.html#demo_html', 'answerui/demo.py'),
45
+ 'answerui.demo.example': ('demo.html#example', 'answerui/demo.py')},
46
+ 'answerui.hdrs': { 'answerui.hdrs.Registry.assets': ('hdrs.html#registry.assets', 'answerui/hdrs.py'),
47
+ 'answerui.hdrs.Registry.ftrs': ('hdrs.html#registry.ftrs', 'answerui/hdrs.py'),
48
+ 'answerui.hdrs.Registry.hdrs': ('hdrs.html#registry.hdrs', 'answerui/hdrs.py'),
49
+ 'answerui.hdrs.Registry.serve': ('hdrs.html#registry.serve', 'answerui/hdrs.py'),
50
+ 'answerui.hdrs._Lazy': ('hdrs.html#_lazy', 'answerui/hdrs.py'),
51
+ 'answerui.hdrs._Lazy.__ft__': ('hdrs.html#_lazy.__ft__', 'answerui/hdrs.py'),
52
+ 'answerui.hdrs._Lazy.__init__': ('hdrs.html#_lazy.__init__', 'answerui/hdrs.py'),
53
+ 'answerui.hdrs._hash': ('hdrs.html#_hash', 'answerui/hdrs.py')},
54
+ 'answerui.layout': { 'answerui.layout.Cluster': ('layout.html#cluster', 'answerui/layout.py'),
55
+ 'answerui.layout.Flow': ('layout.html#flow', 'answerui/layout.py'),
56
+ 'answerui.layout.Grid': ('layout.html#grid', 'answerui/layout.py'),
57
+ 'answerui.layout.Stack': ('layout.html#stack', 'answerui/layout.py'),
58
+ 'answerui.layout._cluster_example': ('layout.html#_cluster_example', 'answerui/layout.py'),
59
+ 'answerui.layout._flow_example': ('layout.html#_flow_example', 'answerui/layout.py'),
60
+ 'answerui.layout._grid_example': ('layout.html#_grid_example', 'answerui/layout.py'),
61
+ 'answerui.layout._layout': ('layout.html#_layout', 'answerui/layout.py'),
62
+ 'answerui.layout._stack_example': ('layout.html#_stack_example', 'answerui/layout.py')},
63
+ 'answerui.navigation': { 'answerui.navigation.NavBar': ('navigation.html#navbar', 'answerui/navigation.py'),
64
+ 'answerui.navigation._links': ('navigation.html#_links', 'answerui/navigation.py'),
65
+ 'answerui.navigation._navbar_example': ('navigation.html#_navbar_example', 'answerui/navigation.py')},
66
+ 'answerui.overlays': { 'answerui.overlays.Dropdown': ('overlays.html#dropdown', 'answerui/overlays.py'),
67
+ 'answerui.overlays.Popover': ('overlays.html#popover', 'answerui/overlays.py'),
68
+ 'answerui.overlays.Tooltip': ('overlays.html#tooltip', 'answerui/overlays.py'),
69
+ 'answerui.overlays._dialog_example': ('overlays.html#_dialog_example', 'answerui/overlays.py'),
70
+ 'answerui.overlays._dropdown_example': ('overlays.html#_dropdown_example', 'answerui/overlays.py'),
71
+ 'answerui.overlays._popover_example': ('overlays.html#_popover_example', 'answerui/overlays.py'),
72
+ 'answerui.overlays._tooltip_example': ('overlays.html#_tooltip_example', 'answerui/overlays.py')},
73
+ 'answerui.registry': { 'answerui.registry.Hyperelement': ('registry.html#hyperelement', 'answerui/registry.py'),
74
+ 'answerui.registry.Hyperelement.__call__': ( 'registry.html#hyperelement.__call__',
75
+ 'answerui/registry.py'),
76
+ 'answerui.registry.Hyperelement.__init__': ( 'registry.html#hyperelement.__init__',
77
+ 'answerui/registry.py'),
78
+ 'answerui.registry.Hyperelement.__repr__': ( 'registry.html#hyperelement.__repr__',
79
+ 'answerui/registry.py'),
80
+ 'answerui.registry.Hyperelement.__signature__': ( 'registry.html#hyperelement.__signature__',
81
+ 'answerui/registry.py'),
82
+ 'answerui.registry.Hyperelement._repr_markdown_': ( 'registry.html#hyperelement._repr_markdown_',
83
+ 'answerui/registry.py'),
84
+ 'answerui.registry.Hyperelement.behaviors': ( 'registry.html#hyperelement.behaviors',
85
+ 'answerui/registry.py'),
86
+ 'answerui.registry.Hyperelement.rule': ('registry.html#hyperelement.rule', 'answerui/registry.py'),
87
+ 'answerui.registry.Hyperelement.variant': ('registry.html#hyperelement.variant', 'answerui/registry.py'),
88
+ 'answerui.registry.Hyperelement.variants': ( 'registry.html#hyperelement.variants',
89
+ 'answerui/registry.py'),
90
+ 'answerui.registry.Registry': ('registry.html#registry', 'answerui/registry.py'),
91
+ 'answerui.registry.Registry.__init__': ('registry.html#registry.__init__', 'answerui/registry.py'),
92
+ 'answerui.registry.Registry.__repr__': ('registry.html#registry.__repr__', 'answerui/registry.py'),
93
+ 'answerui.registry.Registry._repr_markdown_': ( 'registry.html#registry._repr_markdown_',
94
+ 'answerui/registry.py'),
95
+ 'answerui.registry.Registry.build_css': ('registry.html#registry.build_css', 'answerui/registry.py'),
96
+ 'answerui.registry.Registry.file': ('registry.html#registry.file', 'answerui/registry.py'),
97
+ 'answerui.registry.Registry.hypelt': ('registry.html#registry.hypelt', 'answerui/registry.py'),
98
+ 'answerui.registry.Registry.remove': ('registry.html#registry.remove', 'answerui/registry.py'),
99
+ 'answerui.registry.Registry.rule': ('registry.html#registry.rule', 'answerui/registry.py'),
100
+ 'answerui.registry.Rule': ('registry.html#rule', 'answerui/registry.py'),
101
+ 'answerui.registry.Rule.__init__': ('registry.html#rule.__init__', 'answerui/registry.py'),
102
+ 'answerui.registry.Rule.__repr__': ('registry.html#rule.__repr__', 'answerui/registry.py'),
103
+ 'answerui.registry.Rule.compiled_css': ('registry.html#rule.compiled_css', 'answerui/registry.py'),
104
+ 'answerui.registry.Rule.name': ('registry.html#rule.name', 'answerui/registry.py'),
105
+ 'answerui.registry.Rule.owner': ('registry.html#rule.owner', 'answerui/registry.py'),
106
+ 'answerui.registry.RuleView': ('registry.html#ruleview', 'answerui/registry.py'),
107
+ 'answerui.registry.RuleView.__getitem__': ('registry.html#ruleview.__getitem__', 'answerui/registry.py'),
108
+ 'answerui.registry.RuleView.__init__': ('registry.html#ruleview.__init__', 'answerui/registry.py'),
109
+ 'answerui.registry.RuleView.__iter__': ('registry.html#ruleview.__iter__', 'answerui/registry.py'),
110
+ 'answerui.registry.RuleView.__len__': ('registry.html#ruleview.__len__', 'answerui/registry.py'),
111
+ 'answerui.registry.RuleView.__repr__': ('registry.html#ruleview.__repr__', 'answerui/registry.py'),
112
+ 'answerui.registry.RuleView._matches': ('registry.html#ruleview._matches', 'answerui/registry.py'),
113
+ 'answerui.registry._behaviors': ('registry.html#_behaviors', 'answerui/registry.py'),
114
+ 'answerui.registry._generic': ('registry.html#_generic', 'answerui/registry.py'),
115
+ 'answerui.registry._nest': ('registry.html#_nest', 'answerui/registry.py')},
116
+ 'answerui.status': { 'answerui.status.Alert': ('status.html#alert', 'answerui/status.py'),
117
+ 'answerui.status.Skeleton': ('status.html#skeleton', 'answerui/status.py'),
118
+ 'answerui.status.Spinner': ('status.html#spinner', 'answerui/status.py'),
119
+ 'answerui.status.Toast': ('status.html#toast', 'answerui/status.py'),
120
+ 'answerui.status.Toasts': ('status.html#toasts', 'answerui/status.py'),
121
+ 'answerui.status._alert_example': ('status.html#_alert_example', 'answerui/status.py'),
122
+ 'answerui.status._notice': ('status.html#_notice', 'answerui/status.py'),
123
+ 'answerui.status._progress_example': ('status.html#_progress_example', 'answerui/status.py'),
124
+ 'answerui.status._skeleton_example': ('status.html#_skeleton_example', 'answerui/status.py'),
125
+ 'answerui.status._spinner_example': ('status.html#_spinner_example', 'answerui/status.py'),
126
+ 'answerui.status._toast_example': ('status.html#_toast_example', 'answerui/status.py'),
127
+ 'answerui.status.add_toast': ('status.html#add_toast', 'answerui/status.py'),
128
+ 'answerui.status.pop_toasts': ('status.html#pop_toasts', 'answerui/status.py'),
129
+ 'answerui.status.setup_toasts': ('status.html#setup_toasts', 'answerui/status.py'),
130
+ 'answerui.status.toast_after': ('status.html#toast_after', 'answerui/status.py'),
131
+ 'answerui.status.toasts_oob': ('status.html#toasts_oob', 'answerui/status.py')},
132
+ 'answerui.surfaces': { 'answerui.surfaces.Avatar': ('surfaces.html#avatar', 'answerui/surfaces.py'),
133
+ 'answerui.surfaces.Badge': ('surfaces.html#badge', 'answerui/surfaces.py'),
134
+ 'answerui.surfaces.Card': ('surfaces.html#card', 'answerui/surfaces.py'),
135
+ 'answerui.surfaces._avatar_example': ('surfaces.html#_avatar_example', 'answerui/surfaces.py'),
136
+ 'answerui.surfaces._badge_example': ('surfaces.html#_badge_example', 'answerui/surfaces.py'),
137
+ 'answerui.surfaces._card_example': ('surfaces.html#_card_example', 'answerui/surfaces.py')},
138
+ 'answerui.tokens': { 'answerui.tokens.Registry.context': ('tokens.html#registry.context', 'answerui/tokens.py'),
139
+ 'answerui.tokens.Registry.contexts': ('tokens.html#registry.contexts', 'answerui/tokens.py'),
140
+ 'answerui.tokens._context_css': ('tokens.html#_context_css', 'answerui/tokens.py'),
141
+ 'answerui.tokens._palette_css': ('tokens.html#_palette_css', 'answerui/tokens.py'),
142
+ 'answerui.tokens.font_face': ('tokens.html#font_face', 'answerui/tokens.py'),
143
+ 'answerui.tokens.relief': ('tokens.html#relief', 'answerui/tokens.py')}}}
answerui/all.py ADDED
@@ -0,0 +1,14 @@
1
+ """Every answerui module, for `from answerui.all import *`"""
2
+ from .registry import *
3
+ from .behaviors import *
4
+ from .demo import *
5
+ from .hdrs import *
6
+ from .tokens import *
7
+ from .base import *
8
+ from .layout import *
9
+ from .controls import *
10
+ from .surfaces import *
11
+ from .status import *
12
+ from .overlays import *
13
+ from .navigation import *
14
+ from .data import *
answerui/base.py ADDED
@@ -0,0 +1,89 @@
1
+ """The reset, the document on the tokens, and prose
2
+
3
+ The base layer styles native HTML elements on the tokens, so a page written as plain semantic markup is readable before any component is involved. It has three parts: a reset that removes the browser's default styling, document styles for text, links, code, lists, and tables, and typrose for long-form prose. Each registers with `reg.rule` in the `reset` or `base` layer.
4
+
5
+ Docs: https://AnswerDotAI.github.io/answerui/base.html.md"""
6
+
7
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_base.ipynb.
8
+
9
+ # %% auto #0
10
+ __all__ = ['TYPROSE']
11
+
12
+ # %% ../nbs/06_base.ipynb #9408f75c
13
+ from fastcore.utils import *
14
+ from importlib.resources import files
15
+ from .registry import reg
16
+ import answerui.tokens # installs Registry.context
17
+
18
+ # %% ../nbs/06_base.ipynb #33f584b9
19
+ reg.rule('reset', r"""
20
+ *, *::before, *::after { box-sizing: border-box; }
21
+ :where(body, h1, h2, h3, h4, h5, h6, p, ul, ol, dl, dd, blockquote, figure, figcaption, pre, hr, table) { margin: 0; }
22
+ img, picture, video, canvas, svg { max-inline-size: 100%; block-size: auto; }
23
+ button, input, textarea, select { font: inherit; color: inherit; }
24
+ """, layer='reset', doc='Border-box sizing, no block margins, media constrained, controls inheriting text.')
25
+
26
+ # %% ../nbs/06_base.ipynb #7024f5fe
27
+ reg.context('mark', 'var(--hue-warning)')
28
+ reg.rule('document', r"""
29
+ :root { accent-color: var(--v); caret-color: var(--v); background: var(--n-3); --ring: 2px solid var(--v-1); }
30
+ html { scrollbar-gutter: stable; scroll-behavior: smooth; }
31
+ * { scrollbar-color: var(--n1) transparent; }
32
+ body { color: var(--n2); font: var(--weight) var(--text)/var(--leading) var(--font); container-type: inline-size; }
33
+ button, a[button], input:is([type=checkbox], [type=radio], [type=range]) { cursor: pointer; }
34
+ :disabled { cursor: not-allowed; }
35
+ h1, h2, h3, h4, h5, h6 { font-family: var(--font-heading, inherit); font-weight: var(--weight1); line-height: var(--leading-1); text-wrap: balance; }
36
+ h1 { font-size: var(--text3); } h2 { font-size: var(--text2); } h3 { font-size: var(--text1); }
37
+ p { text-wrap: pretty; }
38
+ a { color: var(--v); }
39
+ code, kbd, pre, samp { font-family: var(--font-mono); }
40
+ code, kbd { font-size: .875em; padding: .1em .3em; border-radius: var(--radius-1); }
41
+ code { background: var(--n-2); }
42
+ kbd { border: 1px solid var(--n-1); box-shadow: var(--raised); }
43
+ pre { background: var(--n-2); padding: var(--space); border-radius: var(--radius); overflow-x: auto; }
44
+ pre code { font-size: inherit; padding: 0; background: none; }
45
+ ul, ol { padding-inline-start: var(--space1); }
46
+ blockquote { border-inline-start: 3px solid var(--n-1); padding-inline-start: var(--space); color: var(--n1); }
47
+ hr { border: 0; border-block-start: 1px solid var(--n-1); }
48
+ small, figcaption { font-size: var(--text-1); }
49
+ figcaption { color: var(--n1); }
50
+ table {
51
+ border-collapse: separate; border-spacing: 0;
52
+ caption { caption-side: bottom; text-align: start; padding: var(--space-2) var(--space); font-size: var(--text-1); color: var(--n1); }
53
+ :is(th, td) { padding: var(--space-2) var(--space); text-align: start; }
54
+ thead th { font-weight: var(--weight1); border-block-end: 1px solid var(--n); }
55
+ tbody tr + tr > * { border-block-start: 1px solid var(--n-1); }
56
+ tfoot tr > * { border-block-start: 1px solid var(--n); font-weight: var(--weight1); }
57
+ :is(th, td):is([num], [align=right]) { text-align: end; font-variant-numeric: tabular-nums; }
58
+ }
59
+ :focus-visible { outline: var(--ring); outline-offset: 2px; }
60
+ mark { background: var(--v-3); color: var(--v2); padding: .1em .2em; border-radius: var(--radius-1); }
61
+ @media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
62
+ """, layer='base', doc='The page, text, headings, links, code, lists, quotes, tables, rules, marks, focus, and cursors on the tokens.')
63
+
64
+ # %% ../nbs/06_base.ipynb #ac244267
65
+ reg.rule('hidden', '[hidden]:not([hidden=until-found]) { display: none; }', layer='utilities', doc='The hidden attribute wins over any display a component sets.')
66
+
67
+ # %% ../nbs/06_base.ipynb #d0d95931
68
+ reg.rule('icons', r'''
69
+ svg:is(:has(> use), [icon]) { inline-size: 1.25em; block-size: 1.25em; vertical-align: -.3em; flex: none; }
70
+ ''', layer='base', doc='A sprite icon, or an inline SVG marked `icon`, sized and aligned to the text around it.')
71
+
72
+ # %% ../nbs/06_base.ipynb #191dab74
73
+ TYPROSE = files('answerui').joinpath('css/typrose.css').read_text()
74
+ TYPROSE = re.sub(r'(/\*[^*]*\*/\n)?\.prose-(slate|zinc|neutral|stone|invert) \{[^}]*\}\n*', '', TYPROSE)
75
+ TYPROSE = re.sub(r'\.prose-(\w+)', r'[prose="\1"]', TYPROSE)
76
+ TYPROSE = re.sub(r'\.prose\b', '[prose]', TYPROSE)
77
+ reg.rule('typrose', TYPROSE, layer='base', doc='Prose typography from typrose, on the `prose` attribute.')
78
+
79
+ # %% ../nbs/06_base.ipynb #369818f2
80
+ reg.rule('prose', r"""
81
+ [prose] {
82
+ max-inline-size: 65ch; font-size: calc(var(--text) * var(--prose-font-scale));
83
+ --prose-body: var(--n2); --prose-headings: var(--n2); --prose-bold: var(--n2); --prose-quotes: var(--n2); --prose-code: var(--n2);
84
+ --prose-links: var(--v);
85
+ --prose-lead: var(--n1); --prose-counters: var(--n1); --prose-captions: var(--n1);
86
+ --prose-bullets: var(--n-1); --prose-hr: var(--n-1); --prose-quote-borders: var(--n-1);
87
+ --prose-th-borders: var(--n); --prose-td-borders: var(--n-1);
88
+ }
89
+ """, layer='base', doc='typrose colours mapped onto the ladder, its size from `--text`, and the reading measure.')
answerui/behaviors.py ADDED
@@ -0,0 +1,83 @@
1
+ """The client runtime: delegated event rules that survive htmx swaps
2
+
3
+ The registry stores a component's behaviors as records: a selector, an event, a handler body. Here we turn those into running code in the browser. Wiring a listener onto each element as it appears would work for a static page, but in a FastHTML app elements arrive continuously as htmx swaps in fragments, so per-element wiring would mean re-initialising after every swap.
4
+
5
+ Instead we delegate: every event becomes one document-level listener which, when its event fires anywhere on the page, checks whether the target sits inside an element matching the rule's selector. Matching happens at event time against the live DOM, so elements swapped in later behave the same as those in the original page, with no per-element state and no mutation observer. The rule set is plain data, emitted fresh on each page render, which also lets a hyperelement be redefined in a live notebook.
6
+
7
+ Docs: https://AnswerDotAI.github.io/answerui/behaviors.html.md"""
8
+
9
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_behaviors.ipynb.
10
+
11
+ # %% auto #0
12
+ __all__ = []
13
+
14
+ # %% ../nbs/01_behaviors.ipynb #01a51b47
15
+ from fastcore.utils import *
16
+ from .registry import Registry
17
+
18
+ # %% ../nbs/01_behaviors.ipynb #1bb695c2
19
+ _runtime = r"""(() => {
20
+ %s
21
+ const rules = [%s];
22
+ const by = {};
23
+ rules.forEach(r => r.evs.forEach(ev => (by[ev + '|' + (r.cap ? 1 : 0)] ??= []).push(r)));
24
+ for (const k in by) {
25
+ const [ev, cap] = k.split('|');
26
+ document.addEventListener(ev, e => {
27
+ const t = e.target;
28
+ if (!(t instanceof Element)) return;
29
+ for (const r of by[k]) {
30
+ const el = t.closest(r.sel);
31
+ if (el) r.fn(el, e);
32
+ }
33
+ }, {capture: cap === '1'});
34
+ }
35
+ const init = rules.filter(r => r.init);
36
+ if (init.length) htmx.onLoad(root => {
37
+ const e = new Event('init');
38
+ for (const r of init) {
39
+ if (root.matches(r.sel)) r.fn(root, e);
40
+ root.querySelectorAll(r.sel).forEach(el => r.fn(el, e));
41
+ }
42
+ });
43
+ })();"""
44
+
45
+ # %% ../nbs/01_behaviors.ipynb #10fe79ef
46
+ _cleanup = ['htmx:before-cleanup-element', 'htmx:before:cleanup', 'htmx-before-cleanup']
47
+
48
+ def _event(ev):
49
+ "The canonical event name: a leading `:` is the htmx shorthand, and every spelling of an htmx event becomes `htmx:` plus its hyphenated name"
50
+ if ev.startswith(':'): ev = 'htmx'+ev
51
+ if not ev.startswith(('htmx:', 'htmx-')): return ev
52
+ return 'htmx:' + re.sub(r'(?<=[a-z])(?=[A-Z])', '-', ev[5:]).lower().replace(':', '-')
53
+
54
+ def _spellings(ev):
55
+ "The DOM event names a canonical event fires under: htmx 2's hyphenated alias, htmx 4's colon form, and htmx 4's form under FastHTML's `metaCharacter`"
56
+ if ev == 'init': return []
57
+ if ev == 'cleanup': return _cleanup
58
+ if not ev.startswith('htmx:'): return [ev]
59
+ name = ev[5:]
60
+ return uniqueify([ev, 'htmx:'+name.replace('-', ':'), 'htmx-'+name])
61
+
62
+ def _entry(r, fn):
63
+ ev = _event(r['ev'])
64
+ return f'{{sel: {dumps(r["sel"])}, evs: {dumps(_spellings(ev))}, cap: {dumps(r["cap"])}, init: {dumps(ev=="init")}, fn: {fn}}}'
65
+
66
+
67
+ # %% ../nbs/01_behaviors.ipynb #bb776c31
68
+ @patch
69
+ def behaviors(self:Registry):
70
+ "The effective behavior per (selector, event, capture): the last declared in registry order, with the key of the rule that declared it"
71
+ res = {}
72
+ for k,r in self.rules.items():
73
+ for b in r.on: res[(b['sel'], _event(b['ev']), b['cap'])] = dict(b, ev=_event(b['ev']), rule=k)
74
+ return res
75
+
76
+ @patch
77
+ def build_js(self:Registry):
78
+ "The behavior script: one function per distinct handler, one entry per effective behavior"
79
+ bs = self.behaviors().values()
80
+ fns = {js: f'f{i}' for i,js in enumerate(uniqueify([b['js'] for b in bs]))}
81
+ decls = '\n'.join(f'async function {nm}(el, e) {{ {js} }}' for js,nm in fns.items())
82
+ return _runtime % (decls, ',\n'.join(_entry(b, fns[b['js']]) for b in bs))
83
+