elt547

elt547

I’m trying to get my function components available in all of my views. My shared components are in there own files under /live/components/_shared. And I have one shared.ex file available under /live/components/shared.ex. I don’t know enough about macros in elixir yet.

This is what it looks like:

defmodule HydroplaneWeb.Components.Shared do
  alias HydroplaneWeb.Components.Shared

  defmacro __using__(_opts) do
    quote do
      import Shared.{Button, Card, Dropdown, Lists, Modal}
    end
  end
end

But when I use it from my view_helpers() it is giving me the error Shared.Button.module_info/1 is undefined (function not available).

  defp view_helpers do
    quote do
      # ..................

      use HydroplaneWeb.Components.Shared
    end
  end

I have tried a few different configurations but just know I’m missing something basic. How can I get this working so that all of my function components are available in my liveview templates? Also what key concept am I misunderstand so I can avoid the same mistake in the future?

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The alias HydroplaneWeb.Components.Shared you have at the top of the file is not in the same scope as your import Shared line. You can see this in the error message in that it is Shared.Button.module_info/1 not HydroplaneWeb.Components.Shared.module_info/1.

You should either make the import fully qualified eg import HydroplaneWeb.Components.Shared.{Button, Card, Dropdown, Lists, Modal} or add the alias inside the quote. I’d recommend just making it fully qualified so you don’t inject an alias into the other module.

elt547

elt547 OP

I tried getting rid of the alias and making the module fully qualified inside of __using__/1 but I’m getting the same error. I also tried putting the import HydroplaneWeb.Components.Shared.{Button, Card, Dropdown, Lists, Modal} directly inside of the quote block in view_helpers.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you show how you are calling these components?

krstfk

krstfk

My guess is that you might have a typo somewhere in your

defmodule HydroplaneWeb.Components.Shared.Button do
…
elt547

elt547 OP

There’s no typo, and it happens with more than just the Button module. If I change it to include the individual modules it happens with all of them.

elt547

elt547 OP

Of course, I’m trying to call them like this as function components:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Is the error you get a runtime or compile time error? Is there a stack trace or more information you can provide?

What is the exact error you get if you do

  defp view_helpers do
    quote do
      # ..................

      import HydroplaneWeb.Components.Shared.{Button, Card, Dropdown, Lists, Modal}
    end
  end
elt547

elt547 OP

When I try to start the server I get this:

Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit] [dtrace]

==> hydroplane_web
Compiling 27 files (.ex)

== Compilation error in file lib/hydroplane_web/live/components/_shared/button.ex ==
** (UndefinedFunctionError) function HydroplaneWeb.Components.Shared.Button.module_info/1 is undefined (function not available)
    HydroplaneWeb.Components.Shared.Button.module_info(:exports)
    (elixir 1.13.3) src/elixir_import.erl:98: :elixir_import.calculate/6
    (elixir 1.13.3) src/elixir_import.erl:28: :elixir_import.import/4
krstfk

krstfk

Can you share your button module?

elt547

elt547 OP

Sure, it’s pretty basic.

defmodule HydroplaneWeb.Components.Shared.Button do
  use HydroplaneWeb, :component

  def action_button(assigns) do
    extra = assigns_to_attributes(assigns, [:__slot__, :inner_block])

    ~H"""
    <button {extra} class="bg-slate-100 hover:bg-slate-200 py-1 mx-1 rounded text-slate-500 hover:text-slate-600">
      <%= render_slot(@inner_block) %>
    </button>
    """
  end
end

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews