Skip to content
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

Macro to generate component functions #50

Merged
merged 9 commits into from Jun 3, 2023

Conversation

C-Sinclair
Copy link
Contributor

Changes

This PR creates an experimental macro which checks the assets/svelte folder for any Svelte components, and injects local function defs for those components into the calling module.

My thinking is that this would provide a cleaner more ergonomic interface into Svelte from Liveview, with a better DX. In the future attr macros could even be generated based on Svelte component exports.

Usage

def MyModule do
  use LiveSvelte.Components

  def render(assigns) do
    ~H"""
    <.MySvelteComponent foo="123" bar={456} />
    """
  end
end

Examples

See the live_chat file at example_project/lib/example_web/live/live_chat.ex in the example project for a working example!

Caveats

The assets/svelte directory is parsed once per module which calls use LiveSvelte.Components. A future improvement would be to only check here once per compile, and then reference the previously generated functions.

@woutdp
Copy link
Owner

woutdp commented May 28, 2023

This is very cool, thank you for the PR, let me check it out and see how it works, but will likely merge it. Feel free to add some documentation to the README. Maybe under the usage section.

@C-Sinclair
Copy link
Contributor Author

This is very cool, thank you for the PR, let me check it out and see how it works, but will likely merge it. Feel free to add some documentation to the README. Maybe under the usage section.

Amazing!

One thing I just realised, nested components of the same name will overwrite each other so that will need to be more carefully generated. I'll take a look anyway, and also add to the README 👍

@woutdp
Copy link
Owner

woutdp commented May 28, 2023

One thing I noticed is that it's not fully working, not sure why though. For example the animations in the chat are not visible anymore and also the amount field gets reset on click in the hybrid view.

macro.mp4

lib/components.ex Outdated Show resolved Hide resolved
@woutdp
Copy link
Owner

woutdp commented May 28, 2023

#17 will be closed by this PR

@C-Sinclair C-Sinclair requested a review from woutdp June 3, 2023 20:29
Copy link
Owner

@woutdp woutdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you for working on this, seems to be working!

@woutdp woutdp merged commit 97edfc8 into woutdp:master Jun 3, 2023
@woutdp
Copy link
Owner

woutdp commented Jun 3, 2023

Available in 0.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants