Code Monkey home page Code Monkey logo

Comments (4)

msaraiva avatar msaraiva commented on July 30, 2024 6

Hi @andreasknoepfle!

Is there some way of rendering a component dynamically?

Not yet, but this is definitely on the roadmap and there's no way we'll ship the first version without it.

The plan is to have a built-in <Dynamic> component that can handle those cases. Something that you could use like this:

def render(assigns) do
  props = %{label: "Ok", class: "button"}
  component = Button

  ~H""" 
  <Dynamic component={{ component }} props={{ props }}/>
  """
end

A first naive implementation that would probably work for the most simple cases (like stateless components without nested children) would look like:

  defmodule Dynamic do
    use Surface.Component

    property component, :module, required: true
    property props, :map, default: %{}

    def render(assigns) do
      props =
        assigns
        |> Map.get(:props)
        |> Map.merge(%{__surface__: %{groups: %{__default__: %{binding: false, size: 0}}}}) # Don't worry about this for now :)

      ~H"""
      {{ live_component(@socket, @component, props) }}
      """
    end
  end

I'm currently working on the new event API due to some breaking changes in LiveView so, unfortunately, I wasn't able to validate the above solution but feel free to give it a try if you want and let me know if it works :)

Cheers.

from surface.

msaraiva avatar msaraiva commented on July 30, 2024 2

Implemented in #473.

from surface.

andreasknoepfle avatar andreasknoepfle commented on July 30, 2024 1

Hi @msaraiva 👋

Your proposed solution works like a charm 💚.
Thank you very much! I am already excited and looking forward for a first release of surface 💯

Cheers

from surface.

elliottneilclark avatar elliottneilclark commented on July 30, 2024

Did anything come of this? The above Dynamic solution doesn't seem to work with default props.

from surface.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.