HACK: Disabling buttons created through Button()

Hello again, Coda people! Did you know you can conditionally disable buttons constructed through the Button() formula?

As it turns out, the helper formula _BUTTON_PROPERTIES(), which evaluates the formula-driven display properties of a normal button, has a parameter called “disabled”. I figured that it may be possible to inject this property into a custom-built button, and sure enough, using _Merge() and Object(), it is!

Say we have a table and we want to create a custom button that can be conditionally enabled/disabled by checking a boolean (Checkbox or Toggle) column. We will first create the helper action, the Action column, which executes the simple formula thisRow.ModifyRows(thisRow.Boolean, thisRow.Boolean.Not()). This is the action that will be executed by our custom button and will change our Checkbox column, Boolean, from true → false and vice versa each time it is pressed. We then create a Should disable boolean, which will be the toggle our button checks to enable or disable itself.

Then, we can construct our custom button through Button(). However, to inject the disabled property, we must construct it through Object().

The full formula is:

_DEREF_OBJECT(
  Object(
    "button",
    _Merge(
      Button(thisRow.Action, "Check this out"),
      Object(
        "properties",
        Object("disabled", thisRow.[Should disable])
      )
    )
  ),
  "button"
)

Lets break this formula down. We are creating a Coda object with the name “button” – it must be named button for Coda to properly dereference it just kidding, you can name it anything as long as you _DEREF_OBJECT() at that same path – and injecting it with the JSON of a custom button object. We then must also add the properties object as BUTTON_PROPERTIES() evalutes three arguments for properties: text, badge, and disabled. We only need to evaluate disabled, so we inject an Object of name “properties” with the value “disabled” equal to the value of Should disable. Finally, we dereference the object at path button, which Coda will hydrate as a button object.

As always, this is EXPERIMENTAL and I do not recommend you use this in a production doc as experimental formulas such as Object(), _Merge(), and the structure of button objects can change at any time.

(Yes, I know that disabling the underlying action will disable the button. Sometimes you don’t want this, though! Say, for example, your underlying action sends out an email in stages, and you want a specific button to be disabled but allow a different button to be enabled. I could probably think of a better example but I wrote this in the middle of a lecture.)

Check it out for yourself here!

6 Likes

Ha interesting. You aren’t using the BUTTON_PROPERTIES() formula in your listing though? It’s just a merge/object thing — I guess BUTTON_PROPERTIES() is simply syntactic sugar for that merge/object?

On another note, wondering if you can figure out how to inject the “Show confirmation dialog” properties. Because I couldn’t. I tried the properties I read from the document object model of a button (via JS console) and those didn’t register (i.e. these didn’t result in a confirmation dialog)

3 Likes

This posting was a little poorly worded as I wrote it during a lecture haha. Paying attention to two things at once. What I meant to say is that after reading the parameters of BUTTON_PROPERTIES(), I assumed that we could probably just directly inject them into a Button() object since it already exposes a properties object. There was probably an easier way to figure that out… but just an educated guess.

Ive been toying with trying to inject the confirmation dialog but so far I’ve got nothing :frowning:
It only seems to work when you give your helper button a confirmation dialog and reference your Button() button on the canvas. Will need to push further

2 Likes