poetry-core 0.1.8 → 0.1.10
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.
- checksums.yaml +4 -4
- data/.jsdoc_floor +1 -1
- data/CHANGELOG.md +33 -0
- data/app/components/poetry/core/component.rb +5 -4
- data/app/components/poetry/core/concerns/styles.rb +27 -4
- data/app/javascript/poetry/core/behaviors/announce.js +18 -0
- data/app/javascript/poetry/core/behaviors/before_cache.js +15 -0
- data/app/javascript/poetry/core/behaviors/escape.js +16 -0
- data/app/javascript/poetry/core/behaviors/layers.js +55 -0
- data/app/javascript/poetry/core/behaviors/listen.js +18 -0
- data/app/javascript/poetry/core/behaviors/mobile.js +16 -0
- data/app/javascript/poetry/core/behaviors/portal.js +50 -0
- data/app/javascript/poetry/core/behaviors/presence.js +85 -0
- data/app/javascript/poetry/core/behaviors/scroll_lock.js +41 -0
- data/app/javascript/poetry/core/behaviors/typeahead.js +21 -0
- data/app/javascript/poetry/core/combobox_controller.js +32 -55
- data/app/javascript/poetry/core/date_field_controller.js +11 -10
- data/app/javascript/poetry/core/deferred_controller.js +9 -12
- data/app/javascript/poetry/core/dialog_controller.js +17 -30
- data/app/javascript/poetry/core/dismissable_controller.js +10 -15
- data/app/javascript/poetry/core/helpers/scope.js +120 -0
- data/app/javascript/poetry/core/hover_card_controller.js +39 -57
- data/app/javascript/poetry/core/menu_controller.js +57 -82
- data/app/javascript/poetry/core/message_scroller_controller.js +4 -3
- data/app/javascript/poetry/core/navigation_menu_controller.js +17 -21
- data/app/javascript/poetry/core/popover_controller.js +28 -52
- data/app/javascript/poetry/core/select_controller.js +39 -62
- data/app/javascript/poetry/core/sidebar_controller.js +18 -31
- data/app/javascript/poetry/core/toast_controller.js +8 -15
- data/app/javascript/poetry/core/toaster_controller.js +13 -15
- data/app/javascript/poetry/core/tooltip_controller.js +35 -65
- data/app/javascript/poetry/core/tree_controller.js +10 -2
- data/config/controllers_manifest.json +21 -19
- data/lib/poetry/core/check/compile.rb +137 -0
- data/lib/poetry/core/check/renderable_readers.rb +116 -0
- data/lib/poetry/core/check/tag_helpers.rb +156 -0
- data/lib/poetry/core/check.rb +39 -8
- data/lib/poetry/core/engine.rb +16 -7
- data/lib/poetry/core/host_helpers.rb +38 -0
- data/lib/poetry/core/html/attributes.rb +18 -6
- data/lib/poetry/core/slot_markers.rb +77 -0
- data/lib/poetry/core/tag_helper.rb +43 -1
- data/lib/poetry/core/template_compile.rb +112 -20
- data/lib/poetry/core/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f473efd9c0d12c6d6c8df6fd9273881e7c47ae51049041ec77b4e8ca1cbd4287
|
|
4
|
+
data.tar.gz: 87b7bc488972de80728431ab4888b9160c9a7676ae5f956ff155fe0fa52c6cad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 167f3b0a36846778aeda28b072567ed91af0a67c3f55c1eeae79db7785afc4a947a71314d7e2895cf2205dd829eabbfa88a4d1365a5b7a0d423fd8d7fddf6181
|
|
7
|
+
data.tar.gz: 249b441648327a54d8d906a526c269e8194add9badd1468111636ab03d7499962cd9949f6c981c8e0a158cceb41d1e1490e3f18d2414e48786dab70c90b2466f
|
data/.jsdoc_floor
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
586
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.10] - 2026-10-03
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- The per-connect scope for Stimulus controllers, `@poetry/controllers/helpers/scope`: `scopeOf(controller)` hands out one disposer stack per connect, `teardown(controller)` runs it newest first, aborts the scope's signal and drops it, and the next `connect()` starts clean (Stimulus keeps one instance per element and re-runs `connect()`, which the suite now pins). A controller's `disconnect()` is that one line.
|
|
8
|
+
- Ten behaviors over it, under `@poetry/controllers/behaviors/`: `useListen`, `useEscape`, `useBeforeCache`, `usePresence`, `usePortal`, `useScrollLock`, `useAnnounce`, `useLayers`, `useTypeahead` and `useMobile`. Each is a plain function that does its setup, registers its undo in the scope and returns a handle; none assigns onto the controller. `usePresence` tracks exits per node, so an enter interrupts that node's exit alone; `usePortal` restores every node it moved at teardown, newest first; `useScrollLock` is idempotent per instance; `useLayers` is the one implementation of the layer-token add and remove.
|
|
9
|
+
- A fourth source-scan gate, `behaviors_teardown.test.js`: a controller that uses the scope without `teardown(this)` in its `disconnect()`, or a subclass that overrides `disconnect()` without `super.disconnect()`, fails the suite.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- Sixteen controllers run their lifecycle through the scope: the seven hand-rolled `#wired` lists (combobox, hover card, menu, popover, select, toast, tooltip), the `#unsubscribe` fields (dialog, dismissable, navigation menu, sidebar, deferred), the six copies of the layer-token add and remove, the typeahead and scroll-lock instances, and the message scroller's raw before-cache string. Rendered markup and behaviour are unchanged; the suites, the dommy tier and a browser pass are the oracle.
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
|
|
17
|
+
- Three teardown leaks: a typeahead's reset timer outlived its controller (menu, select, tree), a date field's four per-segment listeners were never removed, and a submenu re-opened while its exit was still running had that exit's removal hide the re-opened level (the sub exits carried no cancel; the per-node presence gives them one).
|
|
18
|
+
- A composed trigger lists its own wiring ahead of the caller's actions again. `Attributes` unified the flat and nested spellings of `data-action` and `data-controller` by appending the flat tokens after the nested ones whichever came first, so a Button composed as a Collapsible, Popover, Tooltip or DropdownMenu trigger rendered the caller's `data: { action: }` ahead of the component's `data-action`. Stimulus runs actions in attribute order, and a caller's action written to read the state the toggle leaves behind read the state before it instead: the docs sidebar remembered each disclosure as the opposite of what was chosen. The two spellings now keep the order the hash gave them.
|
|
19
|
+
- A subclass without a `Style` sidecar of its own renders through its parent's dictionary. `Acme::Badge2 < Poetry::Ui::Badge::Component` with nothing beside it rendered no class attribute at all, in any mode, with no message: the dictionary lookup stopped at the class's own name. It now walks up to the nearest ancestor with a sidecar, short of `Poetry::Core::Component`, so a bare subclass renders what its parent renders and a sidecar of its own still takes over. A component built directly on the base with no sidecar stays dictionaryless, as before.
|
|
20
|
+
|
|
21
|
+
## [0.1.9] - 2026-09-30
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- `element_tag(name, ..., **attributes)`, on every component and in every view: the helper a template builds its elements through, and where a component's template spends what `root_attributes` and `element_attributes` build. It takes what `content_tag` takes and renders the same bytes, and it renders a void element the way `tag.input` does. A host that compiles its templates with Herb's slots has Action View's own tag helpers resolved into markup ahead of the render, and a splat or a tag name held in a variable does not survive that: the element comes out without its attributes, or the template stops compiling. A helper Herb does not know stays a call. Components written on the DSL in a host should build their elements the same way.
|
|
26
|
+
- `poetry check` holds a component's template (an ERB file under `app/components/`) to the element rule, `element-tag`, as a warning: a tag helper handed a splat or a variable for its attributes, `content_tag` given a tag name that is not a literal, and `tag.attributes` handed a splat each name the `element_tag` call to write instead. A view is left alone: handing a tag helper a splat is valid Rails, and whether the host compiles with slots is not something a lint of the source can know.
|
|
27
|
+
- `poetry check` compiles a component's HTML template under Herb, in the posture `bin/rails herb:check` holds an app's views to: the security validator, with the compiled Ruby checked for syntax. Rails renders HTML templates through Herb under its 8.2 defaults and its task walks the view paths, so a template under `app/components/` is outside its reach. Rails checks views, Poetry checks components. A template Herb refuses is a `herb-compile` finding that carries the engine's reason and its line: an error when the app renders through Herb, a warning until then. The runner compiles when it is handed the tier (`Check::Runner.new(catalog, compile:)`), and `Poetry::Core::TemplateCompile` takes `posture: :rails` for it beside the strict posture the gems' own gate runs.
|
|
28
|
+
- `poetry check` holds a component class to the renderable rule, `renderable-reader`: an `option`, a `style` or a slot named `format` defines the reader Rails asks a component for the format of its template, and it answers with the declared value, so a render through the renderer raises `Invalid formats`. From Rails 8.2 every render in a view goes through the renderer, so the host's check reports it as an error there and as a warning before. A class that defines `format` itself, with `def` or `define_method`, is left alone: it keeps the keyword for its callers and reads the value under another name, which is what NumberField does.
|
|
29
|
+
- `Poetry::Core::SlotMarkers.strip(html)` takes off what a slot compile writes around a template's own markup: the region, slot and branch comments, the `data-herb-slot` attribute and the branches parked in a `<template>`. What is left is the markup the template wrote, which is what a test in a host that compiles with slots means to compare.
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- The helpers of an app's own components (`helper :name`) are defined before the first view renders, not while the application is still initializing. The engine used to load every `app/components` directory from `to_prepare`, and a component is a view, so Action View loaded before initialization completed: Rails logs that in a boot that does not eager load, and raises on it when `config.action_on_early_load_hook` is `:raise`. A host that eager loads now syncs once its boot is done, with the classes already loaded; every other host syncs on its first view context, at render time; a reload syncs as before. `Poetry::Core::HostHelpers.sync_now!` is the sync, `ready?` says whether the first one has happened, and `HostHelpers::FirstView` is what Action View's base class gets for it.
|
|
34
|
+
- The template compile gate (`rake herb:compile`, `Poetry::Core::TemplateCompile`) runs on Herb 0.11. It requires the engine itself, since `require "herb"` no longer loads it. It hands the engine every validator Herb ships, each one fatal, since from 0.11 the engine validates nothing it is not handed, and has the compiled Ruby checked for syntax. It lists parse and compile errors, which Herb now raises as syntax errors, where they used to pass the rescue and abort the run. A Herb older than 0.11.0 raises a setup error that names the version found.
|
|
35
|
+
|
|
3
36
|
## [0.1.8] - 2026-09-22
|
|
4
37
|
|
|
5
38
|
### Added
|
|
@@ -39,6 +39,7 @@ module Poetry
|
|
|
39
39
|
include ActiveModel::AttributeAssignment
|
|
40
40
|
include ActiveModel::Validations
|
|
41
41
|
include Poetry::Core::Contrib::WrappedHelper
|
|
42
|
+
include Poetry::Core::TagHelper
|
|
42
43
|
include Poetry::Core::Concerns::Styles
|
|
43
44
|
include Poetry::Core::Concerns::Options
|
|
44
45
|
include Poetry::Core::Concerns::Stimulus
|
|
@@ -296,12 +297,12 @@ module Poetry
|
|
|
296
297
|
# super("role" => "status", "data-variant" => variant)
|
|
297
298
|
# end
|
|
298
299
|
# @example The template
|
|
299
|
-
# <%=
|
|
300
|
+
# <%= element_tag(:div, **root_attributes) do %>
|
|
300
301
|
#
|
|
301
302
|
# @param extra [Hash] the component's own root attributes; a
|
|
302
303
|
# `"data-slot"` here replaces the default
|
|
303
304
|
# @return [Hash] the root's attributes, flat (`data-x`, `aria-x`,
|
|
304
|
-
# booleans as the attribute name), for
|
|
305
|
+
# booleans as the attribute name), for {Poetry::Core::TagHelper#element_tag}
|
|
305
306
|
def root_attributes(extra = {})
|
|
306
307
|
own = { "data-slot" => root_slot }.merge(extra).merge(component_data_attributes)
|
|
307
308
|
wired = self.class.stimulus_elements.key?(:root) ? :root : nil
|
|
@@ -335,7 +336,7 @@ module Poetry
|
|
|
335
336
|
# attrs["hidden"] = true unless open
|
|
336
337
|
# element_attributes(:content, attrs)
|
|
337
338
|
# end
|
|
338
|
-
# # <%=
|
|
339
|
+
# # <%= element_tag(:div, **content_attributes) do %>
|
|
339
340
|
#
|
|
340
341
|
# @param part [Symbol, String, nil] the part; nil (or a Hash in its
|
|
341
342
|
# place) for markup with no part of its own
|
|
@@ -343,7 +344,7 @@ module Poetry
|
|
|
343
344
|
# @param stimulus [Symbol, String, false, nil] the `use_stimulus`
|
|
344
345
|
# element whose wiring joins the markup: nil takes the part's own when
|
|
345
346
|
# one is declared, false takes none
|
|
346
|
-
# @return [Hash] the element's attributes, flat, for
|
|
347
|
+
# @return [Hash] the element's attributes, flat, for {Poetry::Core::TagHelper#element_tag}
|
|
347
348
|
def element_attributes(part = nil, attrs = {}, stimulus: nil)
|
|
348
349
|
if part.is_a?(Hash)
|
|
349
350
|
attrs = part
|
|
@@ -161,11 +161,26 @@ module Poetry
|
|
|
161
161
|
style_attributes.include?(name.to_sym)
|
|
162
162
|
end
|
|
163
163
|
|
|
164
|
-
#
|
|
165
|
-
#
|
|
164
|
+
# The dictionary this component renders through: its own sidecar
|
|
165
|
+
# Style by the naming convention (Poetry::Core::Dot::Component ->
|
|
166
|
+
# Poetry::Core::Dot::Style), else the nearest ancestor's. A bare
|
|
167
|
+
# subclass (`Acme::Badge2 < Poetry::Ui::Badge::Component` with no
|
|
168
|
+
# `Acme::Badge2::Style`) keeps rendering the classes its parent
|
|
169
|
+
# renders, the way a subclass keeps the parent's methods; a
|
|
170
|
+
# sidecar of its own takes over, extending a copy of the parent's
|
|
171
|
+
# dictionary. A component with no dictionary anywhere in its
|
|
172
|
+
# chain (Icon) has none.
|
|
166
173
|
#
|
|
167
|
-
# @return [Class, nil] the style class
|
|
174
|
+
# @return [Class, nil] the style class, or nil when neither this
|
|
175
|
+
# class nor an ancestor below Poetry::Core::Component has one
|
|
168
176
|
def style_class
|
|
177
|
+
own_style_class || inherited_style_class
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
private
|
|
181
|
+
|
|
182
|
+
# This class's own sidecar Style, by name, or nil.
|
|
183
|
+
def own_style_class
|
|
169
184
|
style_class_name = component_module + STYLE_CLASS_SUFFIX
|
|
170
185
|
style_class_name.constantize
|
|
171
186
|
rescue NameError => e
|
|
@@ -178,7 +193,15 @@ module Poetry
|
|
|
178
193
|
nil
|
|
179
194
|
end
|
|
180
195
|
|
|
181
|
-
|
|
196
|
+
# The dictionary of the nearest ancestor that has one, stopping
|
|
197
|
+
# short of Poetry::Core::Component: the base class owns no
|
|
198
|
+
# component dictionary, so a component built directly on it with
|
|
199
|
+
# no sidecar stays dictionaryless.
|
|
200
|
+
def inherited_style_class
|
|
201
|
+
return nil if superclass == Poetry::Core::Component || !superclass.respond_to?(:style_class)
|
|
202
|
+
|
|
203
|
+
superclass.style_class
|
|
204
|
+
end
|
|
182
205
|
|
|
183
206
|
# Extracts and processes style-specific options from the options hash.
|
|
184
207
|
#
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// useAnnounce: the shared live regions held for one connect, over the
|
|
2
|
+
// announce helper; the refcount is released at teardown.
|
|
3
|
+
import { acquire, announce, release } from "@poetry/controllers/helpers/announce"
|
|
4
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Takes a refcount on the shared live regions until teardown(controller)
|
|
8
|
+
* and hands back the announcement surface.
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
11
|
+
* @returns {(message: string, politeness?: "polite" | "assertive") => void} announce
|
|
12
|
+
*/
|
|
13
|
+
export function useAnnounce(controller) {
|
|
14
|
+
acquire()
|
|
15
|
+
scopeOf(controller).defer(release)
|
|
16
|
+
|
|
17
|
+
return announce
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// useBeforeCache: Turbo's before-cache moment for one connect, over the
|
|
2
|
+
// turbo_cache helper; the unsubscribe is registered for you.
|
|
3
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
4
|
+
import { onBeforeCache } from "@poetry/controllers/helpers/turbo_cache"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Subscribes `callback` to turbo:before-cache until teardown(controller).
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
10
|
+
* @param {(event: Event) => void} callback - must finish synchronously
|
|
11
|
+
* @returns {() => void} unsubscribes early; teardown unsubscribes otherwise
|
|
12
|
+
*/
|
|
13
|
+
export function useBeforeCache(controller, callback) {
|
|
14
|
+
return scopeOf(controller).defer(onBeforeCache(callback))
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// useEscape: capture-phase Escape handling (IME cancels filtered) for one
|
|
2
|
+
// connect, over the escape helper; the unsubscribe is registered for you.
|
|
3
|
+
import { onEscapeKeydown } from "@poetry/controllers/helpers/escape"
|
|
4
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Subscribes `callback` to Escape keydowns until teardown(controller).
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
10
|
+
* @param {(event: KeyboardEvent) => void} callback - the handler
|
|
11
|
+
* @param {Object} [options] - the escape helper's options (capture, target)
|
|
12
|
+
* @returns {() => void} unsubscribes early; teardown unsubscribes otherwise
|
|
13
|
+
*/
|
|
14
|
+
export function useEscape(controller, callback, options) {
|
|
15
|
+
return scopeOf(controller).defer(onEscapeKeydown(callback, options))
|
|
16
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// useLayers: the runtime layer activation (focus-scope, dismissable,
|
|
2
|
+
// roving-focus stacked onto content while it is open) for one connect.
|
|
3
|
+
// One implementation of the data-controller token add and remove that
|
|
4
|
+
// six overlay controllers each carried; what is still active at teardown
|
|
5
|
+
// is deactivated, so a torn-down overlay leaves no layer controller alive
|
|
6
|
+
// on an orphaned node.
|
|
7
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Layer activation over the controller's current scope.
|
|
11
|
+
*
|
|
12
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
13
|
+
* @returns {{ activate: (element: Element, identifiers: string[]) => void,
|
|
14
|
+
* deactivate: (element: Element, identifiers: string[]) => void }}
|
|
15
|
+
*/
|
|
16
|
+
export function useLayers(controller) {
|
|
17
|
+
const active = new Map()
|
|
18
|
+
|
|
19
|
+
const deactivate = (element, identifiers) => {
|
|
20
|
+
const tokens = (element.getAttribute("data-controller") ?? "")
|
|
21
|
+
.split(/\s+/)
|
|
22
|
+
.filter((token) => token && !identifiers.includes(token))
|
|
23
|
+
|
|
24
|
+
element.setAttribute("data-controller", tokens.join(" "))
|
|
25
|
+
|
|
26
|
+
const remaining = (active.get(element) ?? []).filter((token) => !identifiers.includes(token))
|
|
27
|
+
|
|
28
|
+
if (remaining.length > 0) active.set(element, remaining)
|
|
29
|
+
else active.delete(element)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
scopeOf(controller).defer(() => {
|
|
33
|
+
for (const [element, identifiers] of [...active]) deactivate(element, identifiers)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
/**
|
|
38
|
+
* Adds the identifiers to the element's data-controller, once each.
|
|
39
|
+
* @param {Element} element
|
|
40
|
+
* @param {string[]} identifiers
|
|
41
|
+
* @returns {void}
|
|
42
|
+
*/
|
|
43
|
+
activate(element, identifiers) {
|
|
44
|
+
const tokens = (element.getAttribute("data-controller") ?? "").split(/\s+/).filter(Boolean)
|
|
45
|
+
|
|
46
|
+
for (const identifier of identifiers) {
|
|
47
|
+
if (!tokens.includes(identifier)) tokens.push(identifier)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
element.setAttribute("data-controller", tokens.join(" "))
|
|
51
|
+
active.set(element, [...new Set([...(active.get(element) ?? []), ...identifiers])])
|
|
52
|
+
},
|
|
53
|
+
deactivate
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// useListen: a DOM listener that lives for one connect. Replaces the
|
|
2
|
+
// hand-rolled `#wired` list seven controllers copied: the listener is
|
|
3
|
+
// added now and removed at teardown(controller), newest first.
|
|
4
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Adds `listener` to `target` for the controller's current connect.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
10
|
+
* @param {EventTarget} target - the element, document or window to listen on
|
|
11
|
+
* @param {string} type - the event type
|
|
12
|
+
* @param {EventListener} listener - the handler
|
|
13
|
+
* @param {AddEventListenerOptions | boolean} [options] - addEventListener's options
|
|
14
|
+
* @returns {() => void} removes the listener early; teardown removes it otherwise
|
|
15
|
+
*/
|
|
16
|
+
export function useListen(controller, target, type, listener, options) {
|
|
17
|
+
return scopeOf(controller).listen(target, type, listener, options)
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// useMobile: the mobile-breakpoint watch for one connect, over the
|
|
2
|
+
// breakpoint helper; the unwatch is registered for you.
|
|
3
|
+
import { watchMobile } from "@poetry/controllers/helpers/breakpoint"
|
|
4
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Watches the mobile breakpoint until teardown(controller): `onChange`
|
|
8
|
+
* runs now with the current state and again on every crossing.
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
11
|
+
* @param {(isMobile: boolean) => void} onChange - the handler
|
|
12
|
+
* @returns {() => void} unwatches early; teardown unwatches otherwise
|
|
13
|
+
*/
|
|
14
|
+
export function useMobile(controller, onChange) {
|
|
15
|
+
return scopeOf(controller).defer(watchMobile(onChange))
|
|
16
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// usePortal: content moved to a portal container for one connect, over
|
|
2
|
+
// the portal helper. Every node this instance portals is restored at
|
|
3
|
+
// teardown (drop-never-strand: a node whose origin is gone is dropped by
|
|
4
|
+
// the helper), so no controller writes its own restore-on-disconnect.
|
|
5
|
+
import { portalContent, restoreContent } from "@poetry/controllers/helpers/portal"
|
|
6
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Portal moves over the controller's current scope.
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
12
|
+
* @returns {{ portal: (content: Element, options?: Object) => boolean,
|
|
13
|
+
* restore: (content: Element) => boolean }}
|
|
14
|
+
*/
|
|
15
|
+
export function usePortal(controller) {
|
|
16
|
+
const moved = new Set()
|
|
17
|
+
|
|
18
|
+
// Newest first: a sub level portaled after its parent goes home before
|
|
19
|
+
// the parent does, while its placeholder still sits inside the parent.
|
|
20
|
+
scopeOf(controller).defer(() => {
|
|
21
|
+
for (const content of [...moved].reverse()) restoreContent(content)
|
|
22
|
+
moved.clear()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
/**
|
|
27
|
+
* Moves `content` to the container; a no-op when already portaled.
|
|
28
|
+
* @param {Element} content
|
|
29
|
+
* @param {Object} [options] - the portal helper's options (container)
|
|
30
|
+
* @returns {boolean} true when the move happened
|
|
31
|
+
*/
|
|
32
|
+
portal(content, options) {
|
|
33
|
+
const happened = portalContent(content, options)
|
|
34
|
+
|
|
35
|
+
if (happened) moved.add(content)
|
|
36
|
+
|
|
37
|
+
return happened
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* Returns `content` home now.
|
|
41
|
+
* @param {Element} content
|
|
42
|
+
* @returns {boolean} true when the content went home
|
|
43
|
+
*/
|
|
44
|
+
restore(content) {
|
|
45
|
+
moved.delete(content)
|
|
46
|
+
|
|
47
|
+
return restoreContent(content)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// usePresence: enter and exit animations for one connect, over the
|
|
2
|
+
// presence helper, with the pending exits tracked PER NODE. An exit's
|
|
3
|
+
// cancel used to live in one `#cancelExit` slot per controller, which a
|
|
4
|
+
// second exiting node (NavigationMenu's lesson) silently overwrote; here
|
|
5
|
+
// every node keeps its own, an enter interrupts that node's exit alone,
|
|
6
|
+
// and teardown abandons whatever is still exiting without running its
|
|
7
|
+
// removal (an interrupted exit, never a removal from a torn-down scope).
|
|
8
|
+
import { enterPresence, exitPresence } from "@poetry/controllers/helpers/presence"
|
|
9
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Presence over the controller's current scope.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
15
|
+
* @returns {{ enter: (element: HTMLElement, options?: Object) => HTMLElement,
|
|
16
|
+
* exit: (element: HTMLElement, options?: Object) => () => void,
|
|
17
|
+
* cancel: (element: HTMLElement) => boolean,
|
|
18
|
+
* exiting: (element: HTMLElement) => boolean }}
|
|
19
|
+
*/
|
|
20
|
+
export function usePresence(controller) {
|
|
21
|
+
const pending = new Map()
|
|
22
|
+
|
|
23
|
+
const cancel = (element) => {
|
|
24
|
+
const abandon = pending.get(element)
|
|
25
|
+
|
|
26
|
+
if (!abandon) return false
|
|
27
|
+
|
|
28
|
+
pending.delete(element)
|
|
29
|
+
abandon()
|
|
30
|
+
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
scopeOf(controller).defer(() => {
|
|
35
|
+
for (const abandon of [...pending.values()]) abandon()
|
|
36
|
+
pending.clear()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
/**
|
|
41
|
+
* Runs the entry on `element`, interrupting an exit of the same node.
|
|
42
|
+
* @param {HTMLElement} element
|
|
43
|
+
* @param {Object} [options] - the presence helper's enter options
|
|
44
|
+
* @returns {HTMLElement} the element
|
|
45
|
+
*/
|
|
46
|
+
enter(element, options) {
|
|
47
|
+
cancel(element)
|
|
48
|
+
|
|
49
|
+
return enterPresence(element, options)
|
|
50
|
+
},
|
|
51
|
+
/**
|
|
52
|
+
* Runs the exit on `element`; `options.onRemove` runs once the exit
|
|
53
|
+
* finishes, unless an enter or a teardown interrupts it first.
|
|
54
|
+
* @param {HTMLElement} element
|
|
55
|
+
* @param {Object} [options] - the presence helper's exit options
|
|
56
|
+
* @param {() => void} [options.onRemove] - the caller's removal step
|
|
57
|
+
* @returns {() => void} abandons this exit without removal
|
|
58
|
+
*/
|
|
59
|
+
exit(element, { onRemove, ...options } = {}) {
|
|
60
|
+
cancel(element)
|
|
61
|
+
|
|
62
|
+
let settled = false
|
|
63
|
+
const abandon = exitPresence(element, {
|
|
64
|
+
...options,
|
|
65
|
+
onRemove: () => {
|
|
66
|
+
settled = true
|
|
67
|
+
pending.delete(element)
|
|
68
|
+
onRemove?.()
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// A node without an exit animation settled inside the call.
|
|
73
|
+
if (!settled) pending.set(element, abandon)
|
|
74
|
+
|
|
75
|
+
return () => cancel(element)
|
|
76
|
+
},
|
|
77
|
+
cancel,
|
|
78
|
+
/**
|
|
79
|
+
* Whether `element` is mid-exit.
|
|
80
|
+
* @param {HTMLElement} element
|
|
81
|
+
* @returns {boolean}
|
|
82
|
+
*/
|
|
83
|
+
exiting: (element) => pending.has(element)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// useScrollLock: the body scroll lock for one connect, over the
|
|
2
|
+
// scroll_lock helper. Idempotent per instance (the dialog's `#locked`
|
|
3
|
+
// flag, generalized): lock twice holds one refcount, unlock when not
|
|
4
|
+
// locked is a no-op, and teardown balances a lock still held.
|
|
5
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
6
|
+
import { lockScroll, unlockScroll } from "@poetry/controllers/helpers/scroll_lock"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A per-instance scroll lock, balanced at teardown(controller).
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
12
|
+
* @returns {{ lock: () => void, unlock: () => void, locked: () => boolean }}
|
|
13
|
+
*/
|
|
14
|
+
export function useScrollLock(controller) {
|
|
15
|
+
let locked = false
|
|
16
|
+
|
|
17
|
+
const unlock = () => {
|
|
18
|
+
if (!locked) return
|
|
19
|
+
|
|
20
|
+
locked = false
|
|
21
|
+
unlockScroll()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
scopeOf(controller).defer(unlock)
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
/** Takes the instance's one refcount on the body lock. */
|
|
28
|
+
lock() {
|
|
29
|
+
if (locked) return
|
|
30
|
+
|
|
31
|
+
locked = true
|
|
32
|
+
lockScroll()
|
|
33
|
+
},
|
|
34
|
+
unlock,
|
|
35
|
+
/**
|
|
36
|
+
* Whether this instance holds its refcount.
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
locked: () => locked
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// useTypeahead: one typeahead per connect, over the typeahead helper,
|
|
2
|
+
// with its buffer timer reset at teardown - the leak the hand-rolled
|
|
3
|
+
// field initializers (menu, select, tree) never closed.
|
|
4
|
+
import { scopeOf } from "@poetry/controllers/helpers/scope"
|
|
5
|
+
import { createTypeahead } from "@poetry/controllers/helpers/typeahead"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A typeahead instance whose pending timer is cleared at
|
|
9
|
+
* teardown(controller).
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} controller - the Stimulus controller instance
|
|
12
|
+
* @returns {{ pending: () => boolean, reset: () => void,
|
|
13
|
+
* search: (key: string, items: Element[], options?: Object) => Element | null }}
|
|
14
|
+
*/
|
|
15
|
+
export function useTypeahead(controller) {
|
|
16
|
+
const typeahead = createTypeahead()
|
|
17
|
+
|
|
18
|
+
scopeOf(controller).defer(() => typeahead.reset())
|
|
19
|
+
|
|
20
|
+
return typeahead
|
|
21
|
+
}
|