Initial version
This commit is contained in:
parent
5506f6e0fa
commit
8771a7188b
@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "@tp/tp-element",
|
||||
"name": "@tp/tp-media-query",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "tp-element.js",
|
||||
"main": "tp-media-query.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://gitea.codeblob.work/tp-elements/tp-element.git"
|
||||
"url": "https://gitea.codeblob.work/tp-elements/tp-media-query.git"
|
||||
},
|
||||
"author": "trading_peter",
|
||||
"license": "Apache-2.0",
|
||||
|
@ -1,35 +0,0 @@
|
||||
/**
|
||||
@license
|
||||
Copyright (c) 2022 trading_peter
|
||||
This program is available under Apache License Version 2.0
|
||||
*/
|
||||
|
||||
import { LitElement, html, css } from 'lit';
|
||||
|
||||
class TpElement extends LitElement {
|
||||
static get styles() {
|
||||
return [
|
||||
css`
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
`
|
||||
];
|
||||
}
|
||||
|
||||
render() {
|
||||
const { } = this;
|
||||
|
||||
return html`
|
||||
|
||||
`;
|
||||
}
|
||||
|
||||
static get properties() {
|
||||
return { };
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
window.customElements.define('tp-element', TpElement);
|
68
tp-media-query.js
Normal file
68
tp-media-query.js
Normal file
@ -0,0 +1,68 @@
|
||||
/**
|
||||
@license
|
||||
Copyright (c) 2022 trading_peter
|
||||
This program is available under Apache License Version 2.0
|
||||
*/
|
||||
|
||||
import { LitElement, css } from 'lit';
|
||||
|
||||
class TpMediaQuery extends LitElement {
|
||||
static get styles() {
|
||||
return [
|
||||
css`
|
||||
:host {
|
||||
display: none;
|
||||
}
|
||||
`
|
||||
];
|
||||
}
|
||||
|
||||
static get properties() {
|
||||
return {
|
||||
query: { type: String },
|
||||
};
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._boundQueryHandler = this.queryHandler.bind(this);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
this.checkQuery();
|
||||
}
|
||||
|
||||
checkQuery() {
|
||||
this._remove();
|
||||
var query = this.query;
|
||||
if (!query) {
|
||||
return;
|
||||
}
|
||||
if (query[0] !== '(') {
|
||||
query = '(' + query + ')';
|
||||
}
|
||||
this._queryMatch = window.matchMedia(query);
|
||||
this._add();
|
||||
this.queryHandler(this._queryMatch);
|
||||
}
|
||||
|
||||
_add() {
|
||||
if (this._queryMatch) {
|
||||
this._queryMatch.addEventListener('change', this._boundQueryHandler);
|
||||
}
|
||||
}
|
||||
|
||||
_remove() {
|
||||
if (this._queryMatch) {
|
||||
this._queryMatch.removeEventListener('change', this._boundQueryHandler);
|
||||
}
|
||||
this._queryMatch = null;
|
||||
}
|
||||
|
||||
queryHandler(mq) {
|
||||
this.dispatchEvent(new CustomEvent('media-query-update', { detail: mq.matches, bubbles: true, composed: true }));
|
||||
}
|
||||
}
|
||||
|
||||
window.customElements.define('tp-media-query', TpMediaQuery);
|
Loading…
Reference in New Issue
Block a user