sveltejs / svelte

Cybernetically enhanced web apps

Home Page:https://svelte.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Context API doesn't work for custom elements

romangutkin opened this issue · comments

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

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

Closed via #8457, to be released in Svelte 4

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/.