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

[Feature request] Automatically open details block if it matches URL anchor #4252

Closed
4 tasks done
thdoan opened this issue Oct 6, 2024 · 2 comments
Closed
4 tasks done

Comments

@thdoan
Copy link

thdoan commented Oct 6, 2024

Is your feature request related to a problem? Please describe.

On my FAQ page I have a set of Q&A like this:

::: details 100. Some question blah blah blah? {#q100}
Some answer blah blah blah.
:::

On some other page I'd like to link to FAQ 100 and have it automatically open for the user to read. On a FAQ page, it can be hard to track which Q&A item you're jumping to even after the page has scrolled to it, so automatically opening the relevant details block improves the user experience here.

Describe the solution you'd like

Let's say you go to URL /faq#q100. The details block with {#q100} (id="q100") should automatically have the open attribute applied to it.

Describe alternatives you've considered

No response

Additional context

No response

Validations

@thdoan thdoan changed the title [Feature request] Open details block when there's an anchor [Feature request] Automatically open details block if it matches URL anchor Oct 6, 2024
@thdoan
Copy link
Author

thdoan commented Oct 6, 2024

Pardon my newbie ignorance -- I realized I can do everything I need to do by adding logic in <script>...</script> in the MD file itself. Feel free to close if this feature request is not planned.

@brc-dd
Copy link
Member

brc-dd commented Oct 12, 2024

Yeah.

# FAQs

:::details question 1 {#q1}
answer 1
:::

<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue'

onMounted(() => {
  window.addEventListener('hashchange', openTarget)
  openTarget()
})

onUnmounted(() => {
  window.removeEventListener('hashchange', openTarget)
})

function openTarget() {
  const hash = location.hash.substring(1)
  const details = hash && document.getElementById(hash)
  if (details && details.tagName.toLowerCase() === 'details') details.open = true
}

// https://stackoverflow.com/a/37033774/11613622
</script>

@brc-dd brc-dd closed this as completed Oct 12, 2024
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