[amp-accordion]The tap event on the first child of section is not fired when clicked.
What's the issue?
The tap event on the first child of section doesn't work.
How do we reproduce the issue?
- Please visit https://codepen.io/kiri/pen/rgzryB.
- Click the the heading for the section (Marked as
Toggle text and accordion[doesn't work]) - 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
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:)
Triaging to @choumx , feel free to re-assign (I think Actions falls under you?) 😄
Oh wait, after reading the issue again, I think maybe @aghassemi ? 😄
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
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>
Hi Sepand, Thank you for the explanation and solutions! Super clear and helpful:)
@cathyxz Adding Cathy, who has context. We may want to add a warning message here if this is working as intended.
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.
In that case, lets add a warning message when people try to add a tap handler to the header itself.
Thanks Cathy!