useContext() from custom element
I'm trying to access a context from a web component.
It's a single global context holding a singleton class as the value which provides access to the logged in user. It works as expected, but I can't find a way to make it accessible from web components.
I use web components to pass template strings as html from a JSON API; the aim here with
wk-user
is to be able to achieve syntax such as Hello, <wk-user property="name"></wk-user>!
rather than creating a whole templating system.
I tried using solid-element
's customElement
:
and component-register
's compose
+ register
+ solid-element
's withSolid
where both cases where undefined
I've also tried messing around with getOwner()
, but getOwner()
's context
prop appears to always be null within web components
Any help or information appreciated as I don't know where to even continue investigating10 Replies
Additional context
With the implementation of the
User
class trimmed, this is the context definition
Also, worth mentionning the User context depends on another context (defined the same way):
with this at the top level of the app's tree
mm, it interestingly enough works if the context-provider is also a webcomponent https://playground.solidjs.com/anonymous/55d0ff7b-114d-45d2-a793-d9791f500bef
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
I would issue it as a bug
Ok I could setup a minimal example of the issue in both cases
Will open an issue but include it here for ref too
nice repro
interesting that
does work.
lmk if u got an answer back! i think it's a rly cool idea for templating
Again, for ref: https://github.com/solidjs/solid/issues/1976
GitHub
Contexts not accessible from web components created by HTML strings...
Describe the bug If required, more context is available in Solid's Discord: https://discord.com/channels/722131463138705510/1182006344128151693/1182006344128151693 General flow I have an API th...
So the answer is somewhat disappointing but Ryan provided a workaround that did the trick
You may find what I came around with here: https://github.com/solidjs/solid/issues/1976#issuecomment-1846801576
GitHub
Contexts not accessible from web components created by HTML strings...
Describe the bug If required, more context is available in Solid's Discord: https://discord.com/channels/722131463138705510/1182006344128151693/1182006344128151693 General flow I have an API th...
Thanks for the update!
For the final solution I came with something that works quite well
I made that wrapper around the compose + register methods (which passes down all logic to the default function, so it has the same features + automatically injects
withSolid
for convenience + pushes tags to registeredComponents
)
Which is then used this way in my <SafeHtml>
comp
I also took the opportunity to slightly improve the types inference and provide the same options as component-register
's compose
method with the simplicity of use of the register
method
nice!