# Getting Inputs.bind() to work on a custom component?

**URL:** <https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756>\
**Category:** Help\
**Created:** [March 17, 2023, 7:58am UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756 "2023-03-17T07:58:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 17, 2023, 7:58am UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/1 "2023-03-17T07:58:46Z")

</div>

For the last few days I’ve been trying to get this [ChoicesJS component work](https://observablehq.com/@bayre/hello-choices) nicely with [Inputs.bind()](https://observablehq.com/@observablehq/synchronized-inputs?collection=@observablehq/inputs). But no matter what I try I can’t get it to work. I’m sure it’s possible but it’s just my limited knowledge here that’s stopping progress. I’ve created [this notebook](https://observablehq.com/d/9f2f23f1b6b45be3) to play around in.

Any help in the right direction is much appreciated!

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 17, 2023, 11:54am UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/2 "2023-03-17T11:54:55Z")

</div>

It looks like your widget is missing a setter. A useful pattern for custom widgets (here demonstrating a fictional widget that exposes its value via functions) is:

```javascript
return Object.defineProperty(htl.html`<div>${myWidget}`, "value", {
  get: () => myWidget.getValue(),
  set: newValue => myWidget.setValue(newValue)
});

```

Note that we don’t dispatch an input event when setting the value for two reasons:

1. it matches the behavior of native input elements
2. it could trigger an update loop

Please also avoid any code that modifies the DOM directly, like document.body.appendChild. The only reason this _seems_ to work in your code is that you include the element in your container, which effectively moves it back from the body into your cell output.

* * *

That said, I wonder if you might want to pick a different library than Choices. Two of its characteristics make it seem like a bad fit for Observable:

1. ~~it requires the select element to be present in the DOM, so that you have to jump through additional hoops (like requestAnimationFrame) to initialize it after your cell has already returned.~~ _(edit: this turned out to be wrong)_
2. It uses an absolute positioned dropdown. Any content inside the notebook sandbox is covered by pinned editors, so chances are high that the dropdown might get hidden behind an open editor.

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 17, 2023, 1:00pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/3 "2023-03-17T13:00:12Z")

</div>

Thanks for helping out @mootari ! I’m noticing that I having a hard time grasping some of the concepts you share 😄. Really a complete newbie when it comes to creating custom widgets. I haven’t created this ChoicesJS widget myself but started out with a fork from a different notebook.

Do you maybe have a working example from a different widget that I could use as inspiration?

I have noticed the behaviour that you describe where the dropbox disappears underneath pinned editors. I’m okay paying that price in return for the features that ChoicesJS offers. Right now I’m using the widget mostly embedded on a page so the absolute positioning isn’t really an issue there.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 17, 2023, 11:56pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/4 "2023-03-17T23:56:58Z")

</div>

> [@marcf](#):
>
> Do you maybe have a working example from a different widget that I could use as inspiration?

A while back I wrote [this introduction to viewof](https://observablehq.com/@mootari/what-has-viewof-ever-done-for-us) which doesn’t cover setters though.

Maybe you could list a few points that you find confusing or that you’re stuck on, and I will try to answer them individually?

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 18, 2023, 7:14am UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/5 "2023-03-18T07:14:25Z")

</div>

Thanks @mootari that’s a helpful article and definitely helps to better understand the inner workings of viewof.

Getting closer but I’m still not quite there!

I’ve updated [my notebook](https://observablehq.com/d/9f2f23f1b6b45be3) and to my best knowledge, added a setter and getter functions inside the widget.

There are 2 things I’m now stuck on:

1. Updating the values of the dropdown only works for adding new values. Removing values isn’t working? Could it be that this is related to the fact the the two dropdowns are binded?

2. I’m not able to update the actual value of `container.value` as that gets me into an infinite loop. So the value of `selection2` doesn’t return the correct value until I manually update that widget.

UPDATE:  
The second problem seems to be fixed by adding `container.dispatchEvent(new CustomEvent("input", {bubbles: true}));` to the setter. Now just stuck on the weird first problem.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 19, 2023, 1:44pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/6 "2023-03-19T13:44:20Z")

</div>

Great to see how quickly you’re making progress!

I’ve created an example for native selects here (still needs more explanations / documentation):

> **[Implementing a custom widget](https://observablehq.com/d/5cf5f45bce49b47b)**
>
> Single Select Multi Select

and a prototype of a Choices widget that matches the API of the Inputs widgets:

> **[Choices Input](https://observablehq.com/d/5cc4d48253a710a5)**
>
> Demo Implementation

Will add more details once I find the time, but maybe this will already help you get going.

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 20, 2023, 4:00pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/7 "2023-03-20T16:00:07Z")

</div>

This is insane @mootari 🙂 Still trying to grasp what’s happening in your example line by line. Appreciate you took the time to put this together. Hope it will help many others in the community.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 20, 2023, 8:17pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/8 "2023-03-20T20:17:44Z")

</div>

Let me know if there’s anything in particular that you’d like explained in more detail!

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 21, 2023, 10:33am UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/9 "2023-03-21T10:33:22Z")

</div>

@mootari I’m wondering what the recommended approach is when you want to bind more than two widgets together. I know the `Inputs.bind()` returns the returns widget but what I want to bind 5 widgets. Is it better to first create the individual `viewof` instances and then do multiple bind calls or should you just use the output of bind()? Hope I’m making sense.

Sidenote, having `widget.lastElementChild.style.cssText += "overflow: hidden";` enabled in one of my other notebooks hides the dropdown with the list of options when clicked. Any pointers on how to debug this?

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 21, 2023, 12:45pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/10 "2023-03-21T12:45:00Z")

</div>

> [@marcf](#):
>
> Is it better to first create the individual `viewof` instances and then do multiple bind calls or should you just use the output of bind()? Hope I’m making sense.

I would recommend to create only one viewof cell and have that be your primary input (and the only one with a default value, if any):

```javascript
viewof text = Inputs.text({value: "default value"}) // first cell

```

```javascript
Inputs.bind(Inputs.text(), viewof text) // second cell

```

```javascript
Inputs.bind(Inputs.text(), viewof text) // third cell

```

While you _can_ link two viewof cells together via Inputs.bind() in a third cell, I would generally advise against that. Avoid side effects whenever you can: the cell that displays the secondary input should also initiate the binding.

> [@marcf](#):
>
> Any pointers on how to debug this?

The dropdown is positioned absolute and thus removed from the layout flow. It has no dimensions that could stretch the parent element.

If you take a closer look at my implementation you may notice that I chose to position the dropdown relatively, so that it expands the cell and cannot be covered by editors.

Debugging is tricky, as the display state is controlled via JavaScript. You could add a CSS rule to always display it:

```css
.choices __list.choices__ list--dropdown { display: block }

```

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 21, 2023, 2:21pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/11 "2023-03-21T14:21:51Z")

</div>

> [@mootari](#):
>
> I would recommend to create only one viewof cell and have that be your primary input (and the only one with a default value, if any):

When I test this approach, I run into the problem that when cell #3 is updated, the output of cell #2 doesn’t change. The value does change in cell #2 dropdown but the values in the resulting array aren’t updated. Here’s [an example](https://observablehq.com/d/cadf615f3d3c5d72) based on your notebook.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 21, 2023, 3:59pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/12 "2023-03-21T15:59:16Z")

</div>

Yes, as mentioned you should only obtain the value from the first widget, which is what I demonstrated in my example above. There isn’t really a point in linking multiple viewof’s, since the widgets should all share the same value.

Inputs.bind won’t dispatch an input event on the widget when setting the widget value, and therefore the cell value won’t register as updated. Consider any additional inputs to be an “extension” (or copy) of your first, not actual new cell values.

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 21, 2023, 4:13pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/13 "2023-03-21T16:13:39Z")

</div>

@mootari Hmmm, but what if you have a really long report and want to have a dropdown (lets say a country filter) in multiple places along the report. Changing the dropdown in one place of the report should update the other instances as well, right? Is that doable? I’m trying to understand the dynamics here.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 21, 2023, 5:55pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/14 "2023-03-21T17:55:30Z")

</div>

That is precisely what Inputs.bind() is for. Your report cells only reference your primary widget cell. The other widgets only act as “remote controls” for your primary widget. 🙂

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 21, 2023, 6:08pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/15 "2023-03-21T18:08:45Z")

</div>

But if you take a [look over here](https://observablehq.com/d/cadf615f3d3c5d72) you’ll see that it’s not working, or at least not how I would expect 🙂 Change the value of `dropdown3` and look at the [value array](https://observablehq.com/d/cadf615f3d3c5d72#cell-216) or `dropdown2`. That array isn’t being updated but the value inside the dropdown is.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 21, 2023, 6:33pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/16 "2023-03-21T18:33:28Z")

</div>

`choicesSelection` is your primary input, and the only one that you should reference in other cells. `dropdown2` and `dropdown3` don’t need to be named.

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 21, 2023, 6:38pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/17 "2023-03-21T18:38:27Z")

</div>

@mootari Got it! That needed to click in my mind. Thanks for your patience 🙂

One more question. If you want to embed the different dropdowns on an external page then naming them would make sense right? Or would you be able to embed the single dropdown instance multiple times?

You just need to make sure that you’re reading values from your “main” dropdown.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 21, 2023, 9:40pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/18 "2023-03-21T21:40:37Z")

</div>

> [@marcf](#):
>
> If you want to embed the different dropdowns on an external page then naming them would make sense right?

Yes, although you don’t need the `viewof` prefix in this case. The name will then just reference the DOM element.

> [@marcf](#):
>
> Got it! That needed to click in my mind. Thanks for your patience 🙂

No worries! Would you be willing to share if there’s a certain perspective or assumption that may have misled you? Building an intuition for these concepts can be tricky, and I’d love to learn what I should look out for. 🙏

---

<div class="post-metadata">

**Author:** ![marcf](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@marcf](https://talk.observablehq.com/u/marcf)\
**Post date:** [March 23, 2023, 8:37pm UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/19 "2023-03-23T20:37:07Z")

</div>

> [@mootari](#):
>
> Yes, although you don’t need the `viewof` prefix in this case. The name will then just reference the DOM element.

Works like a charm!

> [@mootari](#):
>
> No worries! Would you be willing to share if there’s a certain perspective or assumption that may have misled you? Building an intuition for these concepts can be tricky, and I’d love to learn what I should look out for. 🙏

Sure, what clicked was the fact that you just have one `viewof` instance and all the subsequent widgets are just a reference to that. In my head I had multiple `viewof` instances that all referenced (and updated) each other. So in my charts, I was also using a different `viewof` instance that corresponded to a specific section of a notebook. But now I only have one “main” dropdown and all my charts reference that. Of course a much more elegant solution.

To be honest, I don’t think I came across an example in the documentation related to `Inpts.bind()` that uses this approach. Maybe it would be good to point out when you’d use just one instance and when you’d use multiple instances.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 24, 2023, 11:17am UTC](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756/20 "2023-03-24T11:17:15Z")

</div>

That’s great feedback, thank you!

[Next page](https://talk.observablehq.com/t/getting-inputs-bind-to-work-on-a-custom-component/7756.md?page=2)
