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

move all dom interaction to connectedCallback #355

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/client/src/helpers/custom-link-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export class CustomLinkElement extends HTMLElement {
this.anchor.href = this.getAttribute("href") || "";
this.anchor.innerHTML = this.innerHTML;
this.anchor.classList.add(...this.classList);
}

connectedCallback() {
this.classList.remove(...this.classList);

this.innerHTML = "";
this.appendChild(this.anchor);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/views/breadcrumb.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { LenkeMedSporingElement } from "./lenke-med-sporing";

class Breadcrumb extends LenkeMedSporingElement {
constructor() {
super();
}
connectedCallback() {
super.connectedCallback();

if (this.getAttribute("data-handle-in-app") !== null) {
this.addEventListener("click", (e) => {
e.preventDefault();
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/views/context-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ContextLink extends CustomLinkElement {
};

connectedCallback() {
super.connectedCallback();

window.addEventListener("activecontext", this.handleActiveContext);
this.addEventListener("click", this.handleClick);
}
Expand Down
18 changes: 7 additions & 11 deletions packages/client/src/views/decorator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@ import { LanguageSelector } from "./language-selector";
import i18n from "../i18n";

class DecoratorUtils extends HTMLElement {
languageSelector: LanguageSelector;
breadcrumbs: HTMLElement;

constructor() {
super();

this.languageSelector = this.querySelector(
":scope > div > language-selector",
)!;
this.breadcrumbs = this.querySelector(":scope > div > :first-child")!;
}
languageSelector!: LanguageSelector;
breadcrumbs!: HTMLElement;

update = () => {
const { availableLanguages, language, breadcrumbs, utilsBackground } =
Expand All @@ -42,6 +33,11 @@ class DecoratorUtils extends HTMLElement {
}

connectedCallback() {
this.languageSelector = this.querySelector(
":scope > div > language-selector",
)!;
this.breadcrumbs = this.querySelector(":scope > div > :first-child")!;

window.addEventListener("paramsupdated", this.update);
setTimeout(this.update, 0);
}
Expand Down
14 changes: 5 additions & 9 deletions packages/client/src/views/language-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ declare global {
}

export class LanguageSelector extends HTMLElement {
menu;
container: HTMLDivElement;
button: HTMLButtonElement;
menu!: HTMLElement;
container!: HTMLDivElement;
button!: HTMLButtonElement;
#open = false;
options: (HTMLAnchorElement | HTMLButtonElement)[] = [];
#language?: Language;
Expand Down Expand Up @@ -81,17 +81,13 @@ export class LanguageSelector extends HTMLElement {
);
}

constructor() {
super();

this.button = this.querySelector(`.${cls.button}`) as HTMLButtonElement;
connectedCallback() {
this.button = this.querySelector(`.${cls.button}`)!;
this.container = this.querySelector(`.${cls.languageSelector}`)!;
this.menu = document.createElement("ul");
this.menu.classList.add(cls.menu, cls.hidden);
this.container.appendChild(this.menu);
}

connectedCallback() {
this.button.addEventListener("click", () => {
this.open = !this.#open;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/views/lenke-med-sporing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CustomLinkElement } from "../helpers/custom-link-element";
import { amplitudeEvent } from "../analytics/amplitude";

export class LenkeMedSporingElement extends CustomLinkElement {
constructor() {
super();
connectedCallback() {
super.connectedCallback();

const rawEventArgs = this.getAttribute("data-analytics-event-args");
const eventArgs = tryParse<AnalyticsEventArgs, null>(
Expand Down
4 changes: 1 addition & 3 deletions packages/client/src/views/loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import html from "decorator-shared/html";

export class Loader extends HTMLElement {
constructor() {
super();

connectedCallback() {
const shadowRoot = this.attachShadow({ mode: "open" });
const title = this.getAttribute("title") ?? "Laster forhåndsvisning";

Expand Down
4 changes: 3 additions & 1 deletion packages/client/src/views/skip-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class SkipLinkElement extends LenkeMedSporingElement {
setTimeout(() => observer.disconnect(), DEFERRED_UPDATE_TIME);
}

private connectedCallback() {
connectedCallback() {
super.connectedCallback();

this.updateDisplay();
if (!this.hasMainContent()) {
this.deferredUpdate();
Expand Down
20 changes: 13 additions & 7 deletions packages/client/src/views/sticky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import cls from "decorator-client/src/styles/sticky.module.css";
class Sticky extends HTMLElement {
// This element is positioned relative to the top of the document and should
// update when the scroll position changes.
private readonly absoluteElement: HTMLElement = this.querySelector(
`.${cls.absoluteWrapper}`,
)!;
private absoluteElement!: HTMLElement;

// This element is nested under the absolute element, and is set to a fixed
// position when the header is fully scrolled into view. This prevents the
// header from sometimes moving erratically when the user is scrolling upwards.
private readonly fixedElement: HTMLElement = this.querySelector(
`.${cls.fixedWrapper}`,
)!;
private fixedElement!: HTMLElement;

// Why we use two levels of positioning in this implementation:
// 1. Using only fixed positioning (and hiding the header with a negative top position)
Expand Down Expand Up @@ -159,11 +155,21 @@ class Sticky extends HTMLElement {
};

connectedCallback() {
if (!this.absoluteElement || !this.fixedElement) {
const fixedElement = this.querySelector(
`.${cls.fixedWrapper}`,
) as HTMLElement | null;
const absoluteElement = this.querySelector(
`.${cls.absoluteWrapper}`,
) as HTMLElement | null;

if (!absoluteElement || !fixedElement) {
console.error("Required elements not found!");
return;
}

this.fixedElement = fixedElement;
this.absoluteElement = absoluteElement;

window.addEventListener("scroll", this.calculateAndUpdatePosition);
window.addEventListener("resize", this.calculateAndUpdatePosition);

Expand Down
10 changes: 3 additions & 7 deletions packages/client/src/views/user-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@ const Loader = () => html`
`;

class UserMenu extends HTMLElement {
constructor() {
super();
this.innerHTML = Loader().render(window.__DECORATOR_DATA__.params);
}

private onAuthUpdated = (e: CustomEvent<CustomEvents["authupdated"]>) => {
this.classList.add(cls.userMenuContainer);
this.innerHTML = e.detail.usermenuHtml;
};

private connectedCallback() {
connectedCallback() {
this.innerHTML = Loader().render(window.__DECORATOR_DATA__.params);
window.addEventListener("authupdated", this.onAuthUpdated);
}

private disconnectedCallback() {
disconnectedCallback() {
window.removeEventListener("authupdated", this.onAuthUpdated);
}
}
Expand Down