Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sbb-popover): prevent matchMedia call before hydration #2893

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
63 changes: 45 additions & 18 deletions src/elements/popover/popover/popover.ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,58 @@ import { html } from 'lit';
import { ssrHydratedFixture } from '../../core/testing/private.js';

import { SbbPopoverElement } from './popover.js';

import '../../button.js';
import '../../link.js';

describe(`sbb-popover ssr`, () => {
let root: HTMLSpanElement;

beforeEach(async () => {
root = await ssrHydratedFixture(
html`
<span>
<sbb-button id="popover-trigger">Popover trigger</sbb-button>
<sbb-popover id="popover" trigger="popover-trigger">
Popover content.
<sbb-link id="popover-link" href="#" sbb-popover-close>Link</sbb-link>
</sbb-popover>
<sbb-block-link href="#" id="interactive-background-element"
>Other interactive element</sbb-block-link
>
</span>
`,
{ modules: ['../../button.js', './popover.js', '../../link.js'] },
);
describe('default', () => {
beforeEach(async () => {
root = await ssrHydratedFixture(
html`
<span>
<sbb-button id="popover-trigger">Popover trigger</sbb-button>
<sbb-popover id="popover" trigger="popover-trigger">
Popover content.
<sbb-link id="popover-link" href="#" sbb-popover-close>Link</sbb-link>
</sbb-popover>
<sbb-block-link href="#" id="interactive-background-element"
>Other interactive element</sbb-block-link
>
</span>
`,
{ modules: ['../../button.js', './popover.js', '../../link.js'] },
);
});

it('renders', () => {
assert.instanceOf(root.querySelector('sbb-popover'), SbbPopoverElement);
});
});

it('renders', () => {
assert.instanceOf(root.querySelector('sbb-popover'), SbbPopoverElement);
describe('hover trigger', () => {
beforeEach(async () => {
root = await ssrHydratedFixture(
html`
<span>
<sbb-button id="popover-trigger">Popover trigger</sbb-button>
<sbb-popover id="popover" trigger="popover-trigger" hover-trigger>
Popover content.
<sbb-link id="popover-link" href="#" sbb-popover-close>Link</sbb-link>
</sbb-popover>
<sbb-block-link href="#" id="interactive-background-element"
>Other interactive element</sbb-block-link
>
</span>
`,
{ modules: ['../../button.js', './popover.js', '../../link.js'] },
);
});

it('renders', () => {
assert.instanceOf(root.querySelector('sbb-popover'), SbbPopoverElement);
});
});
});
12 changes: 10 additions & 2 deletions src/elements/popover/popover/popover.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CSSResultGroup, PropertyValues, TemplateResult } from 'lit';
import { html, nothing } from 'lit';
import { html, isServer, nothing } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { ref } from 'lit/directives/ref.js';

Expand All @@ -15,6 +15,7 @@
import { composedPathHasAttribute, EventEmitter } from '../../core/eventing.js';
import { i18nClosePopover } from '../../core/i18n.js';
import type { SbbOpenedClosedState } from '../../core/interfaces.js';
import { SbbHydrationMixin } from '../../core/mixins.js';
import {
getElementPosition,
isEventOnElement,
Expand Down Expand Up @@ -47,7 +48,7 @@
* component is set to `var(--sbb-overlay-default-z-index)` with a value of `1000`.
*/
@customElement('sbb-popover')
export class SbbPopoverElement extends SbbOpenCloseBaseElement {
export class SbbPopoverElement extends SbbHydrationMixin(SbbOpenCloseBaseElement) {
public static override styles: CSSResultGroup = style;

/**
Expand Down Expand Up @@ -210,6 +211,13 @@

// Check if the trigger is valid and attach click event listeners.
private _configure(): void {
if (isServer) {
return;

Check warning on line 215 in src/elements/popover/popover/popover.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/popover/popover/popover.ts#L215

Added line #L215 was not covered by tests
} else if (this.hydrationRequired) {
this.hydrationComplete.then(() => this._configure);
return;
}

removeAriaOverlayTriggerAttributes(this._triggerElement);

if (!this.trigger) {
Expand Down
Loading