Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New component: ha-expansion-panel #6789

Merged
merged 25 commits into from
Oct 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions src/components/ha-expansion-panel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {
css,
CSSResult,
customElement,
html,
LitElement,
property,
query,
TemplateResult,
} from "lit-element";
import { fireEvent } from "../common/dom/fire_event";
import "./ha-svg-icon";
import { mdiChevronDown } from "@mdi/js";
import { classMap } from "lit-html/directives/class-map";

@customElement("ha-expansion-panel")
class HaExpansionPanel extends LitElement {
@property({ type: Boolean, reflect: true }) expanded = false;

@property({ type: Boolean, reflect: true }) outlined = false;

@query(".container") private _container!: HTMLDivElement;

protected render(): TemplateResult {
return html`
<div class="summary" @click=${this._toggleContainer}>
<slot name="title"></slot>
<ha-svg-icon
.path=${mdiChevronDown}
class="summary-icon ${classMap({ expanded: this.expanded })}"
></ha-svg-icon>
</div>
<div
class="container ${classMap({ expanded: this.expanded })}"
@transitionend=${this._handleTransitionEnd}
>
<slot></slot>
</div>
`;
}

private _handleTransitionEnd() {
this._container.style.removeProperty("height");
}

private _toggleContainer(): void {
const scrollHeight = this._container.scrollHeight;
this._container.style.height = `${scrollHeight}px`;

if (this.expanded) {
setTimeout(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this so it can transition from the current height to 0? Transition from auto to 0 doesn't work probably?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's why I'm setting the fixed height and in setTimeout I'm setting the height to 0.
Is there a way to remove setTimeout?

this._container.style.height = "0px";
}, 0);
}

this.expanded = !this.expanded;
fireEvent(this, "expanded-changed", { expanded: this.expanded });
}

static get styles(): CSSResult {
return css`
:host {
display: block;
}

:host([outlined]) {
box-shadow: none;
border-width: 1px;
border-style: solid;
border-color: var(
--ha-card-border-color,
var(--divider-color, #e0e0e0)
);
border-radius: var(--ha-card-border-radius, 4px);
}

.summary {
display: flex;
padding: 0px 16px;
min-height: 48px;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually its this that is too large that is making it not close enough to the content

align-items: center;
cursor: pointer;
overflow: hidden;
}

.summary-icon {
transition: transform 150ms cubic-bezier(0.4, 0, 0.2, 1);
margin-left: auto;
}

.summary-icon.expanded {
transform: rotate(180deg);
}

.container {
overflow: hidden;
transition: height 300ms cubic-bezier(0.4, 0, 0.2, 1);
height: 0px;
}

.container.expanded {
height: auto;
}
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-expansion-panel": HaExpansionPanel;
}

// for fire event
interface HASSDomEvents {
"expanded-changed": {
expanded: boolean;
};
}
}