-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Mustache service for Bento standalone components (#37584)
- Loading branch information
Showing
8 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {defineElement} from './web-component'; | ||
|
||
defineElement(); |
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,31 @@ | ||
import mustache from '#bento/components/bento-mustache/1.0/bento-mustache'; | ||
import {getTemplate} from '#bento/util/template'; | ||
|
||
import {defineBentoElement} from '#preact/bento-ce'; | ||
|
||
import {BaseElement, TAG} from './base-element'; | ||
|
||
export class BentoDateCountdown extends BaseElement { | ||
/** @override */ | ||
checkPropsPostMutations() { | ||
const template = getTemplate(this.element); | ||
if (!template) { | ||
// show error? | ||
return; | ||
} | ||
|
||
this.mutateProps({ | ||
'render': (data) => { | ||
const html = mustache.render(template./*OK*/ innerHTML, data); | ||
return {'__html': html}; | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Registers `<bento-date-countdown>` component to CustomElements registry | ||
*/ | ||
export function defineElement() { | ||
defineBentoElement(TAG, BentoDateCountdown); | ||
} |
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,3 @@ | ||
import {defineElement} from './web-component'; | ||
|
||
defineElement(); |
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,31 @@ | ||
import mustache from '#bento/components/bento-mustache/1.0/bento-mustache'; | ||
import {getTemplate} from '#bento/util/template'; | ||
|
||
import {defineBentoElement} from '#preact/bento-ce'; | ||
|
||
import {BaseElement, TAG} from './base-element'; | ||
|
||
export class BentoDateDisplay extends BaseElement { | ||
/** @override */ | ||
checkPropsPostMutations() { | ||
const template = getTemplate(this.element); | ||
if (!template) { | ||
// show error? | ||
return; | ||
} | ||
|
||
this.mutateProps({ | ||
'render': (data) => { | ||
const html = mustache.render(template./*OK*/ innerHTML, data); | ||
return {'__html': html}; | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Registers `<bento-date-display>` component to CustomElements registry | ||
*/ | ||
export function defineElement() { | ||
defineBentoElement(TAG, BentoDateDisplay); | ||
} |
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,5 @@ | ||
// Bento components must import `bento-mustache.js` instead of `mustache` directly. | ||
// This is so that the bundler links it to the external `bento-mustache.js` binary. | ||
import mustache from '#third_party/mustache/mustache'; | ||
|
||
export default mustache; |
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,18 @@ | ||
import {escapeCssSelectorIdent} from '#core/dom/css-selectors'; | ||
|
||
/** | ||
* @param {HTMLElement} element | ||
* @param {string} attribute | ||
* @return {HTMLTemplateElement|null} | ||
*/ | ||
export function getTemplate(element, attribute = 'template') { | ||
if (element.hasAttribute(attribute)) { | ||
const id = element.getAttribute(attribute); | ||
return Boolean(id) | ||
? element.ownerDocument.querySelector( | ||
`template#${escapeCssSelectorIdent(id)}` | ||
) | ||
: null; | ||
} | ||
return element.querySelector('template'); | ||
} |