[lit-helpers] ref directive
See original GitHub issuewhen all you have is a template and not a full web component it can become a hassle if you need to reference one node to another
Example:
return html`
<my-dialog>some content</my-dialog>
<button @click=${() => dialog???.open()}>open dialog</button>
`;
a ref directive could allow getting a reference to the node
import { setRef, getRef } from '@open-wc/lit-helpers';
return html`
<my-dialog ~=${setRef('dialog')}>some content</my-dialog>
<button @click=${() => getRef('dialog').open()}>open dialog</button>
`;
PS: setRef
would be a directive whereas getRef
is just a plain function
Why ~=
?
A directive needs a hook on an attribute or property in order to work. It could be anything… as a best practice proposal it could be ~=
… feel free to propose alternatives
Why setRef
and not setReference
Good question - both are fine… one is short the other is more explicit. Let us know what you would prefer.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:24 (23 by maintainers)
Top Results From Across the Web
Built-in directives - Lit.dev
ref. Gets a reference to an element rendered in the template. Rendering special values. templateContent. Renders the content of a <template> element.
Read more >Doing a FLIP with lit-html@2.0 - Open Web Components
The Open Web Components team and I vend a series of lit-helpers , one of which is a spread directive for lit-html@1.0 that...
Read more >How to spread SVG attributes in lit-element? - Stack Overflow
Open-wc have a spread directive that might be useful: open-wc.org/docs/development/lit-helpers/#spread-directives.
Read more >ng-lit - npm
... ng-lit helpers make it easier for your new lit-element components to ... lit-element with same interface as the old angularjs directive.
Read more >Student Handbook 2013-2014 - my.lakeforest!
Library's Reference Desk or the Business Office in North Hall. ... Failure to follow the staff member's directive is a serious.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This comment may be a bit premature, but im not really sure how I feel about the
~
prefix, for a few reasons: We’re gonna run out of weird symbols to use at some point, and 2: it gives me flashbacks to angularjs where nothing makes senseI think the prefixes lit-html uses make a lot of sense,
@
for events reads as ‘at event’,.
for properties refers to setting props in js, etc. But~
in relation toref
just doesn’t mean anything to me, and I really dont look forward to having my templates filled with a bunch of symbols~%*&
in the future 🙈There is now an official
ref
directive with the latest version of lit.https://lit.dev/docs/api/directives/#ref