|
| 1 | +import { defineElement } from '@umbraco-ui/uui-base/lib/registration'; |
| 2 | +import { css, html, nothing } from 'lit'; |
| 3 | +import { UUICardElement } from '@umbraco-ui/uui-card/lib'; |
| 4 | +import { styleMap } from 'lit/directives/style-map.js'; |
| 5 | +import { ifDefined } from 'lit/directives/if-defined.js'; |
| 6 | + |
| 7 | +import { property } from 'lit/decorators.js'; |
| 8 | + |
| 9 | +export type BlockTypeIcon = { |
| 10 | + name?: string; |
| 11 | + color?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * @element uui-card-block-type |
| 16 | + */ |
| 17 | +@defineElement('uui-card-block-type') |
| 18 | +export class UUICardBlockTypeElement extends UUICardElement { |
| 19 | + /** |
| 20 | + * Block type name |
| 21 | + * @type {string} |
| 22 | + * @attr name |
| 23 | + * @default '' |
| 24 | + */ |
| 25 | + @property({ type: String }) |
| 26 | + name = ''; |
| 27 | + |
| 28 | + /** |
| 29 | + * Block type description |
| 30 | + * @type {string} |
| 31 | + * @attr description |
| 32 | + * @default undefined |
| 33 | + */ |
| 34 | + @property({ type: String }) |
| 35 | + description?: string; |
| 36 | + |
| 37 | + @property({ type: String, attribute: 'background' }) |
| 38 | + background?: string; |
| 39 | + |
| 40 | + render() { |
| 41 | + return html` |
| 42 | + <div |
| 43 | + id="portrait" |
| 44 | + style=${styleMap({ backgroundColor: this.background })}> |
| 45 | + <slot></slot> |
| 46 | + </div> |
| 47 | + ${this.href ? this.#renderLink() : this.#renderButton()} |
| 48 | +
|
| 49 | + <slot name="tag"></slot> |
| 50 | + <slot name="actions"></slot> |
| 51 | + `; |
| 52 | + } |
| 53 | + |
| 54 | + #renderButton() { |
| 55 | + return html` |
| 56 | + <button |
| 57 | + id="open-part" |
| 58 | + tabindex=${this.disabled ? (nothing as any) : '0'} |
| 59 | + @click=${this.handleOpenClick} |
| 60 | + @keydown=${this.handleOpenKeydown}> |
| 61 | + <strong>${this.name}</strong><small>${this.description}</small> |
| 62 | + </button> |
| 63 | + `; |
| 64 | + } |
| 65 | + |
| 66 | + #renderLink() { |
| 67 | + return html` |
| 68 | + <a |
| 69 | + id="open-part" |
| 70 | + tabindex=${this.disabled ? (nothing as any) : '0'} |
| 71 | + href=${ifDefined(!this.disabled ? this.href : undefined)} |
| 72 | + target=${ifDefined(this.target || undefined)} |
| 73 | + rel=${ifDefined( |
| 74 | + this.target === '_blank' ? 'noopener noreferrer' : undefined |
| 75 | + )}> |
| 76 | + <strong>${this.name}</strong><small>${this.description}</small> |
| 77 | + </a> |
| 78 | + `; |
| 79 | + } |
| 80 | + |
| 81 | + static styles = [ |
| 82 | + ...UUICardElement.styles, |
| 83 | + css` |
| 84 | + :host { |
| 85 | + flex-direction: column; |
| 86 | + justify-content: flex-start; |
| 87 | + } |
| 88 | +
|
| 89 | + :host(:hover) #info { |
| 90 | + color: var(--uui-color-interactive-emphasis); |
| 91 | + } |
| 92 | +
|
| 93 | + #portrait { |
| 94 | + background-color: var(--uui-color-surface-alt); |
| 95 | + display: flex; |
| 96 | + justify-content: center; |
| 97 | + min-height: 150px; |
| 98 | + max-height: 150px; |
| 99 | + } |
| 100 | +
|
| 101 | + slot:not([name])::slotted(*) { |
| 102 | + align-self: center; |
| 103 | + font-size: var(--uui-size-8); |
| 104 | + border-radius: var(--uui-border-radius); |
| 105 | + object-fit: cover; |
| 106 | + max-width: 100%; |
| 107 | + max-height: 100%; |
| 108 | + } |
| 109 | +
|
| 110 | + #open-part { |
| 111 | + text-align: left; |
| 112 | + background-color: var(--uui-color-surface); |
| 113 | + cursor: pointer; |
| 114 | + color: var(--uui-color-interactive); |
| 115 | + border: none; |
| 116 | + border-top: 1px solid var(--uui-color-divider); |
| 117 | + border-radius: 0 0 var(--uui-border-radius) var(--uui-border-radius); |
| 118 | + font-family: inherit; |
| 119 | + font-size: var(--uui-type-small-size); |
| 120 | + box-sizing: border-box; |
| 121 | + padding: var(--uui-size-2) var(--uui-size-4); |
| 122 | + display: flex; |
| 123 | + flex-direction: column; |
| 124 | + line-height: var(--uui-size-6); |
| 125 | + } |
| 126 | +
|
| 127 | + :host([disabled]) #open-part { |
| 128 | + pointer-events: none; |
| 129 | + background: var(--uui-color-disabled); |
| 130 | + color: var(--uui-color-contrast-disabled); |
| 131 | + } |
| 132 | +
|
| 133 | + #open-part:hover strong { |
| 134 | + text-decoration: underline; |
| 135 | + } |
| 136 | + #open-part:hover { |
| 137 | + color: var(--uui-color-interactive-emphasis); |
| 138 | + } |
| 139 | +
|
| 140 | + :host([image]:not([image=''])) #open-part { |
| 141 | + transition: opacity 0.5s 0.5s; |
| 142 | + opacity: 0; |
| 143 | + } |
| 144 | + slot[name='tag'] { |
| 145 | + position: absolute; |
| 146 | + top: var(--uui-size-4); |
| 147 | + right: var(--uui-size-4); |
| 148 | + display: flex; |
| 149 | + justify-content: right; |
| 150 | + } |
| 151 | +
|
| 152 | + slot[name='actions'] { |
| 153 | + position: absolute; |
| 154 | + top: var(--uui-size-4); |
| 155 | + right: var(--uui-size-4); |
| 156 | + display: flex; |
| 157 | + justify-content: right; |
| 158 | +
|
| 159 | + opacity: 0; |
| 160 | + transition: opacity 120ms; |
| 161 | + } |
| 162 | + :host(:focus) slot[name='actions'], |
| 163 | + :host(:focus-within) slot[name='actions'], |
| 164 | + :host(:hover) slot[name='actions'] { |
| 165 | + opacity: 1; |
| 166 | + } |
| 167 | +
|
| 168 | + :host( |
| 169 | + [image]:not([image='']):hover, |
| 170 | + [image]:not([image='']):focus, |
| 171 | + [image]:not([image='']):focus-within, |
| 172 | + [selected][image]:not([image='']), |
| 173 | + [error][image]:not([image='']) |
| 174 | + ) |
| 175 | + #open-part { |
| 176 | + opacity: 1; |
| 177 | + transition-duration: 120ms; |
| 178 | + transition-delay: 0s; |
| 179 | + } |
| 180 | + `, |
| 181 | + ]; |
| 182 | +} |
| 183 | + |
| 184 | +declare global { |
| 185 | + interface HTMLElementTagNameMap { |
| 186 | + 'uui-card-block-type': UUICardBlockTypeElement; |
| 187 | + } |
| 188 | +} |
0 commit comments