Skip to content

Commit

Permalink
feat(audits): Add UI with list of audits (#10074)
Browse files Browse the repository at this point in the history
* feat(audits): Add UI with list of audits

* style: slightly better UI

* requested design changes

* chore: changeset

* add test

* remove unwanted file
  • Loading branch information
Princesseuh authored Feb 14, 2024
1 parent 31de1ea commit 7443929
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-yaks-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": minor
---

Add a UI showing the list of found problems when using the audit app in the dev toolbar
17 changes: 17 additions & 0 deletions packages/astro/e2e/dev-toolbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ test.describe('Dev Toolbar', () => {
await expect(auditWindow.locator('astro-dev-toolbar-icon[icon=check-circle]')).toBeVisible();
});

test('audit shows a window with list of problems', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));

const toolbar = page.locator('astro-dev-toolbar');
const appButton = toolbar.locator('button[data-app-id="astro:audit"]');
await appButton.click();

const auditCanvas = toolbar.locator('astro-dev-toolbar-app-canvas[data-app-id="astro:audit"]');
const auditWindow = auditCanvas.locator('astro-dev-toolbar-window');
await expect(auditWindow).toHaveCount(1);
await expect(auditWindow).toBeVisible();

// Toggle app off
await appButton.click();
await expect(auditWindow).not.toBeVisible();
});

test('adjusts tooltip position if off-screen', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/tooltip-position'));

Expand Down
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"@babel/plugin-transform-react-jsx": "^7.22.5",
"@babel/traverse": "^7.23.3",
"@babel/types": "^7.23.3",
"@medv/finder": "^3.1.0",
"@types/babel__core": "^7.20.4",
"acorn": "^8.11.2",
"aria-query": "^5.3.0",
Expand Down
126 changes: 124 additions & 2 deletions packages/astro/src/runtime/client/dev-toolbar/apps/audit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../utils/highlight.js';
import { createWindowElement } from '../utils/window.js';
import { a11y } from './a11y.js';
import { finder } from '@medv/finder';
import { perf } from './perf.js';

const icon =
Expand Down Expand Up @@ -60,7 +61,11 @@ export default {
name: 'Audit',
icon: icon,
async init(canvas, eventTarget) {
let audits: { highlightElement: DevToolbarHighlight; auditedElement: HTMLElement }[] = [];
let audits: {
highlightElement: DevToolbarHighlight;
auditedElement: HTMLElement;
rule: AuditRule;
}[] = [];

await lint();

Expand Down Expand Up @@ -126,6 +131,119 @@ export default {
},
})
);

const auditListWindow = createWindowElement(
`
<style>
astro-dev-toolbar-window {
left: initial;
top: 8px;
right: 8px;
transform: none;
width: 320px;
max-height: 320px;
padding: 0;
overflow: hidden;
}
hr {
margin: 0;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 18px;
}
h1 {
font-size: 22px;
font-weight: 600;
color: #fff;
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
}
h1, h2 {
margin: 0;
}
h3 {
margin: 0;
margin-bottom: 8px;
color: white;
white-space: nowrap;
}
.audit-title {
font-weight: bold;
color: white;
margin-right: 1ch;
}
#audit-list {
display: flex;
flex-direction: column;
overflow: auto;
}
</style>
<header>
<h1>Audits</h1>
<astro-dev-toolbar-badge size="large">${audits.length} problem${
audits.length > 1 ? 's' : ''
} found</astro-dev-toolbar-badge>
</header>
<hr />`
);

const auditListUl = document.createElement('ul');
auditListUl.id = 'audit-list';
audits.forEach((audit, index) => {
const resolvedRule = resolveAuditRule(audit.rule, audit.auditedElement);
const card = document.createElement('astro-dev-toolbar-card');

card.shadowRoot.innerHTML = `
<style>
:host>button {
text-align: left;
box-shadow: none !important;
${
index + 1 < audits.length
? 'border-radius: 0 !important;'
: 'border-radius: 0 0 8px 8px !important;'
}
}
:host>button:hover {
cursor: pointer;
}
</style>`;

card.clickAction = () => {
audit.highlightElement.scrollIntoView();
audit.highlightElement.focus();
};
const h3 = document.createElement('h3');
h3.innerText = finder(audit.auditedElement);
card.appendChild(h3);
const div = document.createElement('div');
const title = document.createElement('span');
title.classList.add('audit-title');
title.innerHTML = resolvedRule.title;
div.appendChild(title);
card.appendChild(div);
auditListUl.appendChild(card);
});

auditListWindow.appendChild(auditListUl);

canvas.append(auditListWindow);
} else {
eventTarget.dispatchEvent(
new CustomEvent('toggle-notification', {
Expand Down Expand Up @@ -229,7 +347,11 @@ export default {
attachTooltipToHighlight(highlight, tooltip, originalElement);

canvas.append(highlight);
audits.push({ highlightElement: highlight, auditedElement: originalElement as HTMLElement });
audits.push({
highlightElement: highlight,
auditedElement: originalElement as HTMLElement,
rule: rule,
});
}

function buildAuditTooltip(rule: AuditRule, element: Element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DevToolbarCard extends HTMLElement {
connectedCallback() {
const element = this.link ? 'a' : this.clickAction ? 'button' : 'div';

this.shadowRoot.innerHTML = `
this.shadowRoot.innerHTML += `
<style>
:host>a, :host>button, :host>div {
box-sizing: border-box;
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 7443929

Please sign in to comment.