-
Notifications
You must be signed in to change notification settings - Fork 0
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
React components #143
React components #143
Conversation
Iframe URL is now the same for block and for popup, so we now configure it once.
It's now under frontend/.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried running it locally, works nicely. Thanks for doing this :)
Is this file still relevant? Perhaps require an update with this PR? I found it wanting to add custom css for Chatrix. |
Good point @ashfame, I think this file can be removed as the styles are defined on the |
I apologise for the large PR. It was an iterative process, so the history isn't super clean. If you want to review the code, it's probably best to do so commit-by-commit, looking at the diff as whole is probably overwhelming.
There are only minimal functional changes (all pluses IMHO), and I've tested a bunch. Will do more extensive testing before issuing the next release. The majority of the changes is "just" moving things around, refactoring in order to remove duplication and for clarity.
Context
Currently, the project is structured around two "targets" (block and popup) with their own separate build. The result of the build is two completely separate targets:
build/frontend/block
build/frontend/popup
In addition, for each target, two separate bundles are produced: the
iframe
and theparent
. Theiframe
bundle contains assets loaded inside the iframe, and theparent
contains assets required for bootstrapping the iframe.This is achieved by having two separate
vite
"projects" (one for block, one for popup), each producing two bundles, one for iframe, one for parent. The result of this is that there are four different bundles (block-iframe
,block-parent
,popup-iframe
,popup-parent
) that the WordPress side of things needs to deal with.Additionally, in WordPress land, the
block
has two separate usages: one in the editor completely in JavaScript, and another in the front when the block gets rendered, in PHP.The problem
Currently the
iframe
is considered to be the center of the project, with theparent
being somewhat of an after-thought. This is visible in the fact that the mainvite.config.ts
is the one of the iframe, with the parent having avite-parent.config.ts
.However, I think that the fact that we use an iframe to render Chatrix onto a page is a detail of the current implementation. We could imagine a future where that would no longer be the case. Having the project structured around an implementation detail is IMHO the root of the problem, and results in several issues:
iframe
, 2x WordPressparent
, 1x Block editor.yarn
commands, e.g.yarn start:popup
.iframe
and what's in theparent
.The solution
The solution implemented in this PR is to effectively make the "root" of the project the "
parent
" (but just call itapp
), with thevite
config invite.config.ts
. Theiframe
then becomes the "secondary" target, with its ownvite-iframe.config.ts
.This means that you can now just do
yarn start
and have both the block and the popup onscreen simultaneously. No longer need to runyarn start:block
, thenctrl+c
andyarn start:popup
.In addition, in this PR, we introduce two React components (
<Block>
and<Popup>
) that allow you to render Chatrix onto the page, and abstract the fact that an iframe is used internally. Two functions (renderBlock()
andrenderPopup()
) were also added to make it easy to render the components from wherever, without needing to deal with React.This strategy reduces bundle size in half, since there is no longer duplication of iframe assets. There's also now a single service worker for both block and popup.
Summary of changes
(Not in commit order, not extensive, just a summary.)
<Chat>
component. Abstracts rendering theiframe
.<Block>
component. Uses<Chat>
internally.<Popup>
component. Uses<Chat>
internally.renderBlock()
andrenderPopup()
functions to abstract rendering the<Block>
and<Popup>
components, respectively. These are the only functions the WordPress side of things needs to know. There's an exception to this rule for the editor, there the<Block>
component is used directly by theEdit
component.vite
"project" for both the block and the popup targets.frontend/
, and all iframe code is infrontend/iframe
.Screen captures
Disk usage
Block and popup on same page in development
Directory structure