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

Phoenix LiveView & React integration #2988

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

zeorin
Copy link
Collaborator

@zeorin zeorin commented Mar 5, 2025

Description

This PR adds more idiomatic integration between Phoenix LiveView and React.

Phoenix LiveView and React Components can be interleaved arbitrarily, and they will still participate in their respective lifecycles. React components that are indirect descendants of another React component will be mounted in the DOM according to Phoenix LiveView's hierarchy, but will form part of their closest React ancestor's React component tree, thanks to portals.

phx-* events Just Work™ because Phoenix LiveView uses event delegation to listen to them.

There's a jsx macro that you can use to create a Phoenix functional component. It will create a functional component with the name of the file, so if you use e.g. jsx("components/Foo.tsx"), you can then use <.Foo> in an Heex template. You can also annotate the macro call with attr and slot like a regular component. The :inner_block slot is automatically translated to the children prop.

React components included in this way will be transpiled separately by ESBuild, each will be their own entrypoint, but code that they share dependencies on will be code-split into separate chunks.

This lays the some of the groundwork for #2850.

I have also reconfigured up TypeScript to be much stricter, and I added types for Phoenix LiveView's JS. I added an ESLint configuration that will help avoid common issues in React/JS/TS code. This config is also rather strict. The idea is that you can get rid of the rules you don't like.

Still to be done:

Validation steps

Check out the branch, update deps, and start dev server. Navigate to http://localhost:4000/dev/react to see some demo components

Additional notes for the reviewer

There are still a bunch of TODO comments in the code that need review.

This work has taken inspiration from:

AI Usage

Please disclose how you've used AI in this work (it's cool, we just want to know!):

  • Code generation (copilot but not intellisense)
  • Learning or fact checking
  • Strategy / design
  • Optimisation / refactoring
  • Translation / spellchecking / doc gen
  • Other
  • I have not used AI

You can read more details in our Responsible AI Policy

Pre-submission checklist

  • I have performed a self-review of my code.
  • I have implemented and tested all related authorization policies. (e.g., :owner, :admin, :editor, :viewer)
  • I have updated the changelog.
  • I have ticked a box in "AI usage" in this PR

@zeorin zeorin requested a review from stuartc March 5, 2025 21:24
@zeorin zeorin changed the title 2850 migrate workflow edit to react Phoenix LiveView & React integration Mar 5, 2025
zeorin added 2 commits March 5, 2025 23:28
Associates a JSX file with a component.  Such files will become
entrypoints for ESBuild, but common code is code-split by ESBuild into
separate chunks.
@zeorin zeorin force-pushed the 2850-migrate-workflow-edit-to-react branch from 63e4327 to add6549 Compare March 5, 2025 21:28
@zeorin zeorin force-pushed the 2850-migrate-workflow-edit-to-react branch from add6549 to aeab027 Compare March 5, 2025 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New Issues
Development

Successfully merging this pull request may close these issues.

2 participants