@@ -29,6 +29,11 @@ export default class FloatingPanelElement extends ImpulseElement {
2929 */
3030 @property ( { type : Boolean } ) active = false ;
3131
32+ /**
33+ * If the trigger lives outside the floating panel, you can set the trigger element's id.
34+ */
35+ @property ( ) triggerId : string ;
36+
3237 /**
3338 * One of 'absolute' or 'fixed'.
3439 * https://floating-ui.com/docs/computeposition#strategy
@@ -61,7 +66,7 @@ export default class FloatingPanelElement extends ImpulseElement {
6166 */
6267 @property ( ) sync ?: 'width' | 'height' | 'both' ;
6368
64- @target ( ) trigger : HTMLElement ;
69+ @target ( ) trigger ? : HTMLElement ;
6570 @target ( ) panel : HTMLElement ;
6671
6772 private cleanup ?: ReturnType < typeof autoUpdate > ;
@@ -82,12 +87,21 @@ export default class FloatingPanelElement extends ImpulseElement {
8287 }
8388 }
8489
90+ /**
91+ * We want to restart the placement logic when the trigger id changes.
92+ */
93+ async triggerIdChanged ( ) {
94+ await this . stop ( ) ;
95+ this . start ( ) ;
96+ }
97+
8598 start ( ) {
99+ if ( ! this . triggerElement ) return ;
86100 this . panel . style . position = this . strategy ;
87101 this . panel . style . top = '0px' ;
88102 this . panel . style . left = '0px' ;
89103
90- this . cleanup = autoUpdate ( this . trigger , this . panel , this . position . bind ( this ) ) ;
104+ this . cleanup = autoUpdate ( this . triggerElement , this . panel , this . position . bind ( this ) ) ;
91105 }
92106
93107 async stop ( ) : Promise < void > {
@@ -104,7 +118,7 @@ export default class FloatingPanelElement extends ImpulseElement {
104118 }
105119
106120 async position ( ) {
107- if ( ! this . active ) return ;
121+ if ( ! this . active || ! this . triggerElement ) return ;
108122
109123 const middleware : Middleware [ ] = [ offset ( presence ( this . offsetOptions ) ) ] ;
110124
@@ -132,7 +146,7 @@ export default class FloatingPanelElement extends ImpulseElement {
132146 ? ( element : Element ) => platform . getOffsetParent ( element , offsetParent )
133147 : platform . getOffsetParent ;
134148
135- const { x, y, strategy, placement } = await computePosition ( this . trigger , this . panel , {
149+ const { x, y, strategy, placement } = await computePosition ( this . triggerElement , this . panel , {
136150 placement : this . placement ,
137151 middleware,
138152 strategy : this . strategy ,
@@ -151,6 +165,10 @@ export default class FloatingPanelElement extends ImpulseElement {
151165 this . setAttribute ( 'data-current-placement' , placement ) ;
152166 this . emit ( 'changed' ) ;
153167 }
168+
169+ private get triggerElement ( ) {
170+ return document . getElementById ( this . triggerId ) || this . trigger ;
171+ }
154172}
155173
156174declare global {
0 commit comments