This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/src/elements/buttons/ModalButton.ts

103 lines
3.4 KiB
TypeScript
Raw Normal View History

import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
import PFButton from "@patternfly/patternfly/components/Button/button.css";
import PFModalBox from "@patternfly/patternfly/components/ModalBox/modal-box.css";
import PFForm from "@patternfly/patternfly/components/Form/form.css";
import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css";
import PFBullseye from "@patternfly/patternfly/layouts/Bullseye/bullseye.css";
import PFBackdrop from "@patternfly/patternfly/components/Backdrop/backdrop.css";
import PFPage from "@patternfly/patternfly/components/Page/page.css";
import PFStack from "@patternfly/patternfly/layouts/Stack/stack.css";
import PFCard from "@patternfly/patternfly/components/Card/card.css";
import PFTitle from "@patternfly/patternfly/components/Title/title.css";
import PFContent from "@patternfly/patternfly/components/Content/content.css";
import AKGlobal from "../../authentik.css";
import { PFSize } from "../Spinner";
export const MODAL_BUTTON_STYLES = css`
:host {
text-align: left;
font-size: var(--pf-global--FontSize--md);
}
.pf-c-modal-box.pf-m-lg {
overflow-y: auto;
}
.pf-c-modal-box > .pf-c-button + * {
margin-right: 0;
}
/* fix multiple selects height */
select[multiple] {
height: 15em;
}
`;
2020-12-05 21:08:42 +00:00
@customElement("ak-modal-button")
export class ModalButton extends LitElement {
@property()
size: PFSize = PFSize.Large;
2020-12-02 14:44:40 +00:00
@property({type: Boolean})
2020-12-01 08:15:41 +00:00
open = false;
static get styles(): CSSResult[] {
return [PFBase, PFButton, PFModalBox, PFForm, PFTitle, PFFormControl, PFBullseye, PFBackdrop, PFPage, PFStack, PFCard, PFContent, AKGlobal, MODAL_BUTTON_STYLES];
}
2020-11-21 14:24:45 +00:00
constructor() {
super();
2020-11-21 19:48:49 +00:00
window.addEventListener("keyup", (e) => {
2020-11-21 14:24:45 +00:00
if (e.code === "Escape") {
this.resetForms();
2020-11-21 14:24:45 +00:00
this.open = false;
}
});
}
resetForms(): void {
this.querySelectorAll<HTMLFormElement>("[slot=form]").forEach(form => {
form.reset();
});
}
onClick(): void {
this.open = true;
this.querySelectorAll("*").forEach(child => {
if ("requestUpdate" in child) {
(child as LitElement).requestUpdate();
}
2020-11-26 22:35:59 +00:00
});
}
renderModalInner(): TemplateResult {
return html`<slot name='modal'></slot>`;
}
renderModal(): TemplateResult {
return html`<div class="pf-c-backdrop">
<div class="pf-l-bullseye">
2020-11-21 19:48:49 +00:00
<div
class="pf-c-modal-box ${this.size}"
2020-11-21 19:48:49 +00:00
role="dialog"
aria-modal="true"
>
<button
@click=${() => (this.open = false)}
class="pf-c-button pf-m-plain"
type="button"
aria-label="Close dialog"
>
<i class="fas fa-times" aria-hidden="true"></i>
</button>
${this.renderModalInner()}
</div>
</div>
</div>`;
}
render(): TemplateResult {
return html` <slot name="trigger" @click=${() => this.onClick()}></slot>
${this.open ? this.renderModal() : ""}`;
}
}