Skip to content

Commit

Permalink
fix: revert slot controller changes
Browse files Browse the repository at this point in the history
This will be addressed in a later PR
  • Loading branch information
bennypowers committed Jan 30, 2025
1 parent 0b3dffa commit dd752d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 115 deletions.
45 changes: 0 additions & 45 deletions core/pfe-core/controllers/slot-controller-server.ts

This file was deleted.

77 changes: 7 additions & 70 deletions core/pfe-core/controllers/slot-controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isServer, type ReactiveController, type ReactiveElement } from 'lit';
import { type ReactiveController, type ReactiveElement } from 'lit';

interface AnonymousSlot {
hasContent: boolean;
Expand Down Expand Up @@ -49,56 +49,7 @@ const isSlot =
n === SlotController.default ? !child.hasAttribute('slot')
: child.getAttribute('slot') === n;

export declare class SlotControllerPublicAPI implements ReactiveController {
static default: symbol;

public host: ReactiveElement;

constructor(host: ReactiveElement, ...args: SlotControllerArgs);

hostConnected?(): Promise<void>;

hostDisconnected?(): void;

hostUpdated?(): void;

/**
* Given a slot name or slot names, returns elements assigned to the requested slots as an array.
* If no value is provided, it returns all children not assigned to a slot (without a slot attribute).
* @param slotNames slots to query
* @example Get header-slotted elements
* ```js
* this.getSlotted('header')
* ```
* @example Get header- and footer-slotted elements
* ```js
* this.getSlotted('header', 'footer')
* ```
* @example Get default-slotted elements
* ```js
* this.getSlotted();
* ```
*/
getSlotted<T extends Element = Element>(...slotNames: string[]): T[];

/**
* Returns a boolean statement of whether or not any of those slots exists in the light DOM.
* @param names The slot names to check.
* @example this.hasSlotted('header');
*/
hasSlotted(...names: (string | null | undefined)[]): boolean;

/**
* Whether or not all the requested slots are empty.
* @param names The slot names to query. If no value is provided, it returns the default slot.
* @example this.isEmpty('header', 'footer');
* @example this.isEmpty();
* @returns
*/
isEmpty(...names: (string | null | undefined)[]): boolean;
}

export class SlotController implements SlotControllerPublicAPI {
export class SlotController implements ReactiveController {
public static default = Symbol('default slot') satisfies symbol as symbol;

/** @deprecated use `default` */
Expand All @@ -110,8 +61,6 @@ export class SlotController implements SlotControllerPublicAPI {

#slotNames: (string | null)[] = [];

#ssrHintHasSlotted: (string | null)[] = [];

#deprecations: Record<string, string> = {};

#mo = new MutationObserver(this.#initSlotMap.bind(this));
Expand All @@ -137,11 +86,6 @@ export class SlotController implements SlotControllerPublicAPI {

async hostConnected(): Promise<void> {
this.#mo.observe(this.host, { childList: true });
this.#ssrHintHasSlotted =
this.host
// @ts-expect-error: this is a ponyfill for ::has-slotted, is not intended as a public API
.ssrHintHasSlotted
?? [];
// Map the defined slots into an object that is easier to query
this.#nodes.clear();
this.#initSlotMap();
Expand Down Expand Up @@ -169,31 +113,24 @@ export class SlotController implements SlotControllerPublicAPI {
const elements = this.#getChildrenForSlot(slotId);
const slot = this.#getSlotElement(slotId);
const hasContent =
isServer ? this.#ssrHintHasSlotted.includes(slotName)
: !!elements.length || !!slot?.assignedNodes?.()?.filter(x => x.textContent?.trim()).length;
!!elements.length || !!slot?.assignedNodes?.()?.filter(x => x.textContent?.trim()).length;
this.#nodes.set(slotId, { elements, name, hasContent, slot });
}
this.host.requestUpdate();
this.#slotMapInitialized = true;
}

#getSlotElement(slotId: string | symbol) {
if (isServer) {
return null;
} else {
const selector =
const selector =
slotId === SlotController.default ? 'slot:not([name])' : `slot[name="${slotId as string}"]`;
return this.host.shadowRoot?.querySelector?.<HTMLSlotElement>(selector) ?? null;
}
return this.host.shadowRoot?.querySelector<HTMLSlotElement>(selector) ?? null;
}

#getChildrenForSlot<T extends Element = Element>(
name: string | typeof SlotController.default,
): T[] {
if (isServer) {
return [];
} else if (this.#nodes.has(name)) {
return (this.#nodes.get(name)!.slot?.assignedElements?.() ?? []) as T[];
if (this.#nodes.has(name)) {
return (this.#nodes.get(name)!.slot?.assignedElements() ?? []) as T[];
} else {
const children = Array.from(this.host.children) as T[];
return children.filter(isSlot(name));
Expand Down

0 comments on commit dd752d5

Please sign in to comment.