11ty / 11ty/webc

Conditional boolean attributes

Open
#155 8 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

education
Dominant language
JavaScript
Stars
1.4k
Forks
46
Avg merge
10m
Merged PRs (30d)
2

Description

Consider this lit-html template

html`<a href="${ifDefined(href)}"></a>`;

if href is undefined, the output will be:

<a></a>

Why is this important?

Consider

<script webc:for="script of scripts"
      :defer="script.defer"
      :async="script.async"
      :src="url(script.src)"></script>

In this case, all scripts will be async and defer because those are boolean attributes whose presence indicates true.

If we wanted to use a template and ifs:

<template webc:nokeep webc:for="script of scripts">
  <script webc:if="defer && async" defer async :src="url(script.src)"></script>
  <script webc:elseif="defer" defer            :src="url(script.src)"></script>
  <script webc:elseif="async" async            :src="url(script.src)"></script>
  <script webc:else                            :src="url(script.src)"></script>
</template>

I guess that's ok? If you have lots of attrs, you have to keep track of a whole tree of if/then/else.

But what if null meant "remove this attribute"?:

<script webc:for="script of scripts"
       :defer="script.defer ?? null"
       :async="script.async ?? null"
       :src="url(script.src)"></script>

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named in the issue. Start by tracing the existing handling of bound attributes using the examples above; done when null removes boolean attributes while truthy values preserve their presence and existing non-boolean behavior remains covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.