Improve styling for track cards
This commit is contained in:
parent
a2ba99d600
commit
df83066303
|
@ -1,5 +1,6 @@
|
|||
import { TrackInfo } from "../client";
|
||||
|
||||
/*
|
||||
export class TrackName extends HTMLElement {
|
||||
container: HTMLElement;
|
||||
|
||||
|
@ -32,24 +33,59 @@ export class TrackName extends HTMLElement {
|
|||
this.appendChild(this.container);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
export class TrackCard extends HTMLElement {
|
||||
export class TextField extends HTMLElement {
|
||||
static get observedAttributes() {
|
||||
return ["id", "trackNumber", "name", "album", "artist", "duration"];
|
||||
return ["text"];
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
attributeChangeCallback(
|
||||
attrName: string,
|
||||
oldValue: string,
|
||||
newValue: string
|
||||
): void {
|
||||
if (newValue !== oldValue) {
|
||||
this.updateContent();
|
||||
get text(): string | null {
|
||||
return this.getAttribute("text");
|
||||
}
|
||||
|
||||
set text(text: string | null) {
|
||||
if (text) {
|
||||
this.setAttribute("text", text);
|
||||
this.innerHTML = text;
|
||||
} else {
|
||||
this.removeAttribute("text");
|
||||
this.innerHTML = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class TrackCard extends HTMLElement {
|
||||
trackNumberContainer: TextField;
|
||||
nameContainer: TextField;
|
||||
albumContainer: TextField;
|
||||
artistContainer: TextField;
|
||||
durationContainer: TextField;
|
||||
|
||||
static get observedAttributes() {
|
||||
return ["id", "trackNumber", "name", "album", "artist", "duration"];
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.trackNumberContainer = document.createElement("text-field");
|
||||
|
||||
this.nameContainer = document.createElement("text-field");
|
||||
this.nameContainer.classList.add("track-card__name");
|
||||
|
||||
this.albumContainer = document.createElement("text-field");
|
||||
this.albumContainer.classList.add("track-card__album");
|
||||
|
||||
this.artistContainer = document.createElement("text-field");
|
||||
this.artistContainer.classList.add("track-card__artist");
|
||||
|
||||
this.durationContainer = document.createElement("text-field");
|
||||
this.durationContainer.classList.add("track-card__duration");
|
||||
}
|
||||
|
||||
get name(): string | null {
|
||||
|
@ -59,10 +95,11 @@ export class TrackCard extends HTMLElement {
|
|||
set name(name: string | null) {
|
||||
if (name) {
|
||||
this.setAttribute("name", name);
|
||||
this.nameContainer.text = name;
|
||||
} else {
|
||||
this.removeAttribute("open");
|
||||
this.nameContainer.text = null;
|
||||
}
|
||||
this.updateContent();
|
||||
}
|
||||
|
||||
get artist(): string | null {
|
||||
|
@ -72,10 +109,11 @@ export class TrackCard extends HTMLElement {
|
|||
set artist(artist: string | null) {
|
||||
if (artist) {
|
||||
this.setAttribute("artist", artist);
|
||||
this.artistContainer.text = artist;
|
||||
} else {
|
||||
this.removeAttribute("open");
|
||||
this.artistContainer.text = null;
|
||||
}
|
||||
this.updateContent();
|
||||
}
|
||||
|
||||
get album(): string | null {
|
||||
|
@ -85,53 +123,34 @@ export class TrackCard extends HTMLElement {
|
|||
set album(album: string | null) {
|
||||
if (album) {
|
||||
this.setAttribute("album", album);
|
||||
this.albumContainer.text = album;
|
||||
} else {
|
||||
this.removeAttribute("open");
|
||||
this.albumContainer.text = null;
|
||||
}
|
||||
this.updateContent();
|
||||
}
|
||||
|
||||
get length(): string | null {
|
||||
return this.getAttribute("length");
|
||||
get duration(): string | null {
|
||||
return this.getAttribute("duration");
|
||||
}
|
||||
|
||||
set length(length: string | null) {
|
||||
if (length) {
|
||||
this.setAttribute("length", length);
|
||||
set duration(duration: string | null) {
|
||||
if (duration) {
|
||||
this.setAttribute("duration", duration);
|
||||
this.durationContainer.text = duration;
|
||||
} else {
|
||||
this.removeAttribute("open");
|
||||
this.durationContainer.text = null;
|
||||
}
|
||||
this.updateContent();
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.updateContent();
|
||||
}
|
||||
this.classList.add("track-card");
|
||||
|
||||
updateContent() {
|
||||
const container = document.createElement("div");
|
||||
container.classList.add("track-card");
|
||||
this.innerHTML = "";
|
||||
|
||||
this.appendChild(container);
|
||||
|
||||
while (container.lastChild) {
|
||||
container.removeChild(container.lastChild);
|
||||
}
|
||||
|
||||
if (this["name"]) {
|
||||
const trackName = document.createElement("track-name");
|
||||
trackName.name = this["name"];
|
||||
container.appendChild(trackName);
|
||||
}
|
||||
if (this["length"]) {
|
||||
container.appendChild(document.createTextNode(this["length"]));
|
||||
}
|
||||
if (this["album"]) {
|
||||
container.appendChild(document.createTextNode(this["album"]));
|
||||
}
|
||||
if (this["artist"]) {
|
||||
container.appendChild(document.createTextNode(this["artist"]));
|
||||
}
|
||||
// this.appendChild(this.trackNumberContainer);
|
||||
this.appendChild(this.nameContainer);
|
||||
this.appendChild(this.albumContainer);
|
||||
this.appendChild(this.artistContainer);
|
||||
this.appendChild(this.durationContainer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import * as _ from "lodash";
|
||||
import { TrackInfo, getTracks } from "./client";
|
||||
import { TrackName, TrackCard } from "./components/track";
|
||||
import { TextField, TrackCard } from "./components/track";
|
||||
|
||||
window.customElements.define("track-name", TrackName);
|
||||
window.customElements.define("text-field", TextField);
|
||||
window.customElements.define("track-card", TrackCard);
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"track-name": TrackName;
|
||||
"text-field": TextField;
|
||||
"track-card": TrackCard;
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ const updateTrackList = (tracks: TrackInfo[]) => {
|
|||
card.name = info.name || null;
|
||||
card.album = info.album || null;
|
||||
card.artist = info.artist || null;
|
||||
card.length = (info.duration && `${info.duration}`) || null;
|
||||
card.duration = (info.duration && `${info.duration}`) || null;
|
||||
return card;
|
||||
});
|
||||
_.map(track_formats, (trackCard) => {
|
||||
|
|
|
@ -29,33 +29,37 @@ body {
|
|||
margin-top: 32px;
|
||||
}
|
||||
|
||||
/*
|
||||
.track-list__row:nth-child(even) {
|
||||
background-color: rgb(255, 255, 255);
|
||||
}
|
||||
|
||||
.track-list__row:nth-child(odd) {
|
||||
background-color: rgb(200, 200, 200);
|
||||
}
|
||||
*/
|
||||
|
||||
.track-card {
|
||||
border: 1px solid black;
|
||||
border-radius: 5px;
|
||||
padding: 8px;
|
||||
width: 300px;
|
||||
height: 100px;
|
||||
display: grid;
|
||||
gap: 4px 4px;
|
||||
}
|
||||
|
||||
.track-card__name {
|
||||
display: block;
|
||||
grid-column: 1 / span 1;
|
||||
grid-row: 1 / span 1;
|
||||
}
|
||||
|
||||
.track-card__length {
|
||||
display: block;
|
||||
grid-column: 1 / span 1;
|
||||
grid-row: 2 / span 1;
|
||||
}
|
||||
|
||||
.track-card__album {
|
||||
display: block;
|
||||
grid-column: 2 / span 1;
|
||||
grid-row: 1 / span 1;
|
||||
}
|
||||
|
||||
.track-card__artist {
|
||||
display: block;
|
||||
grid-column: 2 / span 1;
|
||||
grid-row: 2 / span 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue