Skip to content

Commit

Permalink
Render <turbo-cable-stream-source> as display: none
Browse files Browse the repository at this point in the history
Since the `<turbo-cable-stream-source>` is a Custom Element, it has a
default `style:` property of `display: inline`. As a visible element, it
participates in the page's layout.

This commit changes the element to always be `display: none` so that it
no longer participates in the layout.
  • Loading branch information
seanpdoyle committed Jan 13, 2024
1 parent 00cae6a commit f5230dc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
11 changes: 11 additions & 0 deletions app/assets/javascripts/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4943,7 +4943,18 @@ function walk(obj) {
}), {});
}

const template = Object.assign(document.createElement("template"), {
innerHTML: "<style>:host { display: none; }</style>"
});

class TurboCableStreamSourceElement extends HTMLElement {
constructor() {
super();
this.attachShadow({
mode: "open"
});
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
async connectedCallback() {
connectStreamSource(this);
this.subscription = await subscribeTo(this.channel, {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.min.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions app/javascript/turbo/cable_stream_source_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ import { connectStreamSource, disconnectStreamSource } from "@hotwired/turbo"
import { subscribeTo } from "./cable"
import snakeize from "./snakeize"

const template = Object.assign(document.createElement("template"), {
innerHTML: "<style>:host { display: none; }</style>"
})

class TurboCableStreamSourceElement extends HTMLElement {
constructor() {
super()

this.attachShadow({ mode: "open" })
this.shadowRoot.appendChild(template.content.cloneNode(true))
}

async connectedCallback() {
connectStreamSource(this)
this.subscription = await subscribeTo(this.channel, {
Expand Down
8 changes: 4 additions & 4 deletions test/system/broadcasts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ class BroadcastsTest < ApplicationSystemTestCase
test "Message broadcasts with renderable: render option" do
visit messages_path
wait_for_stream_to_be_connected

assert_broadcasts_text "Test message", to: :messages do |text, target|
Message.create(content: "Ignored").broadcast_append_to(target, renderable: MessageComponent.new(text))
end
end

test "Does not render the layout twice when passed a component" do
visit messages_path
wait_for_stream_to_be_connected

Message.create(content: "Ignored").broadcast_append_to(:messages, renderable: MessageComponent.new("test"))

assert_selector("title", count: 1, visible: false, text: "Dummy")
end

Expand Down

0 comments on commit f5230dc

Please sign in to comment.