-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(no-node slot fallback content): removed all <slot-fb> nodes for …
…fallback content. feat(patch remove / removeChild): non-shadow components will show/hide fallback content on slot node addition/removal
- Loading branch information
1 parent
4e8b9bd
commit f9a0063
Showing
13 changed files
with
314 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type * as d from '../../declarations'; | ||
import { NODE_TYPE } from '../runtime-constants'; | ||
import { patchNodeRemove } from '../dom-extras'; | ||
|
||
const renderSlotFallbackContent = (sr: d.RenderNode, hide: boolean) => { | ||
if (!sr['s-hsf']) return; | ||
let n: d.RenderNode = sr; | ||
while ((n = n.previousSibling as d.RenderNode)) { | ||
if (!n['s-sf'] || n['s-sn'] !== sr['s-sn']) continue; | ||
if (n.nodeType === NODE_TYPE.ElementNode) { | ||
n.hidden = hide; | ||
} else if (!!n['s-sfc']) { | ||
if (hide) { | ||
n['s-sfc'] = n.textContent; | ||
n.textContent = ''; | ||
} else if (n.textContent.trim() === '') { | ||
n.textContent = n['s-sfc']; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const updateFallbackSlotVisibility = (elm: d.RenderNode) => { | ||
let childNodes: d.RenderNode[] = ((elm as d.RenderNode).__childNodes || elm.childNodes as any); | ||
let childNode: d.RenderNode; | ||
let i: number; | ||
let ilen: number; | ||
let j: number; | ||
let slotNameAttr: string; | ||
let nodeType: number; | ||
|
||
for (i = 0, ilen = childNodes.length; i < ilen; i++) { | ||
childNode = childNodes[i]; | ||
|
||
if (childNode['s-sr']) { | ||
// this is a slot fallback node | ||
|
||
// get the slot name for this slot reference node | ||
slotNameAttr = childNode['s-sn']; | ||
|
||
// by default always show a fallback slot node | ||
// then hide it if there are other slots in the light dom | ||
renderSlotFallbackContent(childNode, false); | ||
|
||
for (j = 0; j < ilen; j++) { | ||
nodeType = childNodes[j].nodeType; | ||
|
||
if (childNodes[j]['s-sf']) continue; | ||
|
||
if (childNodes[j]['s-hn'] !== childNode['s-hn'] || slotNameAttr !== '') { | ||
// this sibling node is from a different component OR is a named fallback slot node | ||
if (nodeType === NODE_TYPE.ElementNode && slotNameAttr === childNodes[j]['s-sn']) { | ||
renderSlotFallbackContent(childNode, true); | ||
patchNodeRemove(childNodes[j]); | ||
break; | ||
} | ||
} else { | ||
// this is a default fallback slot node | ||
// any element or text node (with content) | ||
// should hide the default fallback slot node | ||
if ( | ||
nodeType === NODE_TYPE.ElementNode || | ||
(nodeType === NODE_TYPE.TextNode && childNodes[j].textContent.trim() !== '') | ||
) { | ||
renderSlotFallbackContent(childNode, true); | ||
patchNodeRemove(childNodes[j]); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
// keep drilling down | ||
updateFallbackSlotVisibility(childNode); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.