class ScrollingTextCard extends HTMLElement {
setConfig(config) {
if (!config.entity) {
throw new Error('You need to define an entity');
}
const root = this.attachShadow({ mode: 'open' });
const cardConfig = Object.assign({}, config);
const style = document.createElement('style');
style.textContent = `
.scrolling-text-container {
width: 100%; /* Set the width of the container */
height: 50px; /* Set the height of the container */
overflow: hidden; /* Hide overflowing content */
position: relative; /* Position relative to allow absolute positioning of the scrolling text */
}
.scrolling-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
animation: scroll-left 15s linear infinite;
position: absolute; /* Position the text absolutely within the container */
top: 0; /* Position the text at the top of the container */
left: 0; /* Position the text at the left of the container */
}
@keyframes scroll-left {
0% { transform: translateX(100%); }
100% { transform: translateX(-100%); }
}
`;
root.appendChild(style);
const container = document.createElement('div');
container.className = 'scrolling-text-container';
root.appendChild(container);
const textElement = document.createElement('div');
textElement.className = 'scrolling-text';
container.appendChild(textElement);
this._config = cardConfig;
this._root = root;
this._textElement = textElement;
}
set hass(hass) {
const entityId = this._config.entity.split('.')[1];
const state = hass.states[this._config.entity];
this._textElement.textContent = state ? state.state : 'Entity not found';
}
disconnectedCallback() {
this._root.innerHTML = '';
}
}
customElements.define('scrolling-text-card', ScrollingTextCard);