amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

[amp-accordion]The tap event on the first child of section is not fired when clicked.

Open xiaotong-kk9 opened this issue 7 years ago • 10 comments

What's the issue?

The tap event on the first child of section doesn't work.

How do we reproduce the issue?

  1. Please visit https://codepen.io/kiri/pen/rgzryB.
  2. Click the the heading for the section (Marked as Toggle text and accordion[doesn't work])
  3. The text doesn't toggle visibility.

What browsers are affected?

Chrome Version 74.0.3729.157 (Official Build) (64-bit)

Which AMP version is affected?

Version 1905091827220

xiaotong-kk9 avatar May 20 '19 04:05 xiaotong-kk9

According to the Actions and events doc:

AMP defines a tap event globally that you can listen to on any HTML element (including AMP elements).

It's supposed to work:)

xiaotong-kk9 avatar May 20 '19 04:05 xiaotong-kk9

Triaging to @choumx , feel free to re-assign (I think Actions falls under you?) 😄

torch2424 avatar May 21 '19 23:05 torch2424

Oh wait, after reading the issue again, I think maybe @aghassemi ? 😄

torch2424 avatar May 21 '19 23:05 torch2424

This appears to be due to a call to preventDefault here:

https://github.com/ampproject/amphtml/blob/cd89cb3f509c89edbc122a00e6b3d0019b03d385/extensions/amp-accordion/0.1/amp-accordion.js#L523

Which causes the click to not perform the tap action, due to the check here:

https://github.com/ampproject/amphtml/blob/8c0b17377bfb1f0cb5c857d0ca37e82301b110e4/src/service/action-impl.js#L304-L307

This behavior has been present since the first commit of the component, though it is unclear to me why. There is also some logic that does not perform the toggling action of the header if you click on a child has a tap action, but that logic does not extend to the header itself.

I suppose in that case, you could trigger whatever action you wanted, then trigger a toggle on the section. You could do something like:

<amp-accordion id="ac">
    <section expanded id="someId">
      <h4 role="sectiontitle">
        <div tabindex="0" on="tap:foo.toggleVisibility,ac.toggle(section = 'someId')">
          Toggle text and accordion
        </div>
      </h4>
      <p>Bunch of awesome content.</p>
    </section>
</amp-accordion>

Here is an updated code pen:

https://codepen.io/anon/pen/WqNvmE

sparhami avatar Jun 10 '19 22:06 sparhami

An alternative to toggling on tap (running an action) would be to run an action when the section expands/collapses:

https://codepen.io/anon/pen/VJwLoL

    <section expanded on="expand:foo.toggleVisibility;collapse:foo.toggleVisibility">
      <h4 role="sectiontitle" tabindex="0">Toggle text and accordion</h4>
      <p>Bunch of awesome content.</p>
    </section>

sparhami avatar Jun 10 '19 22:06 sparhami

Hi Sepand, Thank you for the explanation and solutions! Super clear and helpful:)

xiaotong-kk9 avatar Jun 11 '19 08:06 xiaotong-kk9

@cathyxz Adding Cathy, who has context. We may want to add a warning message here if this is working as intended.

sparhami avatar Aug 05 '19 21:08 sparhami

This is working as intended. The intention here is to have headings trigger the expand / collapse action for <amp-accordion> on click. A valid workaround is as @sparhami described above, to use the expand and collapse actions.

We haven't really seen any valid use cases that require a tap action on the accordion header. Most of the use cases has been to toggle the styling of the accordion itself based on expansion / collapse. Links also already work out of the box. This can generally be done via CSS styling on the expanded attribute.

cathyxz avatar Aug 05 '19 21:08 cathyxz

In that case, lets add a warning message when people try to add a tap handler to the header itself.

sparhami avatar Aug 05 '19 21:08 sparhami

Thanks Cathy!

xiaotong-kk9 avatar Aug 08 '19 09:08 xiaotong-kk9