Skip to content
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

UX: include custom headers in --header-offset #21059

Merged
merged 18 commits into from May 17, 2023
Merged

Conversation

awesomerobot
Copy link
Member

@awesomerobot awesomerobot commented Apr 11, 2023

In a theme component I've been trying to compensate for a custom header in CSS by adding to --header-offset but this doesn't cover when headerOffset() is used in JS: https://github.com/discourse/discourse-header-submenus/blob/b5661b04839a1e9b78eaf25541422f324b96eac6/desktop/desktop.scss#L18

I documented the need for this earlier over here: https://meta.discourse.org/t/header-submenus-theme-component-pushes-bottom-of-sidebar-out-of-the-screen/255496/2?u=awesomerobot

So here I'm checking the distance of the .d-header-wrap from the top of the page, and adjusting on scroll so this covers both sticky and non-sticky custom headers.

Previously this was covered here in minimumOffset (deprecated):

// if the header has a positive offset from the top of the window, we need to include the offset
but it didn't make it over to headerOffset

Copy link
Member

@davidtaylorhq davidtaylorhq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be extremely careful here - this is super performance-sensitive code. Happy to dig into the example tomorrow if nobody else beats me to it.

Also cc @pmusaraj since I think you have worked on this stuff quite a lot in the past.

@pmusaraj
Copy link
Contributor

@davidtaylorhq please do dig into this tomorrow, I won't have time to do so for the next few days.

Setting a style property causes the browser to recalculate styles, which can be quite time consuming. No need to do that if the value is unchanged.

This should make this feature essentially 'free' on sites without a custom header. On sites with a custom header, it'll only cause style recalculations while --header-top is actually changing
@awesomerobot
Copy link
Member Author

ok finally got tests passing reliably again 😅

@awesomerobot
Copy link
Member Author

@davidtaylorhq I was just reminded this due to another issue, do you think this is ok to merge now... alternatively I could close this and start a dev todo if you think it's worth looking into other approaches

@davidtaylorhq
Copy link
Member

Sorry for the delay @awesomerobot. I think we get get this into a mergeable state. My main remaining concern is the addition of setTimeout in tests - that introduces a lot of potential race conditions in our test suite. I'll assign this PR to myself and will try to take a closer look this week 👀

@davidtaylorhq davidtaylorhq self-assigned this May 15, 2023
@awesomerobot
Copy link
Member Author

@davidtaylorhq thanks! getting tests working at all was frustrating with this, so anything to get them more stable would be appreciated!

When using vanilla JS, requestAnimationFrame makes sense. However, since (almost) all our rendering is handled by Ember, using `beforeRender` accomplishes the same thing, and makes things like tests much more reliable. e.g. Ember will ensure the runloop is drained before moving on to the next step of the test. The same cannot be done with the browser-native `requestAnimationFrame`.
`if(DEBUG)` means that the contents of this if statement will be optimized out of production builds.
These should no longer be required now that the calculation is fixed in test mode, and we're using Ember's runloop for scheduling
@davidtaylorhq
Copy link
Member

@awesomerobot I've made some changes which I think should make this more robust - details in the commit messages. Let me know if you have any questions. If you're happy with the tweaks I've made, please feel free to merge

@davidtaylorhq davidtaylorhq removed their assignment May 16, 2023
@awesomerobot
Copy link
Member Author

This seems to work great, thank you so much for your help with this! 😍 there are at least a few theme components that this will solve minor issues for, as well as a number of customer themes.

@awesomerobot awesomerobot merged commit 14ad0b3 into main May 17, 2023
12 of 13 checks passed
@awesomerobot awesomerobot deleted the custom-header-offset branch May 17, 2023 21:37
@discoursebot
Copy link

This pull request has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/header-submenus-theme-component-pushes-bottom-of-sidebar-out-of-the-screen/255496/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants