-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathuui-card-content-node.element.ts
237 lines (210 loc) · 6.07 KB
/
uui-card-content-node.element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
import { UUICardElement } from '@umbraco-ui/uui-card/lib';
import { css, html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
/**
* @element uui-card-content-node
* @description - Card component for displaying a content-node.
* @slot - slot for the default content area
* @slot icon - slot for the icon with support for `<uui-icon>` elements
* @slot tag - slot for the tag with support for `<uui-tag>` elements
* @slot actions - slot for the actions with support for the `<uui-action-bar>` element
*/
@defineElement('uui-card-content-node')
export class UUICardContentNodeElement extends UUICardElement {
/**
* Node name
* @type {string}
* @attr name
* @default ''
*/
@property({ type: String })
name = '';
/**
* Node details
* @type {string}
* @attr
* @default ''
*/
@property({ type: String })
detail = '';
@state()
private _iconSlotHasContent = false;
protected fallbackIcon = `<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.75"
stroke-linecap="round"
stroke-linejoin="round"
id="icon">
<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z" />
<path d="M14 2v4a2 2 0 0 0 2 2h4" />
</svg>`;
private _onSlotIconChange(event: Event) {
this._iconSlotHasContent =
(event.target as HTMLSlotElement).assignedNodes({ flatten: true })
.length > 0;
}
private _renderFallbackIcon() {
demandCustomElement(this, 'uui-icon');
return html`<uui-icon .svg="${this.fallbackIcon}"></uui-icon>`;
}
protected renderDetail() {
return html`<small id="detail"
>${this.detail}<slot name="detail"></slot></small
><slot id="default"></slot>`;
}
#renderContent() {
return html`
<span id="content" class="uui-text">
<span id="item">
<span id="icon">
<slot name="icon" @slotchange=${this._onSlotIconChange}></slot>
${this._iconSlotHasContent === false
? this._renderFallbackIcon()
: ''}
</span>
<div title="${this.name}" id="name">
${this.name}<slot name="name"></slot>
</div>
</span>
${this.renderDetail()}
</span>
`;
}
#renderButton() {
return html`<button
id="open-part"
tabindex=${this.disabled ? (nothing as any) : 0}
@click=${this.handleOpenClick}
@keydown=${this.handleOpenKeydown}>
${this.#renderContent()}
</button>`;
}
#renderLink() {
return html`<a
id="open-part"
tabindex=${this.disabled ? (nothing as any) : 0}
href=${ifDefined(!this.disabled ? this.href : undefined)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}>
${this.#renderContent()}
</a>`;
}
public render() {
return html`
${this.href ? this.#renderLink() : this.#renderButton()}
<!-- Select border must be right after #open-part -->
<div id="select-border"></div>
<slot name="tag"></slot>
<slot name="actions"></slot>
`;
}
static styles = [
...UUICardElement.styles,
css`
:host {
min-width: 250px;
flex-direction: column;
justify-content: space-between;
}
slot[name='tag'] {
position: absolute;
top: var(--uui-size-4);
right: var(--uui-size-4);
display: flex;
justify-content: right;
}
slot[name='actions'] {
position: absolute;
top: var(--uui-size-4);
right: var(--uui-size-4);
display: flex;
justify-content: right;
opacity: 0;
transition: opacity 120ms;
}
:host(:focus) slot[name='actions'],
:host(:focus-within) slot[name='actions'],
:host(:hover) slot[name='actions'] {
opacity: 1;
}
slot:not([name]) {
display: block;
margin: var(--uui-size-1);
margin-top: var(--uui-size-3);
}
slot:not([name])::slotted(*) {
font-size: var(--uui-type-small-size);
line-height: calc(2 * var(--uui-size-3));
}
#open-part {
display: flex;
position: relative;
align-items: center;
cursor: pointer;
flex-grow: 1;
padding: var(--uui-size-space-4) var(--uui-size-space-5);
}
#open-part #name {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
overflow-wrap: anywhere;
}
#content {
align-self: stretch;
display: flex;
flex-direction: column;
}
#item {
position: relative;
display: flex;
align-self: stretch;
line-height: normal;
align-items: center;
margin-top: var(--uui-size-1);
}
#icon {
font-size: 1.2em;
margin-right: var(--uui-size-1);
}
:host([selectable]) #open-part {
padding: 0;
margin: var(--uui-size-space-4) var(--uui-size-space-5);
}
:host([disabled]) #name {
pointer-events: none;
}
:host(:not([disabled])) #open-part:hover #icon {
color: var(--uui-color-interactive-emphasis);
}
:host(:not([disabled])) #open-part:hover #name {
text-decoration: underline;
color: var(--uui-color-interactive-emphasis);
}
:host(:not([disabled])) #open-part:hover #detail {
color: var(--uui-color-interactive-emphasis);
}
:host(:not([disabled])) #open-part:hover #default {
color: var(--uui-color-interactive-emphasis);
}
`,
];
}
declare global {
interface HTMLElementTagNameMap {
'uui-card-content-node': UUICardContentNodeElement;
}
}