Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/components/popover/popover.scss
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
min-height: var(--min-height);
max-height: var(--max-height);

outline: none;

background: var(--background);

box-shadow: var(--box-shadow);
Expand Down
35 changes: 31 additions & 4 deletions core/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Component, Element, Event, Host, Method, Prop, State, Watch, h } from '@stencil/core';
import { focusFirstDescendant } from '@utils/focus-trap';
import { CoreDelegate, attachComponent, detachComponent } from '@utils/framework-delegate';
import { addEventListener, raf, hasLazyBuild } from '@utils/helpers';
import { addEventListener, raf, hasLazyBuild, inheritAttributes } from '@utils/helpers';
import type { Attributes } from '@utils/helpers';
import { createLockController } from '@utils/lock-controller';
import { printIonWarning } from '@utils/logging';
import { OverlayFocusBoundary } from '@utils/overlay-focus-boundary';
Expand Down Expand Up @@ -71,6 +72,7 @@ export class Popover implements ComponentInterface, PopoverInterface {
private headerResizeObserver?: ResizeObserver;

private inline = false;
private inheritedAttributes: Attributes = {};
private workingDelegate?: FrameworkDelegate;

private focusDescendantOnPresent = false;
Expand Down Expand Up @@ -389,7 +391,21 @@ export class Popover implements ComponentInterface, PopoverInterface {
}

componentWillLoad() {
const { el } = this;
const { el, htmlAttributes } = this;

const attributesToInherit = ['aria-label', 'role'];
this.inheritedAttributes = inheritAttributes(el, attributesToInherit);
if (htmlAttributes !== undefined) {
attributesToInherit.forEach((attribute) => {
if (htmlAttributes[attribute]) {
this.inheritedAttributes = {
...this.inheritedAttributes,
[attribute]: htmlAttributes[attribute],
};
delete htmlAttributes[attribute];
}
});
}
const popoverId = this.htmlAttributes?.id ?? setOverlayId(el);

this.parentPopover = el.closest(`ion-popover:not(#${popoverId})`) as HTMLIonPopoverElement | null;
Expand Down Expand Up @@ -772,7 +788,6 @@ export class Popover implements ComponentInterface, PopoverInterface {

return (
<Host
aria-modal="true"
no-router
Comment thread
anasdahani220 marked this conversation as resolved.
Resolved
tabindex="-1"
{...(htmlAttributes as any)}
Expand Down Expand Up @@ -801,7 +816,19 @@ export class Popover implements ComponentInterface, PopoverInterface {

<div class="popover-wrapper ion-overlay-wrapper" onClick={dismissOnSelect ? () => this.dismiss() : undefined}>
{enableArrow && <div class="popover-arrow" part="arrow"></div>}
<div class="popover-content" part="content">
<div
/*
role and aria-modal go on the same element inside the
shadow DOM, matching ion-modal. tabIndex={-1} lets
present() move focus here.
*/
role="dialog"
{...this.inheritedAttributes}
aria-modal="true"
tabIndex={-1}
class="popover-content"
part="content"
>
<slot></slot>
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion core/src/components/popover/test/basic/popover.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
*/
await expect(popover).toBeFocused();

await page.keyboard.press(tabKey);
/**
* Focus the textarea directly instead of tabbing to it. In Firefox,
* Tab from the focused popover content stops on the scrollable
* ion-textarea first, the same as it does for a native dialog.
*/
await innerNativeTextarea.focus();

await expect(innerNativeTextarea).toBeFocused();

Expand Down
52 changes: 52 additions & 0 deletions core/src/components/popover/test/popover-attributes.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { h } from '@stencil/core';
import { newSpecPage } from '@stencil/core/testing';

import { Popover } from '../popover';

// Fixes https://github.com/ionic-team/ionic-framework/issues/31823
describe('popover: attributes', () => {
it('should set the dialog role and aria-modal on the content', async () => {
const page = await newSpecPage({
components: [Popover],
template: () => <ion-popover overlayIndex={1}></ion-popover>,
});

const popover = page.body.querySelector('ion-popover')!;
const content = popover.shadowRoot!.querySelector('[part="content"]')!;

expect(content.getAttribute('role')).toBe('dialog');
expect(content.getAttribute('aria-modal')).toBe('true');
expect(popover.hasAttribute('aria-modal')).toBe(false);
});

it('should inherit attributes', async () => {
const page = await newSpecPage({
components: [Popover],
template: () => <ion-popover overlayIndex={1} aria-label="my label" role="alertdialog"></ion-popover>,
});

const popover = page.body.querySelector('ion-popover')!;
const content = popover.shadowRoot!.querySelector('[part="content"]')!;

expect(content.getAttribute('aria-label')).toBe('my label');
expect(content.getAttribute('role')).toBe('alertdialog');
});

it('should inherit attributes when set via htmlAttributes', async () => {
const page = await newSpecPage({
components: [Popover],
template: () => (
<ion-popover overlayIndex={1} htmlAttributes={{ 'aria-label': 'my label', role: 'alertdialog' }}></ion-popover>
),
});

const popover = page.body.querySelector('ion-popover')!;
const content = popover.shadowRoot!.querySelector('[part="content"]')!;

expect(content.getAttribute('aria-label')).toBe('my label');
expect(content.getAttribute('role')).toBe('alertdialog');

expect(popover.hasAttribute('aria-label')).toBe(false);
expect(popover.hasAttribute('role')).toBe(false);
});
});
Comment thread
anasdahani220 marked this conversation as resolved.
Resolved