Initial version

This commit is contained in:
trading_peter 2022-03-12 23:07:56 +01:00
parent 5506f6e0fa
commit 8771a7188b
4 changed files with 72 additions and 39 deletions

View File

@ -1 +1 @@
# tp-element # tp-media-query

View File

@ -1,14 +1,14 @@
{ {
"name": "@tp/tp-element", "name": "@tp/tp-media-query",
"version": "0.0.1", "version": "0.0.1",
"description": "", "description": "",
"main": "tp-element.js", "main": "tp-media-query.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "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", "author": "trading_peter",
"license": "Apache-2.0", "license": "Apache-2.0",

View File

@ -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
View 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);