Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dynamic Component Example to Documentation #3632

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions lib/phoenix_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,69 @@ defmodule Phoenix.Component do
config :phoenix_live_view, debug_heex_annotations: true

Changing this configuration will require `mix clean` and a full recompile.

## Dynamic Component Rendering

One of the benefits of using functional components as defined [above](#content) is that we can leverage Elixir's [`apply/3`](https://hexdocs.pm/elixir/Kernel.html#apply/3) function to dynamically call a module and/or function passed in as an assign.
kyleboe marked this conversation as resolved.
Show resolved Hide resolved

For example, using the following functional component definition:

```elixir
attr :module, :atom, required: true
attr :function, :atom, required: true

slot :named_slot, required: true
kyleboe marked this conversation as resolved.
Show resolved Hide resolved

def dynamic_component(assigns) do
{mod, assigns} = Map.pop(assigns, :module)
{func, assigns} = Map.pop(assigns, :function)

apply(mod, func, [assigns])
end
```

You can then use the `dynamic_component` function like so:
kyleboe marked this conversation as resolved.
Show resolved Hide resolved

```heex
<.dynamic_component
module={MyAppWeb.MyModule}
function={:my_function}
shared="Yay Elixir!"
>
<p>Howdy from the inner block!</p>
<:named_slot>
<p>Howdy from the named slot!</p>
</:named_slot>
</.dynamic_component>
```

This will call the `MyAppWeb.MyModule.my_function/1` function passing in the remaining assigns.

```elixir
defmodule MyApp.MyModule do
kyleboe marked this conversation as resolved.
Show resolved Hide resolved
attr :shared, :string, required: true

slot :named_slot, required: true
kyleboe marked this conversation as resolved.
Show resolved Hide resolved

def my_function(assigns) do
~H"""
<p>Dynamic component with shared assigns: {@shared}</p>
{render_slot(@inner_block)}
{render_slot(@named_slot)}
"""
end
end
```

Resulting in the following HTML:

```html
<p>Dynamic component with shared assigns: Yay Elixir!</p>
<p>Howdy from the inner block!</p>
<p>Howdy from the named slot!</p>
```

All while maintaining the benefits of the `attr/3` and `slot/3` macros for compile-time validations.
kyleboe marked this conversation as resolved.
Show resolved Hide resolved
kyleboe marked this conversation as resolved.
Show resolved Hide resolved
'''

## Functions
Expand Down