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.

[lit-helpers] ref directive

See original GitHub issue

when 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:24 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
thepasslecommented, Dec 18, 2019

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 sense

image

I 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 to ref 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 🙈

0reactions
daKmoRcommented, Apr 25, 2021

There is now an official ref directive with the latest version of lit.

https://lit.dev/docs/api/directives/#ref

Read more comments on GitHub >

github_iconTop 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 >

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