Skip to content

root tracking for macro components#3882

Merged
SteffenDE merged 3 commits intomainfrom
sd-macro-component-root
Jul 7, 2025
Merged

root tracking for macro components#3882
SteffenDE merged 3 commits intomainfrom
sd-macro-component-root

Conversation

@SteffenDE
Copy link
Collaborator

Closes #3877.

I'm not sure though if we want this, since it would still fail if you do

# inside LiveComponent
def render(assigns) do
  ~H"""
  <script :type={ColocatedHook} ...>...</script>

  <div>
    ...
  </div>
  """
end

because the extra whitespace between the colocated hook and the div counts as text that sets root to false.

So in practice, this may not help much.

@josevalim
Copy link
Member

I thought we ignored whitespace?

Perhaps what we should do for now is simply to improve the error message. If we fail because of macro component being at root, we tell people to move it inside the root element.

@SteffenDE
Copy link
Collaborator Author

Maybe, but I don't see a straightforward way to differentiate between a macro component and any other reason for root being false. It would be easier to special case the code to prune whitespace if the macro component returned an empty string.

We pass trim: true to EEx, so I think by default any leading whitespace is ignored, but if the macro component is first and happens to return an empty string (removing itself) the whitespace afterwards is considered to be just regular whitespace in the template. We only ignore empty strings:

defp handle_token([{:text, text, %{line_end: line, column_end: column}} | tokens], state) do
if text == "" do
continue(state, tokens)

@josevalim
Copy link
Member

Maybe we trim white space after a macro component that returns nothing at the root?

@SteffenDE
Copy link
Collaborator Author

Yep, that's what bfe39a7 does. Let me know if you're happy with that.

@SteffenDE SteffenDE merged commit 26bb662 into main Jul 7, 2025
12 of 14 checks passed
@SteffenDE SteffenDE deleted the sd-macro-component-root branch July 7, 2025 20:22
zachdaniel pushed a commit to zachdaniel/phoenix_live_view that referenced this pull request Jul 9, 2025
* root tracking for macro components

Closes phoenixframework#3877.

* prune text after empty macro component

* remove prune_text_after_slot
SteffenDE added a commit that referenced this pull request Jul 14, 2025
* root tracking for macro components

Closes #3877.

* prune text after empty macro component

* remove prune_text_after_slot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collocated hooks triggering single root element protection on stateful components

2 participants

Comments