Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add target property to side-nav-item #7088

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/side-nav/src/vaadin-side-nav-item.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ declare class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixi
*/
readonly current: boolean;

/**
* The target of the link. Works only when `path` is set.
*/
target: string | null | undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a type here that only accepts something like '_self' | '_blank' | '_parent' | '_top'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target in HTMLAnchoElement doesn't seem to get any special type (source)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A niche use-case would be to configure a specific browsing context, which could have any name apparently (vaadin/flow-components#5091 (comment)). So the union type would need to include string as well. But just starting with string is fine IMO 👍


addEventListener<K extends keyof SideNavItemEventMap>(
type: K,
listener: (this: SideNavItem, ev: SideNavItemEventMap[K]) => void,
Expand Down
6 changes: 6 additions & 0 deletions packages/side-nav/src/vaadin-side-nav-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixin(Themab
readOnly: true,
reflectToAttribute: true,
},

/**
* The target of the link. Works only when `path` is set.
*/
target: String,
};
}

Expand Down Expand Up @@ -201,6 +206,7 @@ class SideNavItem extends SideNavChildrenMixin(DisabledMixin(ElementMixin(Themab
?disabled="${this.disabled}"
tabindex="${this.disabled || this.path == null ? '-1' : '0'}"
href="${ifDefined(this.disabled ? null : this.path)}"
target="${ifDefined(this.target)}"
part="link"
aria-current="${this.current ? 'page' : 'false'}"
>
Expand Down
22 changes: 22 additions & 0 deletions packages/side-nav/test/side-nav-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,27 @@ describe('side-nav-item', () => {
toggle.click();
expect(spy.called).to.be.false;
});

describe('target property', () => {
it('should set target attribute to the anchor when target is set', async () => {
item.target = '_blank';
await nextRender();
expect(anchor.getAttribute('target')).to.be.equal('_blank');
});

it('should remove target attribute from the anchor when target is removed', async () => {
item.target = '_blank';
await nextRender();
item.target = null;
await nextRender();
expect(anchor.getAttribute('target')).to.be.not.ok;
});

it('should not set target attribute to the anchor when target is empty string', async () => {
item.target = '';
await nextRender();
expect(anchor.getAttribute('target')).to.be.not.ok;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my testing the attribute is still defined for an empty string, the assertion here doesn't work as intended. Would be better to use hasAttribute for checking the attribute existence IMO.

If we want to avoid defining the attribute it requires further changes. Don't see why it would be necessary though. So maybe just remove the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. Based on this comment, I also changed the assertion in the previous test to use hasAttribute instead.

});
});
});
});