Add grid layout options. Bugfixes
This commit is contained in:
parent
5d2842f934
commit
c2808b9396
51
README.md
51
README.md
@ -27,6 +27,8 @@ column_width: <column_width>
|
|||||||
max_width: <max_width>
|
max_width: <max_width>
|
||||||
min_width: <min_width>
|
min_width: <min_width>
|
||||||
flex_grow: <flex_grow>
|
flex_grow: <flex_grow>
|
||||||
|
gridcols: <grid-cols>
|
||||||
|
gridrows: <grid-rows>
|
||||||
justify_content: <justify_content>
|
justify_content: <justify_content>
|
||||||
rtl: <rtl>
|
rtl: <rtl>
|
||||||
cards:
|
cards:
|
||||||
@ -38,12 +40,13 @@ card_options:
|
|||||||
## Options
|
## Options
|
||||||
- `<cards>` **Required** A list of lovelace cards to display.
|
- `<cards>` **Required** A list of lovelace cards to display.
|
||||||
- `<card_options>` are options that are applied to all cards.
|
- `<card_options>` are options that are applied to all cards.
|
||||||
- `<layout>` The layout method to use. `auto`, `vertical` or `horizontal`. See below. Default: `auto`.
|
- `<layout>` The layout method to use. `auto`, `vertical`, `horizontal` or `grid`. See below. Default: `auto`.
|
||||||
- `<min_height>` The minimum length of a column in `auto` layout. Default: `5`.
|
- `<min_height>` The minimum length of a column in `auto` layout. Default: `5`.
|
||||||
- `<min_columns>` The minimum number of columns to use. Default: `1`.
|
- `<min_columns>` The minimum number of columns to use. Default: `1`.
|
||||||
- `<max_columns>` The maximum number of columns to use. Default: `100`.
|
- `<max_columns>` The maximum number of columns to use. Default: `100`.
|
||||||
- `<column_width>` Width of columns. Default: `300px`.
|
- `<column_width>` Width of columns. Default: `300px`.
|
||||||
- `<max_width>`, `<min_width>`, `<flex_grow>` Set the `max-width`, `min-width` and `flex-grow` CSS properties of the columns manually. Default: `column_width or 500px`, `undefined`, `undefined`.
|
- `<max_width>`, `<min_width>`, `<flex_grow>` Set the `max-width`, `min-width` and `flex-grow` CSS properties of the columns manually. Default: `column_width or 500px`, `undefined`, `undefined`.
|
||||||
|
- `<grid-rows>`, `<grid-col>` Set the `grid-template-rows` and `grid-template-columns` CSS properties when using `layout: grid`.
|
||||||
- `<justify_content>` Set the `justify-content` CSS property of the column container. Default: `center`.
|
- `<justify_content>` Set the `justify-content` CSS property of the column container. Default: `center`.
|
||||||
|
|
||||||
## Layouts
|
## Layouts
|
||||||
@ -180,6 +183,52 @@ cards:
|
|||||||
```
|
```
|
||||||

|

|
||||||
|
|
||||||
|
### `grid` layout (experimental)
|
||||||
|
|
||||||
|
For maximum controll, you can place every card manually in a [CSS grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by using the `grid` layout.
|
||||||
|
|
||||||
|
To do this, you need to specify `gridrows` and `gridcols` with the settings for `grid-template-rows` and `grid-template-columns` repectively **and** also add `gridcol:` and `gridrow:` for *each card* with the settings for `grid-column` and `grid-row` respectively.
|
||||||
|
|
||||||
|
> Hint: This may look better if you also have [card-mod](https://github.com/thomasloven/lovelace-card-mod) and set the card heights to `100 %`.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
type: custom:layout-card
|
||||||
|
layout: vertical
|
||||||
|
column_width: 100%
|
||||||
|
cards:
|
||||||
|
- type: markdown
|
||||||
|
content: "# Grid"
|
||||||
|
- type: custom:layout-card
|
||||||
|
layout: grid
|
||||||
|
gridrows: 180px 200px auto
|
||||||
|
gridcols: 180px auto 180px
|
||||||
|
cards:
|
||||||
|
- type: glance
|
||||||
|
entities:
|
||||||
|
- sun.sun
|
||||||
|
gridrow: 1 / 2
|
||||||
|
gridcol: 1 / 2
|
||||||
|
style: "ha-card { height: 100%; }"
|
||||||
|
- type: entities
|
||||||
|
entities: &ents
|
||||||
|
- light.bed_light
|
||||||
|
- light.kitchen_lights
|
||||||
|
- light.ceiling_lights
|
||||||
|
gridrow: 1 / 3
|
||||||
|
gridcol: 2 / 4
|
||||||
|
style: "ha-card { height: 100%; }"
|
||||||
|
- type: markdown
|
||||||
|
content: test
|
||||||
|
gridrow: 2 / 4
|
||||||
|
gridcol: 1 / 2
|
||||||
|
style: "ha-card { height: 100%; }"
|
||||||
|
- type: entities
|
||||||
|
entities: *ents
|
||||||
|
gridrow: 3 / 4
|
||||||
|
gridcol: 2 / 3
|
||||||
|
```
|
||||||
|

|
||||||
|
|
||||||
## Tweaking layouts
|
## Tweaking layouts
|
||||||
|
|
||||||
- First of all `<min_columns>` and `<max_columns>` options, which can be used to force the number of columns displayed:
|
- First of all `<min_columns>` and `<max_columns>` options, which can be used to force the number of columns displayed:
|
||||||
|
File diff suppressed because one or more lines are too long
@ -79,6 +79,7 @@ export function buildLayout(cards, width, config) {
|
|||||||
for (let i = 0; i < colnum; i++) {
|
for (let i = 0; i < colnum; i++) {
|
||||||
const newCol = document.createElement("div");
|
const newCol = document.createElement("div");
|
||||||
newCol.classList.add("column");
|
newCol.classList.add("column");
|
||||||
|
newCol.classList.add("cards");
|
||||||
newCol.length = 0;
|
newCol.length = 0;
|
||||||
cols.push(newCol);
|
cols.push(newCol);
|
||||||
}
|
}
|
||||||
|
36
src/main.js
36
src/main.js
@ -1,5 +1,6 @@
|
|||||||
import { LitElement, html, css } from "card-tools/src/lit-element";
|
import { LitElement, html, css } from "card-tools/src/lit-element";
|
||||||
import "card-tools/src/card-maker";
|
import "card-tools/src/card-maker";
|
||||||
|
import { createCard } from "card-tools/src/lovelace-element";
|
||||||
import { hass } from "card-tools/src/hass";
|
import { hass } from "card-tools/src/hass";
|
||||||
|
|
||||||
import {buildLayout} from "./layout";
|
import {buildLayout} from "./layout";
|
||||||
@ -62,13 +63,20 @@ class LayoutCard extends LitElement {
|
|||||||
async build_card(c) {
|
async build_card(c) {
|
||||||
if(c === "break")
|
if(c === "break")
|
||||||
return null;
|
return null;
|
||||||
const el = document.createElement("card-maker");
|
const config = {...c, ...this._config.card_options};
|
||||||
el.config = {...c, ...this._config.card_options};
|
const el = createCard(config);
|
||||||
el.hass = hass();
|
el.hass = hass();
|
||||||
|
|
||||||
|
el.style.gridColumn = config.gridcol;
|
||||||
|
el.style.gridRow = config.gridrow;
|
||||||
// Cards are initially placed in the staging area
|
// Cards are initially placed in the staging area
|
||||||
// That places them in the DOM and lets us read their getCardSize() function
|
// That places them in the DOM and lets us read their getCardSize() function
|
||||||
this.shadowRoot.querySelector("#staging").appendChild(el);
|
this.shadowRoot.querySelector("#staging").appendChild(el);
|
||||||
return new Promise((resolve, reject) => el.updateComplete.then(() => resolve(el)));
|
return new Promise((resolve, reject) =>
|
||||||
|
el.updateComplete
|
||||||
|
? el.updateComplete.then(() => resolve(el))
|
||||||
|
: resolve(el)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async build_cards() {
|
async build_cards() {
|
||||||
@ -83,6 +91,8 @@ class LayoutCard extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
place_cards() {
|
place_cards() {
|
||||||
|
if(this._config.layout === "grid")
|
||||||
|
return;
|
||||||
const width = this.shadowRoot.querySelector("#columns").clientWidth;
|
const width = this.shadowRoot.querySelector("#columns").clientWidth;
|
||||||
|
|
||||||
this.columns = buildLayout(
|
this.columns = buildLayout(
|
||||||
@ -141,7 +151,7 @@ class LayoutCard extends LitElement {
|
|||||||
if(this.isPanel) return true;
|
if(this.isPanel) return true;
|
||||||
let el = this.parentElement;
|
let el = this.parentElement;
|
||||||
let steps = 10;
|
let steps = 10;
|
||||||
while(steps--) {
|
while(steps-- && el) {
|
||||||
if(el.localName === "hui-panel-view") return true;
|
if(el.localName === "hui-panel-view") return true;
|
||||||
if(el.localName === "div") return false;
|
if(el.localName === "div") return false;
|
||||||
el = el.parentElement;
|
el = el.parentElement;
|
||||||
@ -150,6 +160,15 @@ class LayoutCard extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
if(this._config.layout === "grid")
|
||||||
|
return html`
|
||||||
|
<div id="staging" class="grid cards"
|
||||||
|
style="
|
||||||
|
display: grid;
|
||||||
|
grid-template-rows: ${this._config.gridrows};
|
||||||
|
grid-template-columns: ${this._config.gridcols};
|
||||||
|
"></div>
|
||||||
|
`;
|
||||||
return html`
|
return html`
|
||||||
<div id="columns"
|
<div id="columns"
|
||||||
class="
|
class="
|
||||||
@ -190,18 +209,19 @@ class LayoutCard extends LitElement {
|
|||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
card-maker>* {
|
|
||||||
|
.cards>* {
|
||||||
display: block;
|
display: block;
|
||||||
margin: 4px 4px 8px;
|
margin: 4px 4px 8px;
|
||||||
}
|
}
|
||||||
card-maker:first-child>* {
|
.cards>*:first-child {
|
||||||
margin-top: 8px;
|
margin-top: 8px;
|
||||||
}
|
}
|
||||||
card-maker:last-child>* {
|
.cards>*:last-child {
|
||||||
margin-bottom: 4px;
|
margin-bottom: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#staging {
|
#staging:not(.grid) {
|
||||||
visibility: hidden;
|
visibility: hidden;
|
||||||
height: 0;
|
height: 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user