Intersection Observers
Can someone tell me what I'm doing wrong here?
I'm trying to get this IntersectinoObserver to update other elements than the one actually being observed, when the heading is scrolled into view, i want the corresponding element in the TOC to update and change some styles on it, but i can only get the observed element to change. is this some perf thing that wont let you do this? Source: https://github.com/t3-oss/create-t3-app/blob/38fd589c3c43e21eef19ec177e78192884b79d9e/www/src/components/navigation/tableOfContents.astro
Demo: https://share.cleanshot.com/xddfsF
GitHub
create-t3-app/tableOfContents.astro at 38fd589c3c43e21eef19ec177e78...
Quickest way to start a new web app with full stack typesafety - create-t3-app/tableOfContents.astro at 38fd589c3c43e21eef19ec177e78192884b79d9e ยท t3-oss/create-t3-app
83 Replies
Create T3 App
Environment Variables ๐ Create T3 App
Create T3 App is the quickest way to start a new web app with fullstack typesafety
Fwiw I've never had intersection observers work as expected
They're...rough
Is it possible that Tailwind isn't picking up the classes in the inline script?
im adding the same class to the entry and another item, but only the entry is being updated
Ah
hrrrrm
Another dumb question: Possible the document selector isn't selecting? I know astro loves to mangle ids and classnames
i've had console logs logging the element and it is correctly selected
this file is even adding the correct classname but the dom is not updated
Oh??? fascinating hrm
Possible that classname isn't compiled in?
perhaps... still don't know why this isn't wokring on both elements
same class
I've had a good experience using @react-hook/intersection-observer
im migrating off react
Ah I just saw the code
GitHub
create-t3-app/TableOfContents.tsx at 10ebdda6dac2a0ff8d8f18482a8e72...
Quickest way to start a new web app with full stack typesafety - create-t3-app/TableOfContents.tsx at 10ebdda6dac2a0ff8d8f18482a8e72177e9610ad ยท t3-oss/create-t3-app
this was it before
More and more rethinking using Astro for this
docusaurus has its quirks too but most defaults are good
i suppose i could leave the ToC in react if it doesnt work too
This confused me enough to dig in - bg styles are applied but overridden
So that is working
maybe the script is running at the wrong time ๐ค so that other styles are applied after...
How are the page styles applied? I'm guessing they're "later" in the CSS order
its tailwind
but the observer should add/remove later so shouldn't that take precedense?
Here's the hierarchy that the CSS has
So I'm doubling down on my guess that the element select is bad for the TOC bit
yea last commit i tested this and it updates the dom
Can you console log
tocItem
to see if it exists?last commit does this toohttps://create-t3-71afuplz0-t3-oss.vercel.app/en/usage/env-variables
Create T3 App
Environment Variables ๐ Create T3 App
Create T3 App is the quickest way to start a new web app with fullstack typesafety
some edgecases it doesnt select but should work for the most
The dark and hover styles aren't in the css bundle @julius
Wait maybe they are hmm
can i jsut add a dummy element with hidden and those styles then
I'd try this
but why isn't the text-error applied, or is it overridden?
Huh yeah it isn't applying
auowhraiuwherawer; this sucks grr
wonder if this is an astro quirk or a tailwind one
Is the order of styles in the outputted html different to the order of class names
I think adding and removing tailwind classes is...sketch for this tbh
React and other "js owning html" solutions are much cleaner
Hurts to say but...might be better to make two CSS classes for the different treatments? ๐
Is it possible the else clause here is running erroneously?
It doesn't look "inline" the styles must live somewhere else
commented the else and still same
wdym?
I give up, observers are cursed
is it possible the ToC isn't created when the observer is created?
it should be though, it should be rendered on the server
and the script is clientside
Unknown Userโข3y ago
Message Not Public
Sign In & Join Server To View
wtf
when i log this it says the class is there, but it's not in the dom tree
even though the id is the same
What if you separated the on intention handler and the new intersection instance into different functions
i had that before and then it complained that they were previously defined or smth
I'm only saying that Incase it's getting a ref to something that has re rendered
thia?
now it says this so maybe smth's wrong with execution-order
You haven't set a root element in the observer options
But you are setting targets with observer.observe
I would possibly revert to what it was before that error and try and set the root element and see if that makes a difference
You defo want to set an intersectional observer once not in a loop. You want to set the root component in the options and you want observer.observe all the child elements in a loop
like that?
You have a div of the articles that is your root give that an id and get element by id
Set that to be your intersection observer not in the loop, just at the top of the in line script
but the toc is not in that article
Is this some title colour changing when you stroll past articles that match the title
yea thats just for test
i only want to change the toc items
when the headings on the article is in view
this what i want
Ok but the thing you are observing is the article appearing in the article wrapper. But you want this code to work in the TOC component
so im observing the article, but want to change it's corresponding item in the TOC
I can't figure out what root would be in this scenario it's what ever has the fixed height and causes the article to overflow hidden
The articles are defo the targets
Document element might be it might be window
So I was messing around with your code and got a was able to get a slightly different result in whic all headers are now being colored when scrolled into view and color removed when scrolled out. I dont think thats exactly the result your looking for, I assume you just want the newest to be red.
I should probably have a play with the code too
the pushed code is slightly modified and doing some tests
It's quite hard doing this from a phone ๐
yea i could imagine
i want this behavior but in astro https://github.com/t3-oss/create-t3-app/blob/main/www/src/components/navigation/TableOfContents.tsx
GitHub
create-t3-app/TableOfContents.tsx at main ยท t3-oss/create-t3-app
Quickest way to start a new web app with full stack typesafety - create-t3-app/TableOfContents.tsx at main ยท t3-oss/create-t3-app
what i cant figure out is why i can change some elements' classes but not some
the only thing i can think of is the tocItem it selects isn't whats in the dom, but dont know why that would be the case
found the issue
grrr
forgot we're rendering this component in two places ffs
so querySelector selected the other element haha
why am i not getting errors that i have multiple items with same id then
Can you see it changing the other component
that component is only on mobile so hard to debug that one xd
i'll remove id and select like this i guess
Astro should warn about that I think you might have found a bug
i'll let them know
I thought chrome dev tools would pick that up too
Possibly not
Nice one
is this smelly or ok
It's terse I'll give you that. It's a shame that string literals aren't an option. My main issue is I'm not sure I like different components being able to class swap from other components what happens if it gets added twice from different places then remove once, hard to keep track off.
well the script is in the TOC, and toggles classes for the TOC so that's fine i think
Very true
Unless you have that component twice ๐