GithubHelp home page GithubHelp logo

Comments (9)

duncanwerner avatar duncanwerner commented on May 15, 2024 3

Here's our current version, which saves the detail state:

https://gist.github.com/duncanwerner/d49fd9574ecd7a26d1febd0077dd012b

Example:

https://docs.treb.app

You are welcome to use it, just remember that it is fragile to future changes in Starlight.

from starlight.

Faf4a avatar Faf4a commented on May 15, 2024 1

Wondering, any progress on this?

from starlight.

delucis avatar delucis commented on May 15, 2024

Thanks for the issue. Would be nice to improve this indeed.

For a simple solution in your own project, you can add scripts using the head config option: add your JS to the public/ directory, and then add it to your site’s head in astro.config.mjs:

  head: [{ tag: 'script', attrs: { src: '/my-script.js' } }],

from starlight.

duncanwerner avatar duncanwerner commented on May 15, 2024

Sorry, I missed that. I thought I had scoured for places to insert stuff.

I would still prefer a way to inline it (I know I can inline it in that head object, but I mean as a proper module) but this works great for the time being. If anyone is interested here's my working example (scroll position only):

(() => {

  const key = 'toc-position';
  
  let timeout = 0;

  let position = { scrollTop: 0 };

  const sidebar = document.querySelector('#starlight__sidebar > .sidebar-content');

  if (sidebar) {

    const data = sessionStorage.getItem(key);
    if (data) {
      try {
        const obj = JSON.parse(data);
        position = {
          ...position, ...obj,
        };
        sidebar.scrollTop = position.scrollTop || 0;
      }
      catch (err) {
        // ...
      }
    }

    sidebar.addEventListener('scroll', () => {
      position.scrollTop = sidebar.scrollTop;
      if (!timeout) {
        timeout = window.setTimeout(() => {
          sessionStorage.setItem(key, JSON.stringify(position));
          timeout = 0;
        }, 100);
      }
    });

  }

})();

from starlight.

astridx avatar astridx commented on May 15, 2024

I just tried this and it works great. Thank you @duncanwerner for sharing.
Now the only issue is that the open-state of the detail-elements is not saved. After clicking on a menu item, all previously closed ones open again.

This means that the scoll position is not correct in this case.

Am I missing something or is it not possible to save this state at the moment?

from starlight.

stereobooster avatar stereobooster commented on May 15, 2024

I wonder if @swup/astro with morph config would do the trick

from starlight.

doroved avatar doroved commented on May 15, 2024

Yes, I wish scroll state saving was built into starlight

from starlight.

wassfila avatar wassfila commented on May 15, 2024

I had a similar issue, and also worse, with websites having a high pages count, the menu building on each page slowed down my build time considerably. So I designed this client side shamefully injected menu with vanilla js, the advantage is the open closed state storage and the menu data are fetched only once https://github.com/MicroWebStacks/astro-big-doc/blob/main/src/layout/client_menu.js
but from Starlight I would expect more, closed state and position indeed and I was wondering if it is not possible to keep the whole menu state with ViewTransition.

from starlight.

pReya avatar pReya commented on May 15, 2024

Any reasons why the solution of @duncanwerner couldn't be merged into the project? Looks like a solid implementation to me.

from starlight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.