-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathuui-ref-node.element.ts
253 lines (224 loc) · 6.02 KB
/
uui-ref-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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
import { ifDefined } from 'lit/directives/if-defined.js';
import { UUIRefElement } from '@umbraco-ui/uui-ref/lib';
import { css, html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
/**
* @element uui-ref-node
* @fires {UUIRefEvent} open - fires when the ref title is clicked
* @fires {UUIRefEvent} selected - fires when the ref is selected
* @fires {UUIRefEvent} deselected - fires when the ref is deselected
* @description - Component for displaying a reference to a generic node.
* @slot - for content
* @slot icon - for an icon
* @slot tag - for a tag
* @slot actions - for actions
*/
@defineElement('uui-ref-node')
export class UUIRefNodeElement extends UUIRefElement {
/**
* Node name
* @type {string}
* @attr
* @default ''
*/
@property({ type: String })
name = '';
/**
* Node details
* @type {string}
* @attr
* @default ''
*/
@property({ type: String })
detail = '';
/**
* Set an href, this will turns the name of the card into an anchor tag.
* @type {string}
* @attr
* @default undefined
*/
@property({ type: String })
public href?: string;
/**
* Set an anchor tag target, only used when using href.
* @type {string}
* @attr
* @default undefined
*/
@property({ type: String })
public target?: '_blank' | '_parent' | '_self' | '_top';
/**
* Set the rel attribute for an anchor tag, only used when using href.
* @type {string}
* @attr
* @default undefined
*/
@property({ type: String })
public rel?: string;
@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>`;
connectedCallback() {
super.connectedCallback();
demandCustomElement(this, 'uui-icon');
}
#onSlotIconChange(event: Event) {
this._iconSlotHasContent =
(event.target as HTMLSlotElement).assignedNodes({ flatten: true })
.length > 0;
}
protected renderDetail() {
return html`<small id="detail"
>${this.detail}<slot name="detail"></slot
></small>`;
}
#renderFallbackIcon() {
return html`<uui-icon .svg="${this.fallbackIcon}"></uui-icon>`;
}
#renderContent() {
return html`
<span id="content" class="uui-text">
<span id="icon">
<slot name="icon" @slotchange=${this.#onSlotIconChange}></slot>
${this._iconSlotHasContent === false
? this.#renderFallbackIcon()
: ''}
</span>
<div id="info">
<div id="name">${this.name}<slot name="name"></slot></div>
${this.renderDetail()}
</div>
</span>
`;
}
#renderLink() {
return html`<a
id="open-part"
class="uui-text"
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>`;
}
#renderButton() {
return html`
<button
type="button"
id="open-part"
class="uui-text"
tabindex="0"
@click=${this.handleOpenClick}
@keydown=${this.handleOpenKeydown}
?disabled=${this.disabled}>
${this.#renderContent()}
</button>
`;
}
public render() {
return html`
${this.#renderOpenPart()}
<!-- Select border must be right after #open-part -->
<div id="select-border"></div>
<slot></slot>
<slot name="tag" id="tag-container"></slot>
<slot name="actions" id="actions-container"></slot>
`;
}
#renderOpenPart() {
if (this.readonly) {
return html`${this.#renderContent()}`;
} else {
return this.href ? this.#renderLink() : this.#renderButton();
}
}
static styles = [
...UUIRefElement.styles,
css`
:host {
min-width: 250px;
padding: 1px;
}
#content {
display: flex;
align-items: center;
justify-content: center;
line-height: 1.2em;
padding: calc(var(--uui-size-3));
}
#open-part {
color: inherit;
text-decoration: none;
cursor: pointer;
display: flex;
flex-grow: 1;
}
#icon {
font-size: 1.2em;
margin-right: var(--uui-size-1);
display: flex;
align-items: center;
justify-content: center;
}
#info {
display: flex;
flex-direction: column;
align-items: start;
justify-content: center;
height: 100%;
padding-left: var(--uui-size-2);
}
#detail {
opacity: 0.6;
}
:host([selectable]) #open-part {
flex-grow: 0;
padding: 0;
margin: calc(var(--uui-size-2));
}
: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([disabled]) #icon {
color: var(--uui-color-disabled-contrast);
}
:host([disabled]) #name {
color: var(--uui-color-disabled-contrast);
}
:host([disabled]) #detail {
color: var(--uui-color-disabled-contrast);
}
`,
];
}
declare global {
interface HTMLElementTagNameMap {
'uui-ref-node': UUIRefNodeElement;
}
}