Accordion from <details> that unfolds
Four <details> elements with the same name, drawn by a canvas. Tap a heading. The browser opens that one and closes the other in the same instant.
The canvas has a picture of the closed one from before it closed and a live picture of the one that opened, and joins them with a fold: the content is cut into strips that open like folded paper.
The controls inside the open section work, also while it unfolds.
Show the code the two pictures, the strips
- The markup. Ordinary
<details name>elements, each withdrawable. - Notice a change and keep the open picture. Each paint compares
openwith the last paint. While a section is open and at rest, the canvas keeps a snapshot of it. - Draw the content as strips. One clipped, sheared and squashed draw for each strip, then a shade over it.
- On
paint. Stack the sections at their animated heights. - Styles, including the CSS route.
How it works
A closed <details> has no box for its content, so its picture is only the summary. The open picture exists only while it's open. An animation between the two needs both at once, and the browser changes the state in one step.
Closing. On every paint while a section is open and not animating, the canvas calls captureElementImage() and keeps the result. When a later paint finds the section closed, the content strips are drawn from that snapshot, folding shut.
This also covers a section that the browser closed because another one with the same name was opened: the page gets no warning before that happens.
Opening. The section is already open in layout, so the strips are drawn from the live element. Each strip is a clipped drawElementImage() with a transform that squashes it vertically and shears it sideways, alternating direction, and a dark or light fill over it.
A last draw with no alpha puts the hit region of the whole section at its final size, so its controls can be used during the fold.
Positions. The layout jumps at once. The canvas ignores the layout positions and stacks the sections itself, using an animated height for each.
CSS route. With "CSS height transition" the page does none of that. interpolate-size: allow-keywords and a transition on ::details-content make the browser animate the height, each frame raises a paint event, and the canvas draws every section where layout puts it.
Live checks
These run on their own <details> elements in the corner of the window.
Findings
- In Chromium 157 a
<details drawable>draws in both states. Closed, its picture and itsoffsetHeightare the summary only. - A snapshot from
captureElementImage()taken while the element was open still draws the open content after it has closed. This is the only way found to get the open picture of a closed<details>. - The content of a closed
<details>can't be drawn on its own: a nesteddrawablecontent element throws "No cached paint record" while closed. The check below records it. - Opening one
<details name>closes the other synchronously. Onepaintevent shows both changes, so both animations start on the same frame. - A clipped draw of part of an element, under a shear, works, and several draws of one element in a frame are allowed. The hit region is that of the last draw, so the page ends with a draw at alpha 0 in the resting position.
- The CSS route works in a canvas: a height transition on
::details-contentraises apaintevent for each frame. - Not verified: frame rate on a phone, and what a screen reader announces during the fold (the elements and their states are the native ones).