How can I nest an ion-button
within a clickable ion-item
?
By this I mean legally (markup- and a11y-wise) and accessibly.
Use case
My use case is a list of items. Clicking an item navigates to a new page. Each item also has some action buttons to the side, visually within the item.
For example, rather like mobile Gmail's "star" button.
It needs to work sensibly on touch interfaces and with mice/trackpads, which mostly precludes Ionic's sliding items.
Some relevant GitHub issues regards ion-item-sliding
:
- #22909: bug: improve ion-item-sliding gesture on desktop with trackpad/mouse
- #23155: feat: improve performance when using ion-item-sliding
If there is a different UI pattern that would work, I would be willing to look into/accept that instead of a solution to this exact question.
Investigations and potential solutions
The naive method is to simply nest them:
<ion-item
button="true"
detail="false"
@click="parentClicked"
>
<ion-label> An item </ion-label>
<ion-button slot="end" @click="childClicked">
<ion-label> Action </ion-label>
</ion-button>
</ion-item>
This produces a button within a button, which is invalid HTML, bad for accessibility, and generally all-round bad.
Searching for this issue specifically (along the lines of the title) turns up results that suggest this naive nesting approach, so those have been no help.
A suggested solution I have found is to make the buttons siblings and use relative positioning to align the secondary button atop the primary button. However, that was for vanilla HTML and CSS; with Ionic, it will be fairly complex or brittle to use this approach: the Ionic styles would have to be duplicated to provide the correct alignment, at the least. Doable, but harder.
Is there any "good" method for this?
Potential alternatives
Add an "edit" button to the app's toolbar, which will toggle "edit" mode. When in edit mode, items will no longer be buttons themselves, and the action buttons on each will be shown.
This would of course only work for things like deleting and renaming; it simply wouldn't work for something like a "favorite" button or mobile Gmail's "star" button.
A variation of the above would be to add a checkbox to each item in the list when in edit mode; and instead of buttons on each item, have a single toolbar of action buttons.
This is inspired by Samsung Music: in the playlists view, long-pressing on a playlist turns the list into a checklist; clicking a playlist in this mode selects it instead of navigating; and a toolbar of actions is shown. Renaming is disabled if more than one playlist is selected.