Incorrect/misleading example use-case for `ifDefined` directive
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 142
- Forks
- 230
- PR merge metrics
- No merged PRs in 30d
Description
Hi! In the [docs for the `ifDefined` directive](https://lit.dev/docs/templates/directives/#ifdefined), we read the following:
> This is especially useful for setting URL attributes, when the attribute should not be set if required parts of the URL are not defined, to prevent 404's.
and there's the following code example:
```js
@customElement('my-element')
class MyElement extends LitElement {
@property()
filename: string | undefined = undefined;
@property()
size: string | undefined = undefined;
render() {
// src attribute not rendered if either size or filename are undefined
return html``;
}
}
```
The example clearly shows how it works, however the [`src` is a mandatory attribute of the `img` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#src), and in such a case, I think the better way of doing things would be to not render the `img` at all.
Please don't get me wrong – I'm of course aware that this is just an example, but if such a code snippet is presented in a documentation of a library, it could be considered by many people as a good/common practice.
I wasn't able to find out a good alternative to that in a reasonable timespan, so I'm just leaving this issue as a note or a small issue report.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ifDefined directive section in the linked Lit documentation and review the quoted guidance and image example. Update the example or explanation so it does not present an incomplete img src as a recommended practice; the section is done when the documented use case is accurate and clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100