|
| 1 | +import { |
| 2 | + css, |
| 3 | + customElement, |
| 4 | + html, |
| 5 | + LitElement, |
| 6 | + property, |
| 7 | + TemplateResult, |
| 8 | +} from 'lit-element'; |
| 9 | +import { classMap } from 'lit-html/directives/class-map'; |
| 10 | + |
| 11 | +import '@material/mwc-icon'; |
| 12 | +import { nothing } from 'lit-html'; |
| 13 | + |
| 14 | +function closestTo<E extends Element>(node: Node, selector: string): E | null { |
| 15 | + const closest = |
| 16 | + node.nodeType === Node.ELEMENT_NODE |
| 17 | + ? (<Element>node).closest<E>(selector) |
| 18 | + : null; |
| 19 | + |
| 20 | + if (closest) return closest; |
| 21 | + |
| 22 | + const root = <Document | DocumentFragment>node.getRootNode(); |
| 23 | + |
| 24 | + if (root instanceof ShadowRoot) return closestTo(root.host, selector); |
| 25 | + |
| 26 | + return null; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A responsive container rendering actions in a header. |
| 31 | + * |
| 32 | + * The "action" slot may contain up to eight icon buttons. |
| 33 | + * The "icon" slot, if filled overrides the icon property. |
| 34 | + * The default slot will be rendered into the pane body in a single column. |
| 35 | + */ |
| 36 | +@customElement('action-pane') |
| 37 | +export class ActionPane extends LitElement { |
| 38 | + /** caption text, displayed in the header */ |
| 39 | + @property({ type: String }) |
| 40 | + label?: string; |
| 41 | + /** icon name, displayed unless the "icon" slot is filled */ |
| 42 | + @property({ type: String }) |
| 43 | + icon?: string; |
| 44 | + /** color header with secondary theme color while focus is within */ |
| 45 | + @property({ type: Boolean }) |
| 46 | + secondary = false; |
| 47 | + /** highlight pane with dotted outline */ |
| 48 | + @property({ type: Boolean }) |
| 49 | + highlighted = false; |
| 50 | + /** nesting level, default (closest pane ancestor's level) + 1 */ |
| 51 | + @property({ type: Number }) |
| 52 | + level = 1; |
| 53 | + |
| 54 | + async firstUpdated(): Promise<void> { |
| 55 | + this.tabIndex = 0; |
| 56 | + |
| 57 | + const parentPane = closestTo<ActionPane>(this.parentNode!, 'action-pane'); |
| 58 | + if (parentPane) this.level = parentPane.level + 1; |
| 59 | + |
| 60 | + this.level = Math.floor(this.level); |
| 61 | + } |
| 62 | + |
| 63 | + private renderHeader(): TemplateResult { |
| 64 | + const content = html`<span |
| 65 | + ><slot name="icon" |
| 66 | + >${this.icon |
| 67 | + ? html`<mwc-icon>${this.icon}</mwc-icon>` |
| 68 | + : nothing}</slot |
| 69 | + ></span |
| 70 | + > |
| 71 | + ${this.label ?? nothing} |
| 72 | + <nav><slot name="action"></slot></nav>`; |
| 73 | + |
| 74 | + const headingLevel = Math.floor(Math.max(this.level, 1)); |
| 75 | + // Sometimes a TemplateResult is passed in as Label, not a string. So only when it's a string show a title. |
| 76 | + const title = typeof this.label === 'string' ? this.label : ''; |
| 77 | + switch (headingLevel) { |
| 78 | + case 1: |
| 79 | + return html`<h1 title="${title}">${content}</h1>`; |
| 80 | + case 2: |
| 81 | + return html`<h2 title="${title}">${content}</h2>`; |
| 82 | + case 3: |
| 83 | + return html`<h3 title="${title}">${content}</h3>`; |
| 84 | + default: |
| 85 | + return html`<h4 title="${title}">${content}</h4>`; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + render(): TemplateResult { |
| 90 | + return html`<section |
| 91 | + class="${classMap({ |
| 92 | + secondary: this.secondary, |
| 93 | + highlighted: this.highlighted, |
| 94 | + contrasted: this.level % 2 === 0, |
| 95 | + })}" |
| 96 | + > |
| 97 | + ${this.renderHeader()} |
| 98 | + <div><slot></slot></div> |
| 99 | + </section>`; |
| 100 | + } |
| 101 | + |
| 102 | + static styles = css` |
| 103 | + :host { |
| 104 | + outline: none; |
| 105 | + } |
| 106 | +
|
| 107 | + :host(:focus-within) section { |
| 108 | + box-shadow: 0 8px 10px 1px rgba(0, 0, 0, 0.14), |
| 109 | + 0 3px 14px 2px rgba(0, 0, 0, 0.12), 0 5px 5px -3px rgba(0, 0, 0, 0.2); |
| 110 | + outline-width: 4px; |
| 111 | + transition: all 250ms linear; |
| 112 | + } |
| 113 | +
|
| 114 | + section { |
| 115 | + background-color: var(--mdc-theme-surface); |
| 116 | + transition: all 200ms linear; |
| 117 | + outline-style: solid; |
| 118 | + margin: 0px; |
| 119 | + outline-width: 0px; |
| 120 | + outline-color: var(--mdc-theme-primary); |
| 121 | + } |
| 122 | +
|
| 123 | + section.secondary { |
| 124 | + outline-color: var(--mdc-theme-secondary); |
| 125 | + } |
| 126 | +
|
| 127 | + section > div { |
| 128 | + display: flex; |
| 129 | + flex-direction: column; |
| 130 | + gap: 12px; |
| 131 | + padding: 8px 12px 16px; |
| 132 | + clear: right; |
| 133 | + } |
| 134 | +
|
| 135 | + .highlighted { |
| 136 | + outline-style: dotted; |
| 137 | + outline-width: 2px; |
| 138 | + } |
| 139 | +
|
| 140 | + :host(:focus-within) .highlighted { |
| 141 | + outline-style: solid; |
| 142 | + } |
| 143 | +
|
| 144 | + .contrasted { |
| 145 | + background-color: var(--mdc-theme-on-primary); |
| 146 | + } |
| 147 | +
|
| 148 | + h1, |
| 149 | + h2, |
| 150 | + h3, |
| 151 | + h4 { |
| 152 | + color: var(--mdc-theme-on-surface); |
| 153 | + font-family: 'Roboto', sans-serif; |
| 154 | + font-weight: 300; |
| 155 | + overflow: clip visible; |
| 156 | + white-space: nowrap; |
| 157 | + text-overflow: ellipsis; |
| 158 | + margin: 0px; |
| 159 | + line-height: 52px; |
| 160 | + padding-left: 0.3em; |
| 161 | + } |
| 162 | +
|
| 163 | + nav { |
| 164 | + float: right; |
| 165 | + } |
| 166 | +
|
| 167 | + mwc-icon { |
| 168 | + vertical-align: middle; |
| 169 | + position: relative; |
| 170 | + top: -0.1em; |
| 171 | + --mdc-icon-size: 1em; |
| 172 | + } |
| 173 | +
|
| 174 | + ::slotted([slot='icon']) { |
| 175 | + vertical-align: middle; |
| 176 | + position: relative; |
| 177 | + top: -0.1em; |
| 178 | + --mdc-icon-size: 1em; |
| 179 | + } |
| 180 | + `; |
| 181 | +} |
0 commit comments