-
Notifications
You must be signed in to change notification settings - Fork 147
/
live_patch.ex
53 lines (43 loc) · 1.41 KB
/
live_patch.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
defmodule Surface.Components.LivePatch do
@moduledoc """
Defines a link that will **patch** the current LiveView.
Provides similar capabilities to Phoenix's built-in `live_patch/2`
function.
When navigating to the current LiveView, `handle_params/3` is
immediately invoked to handle the change of params and URL state.
Then the new state is pushed to the client, without reloading the
whole page. For live redirects to another LiveView, use
`<LiveRedirect>` instead.
"""
use Surface.Component
@doc "The required path to link to"
prop to, :string, required: true
@doc "The flag to replace the current history or push a new state"
prop replace, :boolean, default: false
@doc "The CSS class for the generated `<a>` element"
prop class, :css_class, default: ""
@doc """
The label for the generated `<a>` element, if no content (default slot) is provided.
"""
prop label, :string
@doc """
Additional attributes to add onto the generated element
"""
prop opts, :keyword, default: []
@doc """
The content of the generated `<a>` element. If no content is provided,
the value of property `label` is used instead.
"""
slot default
def render(assigns) do
~F"""
<a
class={@class}
data-phx-link="patch"
data-phx-link-state={if @replace, do: "replace", else: "push"}
href={@to}
:attrs={@opts}
><#slot>{@label}</#slot></a>
"""
end
end