Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report: fix element screenshot position, lifecycle, styles #11846

Merged
merged 16 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions lighthouse-core/report/html/renderer/element-screenshot-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,34 @@ class ElementScreenshotRenderer {
* @param {LH.Artifacts.FullPageScreenshot} fullPageScreenshot
*/
static installOverlayFeature(dom, templateContext, fullPageScreenshot) {
const reportEl = dom.find('.lh-report', dom.document());
const screenshotOverlayClass = 'lh-feature-screenshot-overlay';
if (reportEl.classList.contains(screenshotOverlayClass)) return;
reportEl.classList.add(screenshotOverlayClass);

const maxLightboxSize = {
width: dom.document().documentElement.clientWidth,
height: dom.document().documentElement.clientHeight * 0.75,
};
const rootEl = dom.find('.lh-root', dom.document());
if (!rootEl) {
console.warn('No lh-root. Overlay install failed.'); // eslint-disable-line no-console
return;
}

paulirish marked this conversation as resolved.
Show resolved Hide resolved
const screenshotOverlayClass = 'lh-screenshot-overlay--enabled';
// Don't install the feature more than once.
if (rootEl.classList.contains(screenshotOverlayClass)) return;
rootEl.classList.add(screenshotOverlayClass);

dom.document().addEventListener('click', e => {
// Add a single listener to the root element to handle all clicks within (event delegation).
rootEl.addEventListener('click', e => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not now, but for later)

should we listen to resize so we can re-make an open overlay? things get messy when you dock/undock, but also impacts just resizing the window of a standalone report

const target = /** @type {?HTMLElement} */ (e.target);
if (!target) return;
const el = /** @type {?HTMLElement} */ (target.closest('.lh-element-screenshot'));
// 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'));
paulirish marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +157 to +158
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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'));
const thumbnailEl = /** @type {?HTMLElement} */ (target.closest('.lh-node > .lh-element-screenshot'));

maybe says same thing but without a comment?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm to blame for the comment :) WFM 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one way this'll clean up is if we use .lh-element-screenshot--thumbnail and .lh-element-screenshot--lightbox everywhere. i can do this in a followup

if (!el) return;

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

// The newly-added overlay has the dimensions we need.
const maxLightboxSize = {
width: overlay.clientWidth * 0.95,
height: overlay.clientHeight * 0.80,
};

const elementRectSC = {
width: Number(el.dataset['rectWidth']),
height: Number(el.dataset['rectHeight']),
Expand All @@ -172,14 +182,15 @@ class ElementScreenshotRenderer {
elementRectSC,
maxLightboxSize
);
if (!screenshotElement) return;

overlay.appendChild(screenshotElement);
overlay.addEventListener('click', () => {
// This would be unexpected here.
// When `screenshotElement` is `null`, there is also no thumbnail element for the user to have clicked to make it this far.
if (!screenshotElement) {
overlay.remove();
});

reportEl.appendChild(overlay);
return;
}
overlay.appendChild(screenshotElement);
overlay.addEventListener('click', () => overlay.remove());
});
}

Expand Down
19 changes: 13 additions & 6 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.