|
1 | | -import { type ReactiveController, type ReactiveElement } from 'lit'; |
| 1 | +import { isServer, type ReactiveController, type ReactiveElement } from 'lit'; |
2 | 2 |
|
3 | 3 | interface AnonymousSlot { |
4 | 4 | hasContent: boolean; |
@@ -113,24 +113,32 @@ export class SlotController implements ReactiveController { |
113 | 113 | const elements = this.#getChildrenForSlot(slotId); |
114 | 114 | const slot = this.#getSlotElement(slotId); |
115 | 115 | const hasContent = |
116 | | - !!elements.length || !!slot?.assignedNodes?.()?.filter(x => x.textContent?.trim()).length; |
| 116 | + !isServer |
| 117 | + && !!elements.length |
| 118 | + || !!slot?.assignedNodes?.()?.filter(x => x.textContent?.trim()).length; |
117 | 119 | this.#nodes.set(slotId, { elements, name, hasContent, slot }); |
118 | 120 | } |
119 | 121 | this.host.requestUpdate(); |
120 | 122 | this.#slotMapInitialized = true; |
121 | 123 | } |
122 | 124 |
|
123 | 125 | #getSlotElement(slotId: string | symbol) { |
124 | | - const selector = |
| 126 | + if (isServer) { |
| 127 | + return null; |
| 128 | + } else { |
| 129 | + const selector = |
125 | 130 | slotId === SlotController.default ? 'slot:not([name])' : `slot[name="${slotId as string}"]`; |
126 | | - return this.host.shadowRoot?.querySelector<HTMLSlotElement>(selector) ?? null; |
| 131 | + return this.host.shadowRoot?.querySelector?.<HTMLSlotElement>(selector) ?? null; |
| 132 | + } |
127 | 133 | } |
128 | 134 |
|
129 | 135 | #getChildrenForSlot<T extends Element = Element>( |
130 | 136 | name: string | typeof SlotController.default, |
131 | 137 | ): T[] { |
132 | | - if (this.#nodes.has(name)) { |
133 | | - return (this.#nodes.get(name)!.slot?.assignedElements() ?? []) as T[]; |
| 138 | + if (isServer) { |
| 139 | + return []; |
| 140 | + } else if (this.#nodes.has(name)) { |
| 141 | + return (this.#nodes.get(name)!.slot?.assignedElements?.() ?? []) as T[]; |
134 | 142 | } else { |
135 | 143 | const children = Array.from(this.host.children) as T[]; |
136 | 144 | return children.filter(isSlot(name)); |
|
0 commit comments