-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
router.ex
180 lines (148 loc) · 5.44 KB
/
router.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
defmodule PhxLiveStorybook.Router do
@moduledoc """
Provides LiveView routing for storybook.
"""
@doc """
Defines a PhxLiveStorybook route.
It expects the `path` the storybook will be mounted at and a set of options.
This will also generate a named helper called `live_storybook_path/2` which you can use to link
directly to the storybook, such as:
```elixir
<.link href={live_storybook_path(conn, :root)} />
```
Note that you should only use the `href` attribute to link to the storybook,
as it has to set its own session on first rendering. Linking with `patch` or
`navigate` will not work.
## Options
* `:backend_module` - _Required_ - Name of your backend module.
* `:live_socket_path` - Configures the socket path. It must match the
`socket "/live", Phoenix.LiveView.Socket` in your endpoint.
* `:assets_path` - Configures the assets path. It must match the `storybook_assets` in your
router. Defaults to `"/storybook/assets"`.
* `:session_name` - Configures the live session name. Defaults to `:live_storybook`. Use this
option if you want to mount multiple storybooks in the same router.
* `:as` - Allows you to set the route helper name. Defaults to`:live_storybook`.
* `:pipeline` - Set to `false` if you don't want a router pipeline to be created. This is useful
if you want to define your own `:storybook_browser` pipeline, or if you mount multiple
storybooks, in which case the pipeline only has to be defined once. Defaults to `true`.
## Usage
```elixir
# lib/my_app_web/router.ex
use MyAppWeb, :router
import PhxLiveStorybook.Router
...
scope "/" do
pipe_through :browser
live_storybook "/storybook", backend_module: MyAppWeb.Storybook
end
```
Note that it is not possible to use this macro in a scope with a path
different from `/`.
"""
defmacro live_storybook(path, opts) do
opts =
opts
|> Keyword.put(:application_router, __CALLER__.module)
|> Keyword.put_new(:as, :live_storybook)
|> Keyword.put_new(:pipeline, true)
session_name_opt = Keyword.get(opts, :session_name, :live_storybook)
session_name_iframe_opt = :"#{session_name_opt}_iframe"
quote bind_quoted: binding() do
scope path, alias: false, as: false do
import Phoenix.LiveView.Router, only: [live: 4, live_session: 3]
if Keyword.fetch!(opts, :pipeline) do
pipeline :storybook_browser do
plug(:accepts, ["html"])
plug(:fetch_session)
plug(:protect_from_forgery)
end
end
scope path: "/" do
pipe_through(:storybook_browser)
{session_name, session_opts, route_opts} =
PhxLiveStorybook.Router.__options__(opts, path, session_name_iframe_opt, :root_iframe)
live_session session_name, session_opts do
live(
"/iframe/*story",
PhxLiveStorybook.Story.ComponentIframeLive,
:story_iframe,
route_opts
)
end
{session_name, session_opts, route_opts} =
PhxLiveStorybook.Router.__options__(opts, path, session_name_opt, :root)
live_session session_name, session_opts do
live("/", PhxLiveStorybook.StoryLive, :root, route_opts)
live("/*story", PhxLiveStorybook.StoryLive, :story, route_opts)
end
end
end
end
end
@default_assets_path "/storybook/assets"
@doc false
def __options__(opts, path, session_name, root_layout) do
live_socket_path = Keyword.get(opts, :live_socket_path, "/live")
assets_path = Keyword.get(opts, :assets_path, @default_assets_path)
backend_module =
Keyword.get_lazy(opts, :backend_module, fn ->
raise "Missing mandatory :backend_module option."
end)
{
session_name,
[
root_layout: {PhxLiveStorybook.LayoutView, root_layout},
on_mount: PhxLiveStorybook.Mount,
session: %{
"backend_module" => backend_module,
"assets_path" => assets_path,
"root_path" => path
}
],
[
private: %{
live_socket_path: live_socket_path,
backend_module: backend_module,
application_router: Keyword.get(opts, :application_router),
assets_path: assets_path
},
as: Keyword.fetch!(opts, :as)
]
}
end
@gzip_assets Application.compile_env(:phx_live_storybook, :gzip_assets, false)
@doc """
Defines routes for PhxLiveStorybook static assets.
Static assets should not be CSRF protected. So they need to be mounted in your
router in a different pipeline than storybook's.
It can take the `path` the storybook assets will be mounted at.
Default path is `"/storybook/assets"`.
## Usage
```elixir
# lib/my_app_web/router.ex
use MyAppWeb, :router
import PhxLiveStorybook.Router
...
scope "/" do
storybook_assets()
end
```
"""
defmacro storybook_assets(path \\ @default_assets_path) do
gzip_assets? = @gzip_assets
quote bind_quoted: binding() do
scope "/", PhxLiveStorybook do
pipeline :storybook_assets do
plug(Plug.Static,
at: path,
from: :phx_live_storybook,
only: ~w(css js images fonts favicon),
gzip: gzip_assets?
)
end
pipe_through(:storybook_assets)
get("#{path}/*asset", AssetNotFoundController, :asset, as: :storybook_asset)
end
end
end
end