-
Notifications
You must be signed in to change notification settings - Fork 149
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
Surface.component
macro
#109
Comments
Yeah, for the default slot it would be as natural as using |
This feature will also allow users to use the existing Currently, users need to either define a parent LiveView in their tests as in: https://github.com/msaraiva/surface/blob/011ff48401f6b3659f03fbd6a92e7028b0173547/test/live_component_test.exs#L168 or use our render_live/2 macro (which creates one on the fly) as in: https://github.com/msaraiva/surface/blob/011ff48401f6b3659f03fbd6a92e7028b0173547/test/live_component_test.exs#L123 The latter requires users to copy & paste the helper macro from our With |
Using this approach, I'm creating and component library for a design system using Surface to provide a more easy way to develop web application with Phoenix LiveView. So, I'm trying to make a way to test component renderization using I tried to use the same tests that you have on Surface but It's not what I want. For now, I'm trying this way: test "returns Button component without button properties" do
html = """
<button class="a-btn">Sample</button>
"""
assert render_component(Button, %{children: "Sample"}) =~ html
end I don't know if it's the better way to check if it's rendering the |
Hey @aleDsz! If i understood correctly, you're using data children, :any you should have: slot default and the
Also, make sure you define the Now, as explained in the previous comment, def init_surface(inner_block, assigns \\ []) do
wrapped_block = fn _, _ -> inner_block end
surface_assigns = %{provided_templates: [:__default__]}
assigns
|> Map.new()
|> Map.put(:inner_block, wrapped_block)
|> Map.put(:__surface__, surface_assigns)
end And use it as: test "returns Button component without button properties" do
html = """
<button class="a-btn">Sample</button>
"""
assigns = %{}
code = ~H"Sample"
assert render_component(Button, init_surface(code)) =~ html
end If you need, you can pass any additional prop using:
Pay attention that this helper will work only for |
Hi @msaraiva, thanks for your response! Interesting, I'm also providing a test case for Components which i can simply provide a function that make easy to test those components. I'll try today and any problem I'll post here. Also, If I'm trying to generate class automatically, so I'll need to use |
@aleDsz if the module is already closed (compiled), you can use |
Oh, interesting. I was using |
Proposal for passing slotables when using vanilla LV (from the discussion on slack): <%= surface_component @socket, MyComponent, prop1: @value do %>
<% template(:header) -> %>
<div class="header">
...
</div>
<!-- We could have `:default` as default value so you could use just `template()` -->
<% template(:default) -> %>
<div class="header">
...
</div>
<% template(:footer) -> %>
<div class="footer">
...
</div>
<% end %> |
Are there plans to enable writing and using components using the jsx-like syntax in separate files instead of embedding them in For me this has the same advantage as having separate (l)eex files for templates - my editor has better support for them. |
Hi @ohmree, Unless I'm misunderstanding what you're asking for, this is already supported - you can move the code to a file with the same base name using the
It works the same for Surface.LiveComponent and Surface.LiveView |
@lnr0626 This is exactly what I meant, thanks! Just out of interest, is this documented anywhere? |
Hi @ohmree! There’s a small "Colocated templates” section in http://surface-demo.msaraiva.io/components_basics. |
I'm closing this one as it's not a priority and the requirements may change very soon. We can reevaluate it after Phoniex 1.6 is released. |
@msaraiva This issue leaves me somewhat confused as to how I would go about starting to use SurfaceUI in an existing Phoenix LiveVew App. Would the only way be to convert all my existing |
Hi @lastobelus!
Not all. But until phoenix has built-in support for features like slots and contexts, you need to convert at least those that include components that use any of those features. Other simpler components might work without any issue. We'll update the docs to make that clear. See surface-ui/surface_site#25. |
With #15 wrapped up, I think we have most of the things we need to support a
Surface.component
macro that could be used inside LEEx. The generated code should look somewhat similar to https://github.com/msaraiva/surface/blob/master/lib/surface/compiler/eex_engine.ex#L212I think it would be worthwhile to think through the usage a bit though. The simplest approach would be to have something like:
which would be equivalent to:
However, something that could be used more like the following would probably be more natural within an LEEx template:
The text was updated successfully, but these errors were encountered: