Skip to content

Commit

Permalink
report: css var for FPS, move overlay to container (#12201)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Mar 18, 2021
1 parent 11ce6cb commit 2880956
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lighthouse-core/gather/gatherers/full-page-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class FullPageScreenshot extends Gatherer {
const data = 'data:image/jpeg;base64,' + result.data;

return {
data,
width,
height,
data,
};
}

Expand Down
47 changes: 22 additions & 25 deletions lighthouse-core/report/html/renderer/element-screenshot-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
/** @typedef {LH.Artifacts.Rect} Rect */
/** @typedef {{width: number, height: number}} Size */

/**
* @typedef InstallOverlayFeatureParams
* @property {DOM} dom
* @property {Element} el
* @property {ParentNode} templateContext
* @property {LH.Artifacts.FullPageScreenshot} fullPageScreenshot
*/

/**
* @param {LH.Artifacts.FullPageScreenshot['screenshot']} screenshot
* @param {LH.Artifacts.Rect} rect
Expand Down Expand Up @@ -118,48 +126,37 @@ class ElementScreenshotRenderer {
}

/**
* Called externally and must be injected to the report in order to use this renderer.
* @param {DOM} dom
* Called by report renderer. Defines a css variable used by any element screenshots
* in the provided report element.
* Allows for multiple Lighthouse reports to be rendered on the page, each with their
* own full page screenshot.
* @param {HTMLElement} el
* @param {LH.Artifacts.FullPageScreenshot['screenshot']} screenshot
*/
static createBackgroundImageStyle(dom, screenshot) {
const styleEl = dom.createElement('style');
styleEl.id = 'full-page-screenshot-style';
styleEl.textContent = `
.lh-element-screenshot__image {
background-image: url(${screenshot.data})
}`;
return styleEl;
static installFullPageScreenshot(el, screenshot) {
el.style.setProperty('--element-screenshot-url', `url(${screenshot.data})`);
}

/**
* Installs the lightbox elements and wires up click listeners to all .lh-element-screenshot elements.
* @param {DOM} dom
* @param {ParentNode} templateContext
* @param {LH.Artifacts.FullPageScreenshot} fullPageScreenshot
* @param {InstallOverlayFeatureParams} _
*/
static installOverlayFeature(dom, templateContext, fullPageScreenshot) {
const rootEl = dom.find('.lh-root', dom.document());
if (!rootEl) {
console.warn('No lh-root. Overlay install failed.'); // eslint-disable-line no-console
return;
}

static installOverlayFeature({dom, el, templateContext, fullPageScreenshot}) {
const screenshotOverlayClass = 'lh-screenshot-overlay--enabled';
// Don't install the feature more than once.
if (rootEl.classList.contains(screenshotOverlayClass)) return;
rootEl.classList.add(screenshotOverlayClass);
if (el.classList.contains(screenshotOverlayClass)) return;
el.classList.add(screenshotOverlayClass);

// Add a single listener to the root element to handle all clicks within (event delegation).
rootEl.addEventListener('click', e => {
// Add a single listener to the provided element to handle all clicks within (event delegation).
el.addEventListener('click', e => {
const target = /** @type {?HTMLElement} */ (e.target);
if (!target) return;
// Only activate the overlay for clicks on the screenshot *preview* of an element, not the full-size too.
const el = /** @type {?HTMLElement} */ (target.closest('.lh-node > .lh-element-screenshot'));
if (!el) return;

const overlay = dom.createElement('div', 'lh-element-screenshot__overlay');
rootEl.append(overlay);
el.append(overlay);

// The newly-added overlay has the dimensions we need.
const maxLightboxSize = {
Expand Down
9 changes: 5 additions & 4 deletions lighthouse-core/report/html/renderer/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ class ReportRenderer {
const detailsRenderer = new DetailsRenderer(this._dom, {
fullPageScreenshot,
});
const fullPageScreenshotStyleEl = fullPageScreenshot &&
ElementScreenshotRenderer.createBackgroundImageStyle(
this._dom, fullPageScreenshot.screenshot);

const categoryRenderer = new CategoryRenderer(this._dom, detailsRenderer);
categoryRenderer.setTemplateContext(this._templateContext);
Expand Down Expand Up @@ -269,9 +266,13 @@ class ReportRenderer {
reportFragment.appendChild(reportContainer);
reportContainer.appendChild(headerContainer);
reportContainer.appendChild(reportSection);
fullPageScreenshotStyleEl && reportContainer.appendChild(fullPageScreenshotStyleEl);
reportSection.appendChild(this._renderReportFooter(report));

if (fullPageScreenshot) {
ElementScreenshotRenderer.installFullPageScreenshot(
reportContainer, fullPageScreenshot.screenshot);
}

return reportFragment;
}
}
Expand Down
15 changes: 11 additions & 4 deletions lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ReportUIFeatures {
this._setupMediaQueryListeners();
this._dropDown.setup(this.onDropDownMenuClick);
this._setupThirdPartyFilter();
this._setupElementScreenshotOverlay();
this._setupElementScreenshotOverlay(this._dom.find('.lh-container', this._document));
this._setUpCollapseDetailsAfterPrinting();
this._resetUIState();
this._document.addEventListener('keyup', this.onKeyUp);
Expand Down Expand Up @@ -307,16 +307,23 @@ class ReportUIFeatures {
});
}

_setupElementScreenshotOverlay() {
/**
* @param {Element} el
*/
_setupElementScreenshotOverlay(el) {
const fullPageScreenshot =
this.json.audits['full-page-screenshot'] &&
this.json.audits['full-page-screenshot'].details &&
this.json.audits['full-page-screenshot'].details.type === 'full-page-screenshot' &&
this.json.audits['full-page-screenshot'].details;
if (!fullPageScreenshot) return;

ElementScreenshotRenderer.installOverlayFeature(
this._dom, this._templateContext, fullPageScreenshot);
ElementScreenshotRenderer.installOverlayFeature({
dom: this._dom,
el,
templateContext: this._templateContext,
fullPageScreenshot,
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/report/html/report-styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2880956

Please sign in to comment.