___ _ ___ ___ _ | _ (_)__ ___/ __| _ \/_\ | _/ / _/ _ \__ \ _/ _ \ |_| |_\__\___/___/_|/_/ \_\ A PicoSSG tool

Details

PicoSPA picospa.details.* makes the <details> element better, without server-side code.

  1. It remembers which <details> were open and restores them across page loads.
  2. 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:

2️⃣ Try what picospa.details.autoOpen() does:

3️⃣ Try what picospa.details.scrollOnOpen() does:

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:

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