Commit 9efb374
fix(toast): initialize toaster state in initialize() so pre-existing targets don't throw (#499)
* fix(toast): init toaster state in initialize() to survive pre-existing targets
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Update toaster_controller.js
Signed-off-by: Jean Pierry <pierrybm@gmail.com>
* Update toaster_controller.js
Signed-off-by: Jean Pierry <pierrybm@gmail.com>
* chore(mcp): rebuild registry.json for toaster_controller fix
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* fix(toast): clear _listEl on disconnect for symmetric teardown
`connect()` resolves `_listEl`, but `disconnect()` left it pointing at the
detached `<ol>`. Stimulus tears down in this order (Context#disconnect):
`controller.disconnect()` first, then `targetObserver.stop()` ->
`disconnectAllTargets()`, so every `toastTargetDisconnected` — and the
`_reflow()` it triggers — runs *after* the controller disconnected, writing
inline styles onto nodes on their way out.
Null out `_listEl` at the end of `disconnect()` so `_reflow()`'s existing
`if (!this._listEl) return` guard actually does its job.
The global API closure (`window.RubyUI.toast`) outlives the controller and
calls `_dismissById` / `_mutate` directly rather than through the removed
window listeners, so those reads become optional and `_spawn` bails early
instead of throwing on a null list.
* refactor(toast): react to position/expand via value changed callbacks
`position` and `expand` are Stimulus values, but nothing reacted to them
changing: `_spawn` mirrored `positionValue` into `data-position` by hand, and
`expand` was only read once in `connect()`. Flipping either attribute at
runtime (`element.dataset.rubyUiToasterExpandValue = "true"`) had no effect
until the next pointer event.
Add `positionValueChanged` / `expandValueChanged` so the DOM follows the value:
the position callback owns the `data-position` sync the CSS placement rules key
off, and both reflow. `_spawn` now just assigns `positionValue`.
Both callbacks fire during `valueObserver.start()`, before `connect()`, so they
rely on `_reflow()`'s `if (!this._listEl) return` guard — same pattern as the
target callbacks.
`connect()` keeps `_expanded = this.expandValue`: `StringMapObserver#stop` does
not clear its map, so an unchanged attribute fires no callback when the
observer restarts. The region is `data-turbo-permanent`, so it really does
disconnect/reconnect with the same controller instance, and without that line
a toaster left hover-expanded would reconnect still expanded.
* fix(toast): make toastTargetConnected idempotent
Now that `_resizeObservers` lives in `initialize()` it survives
disconnect/reconnect cycles of the same element, so a second
`toastTargetConnected` for an element that still has an observer would
overwrite the WeakMap entry and leak the first one — it keeps observing and
keeps calling `_reflow()`.
Stimulus guards against that today (`TargetObserver#connectTarget` skips
elements already in `targetsByName`), but the callback should not depend on
that internal: disconnect any existing observer for the element first.
---------
Signed-off-by: Jean Pierry <pierrybm@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Djalma Araújo <djalma@nossomos.cc>1 parent 58317ff commit 9efb374
3 files changed
Lines changed: 49 additions & 17 deletions
File tree
- docs/app/javascript/controllers/ruby_ui
- gem/lib/ruby_ui/toast
- mcp/data
Lines changed: 24 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
45 | 49 | | |
46 | 50 | | |
47 | 51 | | |
| |||
59 | 63 | | |
60 | 64 | | |
61 | 65 | | |
| 66 | + | |
| 67 | + | |
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
| |||
67 | 73 | | |
68 | 74 | | |
69 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
70 | 87 | | |
71 | 88 | | |
72 | 89 | | |
| 90 | + | |
73 | 91 | | |
74 | 92 | | |
75 | 93 | | |
| |||
90 | 108 | | |
91 | 109 | | |
92 | 110 | | |
| 111 | + | |
93 | 112 | | |
94 | 113 | | |
95 | 114 | | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 115 | + | |
100 | 116 | | |
101 | 117 | | |
102 | 118 | | |
| |||
151 | 167 | | |
152 | 168 | | |
153 | 169 | | |
154 | | - | |
| 170 | + | |
155 | 171 | | |
156 | 172 | | |
157 | 173 | | |
| |||
243 | 259 | | |
244 | 260 | | |
245 | 261 | | |
246 | | - | |
| 262 | + | |
247 | 263 | | |
248 | 264 | | |
249 | 265 | | |
| |||
278 | 294 | | |
279 | 295 | | |
280 | 296 | | |
281 | | - | |
| 297 | + | |
282 | 298 | | |
283 | 299 | | |
284 | 300 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
45 | 49 | | |
46 | 50 | | |
47 | 51 | | |
| |||
59 | 63 | | |
60 | 64 | | |
61 | 65 | | |
| 66 | + | |
| 67 | + | |
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
| |||
67 | 73 | | |
68 | 74 | | |
69 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
70 | 87 | | |
71 | 88 | | |
72 | 89 | | |
| 90 | + | |
73 | 91 | | |
74 | 92 | | |
75 | 93 | | |
| |||
90 | 108 | | |
91 | 109 | | |
92 | 110 | | |
| 111 | + | |
93 | 112 | | |
94 | 113 | | |
95 | 114 | | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 115 | + | |
100 | 116 | | |
101 | 117 | | |
102 | 118 | | |
| |||
151 | 167 | | |
152 | 168 | | |
153 | 169 | | |
154 | | - | |
| 170 | + | |
155 | 171 | | |
156 | 172 | | |
157 | 173 | | |
| |||
243 | 259 | | |
244 | 260 | | |
245 | 261 | | |
246 | | - | |
| 262 | + | |
247 | 263 | | |
248 | 264 | | |
249 | 265 | | |
| |||
278 | 294 | | |
279 | 295 | | |
280 | 296 | | |
281 | | - | |
| 297 | + | |
282 | 298 | | |
283 | 299 | | |
284 | 300 | | |
| |||
0 commit comments