Skip to content

Commit 35a459e

Browse files
committed
Merge branch 'main' into feat/slot-controller-ssr
2 parents 9f5ecaa + 0277045 commit 35a459e

File tree

15 files changed

+247
-708
lines changed

15 files changed

+247
-708
lines changed

.changeset/clear-pugs-make.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
---
2-
"@patternfly/pfe-core": patch
2+
"@patternfly/pfe-core": major
33
"@patternfly/elements": patch
44
---
5-
Enable context protocol in SSR scenarios.
5+
Enable `connectedCallback()` and context protocol in SSR scenarios.
6+
7+
BREAKING CHANGE
8+
This change affects any element which is expected to execute in node JS when
9+
lit-ssr shims are present. By enabling the `connectedCallback()` to execute
10+
server side. Elements must ensure that their connectedCallbacks do not try to
11+
access the DOM.
12+
13+
Before:
14+
15+
```js
16+
connectedCallback() {
17+
super.connectedCallback();
18+
this.items = this.querySelectorAll('my-item');
19+
}
20+
```
21+
22+
After:
23+
```js
24+
connectedCallback() {
25+
super.connectedCallback();
26+
if (!isServer) {
27+
this.items = this.querySelectorAll('my-item');
28+
}
29+
}
30+
```

.changeset/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"changelog": "@changesets/cli/changelog",
44
"commit": false,
55
"linked": [],
6+
"prettier": false,
67
"access": "restricted",
78
"baseBranch": "main",
89
"updateInternalDependencies": "patch",

.changeset/dull-stingrays-film.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/olive-rivers-stick.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

core/pfe-core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# @patternfly/pfe-core
22

3+
## 4.0.5
4+
### Patch Changes
5+
6+
- 0fb28b6: `SlotController`: `hasContent`/`isEmpty` now respects text nodes
7+
38
## 4.0.4
49

510
### Patch Changes

core/pfe-core/controllers/scroll-spy-controller.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,25 @@ export interface ScrollSpyControllerOptions extends IntersectionObserverInit {
2323
* @default el => el.getAttribute('href');
2424
*/
2525
getHash?: (el: Element) => string | null;
26+
/**
27+
* Optional callback for when an intersection occurs
28+
*/
29+
onIntersection?(): void;
2630
}
2731

2832
export class ScrollSpyController implements ReactiveController {
33+
static #instances = new Set<ScrollSpyController>;
34+
35+
static {
36+
addEventListener('scroll', () => {
37+
if (Math.round(window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
38+
this.#instances.forEach(ssc => {
39+
ssc.#setActive(ssc.#linkChildren.at(-1));
40+
});
41+
}
42+
}, { passive: true });
43+
}
44+
2945
#tagNames: string[];
3046
#activeAttribute: string;
3147

@@ -43,9 +59,11 @@ export class ScrollSpyController implements ReactiveController {
4359
#root: ScrollSpyControllerOptions['root'];
4460
#rootMargin?: string;
4561
#threshold: number | number[];
62+
#intersectingElements: Element[] = [];
4663

4764
#getRootNode: () => Node | null;
4865
#getHash: (el: Element) => string | null;
66+
#onIntersection?: () => void;
4967

5068
get #linkChildren(): Element[] {
5169
return Array.from(this.host.querySelectorAll(this.#tagNames.join(',')))
@@ -94,13 +112,22 @@ export class ScrollSpyController implements ReactiveController {
94112
this.#threshold = options.threshold ?? 0.85;
95113
this.#getRootNode = () => options.rootNode ?? host.getRootNode?.() ?? null;
96114
this.#getHash = options?.getHash ?? ((el: Element) => el.getAttribute('href'));
115+
this.#onIntersection = options?.onIntersection;
97116
}
98117

99118
hostConnected(): void {
119+
ScrollSpyController.#instances.add(this);
100120
this.#initIo();
101121
}
102122

103-
#initIo() {
123+
hostDisconnected(): void {
124+
ScrollSpyController.#instances.delete(this);
125+
this.#io?.disconnect();
126+
}
127+
128+
#initializing = true;
129+
130+
async #initIo() {
104131
const rootNode = this.#getRootNode();
105132
if (rootNode instanceof Document || rootNode instanceof ShadowRoot) {
106133
const { rootMargin, threshold, root } = this;
@@ -151,6 +178,25 @@ export class ScrollSpyController implements ReactiveController {
151178
this.#setActive(last ?? this.#linkChildren.at(0));
152179
}
153180
this.#intersected = true;
181+
this.#intersectingElements =
182+
entries
183+
.filter(x => x.isIntersecting)
184+
.map(x => x.target);
185+
if (this.#initializing) {
186+
const ints = entries?.filter(x => x.isIntersecting) ?? [];
187+
if (this.#intersectingElements) {
188+
const [{ target = null } = {}] = ints;
189+
const { id } = target ?? {};
190+
if (id) {
191+
const link = this.#linkChildren.find(link => this.#getHash(link) === `#${id}`);
192+
if (link) {
193+
this.#setActive(link);
194+
}
195+
}
196+
}
197+
this.#initializing = false;
198+
}
199+
this.#onIntersection?.();
154200
}
155201

156202
/**

core/pfe-core/controllers/slot-controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ export class SlotController implements SlotControllerPublicAPI {
143143
this.host.requestUpdate();
144144
}
145145

146-
hostDisconnected(): void {
147-
this.#mo.disconnect();
148-
}
149-
150146
hostUpdated(): void {
151147
if (!this.#slotMapInitialized) {
152148
this.#initSlotMap();
153149
}
154150
}
155151

152+
hostDisconnected(): void {
153+
this.#mo.disconnect();
154+
}
155+
156156
#initSlotMap() {
157157
// Loop over the properties provided by the schema
158158
for (const slotName of this.#slotNames

core/pfe-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@patternfly/pfe-core",
3-
"version": "4.0.4",
3+
"version": "4.0.5",
44
"license": "MIT",
55
"description": "PatternFly Elements Core Library",
66
"customElements": "custom-elements.json",

0 commit comments

Comments
 (0)