Skip to content
Draft
Changes from all 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
18 changes: 16 additions & 2 deletions packages/ui/atoms/AnchorScrollTo/AnchorScrollTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface AnchorScrollToProps extends BaseProps {
export class AnchorScrollTo<T extends BaseProps = BaseProps> extends Base<AnchorScrollToProps & T> {
static config: BaseConfig = {
name: 'AnchorScrollTo',
options: {
hashCloak: Boolean,
},
};

/**
Expand All @@ -27,10 +30,21 @@ export class AnchorScrollTo<T extends BaseProps = BaseProps> extends Base<Anchor
* @param {MouseEvent} event
* @returns {void}
*/
onClick(event) {
async onClick(event) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
async onClick(event) {
async onClick(event) {
if (
this.$el.pathname !== window.location.pathname ||
this.$el.origin !== window.location.origin
) {
return;
}

const target = document.querySelector(this.targetSelector) as HTMLElement;
if (!target) {
return;
}

try {
scrollTo(this.targetSelector);
event.preventDefault();
await scrollTo(target);

if (this.$options.hashCloak) {
return;
}

window.location.hash = this.targetSelector;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we avoid the problem with a specified offset ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

historyReplace({ hash: 'hello-world' });

} catch {
// Silence is golden.
}
Expand Down