Details
PicoSPA picospa.details.* makes the <details> element better, without server-side code.
- It remembers which
<details>were open and restores them across page loads. - It auto-opens
<details>elements that contain links to the current page.
Useful for: menus, navigation sections, FAQs, or any collapsible content that you want the expanded state to persist across page loads.
Try it out 👏
The menu on the left (or above on mobile) is a set of <details> elements that will remember their open state across page loads.
1️⃣ Try what picospa.details.reopen() does:
- Open the "API" section and
- reload the page.
- Voila, it should still be open!
2️⃣ Try what picospa.details.autoOpen() does:
- Close the "About" section in the menu, and
- click this link to the Changelog.
- It will take you there and automatically open the
<details>for "About" because it contains a link to the current page. - When you come back here, the "About" section will be closed again, since you did not actively open it, but the site linked inside of it was opened.
3️⃣ Try what picospa.details.scrollOnOpen() does:
- See the
<details>element below - Click to open it
- Scroll on open is off!
- ❌
- Close and open the
<details>again, it should scroll to the top of the page.
Scroll on Open Example
🙀 It did NOT SCROLL? Maybe this element was fully
in view when you opened it? Scroll up a bit and try again.
This <details> element
will scroll into view when opened after
enabling the feature.
How To Use
Add the script from the CDN to your page and call picospa.details.reopen():
<script src="https://cdn.jsdelivr.net/npm/@pic0/spa@2026.1.12/_dist/picospa.min.js"></script>
<script>
// Simple usage - affects all <details> elements on the page
picospa.details.reopen();
// Auto-open <details> elements that contain links to the current page
picospa.details.autoOpen();
</script>
For more targeted control, you can specify which elements to track:
<script>
// Do it only for specific DOM nodes, either being an array of selectors
picospa.details.reopen({nodes: ['#faq-section', '[data-details]']});
// ... or NodeList, e.g., the result of document.querySelectorAll()
picospa.details.autoOpen({nodes: document.querySelectorAll('details')});
</script>
Example HTML
<details id="faq-section">
<summary>Frequently Asked Questions</summary>
<p>Content that will remember if it was expanded...</p>
</details>
<details class="collapsible-info">
<summary>More Information</summary>
<p>This state is also preserved...</p>
</details>
Storage
Open states are stored in sessionStorage using the provided page key:
- Key format:
{key} - dialog.reopen - Stores an array of selectors for currently open details
- Only works within the same browser session
Cross-Page State
Using the same page key across multiple pages allows restoring state even when the URL changes:
// Use the same key on all documentation pages
picospa.details.reopen('docs-menu', ['#navigation-menu', '.sidebar-sections']);
This is useful for maintaining expanded menu states across multiple documentation pages or any site with shared navigation elements.
Auto-Opening Sections
The autoOpen() function automatically opens <details> elements that contain links matching the current page URL. This is especially useful for navigation menus where you want to expand the section containing the current page.
// Auto-open details elements containing current page links
picospa.details.autoOpen(['#navigation', '.sidebar-menu']);
Navigation Use Case
Perfect for navigation structures where each section contains links to related pages:
<details>
<summary>Documentation</summary>
<ul>
<li><a href="/docs/getting-started">Getting Started</a></li>
<li><a href="/docs/api">API Reference</a></li>
</ul>
</details>
<details>
<summary>Examples</summary>
<ul>
<li><a href="/examples/basic">Basic Usage</a></li>
<li><a href="/examples/advanced">Advanced</a></li>
</ul>
</details>
<script>
// This will automatically open the section containing the current page
picospa.details.autoOpen();
</script>
When visiting /docs/api, the "Documentation" section will automatically expand because it contains a link to that page.
Smooth Scrolling on Open
The scrollOnOpen() function opens up by smooth scrolling when the <details> element is opened.
This ensures the opened element scrolls into view to provide a better user experience.
// Enable smooth scrolling for all <details> elements
picospa.details.scrollOnOpen();
// Or target specific elements
picospa.details.scrollOnOpen({
nodes: document.querySelectorAll('.collapsible-sections')
});
// Custom scroll behavior
picospa.details.scrollOnOpen({
behavior: 'smooth',
block: 'start',
inline: 'nearest'
});
Important Notes
- Call
reopen()immediately after page load for best results - Only tracks
<details>elements that match your provided selectors - Uses
beforeunloadevent to capture open states - Elements not found during restore are safely ignored
- Works with any valid CSS selector
autoOpen()can be used independently or combined withreopen()