Skip to content

Commit

Permalink
feat: add rel property to relevant elements to override default beh…
Browse files Browse the repository at this point in the history
…avior (#814)

* 721 property added for rel to allow

* 721 fix tab close tag
  • Loading branch information
AndyLongShout authored and iOvergaard committed Jul 25, 2024
1 parent 8d2e211 commit f1978e0
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export class UUIButtonInlineCreateElement extends LitElement {
@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;

private _onMouseMove(e: MouseEvent) {
this._position = (this.vertical ? e.offsetY : e.offsetX) - 5;
}
Expand Down Expand Up @@ -98,7 +107,10 @@ export class UUIButtonInlineCreateElement extends LitElement {
href=${ifDefined(this.href)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}
aria-label=${this.label ? this.label : 'create new element'}>
${this.#renderContent()}
Expand Down
14 changes: 13 additions & 1 deletion packages/uui-button/lib/uui-button.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ export class UUIButtonElement extends UUIFormControlMixin(
@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;

@query('#button')
protected _button!: HTMLInputElement;

Expand Down Expand Up @@ -229,7 +238,10 @@ export class UUIButtonElement extends UUIFormControlMixin(
href=${ifDefined(!this.disabled ? this.href : undefined)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}>
${this.renderState()} ${this.renderLabel()}
<slot name="extra"></slot>
Expand Down
6 changes: 5 additions & 1 deletion packages/uui-button/lib/uui-button.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
args: {
href: undefined,
target: undefined,
rel: undefined,
look: 'default',
color: 'default',
type: undefined,
Expand Down Expand Up @@ -104,6 +105,7 @@ const Template: Story = props => {
.state=${props.state}
.href=${props.href}
.target=${props.target}
.rel=${props.rel}
look=${props.look}
color=${props.color}
label=${props.label}
Expand Down Expand Up @@ -339,6 +341,7 @@ export const AnchorTag = Template.bind({});
AnchorTag.args = {
href: 'https://www.umbraco.com',
target: '_blank',
rel: 'noopener noreferrer',
look: 'primary',
};
AnchorTag.parameters = {
Expand All @@ -349,7 +352,8 @@ AnchorTag.parameters = {
look="primary"
label="Open umbraco.com"
href="http://www.umbraco.com"
target="_blank">
target="_blank"
rel="noopener noreferrer">
</uui-button>
`.strings,
},
Expand Down
20 changes: 19 additions & 1 deletion packages/uui-button/lib/uui-button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ describe('UuiButton', () => {
it('has a target property', () => {
expect(element).to.have.property('target');
});

it('has a rel property', () => {
expect(element).to.have.property('rel');
});
});

describe('template', () => {
Expand Down Expand Up @@ -249,13 +253,27 @@ describe('UuiButton', () => {
expect(anchorElement.getAttribute('target')).to.be.equal('_self');
});

it('when target is _blank rel is set.', async () => {
it('when target is _blank and rel is not defined rel attribute is set.', async () => {
element.target = '_blank';
await elementUpdated(element);
expect(anchorElement.getAttribute('target')).to.be.equal('_blank');
expect(anchorElement.getAttribute('rel')).to.be.equal(
'noopener noreferrer',
);
});

it('when rel is applied to anchor tag.', async () => {
element.rel = 'noreferrer';
await elementUpdated(element);
expect(anchorElement.getAttribute('rel')).to.be.equal('noreferrer');
});

it('when target is _blank and rel is defined rel attribute is set.', async () => {
element.target = '_blank';
element.rel = 'noopener';
await elementUpdated(element);
expect(anchorElement.getAttribute('target')).to.be.equal('_blank');
expect(anchorElement.getAttribute('rel')).to.be.equal('noopener');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export class UUICardBlockTypeElement extends UUICardElement {
href=${ifDefined(!this.disabled ? this.href : undefined)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}>
<strong>${this.name}</strong><small>${this.description}</small>
</a>
Expand Down
39 changes: 26 additions & 13 deletions packages/uui-card-block-type/lib/uui-card-block-type.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export const AAAOverview: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand All @@ -77,7 +78,8 @@ export const Description: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand All @@ -102,7 +104,8 @@ export const Tag: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
<uui-tag slot="tag" size="s" color="danger">Trashed</uui-tag>
</uui-card-block-type>
Expand All @@ -126,7 +129,8 @@ export const Actions: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
<uui-action-bar slot="actions">
Expand Down Expand Up @@ -154,7 +158,8 @@ export const Background: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand All @@ -179,7 +184,8 @@ export const Image: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
<img
src="https://umbraco.com/media/v5gf3w2a/umbraco-toolkit-wide.svg"
alt="" />
Expand All @@ -206,7 +212,8 @@ export const Error: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand All @@ -229,7 +236,8 @@ export const Selectable: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand All @@ -252,7 +260,8 @@ export const Selected: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand All @@ -275,7 +284,8 @@ export const Multiple: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon('rgba(0,0,0,0.5)')}
</uui-card-block-type>
<uui-card-block-type
Expand All @@ -287,7 +297,8 @@ export const Multiple: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon('red')}
</uui-card-block-type>
<uui-card-block-type
Expand All @@ -299,7 +310,8 @@ export const Multiple: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon('rgba(11, 229, 255, 0.5)')}
</uui-card-block-type>
`;
Expand All @@ -324,7 +336,8 @@ export const Disabled: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${renderWandIcon()}
</uui-card-block-type>
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export class UUICardContentNodeElement extends UUICardElement {
href=${ifDefined(!this.disabled ? this.href : undefined)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}>
<span id="icon">
<slot name="icon" @slotchange=${this._onSlotIconChange}></slot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const AAAOverview: Story = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
<uui-tag size="s" slot="tag" color="positive">Published</uui-tag>
${cardContent}
</uui-card-content-node>
Expand Down
5 changes: 4 additions & 1 deletion packages/uui-card-media/lib/uui-card-media.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export class UUICardMediaElement extends UUICardElement {
href=${ifDefined(!this.disabled ? this.href : undefined)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}>
<!--
TODO: Implement when pop-out is ready
Expand Down
3 changes: 2 additions & 1 deletion packages/uui-card-media/lib/uui-card-media.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const AAAOverview: Story = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}></uui-card-media>
target=${props.target}
rel=${props.rel}></uui-card-media>
`;
AAAOverview.storyName = 'Overview';

Expand Down
5 changes: 4 additions & 1 deletion packages/uui-card-user/lib/uui-card-user.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class UUICardUserElement extends UUICardElement {
href=${ifDefined(!this.disabled ? this.href : undefined)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}>
<span>${this.name}</span>
</a>`;
Expand Down
3 changes: 2 additions & 1 deletion packages/uui-card-user/lib/uui-card-user.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const Template: StoryFn = props => html`
?error=${props.error}
?disabled=${props.disabled}
href=${props.href}
target=${props.target}>
target=${props.target}
rel=${props.rel}>
${cardContent}
</uui-card-user>
`;
Expand Down
9 changes: 9 additions & 0 deletions packages/uui-card/lib/uui-card.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export class UUICardElement extends SelectOnlyMixin(
@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;

// This is deprecated - use href instead
protected handleOpenClick(e: Event) {
if (this.disabled) return;
Expand Down
14 changes: 13 additions & 1 deletion packages/uui-menu-item/lib/uui-menu-item.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export class UUIMenuItemElement extends SelectOnlyMixin(
@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;

/**
* Sets the selection mode.
* @type {string}
Expand Down Expand Up @@ -170,7 +179,10 @@ export class UUIMenuItemElement extends SelectOnlyMixin(
href=${ifDefined(this.href)}
target=${ifDefined(this.target || undefined)}
rel=${ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
this.rel ||
ifDefined(
this.target === '_blank' ? 'noopener noreferrer' : undefined,
),
)}
@click=${this._onLabelClicked}
?disabled=${this.disabled}
Expand Down
Loading

0 comments on commit f1978e0

Please sign in to comment.