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

this refer to component/custom element instance #4523

Closed
wants to merge 5 commits into from

Conversation

hontas
Copy link
Contributor

@hontas hontas commented Mar 7, 2020

resolves #3593

Call instance-function with component as this to be able to reference root/custom element.

Now it's possible to reference it like so:

<svelte:options tag="custom-element" />
<div bind:this={topEl} />

<script>
import { onMount } from 'svelte';

let custemElementInstance;
let topEl;

onMount(() => {
  custemElementInstance =  topEl.parentNode.host.parentElement
});
</script>

With this change we could simply use this keyword.

<svelte:options tag="custom-element" />

<script>
import { onMount } from 'svelte';

onMount(() => {
  this.shadowRoot.querySelector('xxx');
});
</script>

* 'master' of https://github.com/sveltejs/svelte: (137 commits)
  -> v3.19.2
  fix lazy code breaks in build
  fit bitmask overflow initial dirty value in 'if' blocks (sveltejs#4507)
  add dev runtime warning for unknown slot names (sveltejs#4501)
  fix render fallback slot content due to whitespace (sveltejs#4500)
  docs: describe falsy and nullish attribute behavior (sveltejs#4498)
  in spread, distinguish never-updating and always-updating props (sveltejs#4487)
  chore: more specific typings, and add README note about Yarn (sveltejs#4483)
  update changelog
  check for unknown props even if component doesn't have writable props (sveltejs#4454)
  Bump codecov from 3.5.0 to 3.6.5 (sveltejs#4433)
  fix bitmask overflow for slot (sveltejs#4485)
  mark module variables as mutated or reassigned (sveltejs#4469)
  docs: referenced_from_script var value (sveltejs#4486)
  docs: clarify default prop behaviour (sveltejs#4460)
  site: turn fancybutton into custombutton (sveltejs#4476)
  update changelog
  exclude global variables from $capture_state (sveltejs#4475)
  -> v3.19.1
  don't treat $$-names as stores during invalidation (sveltejs#4453)
  ...
@hontas hontas changed the title Feature/instance this this refer to component/custom element instance Mar 7, 2020
@Conduitry
Copy link
Member

It looks like this would given all components access to their own instance, which isn't something we want to do (because, among other things, it would mean that components could now programmatically destroy themselves, which would wreak havoc).

I'm not so sure that #3539 is something we want with custom elements either. If the real goal is to solve something like #3119, that's where the focus should be.

@Conduitry Conduitry closed this Mar 8, 2020
@hontas
Copy link
Contributor Author

hontas commented Mar 8, 2020

@Conduitry I'm happy to update the PR so that this only targets custom elements if that makes things better?

@hontas
Copy link
Contributor Author

hontas commented Mar 8, 2020

Updated my branch to only do this for custom elements

@tanhauhau
Copy link
Member

instead of this, a better alternative is to get $$host which exist only for custom elements

#3091

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

Successfully merging this pull request may close these issues.

Get a reference to the "root" element when creating a "custom element"
3 participants