-
Notifications
You must be signed in to change notification settings - Fork 117
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
Deep links to chapter content #7
Comments
Oh, cool!
Yes, I agree 👍 I actually noticed myself and wanted to fix this. The slightly annoying part here is that anchor links can be a bit annoying to handle in Gatsby and in the past, I've always had to work around them manually by tracking route changes. But I'll look into this again. Once we have the anchor value, we should then be able to use that ID to auto-expand the exercise, yes. The nice thing about the expanded state is that we pass this in via the context in the top-level chapter and then handle the expanding in the exercise by checking if its ID matches the currently expanded ID. So at the top-level chapter, we know which exercise is expanded and at any other point in the app we can also auto-expand an exercise if you follow an anchor link that points to it. For the auto-collapsing, I guess it could check whether the click is outside of the exercise and then set Edit:
I just tried this, and it works, but... it feels kinda weird? It's okay for ther slides because they feel a bit more like a modal. But in a regular exercise, it's pretty easy to accidentally click somewhere outside of the container. And then the collapse is unexpected. Also, the exercises currently remount on expand, so if you accidentally collapse them, you'd lose your work. So I'd be reluctant to make this an "official" feature. However, if you do want it, here's the code for useEffect(() => {
if (isExpanded && excRef.current) {
excRef.current.scrollIntoView()
}
document.addEventListener('click', handleClickOutside, false)
return () => document.removeEventListener('click', handleClickOutside, false)
}, [isExpanded])
const handleClickOutside = ({ target }) => {
if (isExpanded && excRef.current && !excRef.current.contains(target)) {
setActiveExc(null)
}
} Edit 2: Just pushed an update for this to the |
Thanks, this is great! Yeah, I was thinking mostly for slides, to be able to collapse them, and it doesn't make as much sense for other exercises. I also several times hit And I tried your |
And for getting back to exactly where you were, having individual slides have their own URL would also be awesome, but I don't know how feasible that is (looks like |
Hmm, and because the links to exercises have numbers instead of names, we'd have to modify links if the course content changes... That unfortunate, but probably even harder to get right than giving slides their own URL, because you rely on exercises having integer ids so that you can go to the next exercise. Maybe there's a way to also give exercises names, and use those for links...? Or make a way to get the I also don't want to be just asking you to do a whole bunch of work for us; if something in here sounds feasible, I'm happy to try getting it to work myself. |
Ah, yeah, that's because it's just writing to the Another option would be to abandon the anchors alltogether and use query parameters. It'd be less "native" (because what we're trying to do here is exactly what anchor links are supposed to be for), but it'd be more flexible, because a URL could be
Yes, that's a good point – when I built this for my course, I didn't really consider use cases where content changes later on (because my course materials were pretty much done). The props of the |
@matt-gardner Did you end up using the solution in https://github.com/ines/course-starter-python/compare/feature/deep-links with good results? Or would you be willing to share any other modifications you have made to get links directly to sections within a chapter? |
Hi @ines, this is awesome, and we're looking into using this to revamp our intro material for allennlp. One issue that would be great to solve is that the content inside of a chapter is not linkable. E.g., if I want to link directly to the "Loading models" exercise in chapter 1 of your spacy course, I can't do it. Is there an easy way to fix this? I tried looking into a few options myself, but I figured you would know better than me.
Similarly (and I think relatedly, which is why I bring it up here), it'd be great if clicking outside of an exercise collapsed it - I believe one way to solve both of these is to make
isExpanded
part of the react component's state (and set from the slug), which is reset if you click outside of an exercise, though I'm not familiar enough with react to be confident of doing this right.The text was updated successfully, but these errors were encountered: