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

context from the parent web-component is not shared to its children | solid-element #2364

Open
Waishnav opened this issue Nov 15, 2024 · 2 comments
Labels
webcomponents related to solid-element, custom elements, shadow dom, or other web component related features

Comments

@Waishnav
Copy link

Describe the bug

For Solid Hack 2024, I experimented with the solid-element API to convert Solid components into web components. I attempted to create a ShadCN-based web component library, aiming to make it particularly useful for the HTMX community. I saw this as a strong use case for Solid.js.

I tried porting an Accordion component to a web component, so that end-users could use it like this:

<accordion class="w-full" collapsible>
  <accordion-item value="item-1">
    <accordion-trigger>Is it accessible?</accordion-trigger>
    <accordion-content>Yes. It adheres to the WAI-ARIA design pattern.</accordion-content>
  </accordion-item>
  <accordion-item value="item-2">
    <accordion-trigger>Is it styled?</accordion-trigger>
    <accordion-content>Yes. It comes with default styles that match the other components' aesthetic.</accordion-content>
  </accordion-item>
  <accordion-item value="item-3">
    <accordion-trigger>Is it animated?</accordion-trigger>
    <accordion-content>Yes. It's animated by default, but you can disable it if you prefer.</accordion-content>
  </accordion-item>
</accordion>

However, since each sub-component like AccordionItem and AccordionContent was converted to accordion-item and accordion-content, I encountered the following errors:

Uncaught Error: [kobalte]: `useAccordionContext` must be used within an `Accordion.Root` component
Uncaught Error: [kobalte]: `useCollapsibleContext` must be used within a `Collapsible.Root` component
Uncaught Error: [kobalte]: `useAccordionItemContext` must be used within an `Accordion.Item` component

These errors are caused because kobalte expects certain contexts to be nested within specific parent components, but the web component conversion disrupts this hierarchy.

Your Example Website or App

https://studio.webcomponents.dev/edit/VcOP3wIWw9SP1xt0fMwf/src/components.jsx?file=https://srv.divriots.com/packd/@kobalte/core/accordion,@kobalte/core/combobox,@kobalte/core/select,@kobalte/core/text-field,clsx,lit-html,lucide-solid,solid-element,solid-js,solid-js/web,tailwind-merge

Steps to Reproduce the Bug or Issue

  1. write a composable ui component using solid-element and solidjs's context api and store api.

  2. it will throw error like this

Expected behavior

context should be shared b/w children in web components too

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: Chrome
  • Version: solid: 1.9.3, solid-elements: 1.9.1

Additional context

No response

@Waishnav Waishnav changed the title web component built using solid-element couldn't able to access the context from the parent web-component context from the parent web-component is not shared to its children | solid-element Nov 27, 2024
@ryansolid
Copy link
Member

I'm gathering these are consumed top level outside of Solid's JSX. It's tricky because of upgrade timing we can't always assume the context isn't present. Solid's JSX sort of strong arms it but I don't think it completely works properly in this case. If you have an example somewhere we can see where it falls down and maybe there is a way we can patch in a certain way. It's just tricky wrangling web component lifecycles which we don't control the timing of.

@ryansolid ryansolid added the webcomponents related to solid-element, custom elements, shadow dom, or other web component related features label Dec 3, 2024
@Waishnav
Copy link
Author

If you have an example somewhere we can see where it falls down and maybe there is a way we can patch in a certain way. It's just tricky wrangling web component lifecycles which we don't control the timing of.

Thank you for your insights. Yes, the components are being consumed at the top level outside of Solid's JSX. The timing of upgrades and ensuring context presence is indeed challenging. I have created an example demonstrating the issue here. It shows the errors related to context not being shared correctly between parent and child web components.

I was thinking we could use a Context API in the global context, making it accessible to any component via a unique identifier attached to the respective parent DOM element where the context is created. I'm not entirely sure if this is the best approach, though. I’d love to hear your thoughts or any suggestions you might have for improving or patching this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
webcomponents related to solid-element, custom elements, shadow dom, or other web component related features
Projects
None yet
Development

No branches or pull requests

2 participants