-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: port over Core base class / Bolt Element updates from #1579
- Loading branch information
Showing
14 changed files
with
924 additions
and
539 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,81 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
import { LitElement } from 'lit-element'; | ||
|
||
const Slotify = Base => | ||
class extends Base { | ||
templateMap = new Map(); | ||
export class Slotify extends LitElement { | ||
templateMap = new Map(); | ||
|
||
assignSlotToContent(child) { | ||
return child.getAttribute | ||
? child.getAttribute('slot') || 'default' | ||
: 'default'; | ||
} | ||
assignSlotToContent(child) { | ||
return child.getAttribute | ||
? child.getAttribute('slot') || 'default' | ||
: 'default'; | ||
} | ||
|
||
isEmptyTextNode(child) { | ||
return child && (!child.textContent || !child.textContent.trim()); | ||
} | ||
isEmptyTextNode(child) { | ||
return child && (!child.textContent || !child.textContent.trim()); | ||
} | ||
|
||
addChildToTemplateMap(slot, child) { | ||
if (!slot) return; | ||
addChildToTemplateMap(slot, child) { | ||
if (!slot) return; | ||
|
||
if (!this.templateMap.has(slot)) { | ||
this.templateMap.set(slot, [child]); | ||
} else { | ||
this.templateMap.set(slot, [...this.templateMap.get(slot), child]); | ||
} | ||
if (!this.templateMap.has(slot)) { | ||
this.templateMap.set(slot, [child]); | ||
} else { | ||
this.templateMap.set(slot, [...this.templateMap.get(slot), child]); | ||
} | ||
} | ||
|
||
// Save a reference to the pseudoSlot content before lit-element renders | ||
saveSlots() { | ||
Array.from(this.childNodes).forEach(child => { | ||
const slot = this.assignSlotToContent(child); | ||
// Save a reference to the pseudoSlot content before lit-element renders | ||
saveSlots() { | ||
Array.from(this.childNodes).forEach(child => { | ||
const slot = this.assignSlotToContent(child); | ||
|
||
if (!child.textContent || child.textContent.trim().length > 0) { | ||
this.addChildToTemplateMap(slot, child); | ||
} else if (slot && child instanceof HTMLElement) { | ||
this.addChildToTemplateMap(slot, child); | ||
} | ||
}); | ||
} | ||
|
||
update(changedProperties) { | ||
if (!this.hasUpdated) { | ||
this.saveSlots(); | ||
if (!child.textContent || child.textContent.trim().length > 0) { | ||
this.addChildToTemplateMap(slot, child); | ||
} else if (slot && child instanceof HTMLElement) { | ||
this.addChildToTemplateMap(slot, child); | ||
} | ||
}); | ||
} | ||
|
||
super.update(changedProperties); | ||
update(changedProperties) { | ||
if (!this.hasUpdated) { | ||
this.saveSlots(); | ||
} | ||
|
||
slotify(slot = 'default', defaultContent) { | ||
const slotContent = this.templateMap.get(slot); | ||
super.update(changedProperties); | ||
} | ||
|
||
// render actualy slots if Shadow DOM supported + getting used | ||
// @todo: what's a better way to allow customizing the checks to perform? | ||
if ( | ||
this.shadowRoot && | ||
(this.useShadow === undefined || this.useShadow === true) && | ||
(this.noShadow === undefined || this.useShadow === false) && | ||
slotContent | ||
) { | ||
const realSlot = document.createElement('slot'); | ||
if (slot !== 'default') { | ||
realSlot.setAttribute('name', slot); | ||
} | ||
return realSlot; | ||
} | ||
slotify(slot = 'default', defaultContent) { | ||
const slotContent = this.templateMap.get(slot); | ||
|
||
if (slotContent && slotContent.content) { | ||
return slotContent.content; | ||
} | ||
if (slotContent && slotContent.childNodes) { | ||
return Array.from(slotContent.childNodes); | ||
} | ||
if (slotContent) { | ||
return slotContent; | ||
} | ||
if (defaultContent) { | ||
return defaultContent; | ||
// render actualy slots if Shadow DOM supported + getting used | ||
// @todo: what's a better way to allow customizing the checks to perform? | ||
if ( | ||
this.shadowRoot && | ||
(this.useShadow === undefined || this.useShadow === true) && | ||
(this.noShadow === undefined || this.useShadow === false) && | ||
slotContent | ||
) { | ||
const realSlot = document.createElement('slot'); | ||
if (slot !== 'default') { | ||
realSlot.setAttribute('name', slot); | ||
} | ||
return realSlot; | ||
} | ||
|
||
return null; | ||
if (slotContent && slotContent.content) { | ||
return slotContent.content; | ||
} | ||
if (slotContent && slotContent.childNodes) { | ||
return Array.from(slotContent.childNodes); | ||
} | ||
if (slotContent) { | ||
return slotContent; | ||
} | ||
if (defaultContent) { | ||
return defaultContent; | ||
} | ||
}; | ||
|
||
export { Slotify }; | ||
return null; | ||
} | ||
} |
Oops, something went wrong.