@@ -285,22 +285,52 @@ class PFElement extends HTMLElement {
285285 * This alerts nested components to a change in the context
286286 */
287287 contextUpdate ( ) {
288- // If a value has been set, alert any nested children of the change
289- [ ...this . querySelectorAll ( "*" ) , ... this . shadowRoot . querySelectorAll ( "*" ) ]
288+ // Loop over light DOM elements, find direct descendants that are components
289+ const lightEls = [ ...this . querySelectorAll ( "*" ) ]
290290 . filter ( item => item . tagName . toLowerCase ( ) . slice ( 0 , 4 ) === `${ prefix } -` )
291- . map ( child => {
292- this . log ( `Update context of ${ child . tag } ` ) ;
293- Promise . all ( [ customElements . whenDefined ( child . tagName . toLowerCase ( ) ) ] ) . then ( ( ) => {
294- // Ask the component to recheck it's context in case it changed
295- child . resetContext ( this . on ) ;
296- } ) ;
291+ // Closest will return itself or it's ancestor matching that selector
292+ . filter ( item => {
293+ // If there is no parent element, return null
294+ if ( ! item . parentElement ) return ;
295+ // Otherwise, find the closest component that's this one
296+ else return item . parentElement . closest ( `[${ this . _pfeClass . _getCache ( "prop2attr" ) . pfelement } ]` ) === this ;
297+ } ) ;
298+
299+ // Loop over shadow elements, find direct descendants that are components
300+ let shadowEls = [ ...this . shadowRoot . querySelectorAll ( "*" ) ]
301+ . filter ( item => item . tagName . toLowerCase ( ) . slice ( 0 , 4 ) === `${ prefix } -` )
302+ // Closest will return itself or it's ancestor matching that selector
303+ . filter ( item => {
304+ // If there is a parent element and we can find another web component in the ancestor tree
305+ if ( item . parentElement && item . parentElement . closest ( `[${ this . _pfeClass . _getCache ( "prop2attr" ) . pfelement } ]` ) ) {
306+ return item . parentElement . closest ( `[${ this . _pfeClass . _getCache ( "prop2attr" ) . pfelement } ]` ) === this ;
307+ }
308+ // Otherwise, check if the host matches this context
309+ if ( item . getRootNode ( ) . host === this ) return true ;
310+
311+ // If neither state is true, return false
312+ return false ;
297313 } ) ;
314+
315+ const nestedEls = lightEls . concat ( shadowEls ) ;
316+
317+ // If nested elements don't exist, return without processing
318+ if ( nestedEls . length === 0 ) return ;
319+
320+ // Loop over the nested elements and reset their context
321+ nestedEls . map ( child => {
322+ this . log ( `Update context of ${ child . tagName . toLowerCase ( ) } ` ) ;
323+ Promise . all ( [ customElements . whenDefined ( child . tagName . toLowerCase ( ) ) ] ) . then ( ( ) => {
324+ // Ask the component to recheck it's context in case it changed
325+ child . resetContext ( this . on ) ;
326+ } ) ;
327+ } ) ;
298328 }
299329
300330 resetContext ( fallback ) {
301331 if ( this . isIE11 ) return ;
302332
303- this . log ( `Resetting context on ${ this . tag } ` ) ;
333+ this . log ( `Resetting context` ) ;
304334 // Priority order for context values to be pulled from:
305335 //--> 1. context (OLD: pfe-theme)
306336 //--> 2. --context (OLD: --theme)
@@ -429,8 +459,8 @@ class PFElement extends HTMLElement {
429459 // Cascade properties to the rendered template
430460 this . cascadeProperties ( ) ;
431461
432- // Reset the display context
433- this . resetContext ( ) ;
462+ // Update the display context
463+ this . contextUpdate ( ) ;
434464
435465 if ( PFElement . trackPerformance ( ) ) {
436466 try {
@@ -494,7 +524,7 @@ class PFElement extends HTMLElement {
494524 const cascade = this . _pfeClass . _getCache ( "cascadingProperties" ) ;
495525
496526 if ( cascade ) {
497- if ( window . ShadyCSS && this . _cascadeObserver ) this . _cascadeObserver . disconnect ( ) ;
527+ if ( this . _cascadeObserver ) this . _cascadeObserver . disconnect ( ) ;
498528
499529 let selectors = Object . keys ( cascade ) ;
500530 // Find out if anything in the nodeList matches any of the observed selectors for cacading properties
@@ -525,7 +555,7 @@ class PFElement extends HTMLElement {
525555 }
526556
527557 // @TODO This is here for IE11 processing; can move this after deprecation
528- if ( window . ShadyCSS && this . _rendered && this . _cascadeObserver )
558+ if ( this . _rendered && this . _cascadeObserver )
529559 this . _cascadeObserver . observe ( this , {
530560 attributes : true ,
531561 childList : true ,
@@ -550,6 +580,7 @@ class PFElement extends HTMLElement {
550580 */
551581 _contextObserver ( oldValue , newValue ) {
552582 if ( newValue && ( ( oldValue && oldValue !== newValue ) || ! oldValue ) ) {
583+ this . log ( `Running the context observer` ) ;
553584 this . on = newValue ;
554585 this . cssVariable ( "context" , newValue ) ;
555586 }
@@ -560,6 +591,7 @@ class PFElement extends HTMLElement {
560591 */
561592 _onObserver ( oldValue , newValue ) {
562593 if ( ( oldValue && oldValue !== newValue ) || ( newValue && ! oldValue ) ) {
594+ this . log ( `Context update` ) ;
563595 // Fire an event for child components
564596 this . contextUpdate ( ) ;
565597 }
@@ -593,8 +625,6 @@ class PFElement extends HTMLElement {
593625 if ( mutation . type === "childList" && mutation . addedNodes . length ) {
594626 this . cascadeProperties ( mutation . addedNodes ) ;
595627 }
596- // @TODO : Do something when mutation type is attribute?
597- // else if (mutation.type === "attributes") {}
598628 }
599629 }
600630 /* --- End observers --- */
@@ -693,7 +723,7 @@ class PFElement extends HTMLElement {
693723 _initializeSlots ( tag , slots ) {
694724 this . log ( "Validate slots..." ) ;
695725
696- if ( window . ShadyCSS && this . _slotsObserver ) this . _slotsObserver . disconnect ( ) ;
726+ if ( this . _slotsObserver ) this . _slotsObserver . disconnect ( ) ;
697727
698728 // Loop over the properties provided by the schema
699729 Object . keys ( slots ) . forEach ( slot => {
@@ -739,7 +769,7 @@ class PFElement extends HTMLElement {
739769
740770 this . log ( "Slots validated." ) ;
741771
742- if ( window . ShadyCSS && this . _slotsObserver ) this . _slotsObserver . observe ( this , { childList : true } ) ;
772+ if ( this . _slotsObserver ) this . _slotsObserver . observe ( this , { childList : true } ) ;
743773 }
744774
745775 /**
0 commit comments