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

Introduce elementTargetClass option (alternative to :target) #2231

Closed
AlttiRi opened this issue May 9, 2024 · 5 comments
Closed

Introduce elementTargetClass option (alternative to :target) #2231

AlttiRi opened this issue May 9, 2024 · 5 comments

Comments

@AlttiRi
Copy link

AlttiRi commented May 9, 2024

What problem is this solving

While it supports scrolling to a hash:

export const router = createRouter({
    history: createWebHashHistory(), // createWebHistory
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (to.hash) {
            return { el: to.hash };
        }
    }
});

Setting the hash with History API do not affect on styling the element with :target selector.

Proposed solution

Add a new createRouters option similar to

For example:

export const router = createRouter({
    /* ... */
    elementTargetClass: "router-target-element"
});

Describe alternatives you've considered

No response

Copy link
Member

posva commented May 10, 2024

And what does this option do?

@AlttiRi
Copy link
Author

AlttiRi commented May 10, 2024

Adds the class ("router-target-element" from the example above) to the element with the id the same as the URL's hash.
So, it can be used as an alternative to :target CSS selector which does not work with vue-router.

:target, .router-target-element {
  font-weight: bold;
}

For example, the route /main#1 will add the router-target-element class to the element with id 1.

@posva
Copy link
Member

posva commented May 10, 2024

I see, so it's specific to the web hash history and what you mentioned on #2230. It can be solved by creating that custom history you proposed, which seems more appropriate than introducing a new option that only applies to a specific history mode.

@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@AlttiRi
Copy link
Author

AlttiRi commented May 10, 2024

it's specific to the web hash history

No, this applicable to both createWebHistory and createWebHashHistory.
With createWebHistory mode the :target CSS selector does not work too.

> Setting the hash with History API do not affect on styling the element with :target selector.


I'm not the only one who faced this issue:

So, it can be simple fixed with the similar option as linkActiveClass and linkExactActiveClass.

@posva
Copy link
Member

posva commented May 10, 2024

Thanks for pointing out #2076 , I forgot about it being a spec limitation. You should follow w3c/csswg-drafts#6942 for a proper implementation of this.

You can implement the feature in many ways. The most reusable one would be a custom Router Link by checking the current route and the this custom link resolved route's hash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants