Skip to content

Commit a67d109

Browse files
authored
Update MacroComponent docs to include tag_attribute directive (#4211)
1 parent 4947c82 commit a67d109

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

lib/phoenix_component/macro_component.ex

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,52 @@ defmodule Phoenix.Component.MacroComponent do
127127
# other elements in the template outside of the macro component at compile-time. For example:
128128
#
129129
# ```elixir
130-
# defmodule MyAppWeb.TagRootSampleComponent do
130+
# defmodule MyAppWeb.TagAttributesSampleComponent do
131131
# @behaviour Phoenix.Component.MacroComponent
132132
#
133133
# @impl true
134134
# def transform(_ast, _meta) do
135-
# {:ok, "", %{}, [root_tag_attribute: {"phx-sample-one", "test"}, root_tag_attribute: {"phx-sample-two", true}]}
135+
# {:ok, "", %{}, [root_tag_attribute: {"phx-sample-one", "test"}, tag_attribute: {"phx-sample-two", true}]}
136136
# end
137137
# end
138138
# ```
139139
#
140140
# The following directives are currently supported:
141141
#
142-
# * `:root_tag_attribute` - A `{name, value}` tuple to apply as an attribute to all root tags during template compilation.
143-
# Requires that a global `:root_tag_attribute` is configured for the application. The attribute name must be a string and the attribute value must be a string or `true`.
144-
# May be provided multiple times to apply multiple attributes.
142+
# * `root_tag_attribute`: A `{key, value}` tuple that will be added as
143+
# an attribute to all "root tags" of the template during template compilation.
144+
# See the section on root tags below for more information.
145+
# * `tag_attribute`: A `{key, value}` tuple that will be added as an attribute to
146+
# all HTML tags in the template during template compilation.
145147
#
148+
# ## Root tags
149+
#
150+
# In a HEEx template, all outermost tags are considered "root tags" and are
151+
# affected by the `root_tag_attribute` directive. If a template uses components,
152+
# the slots of those components are considered as root tags as well.
153+
#
154+
# Here's an example showing which elements would be considered root tags:
155+
#
156+
# ```heex
157+
# <div> <---- root tag
158+
# <span>Hello</span> <---- not a root tag
159+
#
160+
# <.my_component>
161+
# <p>World</p> <---- root tag
162+
# </.my_component>
163+
# </div>
164+
#
165+
# <.my_component>
166+
# <span>World</span> <---- root tag
167+
#
168+
# <:a_named_slot>
169+
# <div> <---- root tag
170+
# Foo
171+
# <p>Bar</p> <---- not a root tag
172+
# </div>
173+
# </:a_named_slot>
174+
# </.my_component>
175+
# ```
146176

147177
@type tag :: binary()
148178
@type attribute :: {binary(), Macro.t()}
@@ -151,7 +181,9 @@ defmodule Phoenix.Component.MacroComponent do
151181
@type tag_meta :: %{closing: :self | :void}
152182
@type heex_ast :: {tag(), attributes(), children(), tag_meta()} | binary()
153183
@type transform_meta :: %{env: Macro.Env.t()}
154-
@type directive :: {:root_tag_attribute, {name :: String.t(), value :: String.t() | true}}
184+
@type directive ::
185+
{:root_tag_attribute, {key :: term(), value :: term()}}
186+
| {:tag_attribute, {key :: term(), value :: term()}}
155187
@type directives :: [directive]
156188

157189
@callback transform(heex_ast :: heex_ast(), meta :: transform_meta()) ::

0 commit comments

Comments
 (0)