Not Another To-Do App
Tags: openwc, litelement, webcomponents, javascript
Published: 2019-08-07 04:53:15

#### Getting your hands dirty and feet wet with Open Web Component Recommendations...sort of: Part 1

> _This a cross-post of a Feb 26, 2019 article from [Medium](https://medium.com/@westbrook/not-another-to-do-app-762ea0bb960) that takes advantage of my recent decision to use Grammarly in my writing (so, small edits have been made here and there), thanks for looking again if you saw it there 🙇🏽‍♂️ and if this is your first time reading, welcome!_

# Why To-Do Apps?

Every person to have written a To-Do app has likely each done so for a slightly different reason. Some out of [boredom](https://dev.to/citizen428/fighting-boredom-with-a-hyperapp-experiment-12a). Others in a sort of [scientific pursuit](http://todomvc.com/). Many to [learn something](https://medium.com/javascript-in-plain-english/i-created-the-exact-same-app-in-react-and-vue-here-are-the-differences-e9a1ae8077fd) new. Often to actually [manage things you’ve got to do](https://zapier.com/blog/best-todo-list-apps/). And, I’m sure, myriad others. Inherently, a To-Do app is a perfect mix of simplicity (you can often put some together in a matter of minutes) and complexity (it begs the answer to many important technical questions). If you’re into simplicity, your off-ramp is below:

```js
let todos = [];

function render() {
    document.body.innerHTML = `
        ${todos.map((todo, index) => `
            <div>
                ${todo}
                <button
                  class="complete"
                  data-index="${index}"
                >
                    Complete
                </button>
            </div>`).join('')
        }
        <div>
            <input />
            <button class="add">Add</button>
        </div>
   `;
}

document.body.addEventListener('click', e => {
    if (e.target.classList.contains('add')) {
        todos.push(document.querySelector('input').value);
        render();
    } else if (e.target.classList.contains('complete')) {
        let index = e.target.dataset.index;
        todos.splice(index, 1);
        render();
    }
});

render();
```

Yes, this could absolutely be both simpler and smaller...I look forward to seeing the posts that prove that to be true! Starting productive conversations is a large part of why I write about the code that I craft, and if I can get a good one this early on I’ll be quite bullish for the rest of this series. However, it’s true that all you need to do is paste the above into the console of your favorite website and suddenly it too will be a To-Do app!

For those of you in Camp Complexity (sounds like a missed business opportunity), a To-Do app is a wide-open door to important questions around code architecture, component boundaries, event handling, state management, styling, testing, etc. and that’s before you even start to get into features beyond the basics of “as a busy person I’d like to be able to store tasks I need to get done” and “as a productive person I’d like to take tasks I’ve completed off the list”. If this camp is for you, welcome to orientation!

To get started, I’ll be honest, getting deep and learning all there is to know about the subjects touched on throughout this series is what [Googling](https://www.google.com/search?q=how+to+be+a+javascript+developer&oq=how+to+be+a+javascript&aqs=chrome.0.0j69i57j0l4.5813j1j7&sourceid=chrome&ie=UTF-8) things is for. Go ahead, I’ll wait... (Jeopardy theme song plays..) Because of that, rather than try to position myself as _THE_ expert or this post as _THE_ final word on any of these things, I was serious when I said that I am more interested in entering into a conversation about the problems/solutions that jumped out at me when recently revisiting the idea of writing a To-Do app. Join the conversation in the comments below (they’ll be waiting for you after each and every installation of this series) when you’ve finished reading, or, if it’s your thing, take the conversation onto the [Twitters](https://twitter.com/WestbrookJ).

# The Long Game

![Gotta catch 'em all!](https://thepracticaldev.s3.amazonaws.com/i/gm50p87j9l921koi57zh.jpeg)

<figcaption>Photo by <a href="https://unsplash.com/@16bitspixelz" target="_blank">Kamil S</a> on <a href="https://unsplash.com/" target="_blank">Unsplash</a></figcaption>

"I wanna be the very best! Like no one ever"... wait, who am I kidding? I wanna be just like the engineers I follow on Twitter. They [care deeply about making the web look good](https://twitter.com/argyleink), [fight to help the web work together](https://twitter.com/stubbornella), [focus on a11y](https://twitter.com/SaraSoueidan), [never test the implementation details](https://twitter.com/kentcdodds), [write the best libraries](https://twitter.com/justinfagnani), are also [sometimes designers](https://twitter.com/Mustafa_x), and much, much more. That means I have to do all those things too, right? **Well, luckily, no.** But, that doesn’t mean I don’t spend a disproportionate part of my time trying. This means that while I’ve already written the _amazing_ 34 line To-Do app featured above, as well as this _other_ amazing [To-Do app](https://dev.to/westbrook/litelement-to-do-app-4ngn) as a port from other popular front end technologies directly to [LitElement](https://lit-element.polymer-project.org/), I feel an undeniable urge to do more, do it again, do it better, and then annoy the world by talking about it.

> You are welcome!

To that end, the list of to-dos that I have for my To-Do app, this time around, looks as follows:

- component based code architecture
- custom property based style API
- event-based state management
- style sharing
- unit testing
- web component-based UI

Hopefully, these areas are all concepts that you also find interesting, and you what follows proves useful as you assess the technical decisions that go into your next application. If not, feel free to complain about it in the comments below...I love comments. What’s more, if you think I’ve left out any major points that you want to suggest I add herein or discuss in a later post, leave a comment for that, too!

There is one hot button front end engineering technique that you may have noticed is missing from the above and that’s “style encapsulation”. I left this out for good reason. Beyond it being a hotbed of disagreement across the community, due to the separate decision to make my UI with web components (with which I enjoy using [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM)), I get style encapsulation for free. That means there’s no need to discuss if/how/when I’ll encapsulate my styles, they just are from the get-go. In that I don’t actually do anything to get that encapsulation, I’ve omitted it from my To-Do app’s to-do list...

<hr/>

# The Short Game

As voted on by a plurality of people with opinions on such topics that are both forced to see my tweets in their Twitter feed and had a free minute this last week, a 9000+ word article is a no, no.

{% twitter 1098196160613896192 %}

So, it is with the deepest reverence to you my dear reader that I’ve broken the upcoming conversations into a measly ten sections. Congratulations, you’re nearing the end of the first! If you’ve enjoyed yourself so far, or are one of those people that give a new sitcom a couple of episodes to hit its stride, here’s a list of the others for you to put on your Netflix queue:

- Not Another To-Do App (you are here)
- [Getting Started](https://dev.to/westbrook/not-another-to-do-app-3jem)
- [Test Early, Test Often](https://dev.to/westbrook/not-another-to-do-app-2m9a)
- [Measure Twice, Lint Once](https://dev.to/westbrook/not-another-to-do-app-part-4-58cd)
- [Make it a Component](https://dev.to/westbrook/not-another-to-do-app-part-5-5d7o)
- [Make it a Reusable Part](https://dev.to/westbrook/not-another-to-do-app-part-6-an)
- [Does Your Component Really Need to Know That?](https://dev.to/westbrook/not-another-to-do-app-part-7-3cm7)
- [Separate Things Early, Often, and Only as Needed](https://dev.to/westbrook/not-another-to-do-app-part-8-3lic) (Clearly I didn’t follow this one when writing this series as a single article to start with...)
- [Some Abstractions Aren’t (Just) For Your App](https://dev.to/westbrook/not-another-to-do-app-part-9-10j3)
- [Reusable and Scaleable Data Management/And, in the end...](https://dev.to/westbrook/not-another-to-do-app-part-10-mp6)
- [See the app in action](https://gifted-lamport-70b774.netlify.com/)

<hr/>

Special thanks to the team at [Open Web Components](https://open-wc.org/) for the great set of tools and recommendations that they’ve been putting together to support the ever-growing community of engineers and companies bringing high-quality web components into the industry. [Visit them on GitHub](https://github.com/open-wc/open-wc) and create an issue, submit a PR, or fork a repo to get in on the action!