Comments (9)
Here's our current version, which saves the detail state:
https://gist.github.com/duncanwerner/d49fd9574ecd7a26d1febd0077dd012b
Example:
You are welcome to use it, just remember that it is fragile to future changes in Starlight.
from starlight.
Wondering, any progress on this?
from starlight.
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.
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.
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.
I wonder if @swup/astro
with morph config would do the trick
from starlight.
Yes, I wish scroll state saving was built into starlight
from starlight.
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.
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)
- Missing "./constants" specifier in "@astrojs/starlight" package HOT 2
- lastUpdated displays incorrect date HOT 2
- Failed to Initialize Starlight Project: NPM create command issue HOT 4
- Sidebar folders order depends on their page order property HOT 3
- Support for Astro v4
- Installing from template is broken HOT 2
- Scroll bar on package manager tabs HOT 2
- GH Pages Deployment has no styles HOT 3
- `lastUpdated` option causes performance degradation HOT 1
- Built-in components are not tree shakable HOT 4
- alphabetical sorting not functioning properly in autogenerated sidebars
- Slow sidebar generation when there are a lot of items HOT 13
- "overflow-wrap: anywhere" is problematic in table cell code blocks HOT 2
- Pagefind doesn't work with a gitlab pages deployment and bun HOT 1
- Can't deploy on Cloudflare HOT 1
- Left sidebar scroll bar problem HOT 2
- config options are not followed in pagefind search
- CSS not displaying on pages other than homepage HOT 4
- Hot Updates Reporting Errors Causing Project Crashes HOT 1
- Throws on reading `import.meta.env.BASE_URL` in Deno environment HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from starlight.