question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add this.$ to LitElement as it's still very light-weight but usefule

See original GitHub issue

Polymer has this.$ as a convenient way to access all shadow root elements with an id defined, by id. This can be easily added to LitElement as:

constructor() {
	super(...arguments);
	this.__renderComplete = null;
	this.__resolveRenderComplete = null;
	this.__isInvalid = false;
	this.__isChanging = false;

	// Add support for this.$
	this.$ = {};
}

ready() {
	this._root = this._createRoot();
	super.ready();

	// Add support for this.$
	for( const el of this._root.querySelectorAll( "*[id]" ) )
		this.$[el.id] = el;

	this._firstRendered();
}

and then accessed the same way as in Polymer: this.$.name.value for example, if we have an <input id="name"/>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Lodincommented, Jun 9, 2018

I would be against this feature request. Since lit-element uses lit-html to handle elements declaratively, it’s almost unnecessary to adjust elements manually, and if you still need it, you can do it by built-in tools like querySelector or getElementById called from this.shadowRoot.

1reaction
bennypowerscommented, Jul 4, 2018

For posterity, and anyone landing here from google (greetings!), I present a simple mixin:

const isFunction = fn => typeof fn === 'function';

export default superclass => class $Element extends superclass {
  $(selector) {
    return (this.shadowRoot && isFunction(this.shadowRoot.querySelector))
      ? this.shadowRoot.querySelector(selector)
      : undefined;
  }
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add this.$ to LitElement as it's still very light-weight but usefule
Polymer has this.$ as a convenient way to access all shadow root elements with an id defined, by id. This can be easily...
Read more >
Lightweight And Fast Web Components With LitElement
LitElement is a pure base class for creating fast, lightweight Web Components that work in any web page with or without a framework...
Read more >
LitElement
Lit templates, based on tagged template literals, are simple, expressive and fast, featuring HTML markup with native JavaScript expressions inline. No custom ...
Read more >
Getting started with LitElement and TypeScript - This Dot Labs
LitElement is an excellent alternative to build lightweight web applications since it's based on the Web Components standard, with the addition ...
Read more >
From Web Component to Lit Element - Google Codelabs
Lit is a simple library for building fast, lightweight web components that work in any framework, or with no framework at all. With...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found