Skip to content

Commit 12feafb

Browse files
JesmoDeviOvergaard
andauthored
feat: uui-ref-node should have href and target properties (#715)
* add href and target --------- Co-authored-by: Jacob Overgaard <[email protected]>
1 parent 278a6dd commit 12feafb

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

packages/uui-ref-node/lib/uui-ref-node.element.ts

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
22
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
3+
import { ifDefined } from 'lit/directives/if-defined.js';
34
import { UUIRefElement } from '@umbraco-ui/uui-ref/lib';
4-
import { css, html } from 'lit';
5+
import { css, html, nothing } from 'lit';
56
import { property, state } from 'lit/decorators.js';
67

78
/**
@@ -36,6 +37,24 @@ export class UUIRefNodeElement extends UUIRefElement {
3637
@property({ type: String })
3738
detail = '';
3839

40+
/**
41+
* Set an href, this will turns the name of the card into an anchor tag.
42+
* @type {string}
43+
* @attr
44+
* @default undefined
45+
*/
46+
@property({ type: String })
47+
public href?: string;
48+
49+
/**
50+
* Set an anchor tag target, only used when using href.
51+
* @type {string}
52+
* @attr
53+
* @default undefined
54+
*/
55+
@property({ type: String })
56+
public target?: '_blank' | '_parent' | '_self' | '_top';
57+
3958
@state()
4059
private _iconSlotHasContent = false;
4160

@@ -48,7 +67,7 @@ export class UUIRefNodeElement extends UUIRefElement {
4867
demandCustomElement(this, 'uui-icon');
4968
}
5069

51-
private _onSlotIconChange(event: Event) {
70+
#onSlotIconChange(event: Event) {
5271
this._iconSlotHasContent =
5372
(event.target as HTMLSlotElement).assignedNodes({ flatten: true })
5473
.length > 0;
@@ -60,11 +79,37 @@ export class UUIRefNodeElement extends UUIRefElement {
6079
></small>`;
6180
}
6281

63-
private _renderFallbackIcon() {
82+
#renderFallbackIcon() {
6483
return html`<uui-icon .svg="${this.fallbackIcon}"></uui-icon>`;
6584
}
6685

67-
public render() {
86+
#renderContent() {
87+
return html`
88+
<span id="icon">
89+
<slot name="icon" @slotchange=${this.#onSlotIconChange}></slot>
90+
${this._iconSlotHasContent === false ? this.#renderFallbackIcon() : ''}
91+
</span>
92+
<div id="info">
93+
<div id="name">${this.name}</div>
94+
${this.renderDetail()}
95+
</div>
96+
`;
97+
}
98+
99+
#renderLink() {
100+
return html`<a
101+
id="open-part"
102+
tabindex=${this.disabled ? (nothing as any) : '0'}
103+
href=${ifDefined(!this.disabled ? this.href : undefined)}
104+
target=${ifDefined(this.target || undefined)}
105+
rel=${ifDefined(
106+
this.target === '_blank' ? 'noopener noreferrer' : undefined
107+
)}>
108+
${this.#renderContent()}
109+
</a>`;
110+
}
111+
112+
#renderButton() {
68113
return html`
69114
<button
70115
type="button"
@@ -73,17 +118,14 @@ export class UUIRefNodeElement extends UUIRefElement {
73118
@click=${this.handleOpenClick}
74119
@keydown=${this.handleOpenKeydown}
75120
?disabled=${this.disabled}>
76-
<span id="icon">
77-
<slot name="icon" @slotchange=${this._onSlotIconChange}></slot>
78-
${this._iconSlotHasContent === false
79-
? this._renderFallbackIcon()
80-
: ''}
81-
</span>
82-
<div id="info">
83-
<div id="name">${this.name}</div>
84-
${this.renderDetail()}
85-
</div>
121+
${this.#renderContent()}
86122
</button>
123+
`;
124+
}
125+
126+
public render() {
127+
return html`
128+
${this.href ? this.#renderLink() : this.#renderButton()}
87129
<!-- Select border must be right after #open-part -->
88130
<div id="select-border"></div>
89131
@@ -102,7 +144,10 @@ export class UUIRefNodeElement extends UUIRefElement {
102144
}
103145
104146
#open-part {
147+
text-decoration: none;
148+
color: inherit;
105149
align-self: stretch;
150+
line-height: normal;
106151
107152
display: flex;
108153
position: relative;

packages/uui-ref-node/lib/uui-ref-node.story.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import '.';
2-
31
import { Story } from '@storybook/web-components';
42
import { html } from 'lit';
53

64
import { ArrayOfUmbracoWords } from '../../../storyhelpers/UmbracoWordGenerator';
75
import readme from '../README.md?raw';
86

7+
import './index';
8+
import '@umbraco-ui/uui-tag/lib';
9+
import '@umbraco-ui/uui-action-bar/lib';
10+
import '@umbraco-ui/uui-button/lib';
11+
import '@umbraco-ui/uui-icon/lib';
12+
import '@umbraco-ui/uui-icon-registry-essential/lib';
13+
914
export default {
1015
id: 'uui-ref-node',
1116
title: 'Displays/References/Node',
@@ -26,6 +31,8 @@ const Template: Story = props => html`
2631
<uui-ref-node
2732
name="${props.name}"
2833
detail="${props.detail}"
34+
href="${props.href}"
35+
target="${props.target}"
2936
?selectable=${props.selectable}
3037
?selectOnly=${props.selectOnly}
3138
?error=${props.error}
@@ -42,6 +49,7 @@ export const AAAOverview = Template.bind({});
4249
AAAOverview.args = {
4350
name: 'Rabbit Suit Product Page',
4451
detail: 'path/to/nowhere',
52+
href: 'umbraco.com',
4553
};
4654
AAAOverview.storyName = 'Overview';
4755
AAAOverview.parameters = {
@@ -62,16 +70,14 @@ AAAOverview.parameters = {
6270
};
6371

6472
export const CustomIcon: Story = () => html`
65-
<essential-icon-registry>
66-
<uui-ref-node-data-type
67-
name="Rabbit Suit Product Page"
68-
detail="path/to/nowhere">
73+
<uui-icon-registry-essential>
74+
<uui-ref-node name="Rabbit Suit Product Page" detail="path/to/nowhere">
6975
<uui-icon slot="icon" name="colorpicker"></uui-icon>
7076
<uui-action-bar slot="actions">
7177
<uui-button label="Remove">Remove</uui-button>
7278
</uui-action-bar>
73-
</uui-ref-node-data-type>
74-
</essential-icon-registry>
79+
</uui-ref-node>
80+
</uui-icon-registry-essential>
7581
`;
7682

7783
CustomIcon.parameters = {

0 commit comments

Comments
 (0)