@@ -13,8 +13,10 @@ interface NamedSlot extends AnonymousSlot {
13
13
14
14
export type Slot = NamedSlot | AnonymousSlot ;
15
15
16
+ export type SlotName = string | null ;
17
+
16
18
export interface SlotsConfig {
17
- slots : ( string | null ) [ ] ;
19
+ slots : SlotName [ ] ;
18
20
/**
19
21
* Object mapping new slot name keys to deprecated slot name values
20
22
* @example `pf-modal--header` is deprecated in favour of `header`
@@ -30,11 +32,9 @@ export interface SlotsConfig {
30
32
deprecations ?: Record < string , string > ;
31
33
}
32
34
33
- export type SlotControllerArgs = [ SlotsConfig ] | ( string | null ) [ ] ;
35
+ export type SlotControllerArgs = [ SlotsConfig ] | SlotName [ ] ;
34
36
35
- function isObjectSpread (
36
- config : ( [ SlotsConfig ] | ( string | null ) [ ] ) ,
37
- ) : config is [ SlotsConfig ] {
37
+ export function isObjectSpread ( config : SlotControllerArgs ) : config is [ SlotsConfig ] {
38
38
return config . length === 1 && typeof config [ 0 ] === 'object' && config [ 0 ] !== null ;
39
39
}
40
40
@@ -49,14 +49,61 @@ const isSlot =
49
49
n === SlotController . default ? ! child . hasAttribute ( 'slot' )
50
50
: child . getAttribute ( 'slot' ) === n ;
51
51
52
- export class SlotController implements ReactiveController {
52
+ export declare class SlotControllerPublicAPI implements ReactiveController {
53
+ static default : symbol ;
54
+
55
+ public host : ReactiveElement ;
56
+
57
+ constructor ( host : ReactiveElement , ...args : SlotControllerArgs ) ;
58
+
59
+ hostConnected ?( ) : Promise < void > ;
60
+
61
+ hostDisconnected ?( ) : void ;
62
+
63
+ hostUpdated ?( ) : void ;
64
+
65
+ /**
66
+ * Given a slot name or slot names, returns elements assigned to the requested slots as an array.
67
+ * If no value is provided, it returns all children not assigned to a slot (without a slot attribute).
68
+ * @param slotNames slots to query
69
+ * @example Get header-slotted elements
70
+ * ```js
71
+ * this.getSlotted('header')
72
+ * ```
73
+ * @example Get header- and footer-slotted elements
74
+ * ```js
75
+ * this.getSlotted('header', 'footer')
76
+ * ```
77
+ * @example Get default-slotted elements
78
+ * ```js
79
+ * this.getSlotted();
80
+ * ```
81
+ */
82
+ getSlotted < T extends Element = Element > ( ...slotNames : string [ ] ) : T [ ] ;
83
+
84
+ /**
85
+ * Returns a boolean statement of whether or not any of those slots exists in the light DOM.
86
+ * @param names The slot names to check.
87
+ * @example this.hasSlotted('header');
88
+ */
89
+ hasSlotted ( ...names : ( string | null | undefined ) [ ] ) : boolean ;
90
+
91
+ /**
92
+ * Whether or not all the requested slots are empty.
93
+ * @param names The slot names to query. If no value is provided, it returns the default slot.
94
+ * @example this.isEmpty('header', 'footer');
95
+ * @example this.isEmpty();
96
+ * @returns
97
+ */
98
+ isEmpty ( ...names : ( string | null | undefined ) [ ] ) : boolean ;
99
+ }
100
+
101
+ export class SlotController implements SlotControllerPublicAPI {
53
102
public static default = Symbol ( 'default slot' ) satisfies symbol as symbol ;
54
103
55
104
/** @deprecated use `default` */
56
105
public static anonymous : symbol = this . default ;
57
106
58
- private static singletons = new WeakMap < ReactiveElement , SlotController > ( ) ;
59
-
60
107
#nodes = new Map < string | typeof SlotController . default , Slot > ( ) ;
61
108
62
109
#slotMapInitialized = false ;
@@ -70,14 +117,8 @@ export class SlotController implements ReactiveController {
70
117
#mo = new MutationObserver ( this . #initSlotMap. bind ( this ) ) ;
71
118
72
119
constructor ( public host : ReactiveElement , ...args : SlotControllerArgs ) {
73
- const singleton = SlotController . singletons . get ( host ) ;
74
- if ( singleton ) {
75
- singleton . #initialize( ...args ) ;
76
- return singleton ;
77
- }
78
120
this . #initialize( ...args ) ;
79
121
host . addController ( this ) ;
80
- SlotController . singletons . set ( host , this ) ;
81
122
if ( ! this . #slotNames. length ) {
82
123
this . #slotNames = [ null ] ;
83
124
}
0 commit comments