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

refactor(cxl-ui): use @lit/context for media queries #356

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/cxl-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"@conversionxl/normalize-wheel": "^1.0.1",
"@lit/context": "^1.0.1",
"@vaadin/accordion": "^23.3.7",
"@vaadin/button": "^23.3.7",
"@vaadin/checkbox": "^23.3.7",
Expand Down
25 changes: 22 additions & 3 deletions packages/cxl-ui/src/components/cxl-app-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import '@conversionxl/cxl-lumo-styles';
import { registerGlobalStyles } from '@conversionxl/cxl-lumo-styles/src/utils';
import normalizeWheel from '@conversionxl/normalize-wheel';
import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
import { provide } from '@lit/context';
import cxlAppLayoutStyles from '../styles/cxl-app-layout-css.js';
import cxlAppLayoutGlobalStyles from '../styles/global/cxl-app-layout-css.js';
import '@vaadin/button';
import { mediaContext } from '../media-context.js';

const ASIDE_LOCAL_STORAGE_KEY = 'cxl-app-layout-aside-opened';

Expand Down Expand Up @@ -54,14 +56,21 @@ export class CXLAppLayoutElement extends LitElement {
@property()
layout = '1c';

// MediaQueryController.
@property({ type: Boolean, reflect: true })
wide;
// @see https://github.com/vaadin/web-components/blob/de3db720ec8448a26d2f84d00965a9e369a1c3fb/packages/select/src/vaadin-select.js#L297
_phoneMediaQuery = '(max-width: 568px), (max-height: 568px)';

// @TODO: Improve media query using shared state, possibly @lit/Context (https://lit.dev/docs/data/context/)
// Device Detector media query.
_wideMediaQuery = '(min-width: 528px)';

@provide({ context: mediaContext })
@property()
_phone = false;

@provide({ context: mediaContext })
@property()
_wide = false;

static get styles() {
return [cxlAppLayoutStyles];
}
Expand Down Expand Up @@ -116,9 +125,19 @@ export class CXLAppLayoutElement extends LitElement {

this.addController(
new MediaQueryController(this._wideMediaQuery, (matches) => {
if (this.wide !== undefined && this.wide !== matches) {
window.location.reload();
}

this.wide = matches;
})
);

this.addController(
new MediaQueryController(this._phoneMediaQuery, (matches) => {
this._phone = matches;
})
);
}

updated(changedProperties) {
Expand Down
33 changes: 8 additions & 25 deletions packages/cxl-ui/src/components/cxl-marketing-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { customElement, property, query } from 'lit/decorators.js';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import '@conversionxl/cxl-lumo-styles';
import { registerGlobalStyles } from '@conversionxl/cxl-lumo-styles/src/utils';
import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
import { throttle } from 'lodash-es';
import { consume } from '@lit/context';
import cxlMarketingNavStyles from '../styles/cxl-marketing-nav-css.js';
import cxlMarketingNavGlobalStyles from '../styles/global/cxl-marketing-nav-css.js';
import '@vaadin/button';
import '@vaadin/tabs';
import '@vaadin/context-menu';
import { mediaContext } from '../media-context.js';

@customElement('cxl-marketing-nav')
export class CXLMarketingNavElement extends LitElement {
Expand Down Expand Up @@ -40,19 +41,17 @@ export class CXLMarketingNavElement extends LitElement {
@property({ type: Boolean, reflect: true })
minimal = false;

// MediaQueryController.
@property({ type: Boolean, reflect: true })
@consume({ context: mediaContext, subscribe: true })
@property({ type: Boolean })
_phone;

@consume({ context: mediaContext, subscribe: true })
@property({ type: Boolean })
wide;

// Device Detector media query.
_wideMediaQuery = '(min-width: 568px)';

@property({ type: Boolean, reflect: true })
_phone;

// @see https://github.com/vaadin/web-components/blob/de3db720ec8448a26d2f84d00965a9e369a1c3fb/packages/select/src/vaadin-select.js#L297
_phoneMediaQuery = '(max-width: 568px), (max-height: 568px)';

@property({ type: HTMLElement })
submenuOverlay = null;

Expand Down Expand Up @@ -123,22 +122,6 @@ export class CXLMarketingNavElement extends LitElement {
connectedCallback() {
super.connectedCallback();

this.addController(
new MediaQueryController(this._wideMediaQuery, (matches) => {
if (this.wide !== undefined && this.wide !== matches) {
window.location.reload();
}

this.wide = matches;
})
);

this.addController(
new MediaQueryController(this._phoneMediaQuery, (matches) => {
this._phone = matches;
})
);

this._boundOnOverlayOpen = this._onOverlayOpen.bind(this);
const overlaysWrapper = document.createElement('div');
overlaysWrapper.id = 'overlays-wrapper';
Expand Down
3 changes: 3 additions & 0 deletions packages/cxl-ui/src/media-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createContext } from '@lit/context';

export const mediaContext = createContext(Symbol('media-context'));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be a more specific mediaQueryContext maybe?

"Media" alone seems like could be related to video, or whatnot more broad concept.

19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2365,6 +2365,18 @@
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.1.tgz#64df34e2f12e68e78ac57e571d25ec07fa460ca9"
integrity sha512-kXOeFbfCm4fFf2A3WwVEeQj55tMZa8c8/f9AKHMobQMkzNUfUj+antR3fRPaZJawsa1aZiP/Da3ndpZrwEe4rQ==

"@lit-labs/ssr-dom-shim@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.2.tgz#d693d972974a354034454ec1317eb6afd0b00312"
integrity sha512-jnOD+/+dSrfTWYfSXBXlo5l5f0q1UuJo3tkbMDCYA2lKUYq79jaxqtGEvnRoh049nt1vdo1+45RinipU6FGY2g==

"@lit/context@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@lit/context/-/context-1.0.1.tgz#4f4293096d4a1c9386f4ef7c0dbadf418ab4ece6"
integrity sha512-ujQEV42vRjhNzCu/YNc+dy8eq48cEYO2SvEl7iCmMzt8X7ixUYcDt8DPV1UBr58FJ7EUe9vTLPv/0soAi9bfRw==
dependencies:
"@lit/reactive-element" "^1.6.2 || ^2.0.0"

"@lit/reactive-element@^1.1.0":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@lit/reactive-element/-/reactive-element-1.6.3.tgz#25b4eece2592132845d303e091bad9b04cdcfe03"
Expand All @@ -2379,6 +2391,13 @@
dependencies:
"@lit-labs/ssr-dom-shim" "^1.0.0"

"@lit/reactive-element@^1.6.2 || ^2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@lit/reactive-element/-/reactive-element-2.0.1.tgz#b17d8818d9c72ccc489f44e35d87cfa18a9c8c93"
integrity sha512-eu50SQXHRthFwWJMp0oAFg95Rvm6MTPjxSXWuvAu7It90WVFLFpNBoIno7XOXSDvVgTrtKnUV4OLJqys2Svn4g==
dependencies:
"@lit-labs/ssr-dom-shim" "^1.1.2"

"@mdx-js/mdx@^1.6.22":
version "1.6.22"
resolved "https://registry.yarnpkg.com/@mdx-js/mdx/-/mdx-1.6.22.tgz#8a723157bf90e78f17dc0f27995398e6c731f1ba"
Expand Down
Loading