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: fix width breakpoints of SbbMediaMatcherController #3226

Merged
merged 3 commits into from
Nov 21, 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
20 changes: 11 additions & 9 deletions src/elements/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { classMap } from 'lit/directives/class-map.js';

import { isArrowKeyOrPageKeysPressed, sbbInputModalityDetector } from '../core/a11y.js';
import { readConfig } from '../core/config.js';
import { SbbConnectedAbortController, SbbLanguageController } from '../core/controllers.js';
import {
SbbLanguageController,
SbbMediaMatcherController,
SbbMediaQueryBreakpointMediumAndAbove,
} from '../core/controllers.js';
import type { DateAdapter } from '../core/datetime.js';
import {
DAYS_PER_ROW,
Expand All @@ -22,7 +26,6 @@ import {
YEARS_PER_ROW,
} from '../core/datetime.js';
import { forceType } from '../core/decorators.js';
import { isBreakpoint } from '../core/dom.js';
import { EventEmitter } from '../core/eventing.js';
import {
i18nCalendarDateSelection,
Expand Down Expand Up @@ -229,11 +232,13 @@ class SbbCalendarElement<T = Date> extends SbbHydrationMixin(LitElement) {
@state()
private accessor _initialized = false;

private _abort = new SbbConnectedAbortController(this);
private _language = new SbbLanguageController(this).withHandler(() => {
this._monthNames = this._dateAdapter.getMonthNames('long');
this._createMonthRows();
});
private _mediaMatcher = new SbbMediaMatcherController(this, {
[SbbMediaQueryBreakpointMediumAndAbove]: () => this._init(),
});

public constructor() {
super();
Expand All @@ -258,10 +263,6 @@ class SbbCalendarElement<T = Date> extends SbbHydrationMixin(LitElement) {
this._resetFocus = true;
this._focusCell();
};
globalThis.window?.addEventListener('resize', () => this._init(), {
passive: true,
signal: this._abort.signal,
});
}

protected override willUpdate(changedProperties: PropertyValues<this>): void {
Expand Down Expand Up @@ -297,7 +298,7 @@ class SbbCalendarElement<T = Date> extends SbbHydrationMixin(LitElement) {

/** Initializes the component. */
private _init(activeDate?: T): void {
//Due to its complexity, the caledar is only initialized on client side
// Due to its complexity, the calendar is only initialized on client side
if (isServer) {
return;
} else if (this.hydrationRequired) {
Expand All @@ -308,7 +309,8 @@ class SbbCalendarElement<T = Date> extends SbbHydrationMixin(LitElement) {
if (activeDate) {
this._assignActiveDate(activeDate);
}
this._wide = isBreakpoint('medium') && this.wide;
this._wide =
(this._mediaMatcher.matches(SbbMediaQueryBreakpointMediumAndAbove) ?? false) && this.wide;
this._weeks = this._createWeekRows(this._activeDate);
this._years = this._createYearRows();
this._nextMonthWeeks = [[]];
Expand Down
12 changes: 9 additions & 3 deletions src/elements/core/controllers/media-matchers-controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { SbbBreakpointMediumMin, SbbBreakpointSmallMax } from '@sbb-esta/lyne-design-tokens';
import {
SbbBreakpointMediumMin,
SbbBreakpointSmallMax,
SbbTypoScaleDefault,
} from '@sbb-esta/lyne-design-tokens';
import { isServer, type ReactiveController, type ReactiveControllerHost } from 'lit';

const pxToRem = (px: number): number => px / SbbTypoScaleDefault;

/* eslint-disable @typescript-eslint/naming-convention */
export const SbbMediaQueryForcedColors = '(forced-colors: active)';
export const SbbMediaQueryHover = '(any-hover: hover)';
export const SbbMediaQueryPointerCoarse = '(pointer: coarse)';
export const SbbMediaQueryBreakpointMediumAndAbove = `(min-width: ${SbbBreakpointMediumMin})`;
export const SbbMediaQueryBreakpointSmallAndBelow = `(max-width: ${SbbBreakpointSmallMax})`;
export const SbbMediaQueryBreakpointMediumAndAbove = `(min-width: ${pxToRem(SbbBreakpointMediumMin)}rem)`;
export const SbbMediaQueryBreakpointSmallAndBelow = `(max-width: ${pxToRem(SbbBreakpointSmallMax)}rem)`;
/* eslint-enable @typescript-eslint/naming-convention */

/**
Expand Down
6 changes: 3 additions & 3 deletions src/elements/core/dom/breakpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
*
* @param from The breakpoint corresponding to the `min-width` value of the media query (optional).
* @param to The breakpoint corresponding to the `max-width` value of the media query (optional).
* @param properties Whether the max breakpoint should be included
* @returns A boolean indicating whether the window matches the breakpoint.
*/
export function isBreakpoint(
from?: Breakpoint,
to?: Breakpoint,
properties?: { includeMaxBreakpoint: boolean },
): boolean {
): boolean | null {
if (isServer) {
// TODO: Remove and decide case by case what should be done on consuming end
return false;
return null;

Check warning on line 21 in src/elements/core/dom/breakpoint.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/core/dom/breakpoint.ts#L21

Added line #L21 was not covered by tests
}

const computedStyle = getComputedStyle(document.documentElement);
Expand Down
2 changes: 1 addition & 1 deletion src/elements/dialog/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
const hideHeader =
typeof hideOnScroll === 'boolean'
? hideOnScroll
: isBreakpoint('zero', hideOnScroll, { includeMaxBreakpoint: true });
: (isBreakpoint('zero', hideOnScroll, { includeMaxBreakpoint: true }) ?? false);

Check warning on line 272 in src/elements/dialog/dialog/dialog.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/dialog/dialog/dialog.ts#L272

Added line #L272 was not covered by tests
this.toggleAttribute('data-hide-header', !hideHeader ? false : value);
if (this._dialogTitleElement) {
this._dialogTitleElement.toggleAttribute('data-hide-header', !hideHeader ? false : value);
Expand Down
4 changes: 1 addition & 3 deletions src/elements/image/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ const eventListenerOptions = {
passive: true,
};

const pxToRem = (px: number): number => {
return px / SbbTypoScaleDefault;
};
const pxToRem = (px: number): number => px / SbbTypoScaleDefault;

const breakpointMap: Record<string, number> = {
'sbb-breakpoint-zero-min': pxToRem(SbbBreakpointZeroMin),
Expand Down
4 changes: 2 additions & 2 deletions src/elements/menu/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
} from '../../core/a11y.js';
import { SbbOpenCloseBaseElement } from '../../core/base-elements.js';
import {
SbbMediaQueryBreakpointSmallAndBelow,
SbbConnectedAbortController,
SbbInertController,
SbbMediaMatcherController,
SbbMediaQueryBreakpointSmallAndBelow,
} from '../../core/controllers.js';
import { forceType } from '../../core/decorators.js';
import { findReferencedElement, SbbScrollHandler } from '../../core/dom.js';
Expand Down Expand Up @@ -124,7 +124,7 @@ class SbbMenuElement extends SbbNamedSlotListMixin<
this._setMenuPosition();
this._triggerElement?.setAttribute('aria-expanded', 'true');

// Starting from breakpoint medium, disable scroll
// From zero to medium, disable scroll
if (this._mediaMatcher.matches(SbbMediaQueryBreakpointSmallAndBelow)) {
this._scrollHandler.disableScroll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class SbbNavigationSectionElement extends SbbUpdateSchedulerMixin(LitElement) {
}

private _isZeroToLargeBreakpoint(): boolean {
return isBreakpoint('zero', 'large');
return isBreakpoint('zero', 'large') ?? false;
}

// Closes the navigation on "Esc" key pressed.
Expand Down
Loading