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 API doesn't work for custom elements #3422

Closed
romangutkin opened this issue Aug 18, 2019 · 4 comments · Fixed by #8457
Closed

Context API doesn't work for custom elements #3422

romangutkin opened this issue Aug 18, 2019 · 4 comments · Fixed by #8457

Comments

@romangutkin
Copy link

When compiling to custom elements, getContext returns undefined

// Select.svelte
import { setContext } from 'svelte'

setContext('select', {
  selected: 'one'
})

// SelectOption.svelte
import { getContext } from 'svelte'

const { selected } = getContext('select') // undefined
@godsnuts
Copy link

godsnuts commented Sep 1, 2019

it maybe work now.
https://svelte.dev/repl/56b1169a2e2541538225680abd34d2b4?version=3.9.1

@antony
Copy link
Member

antony commented Apr 9, 2020

A sample repl (I think it needs to be downloaded in order to see the custom element result) https://svelte.dev/repl/138e4c0eb0f340d19e7952064bb9fd19?version=3.20.1

@baseballyama baseballyama added this to the 4.x milestone Feb 26, 2023
dummdidumm added a commit that referenced this issue May 2, 2023
This is an overhaul of custom elements in Svelte. Instead of compiling to a custom element class, the Svelte component class is mostly preserved as-is. Instead a wrapper is introduced which wraps a Svelte component constructor and returns a HTML element constructor. This has a couple of advantages:

- component can be used both as a custom element as well as a regular component. This allows creating one wrapper custom element and using regular Svelte components inside. Fixes #3594, fixes #3128, fixes #4274, fixes #5486, fixes #3422, fixes #2969, helps with sveltejs/kit#4502
- all components are compiled with injected styles (inlined through Javascript), fixes #4274
- the wrapper instantiates the component in `connectedCallback` and disconnects it in `disconnectedCallback` (but only after one tick, because this could be a element move). Mount/destroy works as expected inside, fixes #5989, fixes #8191
- the wrapper forwards `addEventListener` calls to `component.$on`, which allows to listen to custom events, fixes #3119, closes #4142 
- some things are hard to auto-configure, like attribute hyphen preferences or whether or not setting a property should reflect back to the attribute. This is why `<svelte:options customElement={..}>` can also take an object to modify such aspects. This option allows to specify whether setting a prop should be reflected back to the attribute (default `false`), what to use when converting the property to the attribute value and vice versa (through `type`, default `String`, or when `export let prop = false` then `Boolean`), and what the corresponding attribute for the property is (`attribute`, default lowercased prop name). These options are heavily inspired by lit: https://lit.dev/docs/components/properties. Closes #7638, fixes #5705
- adds a `shadowdom` option to control whether or not encapsulate the custom element. Closes #4330, closes #1748 

Breaking changes:
- Wrapped Svelte component now stays as a regular Svelte component (invokeing it like before with `new Component({ target: ..})` won't create a custom element). Its custom element constructor is now a static property named `element` on the class (`Component.element`) and should be regularly invoked through setting it in the html.
- The timing of mount/destroy/update is different. Mount/destroy/updating a prop all happen after a tick, so `shadowRoot.innerHTML` won't immediately reflect the change (Lit does this too). If you rely on it, you need to await a promise
@dummdidumm
Copy link
Member

Closed via #8457, to be released in Svelte 4

@patricknelson
Copy link

If anyone is having trouble still getting context to work with custom elements, give svelte-retag a gander. 👀

Here's a REPL demonstrating a simple version of context with custom elements: https://svelte.dev/repl/b404a1addaf348eabcff3f6089707297?version=4.2.1

You can see a slightly more advanced demo (tabs with nesting) here: https://svelte-retag.vercel.app/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants