mirror of
https://github.com/discourse/discourse.git
synced 2025-06-10 18:03:44 +08:00
UX: include custom headers in --header-offset
(#21059)
This commit is contained in:
@ -8,7 +8,7 @@ import Docking from "discourse/mixins/docking";
|
|||||||
import MountWidget from "discourse/components/mount-widget";
|
import MountWidget from "discourse/components/mount-widget";
|
||||||
import ItsATrap from "@discourse/itsatrap";
|
import ItsATrap from "@discourse/itsatrap";
|
||||||
import RerenderOnDoNotDisturbChange from "discourse/mixins/rerender-on-do-not-disturb-change";
|
import RerenderOnDoNotDisturbChange from "discourse/mixins/rerender-on-do-not-disturb-change";
|
||||||
import { observes } from "discourse-common/utils/decorators";
|
import { bind, observes } from "discourse-common/utils/decorators";
|
||||||
import { topicTitleDecorators } from "discourse/components/topic-title";
|
import { topicTitleDecorators } from "discourse/components/topic-title";
|
||||||
import { isTesting } from "discourse-common/config/environment";
|
import { isTesting } from "discourse-common/config/environment";
|
||||||
import { DEBUG } from "@glimmer/env";
|
import { DEBUG } from "@glimmer/env";
|
||||||
@ -425,46 +425,69 @@ export default SiteHeaderComponent.extend({
|
|||||||
|
|
||||||
init() {
|
init() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
|
|
||||||
this._resizeObserver = null;
|
this._resizeObserver = null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@bind
|
||||||
|
updateHeaderOffset() {
|
||||||
|
let headerWrapTop = this.headerWrap.getBoundingClientRect().top;
|
||||||
|
|
||||||
|
if (DEBUG && isTesting()) {
|
||||||
|
headerWrapTop -= document
|
||||||
|
.getElementById("ember-testing-container")
|
||||||
|
.getBoundingClientRect().top;
|
||||||
|
|
||||||
|
headerWrapTop -= 1; // For 1px border on testing container
|
||||||
|
}
|
||||||
|
|
||||||
|
const documentStyle = document.documentElement.style;
|
||||||
|
|
||||||
|
const currentValue = documentStyle.getPropertyValue("--header-offset");
|
||||||
|
const newValue = `${this.headerWrap.offsetHeight + headerWrapTop}px`;
|
||||||
|
|
||||||
|
if (currentValue !== newValue) {
|
||||||
|
documentStyle.setProperty("--header-offset", newValue);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
@bind
|
||||||
|
onScroll() {
|
||||||
|
schedule("afterRender", this.updateHeaderOffset);
|
||||||
|
},
|
||||||
|
|
||||||
didInsertElement() {
|
didInsertElement() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
|
|
||||||
this.appEvents.on("site-header:force-refresh", this, "queueRerender");
|
this.appEvents.on("site-header:force-refresh", this, "queueRerender");
|
||||||
|
|
||||||
this.headerWrap = document.querySelector(".d-header-wrap");
|
this.headerWrap = document.querySelector(".d-header-wrap");
|
||||||
|
|
||||||
if (this.headerWrap) {
|
if (this.headerWrap) {
|
||||||
schedule("afterRender", () => {
|
schedule("afterRender", () => {
|
||||||
this.header = this.headerWrap.querySelector("header.d-header");
|
this.header = this.headerWrap.querySelector("header.d-header");
|
||||||
const headerOffset = this.headerWrap.offsetHeight;
|
this.updateHeaderOffset();
|
||||||
const headerTop = this.header.offsetTop;
|
const headerTop = this.header.offsetTop;
|
||||||
document.documentElement.style.setProperty(
|
|
||||||
"--header-offset",
|
|
||||||
`${headerOffset}px`
|
|
||||||
);
|
|
||||||
document.documentElement.style.setProperty(
|
document.documentElement.style.setProperty(
|
||||||
"--header-top",
|
"--header-top",
|
||||||
`${headerTop}px`
|
`${headerTop}px`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
window.addEventListener("scroll", this.onScroll, {
|
||||||
|
passive: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("ResizeObserver" in window) {
|
if ("ResizeObserver" in window) {
|
||||||
this._resizeObserver = new ResizeObserver((entries) => {
|
this._resizeObserver = new ResizeObserver((entries) => {
|
||||||
for (let entry of entries) {
|
for (let entry of entries) {
|
||||||
if (entry.contentRect) {
|
if (entry.contentRect) {
|
||||||
const headerOffset = entry.contentRect.height;
|
|
||||||
const headerTop = this.header.offsetTop;
|
const headerTop = this.header.offsetTop;
|
||||||
document.documentElement.style.setProperty(
|
|
||||||
"--header-offset",
|
|
||||||
`${headerOffset}px`
|
|
||||||
);
|
|
||||||
document.documentElement.style.setProperty(
|
document.documentElement.style.setProperty(
|
||||||
"--header-top",
|
"--header-top",
|
||||||
`${headerTop}px`
|
`${headerTop}px`
|
||||||
);
|
);
|
||||||
|
this.updateHeaderOffset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -475,7 +498,7 @@ export default SiteHeaderComponent.extend({
|
|||||||
|
|
||||||
willDestroyElement() {
|
willDestroyElement() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
|
window.removeEventListener("scroll", this.onScroll);
|
||||||
this._resizeObserver?.disconnect();
|
this._resizeObserver?.disconnect();
|
||||||
this.appEvents.off("site-header:force-refresh", this, "queueRerender");
|
this.appEvents.off("site-header:force-refresh", this, "queueRerender");
|
||||||
},
|
},
|
||||||
|
@ -66,9 +66,11 @@ module("Integration | Component | site-header", function (hooks) {
|
|||||||
await render(hbs`<SiteHeader />`);
|
await render(hbs`<SiteHeader />`);
|
||||||
|
|
||||||
function getProperty() {
|
function getProperty() {
|
||||||
return getComputedStyle(document.body).getPropertyValue(
|
const rawValue = getComputedStyle(document.body).getPropertyValue(
|
||||||
"--header-offset"
|
"--header-offset"
|
||||||
);
|
);
|
||||||
|
const roundedValue = Math.floor(parseFloat(rawValue));
|
||||||
|
return roundedValue + "px";
|
||||||
}
|
}
|
||||||
|
|
||||||
document.querySelector(".d-header").style.height = 90 + "px";
|
document.querySelector(".d-header").style.height = 90 + "px";
|
||||||
|
Reference in New Issue
Block a user