feat: allow to use reactive markup generators inside web-components #44
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #43
Related with #152
DOCS:
Reusing Elements without Shadow DOM
In Brisa, incorporating components using the syntax
<Component />
within a web component is not feasible. This invocation method is exclusive to server components. Brisa follows this approach to maintain a clear separation of concerns, where client components solely contain client code, and server components house server code, along with the markup of theweb-components
and other elements like adiv
.<Component />
- Smart/dummy server component.<web-component />
- Smart web component element. Dummy versions can be created, but it's advisable to leverage server components to avoid client-side JavaScript or use markup generators inside.{markupGenerator()}
- Generates dummy markup within web-server components.Efforts have been invested in facilitating communication between these two realms, ensuring a seamless integration without confusion from intermingling code.
Concepts:
This distinctive approach distinguishes Brisa from other frameworks in reusing web component code without necessitating the creation of shadow DOM.
In frameworks like React, to reuse markup (HTML elements), dummy components must be created—components without state. However, in Brisa, it is recommended to employ markup generators (functions) and invoke them during rendering.
Subsequently, these markup generators can be directly invoked within JSX:
Markup generators execute only when the web component is mounted, and the content returned by the generator becomes reactive. However, an exception exists when the argument holds the value of a signal rather than the full signal.
Do this:
Avoid this:
Unlike web components, markup generators lack their own state.
Choosing between a web component and a markup generator depends on the need:
Note
Performance concerns are alleviated in Brisa, as there are no rerenders, and markup generators execute only once.
Tip
You have the flexibility to establish directories or files with the
@-
prefix, steering clear of the need to create web components within thesrc/web-components
directory. This proves beneficial when incorporating reusable markup generators within web components. The sole exception to this practice is@-native
, a dedicated directory specifically designed for crafting native web components, free from both Brisa and JSX influences.Caution
Avoid sending the
WebContext
or parts of them to the markup generators, you can send signals (props, state, derived,context
and store signals) and static values without issues.