Skip to content

Commit

Permalink
✨ Mustache service for Bento standalone components (#37584)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanek committed Feb 11, 2022
1 parent b6877bd commit 6f88c91
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extensions/amp-date-countdown/1.0/base-element.js
Expand Up @@ -4,6 +4,8 @@ import {PreactBaseElement} from '#preact/base-element';

import {BentoDateCountdown} from './component';

export const TAG = 'bento-date-countdown';

export class BaseElement extends PreactBaseElement {}

/** @override */
Expand Down
3 changes: 3 additions & 0 deletions extensions/amp-date-countdown/1.0/bento-date-countdown.js
@@ -0,0 +1,3 @@
import {defineElement} from './web-component';

defineElement();
31 changes: 31 additions & 0 deletions extensions/amp-date-countdown/1.0/web-component.js
@@ -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);
}
2 changes: 2 additions & 0 deletions extensions/amp-date-display/1.0/base-element.js
Expand Up @@ -5,6 +5,8 @@ import {createParseAttrsWithPrefix} from '#preact/parse-props';

import {BentoDateDisplay} from './component';

export const TAG = 'bento-date-display';

export class BaseElement extends PreactBaseElement {}

/** @override */
Expand Down
3 changes: 3 additions & 0 deletions extensions/amp-date-display/1.0/bento-date-display.js
@@ -0,0 +1,3 @@
import {defineElement} from './web-component';

defineElement();
31 changes: 31 additions & 0 deletions extensions/amp-date-display/1.0/web-component.js
@@ -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);
}
5 changes: 5 additions & 0 deletions src/bento/components/bento-mustache/1.0/bento-mustache.js
@@ -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;
18 changes: 18 additions & 0 deletions src/bento/util/template.js
@@ -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');
}

0 comments on commit 6f88c91

Please sign in to comment.