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

Support context from custom element ancestors #10

Closed
patricknelson opened this issue Jun 10, 2023 · 3 comments
Closed

Support context from custom element ancestors #10

patricknelson opened this issue Jun 10, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@patricknelson
Copy link
Owner

Stub for an issue originally created by @Vanillabacke, see crisward/svelte-tag#8

Just wanna share a quick and dirty solution of getting the context kinda working.

Needs a new method in the wrapper class:

getAncestorContext( node = false )  {
    while ( node.parentNode ) {
        if ( node._root?.elem?.$$?.context  ) {
            return node._root?.elem?.$$?.context
        }
        node = node.parentNode
    }
    return false
}

And then adopt the context in the opts.component constructor:

connectedCallback(){
    let props = opts.defaults ? opts.defaults : {};

    // ...

    props.$$slots = createSlots(slots)
    this.elem = new opts.component({
        target: this._root,
        context: this.getAncestorContext(this?._root), // add the context to the component
        props
    });
}

Usage for example with a writable: Parent:

<script>
import { setContext} from 'svelte';
import {writable} from 'svelte/store';

const yourName= writable('default name');
setContext('yourName',yourName);
</script>

<div>{$yourName}<div>

Child:

<script>
import { getContext } from 'svelte';
let yourName= getContext('yourName')

updateName(){
    $yourName = 'your new name';
}
</script>

<div>{$yourName}<div>
<button on:click={updateName}>Update the Name</button>
@patricknelson patricknelson added the enhancement New feature or request label Jun 11, 2023
@patricknelson
Copy link
Owner Author

Great example of a use case that I’d like to reproduce purely in web component form, where each Svelte component (Tabs, TabList, TabPanel and Tab) have a corresponding custom element in svelte-retag: https://svelte.dev/repl/f2981102a577452a9a6c656680ea8671?version=4.2.0

(thanks to Amir at SO for providing this REPL)

@patricknelson patricknelson self-assigned this Sep 21, 2023
patricknelson added a commit that referenced this issue Sep 28, 2023
…instantiating them outside of their context (before it's parent has initialized it or not inside of parent).
@patricknelson
Copy link
Owner Author

Note: Working on this may resolve #12 due to the complexities involving context and rendering the tree from the top down. This is because it may make sense to wait and instead queue component rendering until the next animation frame when we we have enough custom elements initialized (or constructed) to justify traversing the DOM to render in order (to support said context).

patricknelson added a commit that referenced this issue Sep 30, 2023
…ment tag definitions) to facilitate testing of context.
patricknelson added a commit that referenced this issue Sep 30, 2023
…hadow DOM rendering, ensuring components are rendered/initialized from the top down (at least in light DOM, WIP for shadow).
patricknelson added a commit that referenced this issue Sep 30, 2023
…re querySelectorAll() for compatibility across both light DOM and shadow DOM (since that always returns document order, per spec). Cleaning up some console logs and debug code.
patricknelson added a commit that referenced this issue Sep 30, 2023
…te-retag-render', as it's only necessary for render. Cleans final rendered HTML (leaves no extra attribs behind).
patricknelson added a commit that referenced this issue Sep 30, 2023
…up unnecessary mutex/semaphor for requestAnimationFrame. Refactoring queueForRender() for simplicity and reducing redundancy there, too.
patricknelson added a commit that referenced this issue Sep 30, 2023
…ll need to optimize heavily (without breaking context).
patricknelson added a commit that referenced this issue Oct 3, 2023
patricknelson added a commit that referenced this issue Oct 3, 2023
patricknelson added a commit that referenced this issue Oct 3, 2023
…ng of element attributes to component props without having to instantiate it any extra times (which not only can break context but also is likely unexpected in many situations). So, 1.) We can pass case-sensitive props at FIRST instantiation and then 2.) Retain them for mapping back if/when attributeChangedCallback() is called later.
patricknelson added a commit that referenced this issue Oct 3, 2023
…to ensure immediate results. Also ensuring we wrap with <svelte-retag> for now in Shadow DOM tests instead of <div>; it was replaced since it potentially adds extra formatting that we may not want.
patricknelson added a commit that referenced this issue Oct 3, 2023
…mock that ensures each queued callback runs to completion AND runs in sequential order (i.e. preventing nesting). Found quirk after first unit test made for validating context that couldn't be resolved (parent didn't complete initialization before child attempted to render).
@patricknelson
Copy link
Owner Author

Implemented in 1.3.0 via #18! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant